aptible-cli 0.18.0 → 0.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +63 -50
  3. data/lib/aptible/cli/agent.rb +9 -3
  4. data/lib/aptible/cli/helpers/config_path.rb +11 -0
  5. data/lib/aptible/cli/helpers/log_drain.rb +85 -0
  6. data/lib/aptible/cli/helpers/metric_drain.rb +39 -0
  7. data/lib/aptible/cli/helpers/ssh.rb +5 -1
  8. data/lib/aptible/cli/helpers/token.rb +5 -1
  9. data/lib/aptible/cli/resource_formatter.rb +66 -3
  10. data/lib/aptible/cli/subcommands/apps.rb +5 -40
  11. data/lib/aptible/cli/subcommands/backup.rb +15 -7
  12. data/lib/aptible/cli/subcommands/db.rb +24 -21
  13. data/lib/aptible/cli/subcommands/log_drain.rb +159 -0
  14. data/lib/aptible/cli/subcommands/metric_drain.rb +137 -0
  15. data/lib/aptible/cli/version.rb +1 -1
  16. data/spec/aptible/cli/resource_formatter_spec.rb +1 -0
  17. data/spec/aptible/cli/subcommands/apps_spec.rb +10 -51
  18. data/spec/aptible/cli/subcommands/backup_spec.rb +1 -1
  19. data/spec/aptible/cli/subcommands/db_spec.rb +0 -26
  20. data/spec/aptible/cli/subcommands/environment_spec.rb +4 -2
  21. data/spec/aptible/cli/subcommands/log_drain_spec.rb +207 -0
  22. data/spec/aptible/cli/subcommands/metric_drain_spec.rb +183 -0
  23. data/spec/fabricators/account_fabricator.rb +3 -0
  24. data/spec/fabricators/app_fabricator.rb +1 -0
  25. data/spec/fabricators/database_disk_fabricator.rb +2 -1
  26. data/spec/fabricators/database_fabricator.rb +1 -0
  27. data/spec/fabricators/log_drain_fabricator.rb +21 -0
  28. data/spec/fabricators/metric_drain_fabricator.rb +8 -0
  29. data/spec/fabricators/service_fabricator.rb +1 -0
  30. data/spec/fabricators/vhost_fabricator.rb +1 -0
  31. data/spec/spec_helper.rb +4 -0
  32. metadata +15 -5
  33. data/lib/aptible/cli/subcommands/domains.rb +0 -40
  34. data/spec/aptible/cli/subcommands/domains_spec.rb +0 -76
@@ -52,52 +52,17 @@ module Aptible
52
52
  app_options
53
53
  option :container_count, type: :numeric
54
54
  option :container_size, type: :numeric
55
- option :size, type: :numeric,
56
- desc: 'DEPRECATED, use --container-size'
57
- define_method 'apps:scale' do |type, *more|
55
+ define_method 'apps:scale' do |type|
58
56
  service = ensure_service(options, type)
59
57
 
60
58
  container_count = options[:container_count]
61
59
  container_size = options[:container_size]
62
60
 
63
- # There are two legacy options we have to process here:
64
- # - We used to accept apps:scale SERVICE COUNT
65
- # - We used to accept --size
66
- case more.size
67
- when 0
68
- # Noop
69
- when 1
70
- if container_count.nil?
71
- m = 'Passing container count as a positional ' \
72
- 'argument is deprecated, use --container-count'
73
- CLI.logger.warn(m)
74
- container_count = Integer(more.first)
75
- else
76
- raise Thor::Error, 'Container count was passed via both ' \
77
- 'the --container-count keyword argument ' \
78
- 'and a positional argument. ' \
79
- 'Use only --container-count to proceed.'
80
- end
81
- else
82
- # Unfortunately, Thor does not want to let us easily hook into
83
- # its usage formatting, so we have to work around it here.
84
- command = thor.commands.fetch('apps:scale')
85
- error = ArgumentError.new
86
- args = [type] + more
87
- thor.handle_argument_error(command, error, args, 1)
88
- end
89
-
90
61
  if options[:size]
