Module: Aptible::CLI::Subcommands::Apps

Included in:
Agent
Defined in:
lib/aptible/cli/subcommands/apps.rb

Class Method Summary collapse

Class Method Details

.included(thor) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/aptible/cli/subcommands/apps.rb', line 5

def self.included(thor)
  thor.class_eval do
    include Helpers::App
    include Helpers::Environment
    include Helpers::Token
    include Helpers::Telemetry

    desc 'apps', 'List all applications'
    option :environment, aliases: '--env'
    def apps
      telemetry(__method__, options)

      Formatter.render(Renderer.current) do |root|
        root.grouped_keyed_list(
          { 'environment' => 'handle' },
          'handle'
        ) do |node|
          accounts = scoped_environments(options)
          acc_map = environment_map(accounts)

          if Renderer.format == 'json'
            accounts.each do ||
              .each_app do |app|
                node.object do |n|
                  ResourceFormatter.inject_app(n, app, )
                end
              end
            end
          else
            apps_all.each do |app|
               = acc_map[app.links..href]
              next if .nil?

              node.object do |n|
                ResourceFormatter.inject_app(n, app, )
              end
            end
          end
        end
      end
    end

    desc 'apps:create HANDLE', 'Create a new application'
    option :environment, aliases: '--env'
    define_method 'apps:create' do |handle|
      telemetry(__method__, options.merge(handle: handle))

      environment = ensure_environment(options)
      app = environment.create_app(handle: handle)

      if app.errors.any?
        raise Thor::Error, app.errors.full_messages.first
      else
        CLI.logger.info "App #{handle} created!"

        Formatter.render(Renderer.current) do |root|
          root.object do |o|
            o.value('git_remote', app.git_repo)
          end
        end
      end
    end

    desc 'apps:scale SERVICE ' \
         '[--container-count COUNT] [--container-size SIZE_MB] ' \
         '[--container-profile PROFILE]',
         'Scale a service'
    app_options
    option :container_count, type: :numeric
    option :container_size, type: :numeric
    option :container_profile, type: :string,
                               desc: 'Examples: m c r'
    define_method 'apps:scale' do |type|
      telemetry(__method__, options.merge(type: type))

      service = ensure_service(options, type)

      container_count = options[:container_count]
      container_size = options[:container_size]
      container_profile = options[:container_profile]

      if options[:size]
        m = 'You have used the "--size" option to specify a container '\
            'size. This abiguous option has been removed.'\
            'Please use the "--container-size" option, instead.'
        raise Thor::Error, m
      end

      if container_count.nil? && container_size.nil? &&
         container_profile.nil?
        m = 'Provide at least --container-count, --container-size, ' \
            'or --container-profile'
        raise Thor::Error, m
      end

      # We don't validate any parameters here: API will do that for us.
      opts = { type: 'scale' }
      opts[:container_count] = container_count if container_count
      opts[:container_size] = container_size if container_size
      opts[:instance_profile] = container_profile if container_profile

      op = service.create_operation!(opts)
      attach_to_operation_logs(op)
    end

    desc 'apps:deprovision', 'Deprovision an app'
    app_options
    define_method 'apps:deprovision' do
      telemetry(__method__, options)

      app = ensure_app(options)
      CLI.logger.info "Deprovisioning #{app.handle}..."
      op = app.create_operation!(type: 'deprovision')
      begin
        attach_to_operation_logs(op)
      rescue HyperResource::ClientError => e
        # A 404 here means that the operation completed successfully,
        # and was removed faster than attach_to_operation_logs
        # could attach to the logs.
        raise if e.response.status != 404
      end
    end

    desc 'apps:rename OLD_HANDLE NEW_HANDLE [--environment'\
         ' ENVIRONMENT_HANDLE]', 'Rename an app handle. In order'\
         ' for the new app handle to appear in log drain and metric'\
         ' drain destinations, you must restart the app.'
    option :environment, aliases: '--env'
    define_method 'apps:rename' do |old_handle, new_handle|
      opts = options.merge(
        old_handle: old_handle,
        new_handle: new_handle
      )
      telemetry(__method__, opts)

      env = ensure_environment(options)
      app = ensure_app(options.merge(app: old_handle))
      app.update!(handle: new_handle)
      m1 = "In order for the new app name (#{new_handle}) to appear"\
           ' in log drain and metric drain destinations, you must'\
           ' restart the app.'
      m2 = 'You can restart your app with this command: "aptible '\
           "restart --app #{new_handle} --environment #{env.handle}\""
      m3 = 'Warning - Git remote addresses must be updated to match'\
           ' the new handle, if using Dockerfile deploy. '\
           "(git@beta.aptible.com:#{app..handle}"\
           "/#{new_handle}.git)"
      CLI.logger.warn m1
      CLI.logger.info m2
      CLI.logger.warn m3
    end
  end
end