91
- if container_size.nil?
92
- m = 'Passing container size via the --size keyword ' \
93
- 'argument is deprecated, use --container-size'
94
- CLI.logger.warn(m)
95
- container_size = options[:size]
96
- else
97
- raise Thor::Error, 'Container size was passed via both ' \
98
- '--container-size and --size. ' \
99
- 'Use only --container-size to proceed.'
100
- end
62
+ m = 'You have used the "--size" option to specify a container '\
63
+ 'size. This abiguous option has been removed.'\
64
+ 'Please use the "--container-size" option, instead.'
65
+ raise Thor::Error, m
101
66
  end
102
67
 
103
68
  if container_count.nil? && container_size.nil?
@@ -39,16 +39,17 @@ module Aptible
39
39
  type: 'restore',
40
40
  handle: handle,
41
41
  container_size: options[:container_size],
42
- disk_size: options[:disk_size] || options[:size],
42
+ disk_size: options[:disk_size],
43
43
  destination_account: destination_account,
44
44
  key_arn: options[:key_arn]
45
45
  }.delete_if { |_, v| v.nil? }
46
46
 
47
- CLI.logger.warn([
48
- 'You have used the "--size" option to specify a disk size.',
49
- 'This option which be deprecated in a future version.',
50
- 'Please use the "--disk-size" option, instead.'
51
- ].join("\n")) if options[:size]
47
+ if options[:size]
48
+ m = 'You have used the "--size" option to specify a disk size.'\
49
+ 'This abiguous option has been removed.'\
50
+ 'Please use the "--disk-size" option, instead.'
51
+ raise Thor::Error, m
52
+ end
52
53
 
53
54
  operation = backup.create_operation!(opts)
54
55
  CLI.logger.info "Restoring backup into #{handle}"
@@ -123,7 +124,14 @@ module Aptible
123
124
 
124
125
  operation = backup.create_operation!(type: 'purge')
125
126
  CLI.logger.info "Purging backup #{backup_id}"
126
- attach_to_operation_logs(operation)
127
+ begin
128
+ attach_to_operation_logs(operation)
129
+ rescue HyperResource::ClientError => e
130
+ # A 404 here means that the operation completed successfully,
131
+ # and was removed faster than attach_to_operation_logs
132
+ # could attach to the logs.
133
+ raise if e.response.status != 404
134
+ end
127
135
  end
128
136
  end
129
137
  end
@@ -59,8 +59,8 @@ module Aptible
59
59
  option :type, type: :string
60
60
  option :version, type: :string
61
61
  option :container_size, type: :numeric
62
- option :size, type: :numeric
63
62
  option :disk_size, default: 10, type: :numeric
63
+ option :size, type: :numeric
64
64
  option :key_arn, type: :string
65
65
  option :environment
66
66
  define_method 'db:create' do |handle|
@@ -69,15 +69,16 @@ module Aptible
69
69
  db_opts = {
70
70
  handle: handle,
71
71
  initial_container_size: options[:container_size],
72
- initial_disk_size: options[:disk_size] || options[:size],
72
+ initial_disk_size: options[:disk_size],
73
73
  current_kms_arn: options[:key_arn]
74
74
  }.delete_if { |_, v| v.nil? }
75
75
 
76
- CLI.logger.warn([
77
- 'You have used the "--size" option to specify a disk size.',
78
- 'This option which be deprecated in a future version.',
79
- 'Please use the "--disk-size" option, instead.'
80
- ].join("\n")) if options[:size]
76
+ if options[:size]
77
+ m = 'You have used the "--size" option to specify a disk size.'\
78
+ 'This abiguous option has been removed.'\
79
+ 'Please use the "--disk-size" option, instead.'
80
+ raise Thor::Error, m
81
+ end
81
82
 
82
83
  type = options[:type]
83
84
  version = options[:version]
@@ -97,7 +98,7 @@ module Aptible
97
98
  op_opts = {
98
99
  type: 'provision',
99
100
  container_size: options[:container_size],
100
- disk_size: options[:disk_size] || options[:size]
101
+ disk_size: options[:disk_size]
101
102
  }.delete_if { |_, v| v.nil? }
102
103
  op = database.create_operation(op_opts)
103
104
 
@@ -156,17 +157,18 @@ module Aptible
156
157
  opts = {
157
158
  environment: options[:environment],
158
159
  container_size: options[:container_size],
159
- size: options[:disk_size] || options[:size],
160
+ size: options[:disk_size],
160
161
  logical: options[:logical],
161
162
  database_image: image || nil,
162
163
  key_arn: options[:key_arn]
163
164
  }.delete_if { |_, v| v.nil? }
164
165
 
165
- CLI.logger.warn([
166
- 'You have used the "--size" option to specify a disk size.',
167
- 'This option which be deprecated in a future version.',
168
- 'Please use the "--disk-size" option, instead.'
169
- ].join("\n")) if options[:size]
166
+ if options[:size]
167
+ m = 'You have used the "--size" option to specify a disk size.'\
168
+ 'This abiguous option has been removed.'\
169
+ 'Please use the "--disk-size" option, instead.'
170
+ raise Thor::Error, m
171
+ end
170
172
 
171
173
  database = replicate_database(source, dest_handle, opts)
172
174
  render_database(database.reload, database.account)
@@ -279,7 +281,7 @@ module Aptible
279
281
  end
280
282
 
281
283
  desc 'db:restart HANDLE ' \
282
- '[--container-size SIZE_MB] [--disk-size SIZE_GB]' \
284
+ '[--container-size SIZE_MB] [--disk-size SIZE_GB] ' \
283
285
  '[--iops IOPS] [--volume-type [gp2, gp3]]',
284
286
  'Restart a database'
285
287
  option :environment
@@ -294,16 +296,17 @@ module Aptible
294
296
  opts = {
295
297
  type: 'restart',
296
298
  container_size: options[:container_size],
297
- disk_size: options[:disk_size] || options[:size],
299
+ disk_size: options[:disk_size],
298
300
  provisioned_iops: options[:iops],
299
301
  ebs_volume_type: options[:volume_type]
300
302
  }.delete_if { |_, v| v.nil? }
301
303
 
302
- CLI.logger.warn([
303
- 'You have used the "--size" option to specify a disk size.',
304
- 'This option which be deprecated in a future version.',
305
- 'Please use the "--disk-size" option, instead.'
306
- ].join("\n")) if options[:size]
304
+ if options[:size]
305
+ m = 'You have used the "--size" option to specify a disk size.'\
306
+ 'This abiguous option has been removed.'\
307
+ 'Please use the "--disk-size" option, instead.'
308
+ raise Thor::Error, m
309
+ end
307
310
 
308
311
  CLI.logger.info "Restarting #{database.handle}..."
309
312
  op = database.create_operation!(opts)
@@ -0,0 +1,159 @@
1
+ module Aptible
2
+ module CLI
3
+ module Subcommands
4
+ module LogDrain
5
+ def self.included(thor)
6
+ thor.class_eval do
7
+ include Helpers::Token
8
+ include Helpers::Database
9
+ include Helpers::LogDrain
10
+
11
+ drain_flags = '--environment ENVIRONMENT ' \
12
+ '[--drain-apps true/false] ' \
13
+ '[--drain_databases true/false] ' \
14
+ '[--drain_ephemeral_sessions true/false] ' \
15
+ '[--drain_proxies true/false]'
16
+
17
+ def self.drain_options
18
+ option :drain_apps, default: true, type: :boolean
19
+ option :drain_databases, default: true, type: :boolean
20
+ option :drain_ephemeral_sessions, default: true, type: :boolean
21
+ option :drain_proxies, default: true, type: :boolean
22
+ option :environment
23
+ end
24
+
25
+ desc 'log_drain:list', 'List all Log Drains'
26
+ option :environment
27
+ define_method 'log_drain:list' do
28
+ Formatter.render(Renderer.current) do |root|
29
+ root.grouped_keyed_list(
30
+ { 'environment' => 'handle' },
31
+ 'handle'
32
+ ) do |node|
33
+ scoped_environments(options).each do |account|
34
+ account.log_drains.each do |drain|
35
+ node.object do |n|
36
+ ResourceFormatter.inject_log_drain(n, drain, account)
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+
44
+ desc 'log_drain:create:elasticsearch HANDLE '\
45
+ '--db DATABASE_HANDLE ' \
46
+ + drain_flags,
47
+ 'Create an Elasticsearch Log Drain'
48
+ drain_options
49
+ option :db, type: :string
50
+ option :pipeline, type: :string
51
+ define_method 'log_drain:create:elasticsearch' do |handle|
52
+ account = ensure_environment(options)
53
+ database = ensure_database(options)
54
+
55
+ opts = {
56
+ handle: handle,
57
+ database_id: database.id,
58
+ logging_token: options[:pipeline],
59
+ drain_apps: options[:drain_apps],
60
+ drain_databases: options[:drain_databases],
61
+ drain_ephemeral_sessions: options[:drain_ephemeral_sessions],
62
+ drain_proxies: options[:drain_proxies],
63
+ drain_type: :elasticsearch_database
64
+ }
65
+
66
+ create_log_drain(account, opts)
67
+ end
68
+
69
+ desc 'log_drain:create:datadog HANDLE ' \
70
+ '--url DATADOG_URL ' \
71
+ + drain_flags,
72
+ 'Create a Datadog Log Drain'
73
+ drain_options
74
+ option :url, type: :string
75
+ define_method 'log_drain:create:datadog' do |handle|
76
+ msg = 'Must be in the format of ' \
77
+ '"https://http-intake.logs.datadoghq.com' \
78
+ '/v1/input/<DD_API_KEY>".'
79
+ create_https_based_log_drain(handle, options, url_format_msg: msg)
80
+ end
81
+
82
+ desc 'log_drain:create:https HANDLE ' \
83
+ '--url URL ' \
84
+ + drain_flags,
85
+ 'Create a HTTPS Drain'
86
+ option :url, type: :string
87
+ drain_options
88
+ define_method 'log_drain:create:https' do |handle|
89
+ create_https_based_log_drain(handle, options)
90
+ end
91
+
92
+ desc 'log_drain:create:sumologic HANDLE ' \
93
+ '--url SUMOLOGIC_URL ' \
94
+ + drain_flags,
95
+ 'Create a Sumologic Drain'
96
+ option :url, type: :string
97
+ drain_options
98
+ define_method 'log_drain:create:sumologic' do |handle|
99
+ create_https_based_log_drain(handle, options)
100
+ end
101
+
102
+ desc 'log_drain:create:logdna HANDLE ' \
103
+ '--url LOGDNA_URL ' \
104
+ + drain_flags,
105
+ 'Create a LogDNA Log Drain'
106
+ option :url, type: :string
107
+ drain_options
108
+ define_method 'log_drain:create:logdna' do |handle|
109
+ msg = 'Must be in the format of ' \
110
+ '"https://logs.logdna.com/aptible/ingest/<INGESTION KEY>".'
111
+ create_https_based_log_drain(handle, options, url_format_msg: msg)
112
+ end
113
+
114
+ desc 'log_drain:create:papertrail HANDLE ' \
115
+ '--host PAPERTRAIL_HOST --port PAPERTRAIL_PORT ' \
116
+ + drain_flags,
117
+ 'Create a Papertrail Log Drain'
118
+ option :host, type: :string
119
+ option :port, type: :string
120
+ drain_options
121
+ define_method 'log_drain:create:papertrail' do |handle|
122
+ create_syslog_based_log_drain(handle, options)
123
+ end
124
+
125
+ desc 'log_drain:create:syslog HANDLE ' \
126
+ '--host SYSLOG_HOST --port SYSLOG_PORT ' \
127
+ '[--token TOKEN] ' \
128
+ + drain_flags,
129
+ 'Create a Papertrail Log Drain'
130
+ option :host, type: :string
131
+ option :port, type: :string
132
+ option :token, type: :string
133
+ drain_options
134
+ define_method 'log_drain:create:syslog' do |handle|
135
+ create_syslog_based_log_drain(handle, options)
136
+ end
137
+
138
+ desc 'log_drain:deprovision HANDLE --environment ENVIRONMENT',
139
+ 'Deprovisions a log drain'
140
+ option :environment
141
+ define_method 'log_drain:deprovision' do |handle|
142
+ account = ensure_environment(options)
143
+ drain = ensure_log_drain(account, handle)
144
+ op = drain.create_operation(type: :deprovision)
145
+ begin
146
+ attach_to_operation_logs(op)
147
+ rescue HyperResource::ClientError => e
148
+ # A 404 here means that the operation completed successfully,
149
+ # and was removed faster than attach_to_operation_logs
150
+ # could attach to the logs.
151
+ raise if e.response.status != 404
152
+ end
153
+ end
154
+ end
155
+ end
156
+ end
157
+ end
158
+ end
159
+ end
@@ -0,0 +1,137 @@
1
+ module Aptible
2
+ module CLI
3
+ module Subcommands
4
+ module MetricDrain
5
+ SITES = {
6
+ 'US1' => 'https://app.datadoghq.com',
7
+ 'US3' => 'https://us3.datadoghq.com',
8
+ 'EU1' => 'https://app.datadoghq.eu',
9
+ 'US1-FED' => 'https://app.ddog-gov.com'
10
+ }.freeze
11
+
12
+ def self.included(thor)
13
+ thor.class_eval do
14
+ include Helpers::Token
15
+ include Helpers::Database
16
+ include Helpers::MetricDrain
17
+
18
+ desc 'metric_drain:list', 'List all Metric Drains'
19
+ option :environment
20
+ define_method 'metric_drain:list' do
21
+ Formatter.render(Renderer.current) do |root|
22
+ root.grouped_keyed_list(
23
+ { 'environment' => 'handle' },
24
+ 'handle'
25
+ ) do |node|
26
+ scoped_environments(options).each do |account|
27
+ account.metric_drains.each do |drain|
28
+ node.object do |n|
29
+ ResourceFormatter.inject_metric_drain(n, drain, account)
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+ desc 'metric_drain:create:influxdb HANDLE '\
38
+ '--db DATABASE_HANDLE --environment ENVIRONMENT',
39
+ 'Create an InfluxDB Metric Drain'
40
+ option :db, type: :string
41
+ option :environment
42
+
43
+ define_method 'metric_drain:create:influxdb' do |handle|
44
+ account = ensure_environment(options)
45
+ database = ensure_database(options)
46
+
47
+ opts = {
48
+ handle: handle,
49
+ database_id: database.id,
50
+ drain_type: :influxdb_database
51
+ }
52
+
53
+ create_metric_drain(account, opts)
54
+ end
55
+
56
+ desc 'metric_drain:create:influxdb:custom HANDLE '\
57
+ '--username USERNAME --password PASSWORD ' \
58
+ '--url URL_INCLUDING_PORT',
59
+ 'Create an InfluxDB Metric Drain'
60
+ option :db, type: :string
61
+ option :username, type: :string
62
+ option :password, type: :string
63
+ option :url, type: :string
64
+ option :db, type: :string
65
+ option :environment
66
+ define_method 'metric_drain:create:influxdb:custom' do |handle|
67
+ account = ensure_environment(options)
68
+
69
+ config = {
70
+ address: options[:url],
71
+ username: options[:username],
72
+ password: options[:password],
73
+ database: options[:db]
74
+ }
75
+ opts = {
76
+ handle: handle,
77
+ drain_configuration: config,
78
+ drain_type: :influxdb
79
+ }
80
+
81
+ create_metric_drain(account, opts)
82
+ end
83
+
84
+ desc 'metric_drain:create:datadog HANDLE '\
85
+ '--api_key DATADOG_API_KEY --environment ENVIRONMENT',
86
+ 'Create a Datadog Metric Drain'
87
+ option :api_key, type: :string
88
+ option :site, type: :string
89
+ option :environment
90
+ define_method 'metric_drain:create:datadog' do |handle|
91
+ account = ensure_environment(options)
92
+
93
+ config = {
94
+ api_key: options[:api_key]
95
+ }
96
+ unless options[:site].nil?
97
+ site = SITES[options[:site]]
98
+
99
+ unless site
100
+ sites = SITES.keys.join(', ')
101
+ raise Thor::Error, 'Invalid Datadog site. ' \
102
+ "Valid options are #{sites}"
103
+ end
104
+
105
+ config[:series_url] = site
106
+ end
107
+ opts = {
108
+ handle: handle,
109
+ drain_type: :datadog,
110
+ drain_configuration: config
111
+ }
112
+
113
+ create_metric_drain(account, opts)
114
+ end
115
+
116
+ desc 'metric_drain:deprovision HANDLE --environment ENVIRONMENT',
117
+ 'Deprovisions a Metric Drain'
118
+ option :environment
119
+ define_method 'metric_drain:deprovision' do |handle|
120
+ account = ensure_environment(options)
121
+ drain = ensure_metric_drain(account, handle)
122
+ op = drain.create_operation(type: :deprovision)
123
+ begin
124
+ attach_to_operation_logs(op)
125
+ rescue HyperResource::ClientError => e
126
+ # A 404 here means that the operation completed successfully,
127
+ # and was removed faster than attach_to_operation_logs
128
+ # could attach to the logs.
129
+ raise if e.response.status != 404
130
+ end
131
+ end
132
+ end
133
+ end
134
+ end
135
+ end
136
+ end
137
+ end