morpheus-cli 2.10.0 → 2.10.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (80) hide show
  1. checksums.yaml +4 -4
  2. data/bin/morpheus +27 -32
  3. data/lib/morpheus/api/accounts_interface.rb +36 -47
  4. data/lib/morpheus/api/api_client.rb +141 -110
  5. data/lib/morpheus/api/app_templates_interface.rb +56 -72
  6. data/lib/morpheus/api/apps_interface.rb +111 -132
  7. data/lib/morpheus/api/auth_interface.rb +30 -0
  8. data/lib/morpheus/api/clouds_interface.rb +71 -76
  9. data/lib/morpheus/api/custom_instance_types_interface.rb +21 -46
  10. data/lib/morpheus/api/dashboard_interface.rb +10 -17
  11. data/lib/morpheus/api/deploy_interface.rb +60 -72
  12. data/lib/morpheus/api/deployments_interface.rb +53 -71
  13. data/lib/morpheus/api/groups_interface.rb +55 -45
  14. data/lib/morpheus/api/instance_types_interface.rb +19 -23
  15. data/lib/morpheus/api/instances_interface.rb +179 -177
  16. data/lib/morpheus/api/key_pairs_interface.rb +11 -17
  17. data/lib/morpheus/api/license_interface.rb +18 -23
  18. data/lib/morpheus/api/load_balancers_interface.rb +54 -69
  19. data/lib/morpheus/api/logs_interface.rb +25 -29
  20. data/lib/morpheus/api/options_interface.rb +13 -17
  21. data/lib/morpheus/api/provision_types_interface.rb +19 -22
  22. data/lib/morpheus/api/roles_interface.rb +75 -94
  23. data/lib/morpheus/api/security_group_rules_interface.rb +28 -37
  24. data/lib/morpheus/api/security_groups_interface.rb +39 -51
  25. data/lib/morpheus/api/servers_interface.rb +113 -115
  26. data/lib/morpheus/api/setup_interface.rb +31 -0
  27. data/lib/morpheus/api/task_sets_interface.rb +36 -38
  28. data/lib/morpheus/api/tasks_interface.rb +56 -69
  29. data/lib/morpheus/api/users_interface.rb +67 -76
  30. data/lib/morpheus/api/virtual_images_interface.rb +61 -61
  31. data/lib/morpheus/api/whoami_interface.rb +12 -15
  32. data/lib/morpheus/cli.rb +71 -60
  33. data/lib/morpheus/cli/accounts.rb +254 -315
  34. data/lib/morpheus/cli/alias_command.rb +219 -0
  35. data/lib/morpheus/cli/app_templates.rb +264 -272
  36. data/lib/morpheus/cli/apps.rb +608 -671
  37. data/lib/morpheus/cli/cli_command.rb +259 -21
  38. data/lib/morpheus/cli/cli_registry.rb +99 -14
  39. data/lib/morpheus/cli/clouds.rb +599 -372
  40. data/lib/morpheus/cli/config_file.rb +126 -0
  41. data/lib/morpheus/cli/credentials.rb +141 -117
  42. data/lib/morpheus/cli/dashboard_command.rb +48 -56
  43. data/lib/morpheus/cli/deployments.rb +254 -268
  44. data/lib/morpheus/cli/deploys.rb +150 -142
  45. data/lib/morpheus/cli/error_handler.rb +38 -0
  46. data/lib/morpheus/cli/groups.rb +551 -179
  47. data/lib/morpheus/cli/hosts.rb +862 -617
  48. data/lib/morpheus/cli/instance_types.rb +103 -95
  49. data/lib/morpheus/cli/instances.rb +1335 -1009
  50. data/lib/morpheus/cli/key_pairs.rb +82 -90
  51. data/lib/morpheus/cli/library.rb +498 -499
  52. data/lib/morpheus/cli/license.rb +83 -101
  53. data/lib/morpheus/cli/load_balancers.rb +314 -300
  54. data/lib/morpheus/cli/login.rb +66 -44
  55. data/lib/morpheus/cli/logout.rb +47 -46
  56. data/lib/morpheus/cli/mixins/accounts_helper.rb +69 -31
  57. data/lib/morpheus/cli/mixins/infrastructure_helper.rb +106 -0
  58. data/lib/morpheus/cli/mixins/print_helper.rb +181 -17
  59. data/lib/morpheus/cli/mixins/provisioning_helper.rb +535 -458
  60. data/lib/morpheus/cli/mixins/whoami_helper.rb +2 -2
  61. data/lib/morpheus/cli/option_parser.rb +35 -0
  62. data/lib/morpheus/cli/option_types.rb +232 -192
  63. data/lib/morpheus/cli/recent_activity_command.rb +61 -65
  64. data/lib/morpheus/cli/remote.rb +446 -199
  65. data/lib/morpheus/cli/roles.rb +884 -906
  66. data/lib/morpheus/cli/security_group_rules.rb +213 -203
  67. data/lib/morpheus/cli/security_groups.rb +237 -192
  68. data/lib/morpheus/cli/shell.rb +338 -231
  69. data/lib/morpheus/cli/tasks.rb +326 -308
  70. data/lib/morpheus/cli/users.rb +457 -462
  71. data/lib/morpheus/cli/version.rb +1 -1
  72. data/lib/morpheus/cli/version_command.rb +16 -18
  73. data/lib/morpheus/cli/virtual_images.rb +526 -345
  74. data/lib/morpheus/cli/whoami.rb +125 -111
  75. data/lib/morpheus/cli/workflows.rb +338 -185
  76. data/lib/morpheus/formatters.rb +8 -1
  77. data/lib/morpheus/logging.rb +1 -1
  78. data/lib/morpheus/rest_client.rb +17 -8
  79. metadata +9 -3
  80. data/lib/morpheus/api/custom_instance_types.rb +0 -55
@@ -11,157 +11,113 @@ class Morpheus::Cli::Apps
11
11
  include Morpheus::Cli::CliCommand
12
12
  include Morpheus::Cli::ProvisioningHelper
13
13
 
14
- def initialize()
15
- @appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance
16
- end
17
-
18
- def connect(opts)
19
- @access_token = Morpheus::Cli::Credentials.new(@appliance_name,@appliance_url).request_credentials()
20
- if @access_token.empty?
21
- print_red_alert "Invalid Credentials. Unable to acquire access token. Please verify your credentials and try again."
22
- exit 1
23
- end
24
- @api_client = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url)
25
- @apps_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).apps
26
- @instance_types_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).instance_types
27
- @instances_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).instances
14
+ register_subcommands :list, :get, :add, :update, :remove, :add_instance, :remove_instance, :logs, :firewall_disable, :firewall_enable, :security_groups, :apply_security_groups
15
+ alias_subcommand :details, :get
16
+ set_default_subcommand :list
17
+
18
+ def initialize()
19
+ # @appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance
20
+ end
21
+
22
+ def connect(opts)
23
+ @api_client = establish_remote_appliance_connection(opts)
24
+ @apps_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).apps
25
+ @instance_types_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).instance_types
26
+ @instances_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).instances
28
27
  @options_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).options
29
- @groups_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).groups
30
- @logs_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).logs
31
- @active_groups = ::Morpheus::Cli::Groups.load_group_file
32
- end
33
-
34
- def handle(args)
35
- usage = "Usage: morpheus apps [list,details,add,update,remove,add-instance,remove-instance,logs,firewall_disable,firewall_enable,security_groups,apply_security_groups] [name]"
36
- case args[0]
37
- when 'list'
38
- list(args[1..-1])
39
- when 'details'
40
- details(args[1..-1])
41
- when 'add'
42
- add(args[1..-1])
43
- when 'update'
44
- update(args[1..-1])
45
- when 'remove'
46
- remove(args[1..-1])
47
- when 'add-instance'
48
- add_instance(args[1..-1])
49
- when 'remove-instance'
50
- remove_instance(args[1..-1])
51
- when 'stop'
52
- stop(args[1..-1])
53
- when 'start'
54
- start(args[1..-1])
55
- when 'restart'
56
- restart(args[1..-1])
57
- when 'logs'
58
- logs(args[1..-1])
59
- # when 'stats'
60
- # stats(args[1..-1])
61
- # when 'envs'
62
- # envs(args[1..-1])
63
- # when 'setenv'
64
- # setenv(args[1..-1])
65
- # when 'delenv'
66
- # delenv(args[1..-1])
67
- when 'firewall_disable'
68
- firewall_disable(args[1..-1])
69
- when 'firewall_enable'
70
- firewall_enable(args[1..-1])
71
- when 'security_groups'
72
- security_groups(args[1..-1])
73
- when 'apply_security_groups'
74
- apply_security_groups(args[1..-1])
75
- else
76
- puts "\n#{usage}\n\n"
77
- exit 127
78
- end
79
- end
80
-
81
- def list(args)
82
- options = {}
83
- optparse = OptionParser.new do|opts|
84
- opts.banner = "Usage: morpheus apps list"
85
- build_common_options(opts, options, [:list, :json])
86
- end
87
- optparse.parse(args)
88
- connect(options)
89
- begin
28
+ @groups_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).groups
29
+ @logs_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).logs
30
+ @active_group_id = Morpheus::Cli::Groups.active_groups[@appliance_name]
31
+ end
32
+
33
+ def handle(args)
34
+ handle_subcommand(args)
35
+ end
36
+
37
+ def list(args)
38
+ options = {}
39
+ optparse = OptionParser.new do|opts|
40
+ opts.banner = subcommand_usage()
41
+ build_common_options(opts, options, [:list, :json, :dry_run])
42
+ end
43
+ optparse.parse!(args)
44
+ connect(options)
45
+ begin
90
46
  params = {}
91
- [:phrase, :offset, :max, :sort, :direction].each do |k|
92
- params[k] = options[k] unless options[k].nil?
93
- end
94
-
95
- json_response = @apps_interface.get(params)
96
-
97
- if options[:json]
98
- print JSON.pretty_generate(json_response)
99
- print "\n"
100
- return
101
- end
102
- apps = json_response['apps']
103
- print "\n" ,cyan, bold, "Morpheus Apps\n","==================", reset, "\n\n"
104
- if apps.empty?
105
- puts yellow,"No apps currently configured.",reset
106
- else
107
- print_apps_table(apps)
108
- end
109
- print reset,"\n\n"
110
- rescue RestClient::Exception => e
111
- print_rest_exception(e, options)
112
- exit 1
113
- end
114
- end
115
-
116
- def add(args)
117
- usage = "Usage: morpheus apps add"
47
+ [:phrase, :offset, :max, :sort, :direction].each do |k|
48
+ params[k] = options[k] unless options[k].nil?
49
+ end
50
+
51
+ if options[:dry_run]
52
+ print_dry_run @apps_interface.dry.get(params)
53
+ return
54
+ end
55
+
56
+ json_response = @apps_interface.get(params)
57
+ if options[:json]
58
+ print JSON.pretty_generate(json_response)
59
+ print "\n"
60
+ return
61
+ end
62
+ apps = json_response['apps']
63
+ print "\n" ,cyan, bold, "Morpheus Apps\n","==================", reset, "\n\n"
64
+ if apps.empty?
65
+ puts yellow,"No apps currently configured.",reset
66
+ else
67
+ print_apps_table(apps)
68
+ end
69
+ print reset,"\n"
70
+ rescue RestClient::Exception => e
71
+ print_rest_exception(e, options)
72
+ exit 1
73
+ end
74
+ end
75
+
76
+ def add(args)
118
77
  options = {}
119
78
  optparse = OptionParser.new do|opts|
120
- opts.banner = usage
121
- opts.on( '-g', '--group GROUP', "Group Name" ) do |val|
122
- options[:group_name] = val
123
- end
124
- opts.on( '-G', '--group-id ID', "Group Id" ) do |val|
125
- options[:group_id] = val
126
- end
127
- build_common_options(opts, options, [:options, :json, :dry_run])
79
+ opts.banner = subcommand_usage("[name]")
80
+ opts.on( '-g', '--group GROUP', "Group Name or ID" ) do |val|
81
+ options[:group] = val
82
+ end
83
+ build_common_options(opts, options, [:options, :json, :dry_run, :quiet])
128
84
  end
129
- optparse.parse(args)
85
+ optparse.parse!(args)
130
86
  connect(options)
131
87
  begin
132
-
133
- # use active group by default
134
- if !options[:group_name] && !options[:group_id]
135
- options[:group_id] = @active_groups[@appliance_name.to_sym]
136
- end
137
- group = find_group_from_options(options)
138
-
139
- payload = {
140
- 'app' => {}
141
- }
142
-
143
- v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'name', 'fieldLabel' => 'Name', 'type' => 'text', 'required' => true, 'description' => 'Enter a name for this app'}], options[:options])
144
- payload['app']['name'] = v_prompt['name']
145
- v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'description', 'fieldLabel' => 'Description', 'type' => 'text', 'required' => false}], options[:options])
146
- payload['app']['description'] = v_prompt['description']
147
- if group
148
- payload['app']['site'] = {id: group["id"]}
149
- else
150
- v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'group', 'fieldLabel' => 'Group', 'type' => 'select', 'selectOptions' => get_available_groups(), 'required' => true}], options[:options])
151
- payload['app']['site'] = {id: v_prompt["group"]}
152
- end
153
-
154
- # todo: allow adding instances with creation..
88
+ # use active group by default
89
+ options[:group] ||= @active_group_id
90
+ group = options[:group] ? find_group_by_name_or_id_for_provisioning(options[:group]) : nil
91
+
92
+ payload = {
93
+ 'app' => {}
94
+ }
95
+ if args[0]
96
+ payload['app']['name'] = args[0]
97
+ else
98
+ v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'name', 'fieldLabel' => 'Name', 'type' => 'text', 'required' => true, 'description' => 'Enter a name for this app'}], options[:options])
99
+ payload['app']['name'] = v_prompt['name']
100
+ end
101
+ v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'description', 'fieldLabel' => 'Description', 'type' => 'text', 'required' => false}], options[:options])
102
+ payload['app']['description'] = v_prompt['description']
103
+ if group
104
+ payload['app']['site'] = {id: group["id"]}
105
+ else
106
+ v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'group', 'fieldLabel' => 'Group', 'type' => 'select', 'selectOptions' => get_available_groups(), 'required' => true}], options[:options])
107
+ payload['app']['site'] = {id: v_prompt["group"]}
108
+ end
109
+
110
+ # todo: allow adding instances with creation..
155
111
 
156
112
  if options[:dry_run]
157
- print_dry_run("POST #{@appliance_url}/api/apps", payload)
113
+ print_dry_run @apps_interface.dry.create(payload)
158
114
  return
159
115
  end
160
116
  json_response = @apps_interface.create(payload)
161
117
  if options[:json]
162
118
  print JSON.pretty_generate(json_response)
163
119
  print "\n"
164
- else
120
+ elsif !options[:quiet]
165
121
  print_green_success "Added app #{payload['app']['name']}"
166
122
  list([])
167
123
  # details_options = [payload['app']['name']]
@@ -172,114 +128,120 @@ class Morpheus::Cli::Apps
172
128
  print_rest_exception(e, options)
173
129
  exit 1
174
130
  end
175
- end
176
-
177
- def details(args)
178
- options = {}
179
- optparse = OptionParser.new do|opts|
180
- opts.banner = "Usage: morpheus apps details [name]"
181
- build_common_options(opts, options, [:json])
182
- end
183
- optparse.parse(args)
184
- if args.count < 1
185
- puts "\n#{optparse.banner}\n\n"
186
- exit 1
187
- end
188
- connect(options)
189
- begin
190
- app = find_app_by_name_or_id(args[0])
191
- if options[:json]
192
- print JSON.pretty_generate({app: app})
193
- return
194
- end
195
-
196
- print "\n" ,cyan, bold, "App Details\n","==================", reset, "\n\n"
197
- print cyan
198
- puts "ID: #{app['id']}"
199
- puts "Name: #{app['name']}"
200
- puts "Description: #{app['description']}"
201
- puts "Account: #{app['account'] ? app['account']['name'] : ''}"
202
- # puts "Group: #{app['siteId']}"
203
-
204
- stats = app['stats']
205
- print cyan, "Memory: \t#{Filesize.from("#{stats['usedMemory']} B").pretty} / #{Filesize.from("#{stats['maxMemory']} B").pretty}\n"
206
- print cyan, "Storage: \t#{Filesize.from("#{stats['usedStorage']} B").pretty} / #{Filesize.from("#{stats['maxStorage']} B").pretty}\n\n",reset
207
-
208
- app_tiers = app['appTiers']
209
- if app_tiers.empty?
210
- puts yellow, "This app is empty", reset
211
- else
212
- app_tiers.each do |app_tier|
213
- print "\n" ,cyan, bold, "Tier: #{app_tier['tier']['name']}\n","==================", reset, "\n\n"
214
- print cyan
215
- instances = (app_tier['appInstances'] || []).collect {|it| it['instance']}
216
- if instances.empty?
217
- puts yellow, "This tier is empty", reset
218
- else
219
- instance_table = instances.collect do |instance|
220
- status_string = instance['status']
221
- if status_string == 'running'
222
- status_string = "#{green}#{status_string.upcase}#{cyan}"
223
- elsif status_string == 'stopped' or status_string == 'failed'
224
- status_string = "#{red}#{status_string.upcase}#{cyan}"
225
- elsif status_string == 'unknown'
226
- status_string = "#{white}#{status_string.upcase}#{cyan}"
227
- else
228
- status_string = "#{yellow}#{status_string.upcase}#{cyan}"
229
- end
230
- connection_string = ''
231
- if !instance['connectionInfo'].nil? && instance['connectionInfo'].empty? == false
232
- connection_string = "#{instance['connectionInfo'][0]['ip']}:#{instance['connectionInfo'][0]['port']}"
233
- end
234
- {id: instance['id'], name: instance['name'], connection: connection_string, environment: instance['instanceContext'], nodes: instance['containers'].count, status: status_string, type: instance['instanceType']['name'], group: !instance['group'].nil? ? instance['group']['name'] : nil, cloud: !instance['cloud'].nil? ? instance['cloud']['name'] : nil}
235
- end
236
- tp instance_table, :id, :name, :cloud, :type, :environment, :nodes, :connection, :status
237
- end
238
- end
239
- end
240
- print cyan
241
-
242
- print reset,"\n"
243
-
244
- rescue RestClient::Exception => e
245
- print_rest_exception(e, options)
246
- exit 1
247
- end
248
- end
249
-
250
- def update(args)
251
- usage = "Usage: morpheus apps update [name] [options]"
131
+ end
132
+
133
+ def get(args)
134
+ options = {}
135
+ optparse = OptionParser.new do|opts|
136
+ opts.banner = subcommand_usage("[name]")
137
+ build_common_options(opts, options, [:json, :dry_run])
138
+ end
139
+ optparse.parse!(args)
140
+ if args.count < 1
141
+ puts optparse
142
+ exit 1
143
+ end
144
+ connect(options)
145
+ begin
146
+ app = find_app_by_name_or_id(args[0])
147
+ if options[:dry_run]
148
+ print_dry_run @apps_interface.dry.get(app['id'])
149
+ return
150
+ end
151
+ json_response = @apps_interface.get(app['id'])
152
+ app = json_response['app']
153
+ if options[:json]
154
+ print JSON.pretty_generate(json_response)
155
+ return
156
+ end
157
+ print "\n" ,cyan, bold, "App Details\n","==================", reset, "\n\n"
158
+ print cyan
159
+ puts "ID: #{app['id']}"
160
+ puts "Name: #{app['name']}"
161
+ puts "Description: #{app['description']}"
162
+ puts "Account: #{app['account'] ? app['account']['name'] : ''}"
163
+ # puts "Group: #{app['siteId']}"
164
+ stats = app['stats']
165
+ if ((stats['maxMemory'].to_i != 0) || (stats['maxStorage'].to_i != 0))
166
+ print "\n"
167
+ # print cyan, "Memory: \t#{Filesize.from("#{stats['usedMemory']} B").pretty} / #{Filesize.from("#{stats['maxMemory']} B").pretty}\n"
168
+ # print cyan, "Storage: \t#{Filesize.from("#{stats['usedStorage']} B").pretty} / #{Filesize.from("#{stats['maxStorage']} B").pretty}\n\n",reset
169
+ print_stats_usage(stats, {include: [:memory, :storage]})
170
+ else
171
+ #print yellow, "No stat data.", reset
172
+ end
173
+
174
+ app_tiers = app['appTiers']
175
+ if app_tiers.empty?
176
+ puts yellow, "This app is empty", reset
177
+ else
178
+ app_tiers.each do |app_tier|
179
+ print "\n" ,cyan, bold, "Tier: #{app_tier['tier']['name']}\n","==================", reset, "\n\n"
180
+ print cyan
181
+ instances = (app_tier['appInstances'] || []).collect {|it| it['instance']}
182
+ if instances.empty?
183
+ puts yellow, "This tier is empty", reset
184
+ else
185
+ instance_table = instances.collect do |instance|
186
+ status_string = instance['status'].to_s
187
+ if status_string == 'running'
188
+ status_string = "#{green}#{status_string.upcase}#{cyan}"
189
+ elsif status_string == 'stopped' or status_string == 'failed'
190
+ status_string = "#{red}#{status_string.upcase}#{cyan}"
191
+ elsif status_string == 'unknown'
192
+ status_string = "#{white}#{status_string.upcase}#{cyan}"
193
+ else
194
+ status_string = "#{yellow}#{status_string.upcase}#{cyan}"
195
+ end
196
+ connection_string = ''
197
+ if !instance['connectionInfo'].nil? && instance['connectionInfo'].empty? == false
198
+ connection_string = "#{instance['connectionInfo'][0]['ip']}:#{instance['connectionInfo'][0]['port']}"
199
+ end
200
+ {id: instance['id'], name: instance['name'], connection: connection_string, environment: instance['instanceContext'], nodes: instance['containers'].count, status: status_string, type: instance['instanceType']['name'], group: !instance['group'].nil? ? instance['group']['name'] : nil, cloud: !instance['cloud'].nil? ? instance['cloud']['name'] : nil}
201
+ end
202
+ tp instance_table, :id, :name, :cloud, :type, :environment, :nodes, :connection, :status
203
+ end
204
+ end
205
+ end
206
+ print cyan
207
+
208
+ print reset,"\n"
209
+
210
+ rescue RestClient::Exception => e
211
+ print_rest_exception(e, options)
212
+ exit 1
213
+ end
214
+ end
215
+
216
+ def update(args)
252
217
  options = {}
253
218
  optparse = OptionParser.new do|opts|
254
- opts.banner = usage
219
+ opts.banner = subcommand_usage("[name]")
255
220
  build_common_options(opts, options, [:options, :json, :dry_run])
256
221
  end
257
- optparse.parse(args)
222
+ optparse.parse!(args)
258
223
  if args.count < 1
259
- puts "\n#{optparse.banner}\n\n"
260
- exit 1
261
- end
224
+ puts optparse
225
+ exit 1
226
+ end
262
227
  connect(options)
263
228
 
264
229
  begin
265
-
266
- app = find_app_by_name_or_id(args[0])
267
-
268
- # group = find_group_from_options(options)
230
+ app = find_app_by_name_or_id(args[0])
269
231
 
270
- payload = {
271
- 'app' => {id: app["id"]}
272
- }
232
+ payload = {
233
+ 'app' => {id: app["id"]}
234
+ }
273
235
 
274
- update_app_option_types = [
275
- {'fieldName' => 'name', 'fieldLabel' => 'Name', 'type' => 'text', 'required' => true, 'description' => 'Enter a name for this app'},
276
- {'fieldName' => 'description', 'fieldLabel' => 'Description', 'type' => 'text', 'required' => false}
277
- ]
236
+ update_app_option_types = [
237
+ {'fieldName' => 'name', 'fieldLabel' => 'Name', 'type' => 'text', 'required' => true, 'description' => 'Enter a name for this app'},
238
+ {'fieldName' => 'description', 'fieldLabel' => 'Description', 'type' => 'text', 'required' => false}
239
+ ]
278
240
 
279
- params = options[:options] || {}
241
+ params = options[:options] || {}
280
242
 
281
243
  if params.empty?
282
- puts "\n#{usage}\n"
244
+ puts opts
283
245
  option_lines = update_app_option_types.collect {|it| "\t-O #{it['fieldName']}=\"value\"" }.join("\n")
284
246
  puts "\nAvailable Options:\n#{option_lines}\n\n"
285
247
  exit 1
@@ -291,9 +253,10 @@ class Morpheus::Cli::Apps
291
253
  payload['app'].merge!(params)
292
254
 
293
255
  if options[:dry_run]
294
- print_dry_run("PUT #{@appliance_url}/api/apps/#{app['id']}", payload)
256
+ print_dry_run @apps_interface.dry.update(app["id"], payload)
295
257
  return
296
258
  end
259
+
297
260
  json_response = @apps_interface.update(app["id"], payload)
298
261
  if options[:json]
299
262
  print JSON.pretty_generate(json_response)
@@ -309,54 +272,52 @@ class Morpheus::Cli::Apps
309
272
  print_rest_exception(e, options)
310
273
  exit 1
311
274
  end
312
- end
275
+ end
313
276
 
314
277
 
315
- def add_instance(args)
316
- usage = "Usage: morpheus apps add-instance [name] [instance] [tier]"
278
+ def add_instance(args)
317
279
  options = {}
318
280
  optparse = OptionParser.new do|opts|
319
- opts.banner = usage
281
+ opts.banner = subcommand_usage("[name] [instance] [tier]")
320
282
  build_common_options(opts, options, [:options, :json, :dry_run])
321
283
  end
322
- optparse.parse(args)
284
+ optparse.parse!(args)
323
285
  if args.count < 1
324
- puts "\n#{optparse.banner}\n\n"
325
- exit 1
326
- end
327
- # optional [tier] and [instance] arguments
328
- if args[1] && args[1] !~ /\A\-/
329
- options[:instance_name] = args[1]
330
- if args[2] && args[2] !~ /\A\-/
331
- options[:tier_name] = args[2]
332
- end
333
- end
286
+ puts optparse
287
+ exit 1
288
+ end
289
+ # optional [tier] and [instance] arguments
290
+ if args[1] && args[1] !~ /\A\-/
291
+ options[:instance_name] = args[1]
292
+ if args[2] && args[2] !~ /\A\-/
293
+ options[:tier_name] = args[2]
294
+ end
295
+ end
334
296
  connect(options)
335
297
  begin
336
-
337
- app = find_app_by_name_or_id(args[0])
298
+ app = find_app_by_name_or_id(args[0])
338
299
 
339
- # Only supports adding an existing instance right now..
300
+ # Only supports adding an existing instance right now..
340
301
 
341
- payload = {}
302
+ payload = {}
342
303
 
343
- if options[:instance_name]
344
- instance = find_instance_by_name_or_id(options[:instance_name])
345
- else
346
- v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'instance', 'fieldLabel' => 'Instance', 'type' => 'text', 'required' => true, 'description' => 'Enter the instance name or id'}], options[:options])
347
- instance = find_instance_by_name_or_id(v_prompt['instance'])
348
- end
349
- payload[:instanceId] = instance['id']
304
+ if options[:instance_name]
305
+ instance = find_instance_by_name_or_id(options[:instance_name])
306
+ else
307
+ v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'instance', 'fieldLabel' => 'Instance', 'type' => 'text', 'required' => true, 'description' => 'Enter the instance name or id'}], options[:options])
308
+ instance = find_instance_by_name_or_id(v_prompt['instance'])
309
+ end
310
+ payload[:instanceId] = instance['id']
350
311
 
351
- if options[:tier_name]
352
- payload[:tierName] = options[:tier_name]
353
- else
354
- v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'tier', 'fieldLabel' => 'Tier', 'type' => 'text', 'required' => true, 'description' => 'Enter the name of the tier'}], options[:options])
355
- payload[:tierName] = v_prompt['tier']
356
- end
312
+ if options[:tier_name]
313
+ payload[:tierName] = options[:tier_name]
314
+ else
315
+ v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'tier', 'fieldLabel' => 'Tier', 'type' => 'text', 'required' => true, 'description' => 'Enter the name of the tier'}], options[:options])
316
+ payload[:tierName] = v_prompt['tier']
317
+ end
357
318
 
358
319
  if options[:dry_run]
359
- print_dry_run("POST #{@appliance_url}/api/apps/#{app['id']}/add-instance", payload)
320
+ print_dry_run @apps_interface.dry.add_instance(app['id'], payload)
360
321
  return
361
322
  end
362
323
  json_response = @apps_interface.add_instance(app['id'], payload)
@@ -374,67 +335,75 @@ class Morpheus::Cli::Apps
374
335
  print_rest_exception(e, options)
375
336
  exit 1
376
337
  end
377
- end
378
-
379
- def remove(args)
380
- options = {}
381
- optparse = OptionParser.new do|opts|
382
- opts.banner = "Usage: morpheus apps remove [name]"
383
- build_common_options(opts, options, [:json])
384
- end
385
- optparse.parse(args)
386
- if args.count < 1
387
- puts "\n#{optparse.banner}\n\n"
388
- exit 1
389
- end
390
- connect(options)
391
-
392
- begin
393
- app = find_app_by_name_or_id(args[0])
394
- unless options[:yes] || ::Morpheus::Cli::OptionTypes::confirm("Are you sure you would like to remove the app '#{app['name']}'?", options)
395
- exit 1
396
- end
397
- @apps_interface.destroy(app_results['app']['id'])
398
- list([])
399
- rescue RestClient::Exception => e
400
- print_rest_exception(e, options)
401
- exit 1
402
- end
403
- end
404
-
405
- def remove_instance(args)
406
- usage = "Usage: morpheus apps remove-instance [name] [instance]"
338
+ end
339
+
340
+ def remove(args)
341
+ options = {}
342
+ optparse = OptionParser.new do|opts|
343
+ opts.banner = subcommand_usage("[name]")
344
+ build_common_options(opts, options, [:json, :dry_run, :quiet])
345
+ end
346
+ optparse.parse!(args)
347
+ if args.count < 1
348
+ puts optparse
349
+ exit 1
350
+ end
351
+ connect(options)
352
+
353
+ begin
354
+ app = find_app_by_name_or_id(args[0])
355
+ unless options[:yes] || ::Morpheus::Cli::OptionTypes::confirm("Are you sure you would like to remove the app '#{app['name']}'?", options)
356
+ exit 1
357
+ end
358
+ if options[:dry_run]
359
+ print_dry_run @apps_interface.dry.destroy(app['id'])
360
+ return
361
+ end
362
+ json_response = @apps_interface.destroy(app['id'])
363
+ if options[:json]
364
+ print JSON.pretty_generate(json_response)
365
+ print "\n"
366
+ elsif !options[:quiet]
367
+ print_green_success "Removed app #{app['name']}"
368
+ list([])
369
+ end
370
+ rescue RestClient::Exception => e
371
+ print_rest_exception(e, options)
372
+ exit 1
373
+ end
374
+ end
375
+
376
+ def remove_instance(args)
407
377
  options = {}
408
378
  optparse = OptionParser.new do|opts|
409
- opts.banner = usage
379
+ opts.banner = subcommand_usage("[name] [instance]")
410
380
  build_common_options(opts, options, [:options, :json, :dry_run])
411
381
  end
412
- optparse.parse(args)
382
+ optparse.parse!(args)
413
383
  if args.count < 1
414
- puts "\n#{optparse.banner}\n\n"
415
- exit 1
416
- end
417
- # optional [tier] and [instance] arguments
418
- if args[1] && args[1] !~ /\A\-/
419
- options[:instance_name] = args[1]
420
- end
384
+ puts optparse
385
+ exit 1
386
+ end
387
+ # optional [tier] and [instance] arguments
388
+ if args[1] && args[1] !~ /\A\-/
389
+ options[:instance_name] = args[1]
390
+ end
421
391
  connect(options)
422
392
  begin
423
-
424
- app = find_app_by_name_or_id(args[0])
393
+ app = find_app_by_name_or_id(args[0])
425
394
 
426
- payload = {}
395
+ payload = {}
427
396
 
428
- if options[:instance_name]
429
- instance = find_instance_by_name_or_id(options[:instance_name])
430
- else
431
- v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'instance', 'fieldLabel' => 'Instance', 'type' => 'text', 'required' => true, 'description' => 'Enter the instance name or id'}], options[:options])
432
- instance = find_instance_by_name_or_id(v_prompt['instance'])
433
- end
434
- payload[:instanceId] = instance['id']
397
+ if options[:instance_name]
398
+ instance = find_instance_by_name_or_id(options[:instance_name])
399
+ else
400
+ v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'instance', 'fieldLabel' => 'Instance', 'type' => 'text', 'required' => true, 'description' => 'Enter the instance name or id'}], options[:options])
401
+ instance = find_instance_by_name_or_id(v_prompt['instance'])
402
+ end
403
+ payload[:instanceId] = instance['id']
435
404
 
436
405
  if options[:dry_run]
437
- print_dry_run("POST #{@appliance_url}/api/apps/#{app['id']}/remove-instance", payload)
406
+ print_dry_run @apps_interface.dry.remove_instance(app['id'], payload)
438
407
  return
439
408
  end
440
409
 
@@ -454,377 +423,345 @@ class Morpheus::Cli::Apps
454
423
  print_rest_exception(e, options)
455
424
  exit 1
456
425
  end
457
- end
458
-
459
- def logs(args)
460
- options = {}
461
- optparse = OptionParser.new do|opts|
462
- opts.banner = "Usage: morpheus apps logs [name]"
463
- build_common_options(opts, options, [:list, :json])
464
- end
465
- optparse.parse(args)
466
- if args.count < 1
467
- puts "\n#{optparse.banner}\n\n"
468
- exit 1
469
- end
470
- connect(options)
471
- begin
472
- app = find_app_by_name_or_id(args[0])
473
- containers = []
474
- app['appTiers'].each do |app_tier|
475
- app_tier['appInstances'].each do |app_instance|
476
- containers += app_instance['instance']['containers']
477
- end
478
- end
479
- params = {}
480
- [:phrase, :offset, :max, :sort, :direction].each do |k|
481
- params[k] = options[k] unless options[k].nil?
482
- end
483
- logs = @logs_interface.container_logs(containers, params)
484
- if options[:json]
485
- print JSON.pretty_generate(logs)
486
- print "\n"
487
- else
488
- logs['data'].reverse.each do |log_entry|
489
- log_level = ''
490
- case log_entry['level']
491
- when 'INFO'
492
- log_level = "#{blue}#{bold}INFO#{reset}"
493
- when 'DEBUG'
494
- log_level = "#{white}#{bold}DEBUG#{reset}"
495
- when 'WARN'
496
- log_level = "#{yellow}#{bold}WARN#{reset}"
497
- when 'ERROR'
498
- log_level = "#{red}#{bold}ERROR#{reset}"
499
- when 'FATAL'
500
- log_level = "#{red}#{bold}FATAL#{reset}"
501
- end
502
- puts "[#{log_entry['ts']}] #{log_level} - #{log_entry['message']}"
503
- end
504
- print reset,"\n"
505
- end
506
- rescue RestClient::Exception => e
507
- print_rest_exception(e, options)
508
- exit 1
509
- end
510
- end
426
+ end
427
+
428
+ def logs(args)
429
+ options = {}
430
+ optparse = OptionParser.new do|opts|
431
+ opts.banner = subcommand_usage("[name]")
432
+ build_common_options(opts, options, [:list, :json, :dry_run])
433
+ end
434
+ optparse.parse!(args)
435
+ if args.count < 1
436
+ puts optparse
437
+ exit 1
438
+ end
439
+ connect(options)
440
+ begin
441
+ app = find_app_by_name_or_id(args[0])
442
+ containers = []
443
+ app['appTiers'].each do |app_tier|
444
+ app_tier['appInstances'].each do |app_instance|
445
+ containers += app_instance['instance']['containers']
446
+ end
447
+ end
448
+ params = {}
449
+ [:phrase, :offset, :max, :sort, :direction].each do |k|
450
+ params[k] = options[k] unless options[k].nil?
451
+ end
452
+ if options[:dry_run]
453
+ print_dry_run @logs_interface.dry.container_logs(containers, params)
454
+ return
455
+ end
456
+ logs = @logs_interface.container_logs(containers, params)
457
+ if options[:json]
458
+ print JSON.pretty_generate(logs)
459
+ print "\n"
460
+ else
461
+ logs['data'].reverse.each do |log_entry|
462
+ log_level = ''
463
+ case log_entry['level']
464
+ when 'INFO'
465
+ log_level = "#{blue}#{bold}INFO#{reset}"
466
+ when 'DEBUG'
467
+ log_level = "#{white}#{bold}DEBUG#{reset}"
468
+ when 'WARN'
469
+ log_level = "#{yellow}#{bold}WARN#{reset}"
470
+ when 'ERROR'
471
+ log_level = "#{red}#{bold}ERROR#{reset}"
472
+ when 'FATAL'
473
+ log_level = "#{red}#{bold}FATAL#{reset}"
474
+ end
475
+ puts "[#{log_entry['ts']}] #{log_level} - #{log_entry['message']}"
476
+ end
477
+ print reset,"\n"
478
+ end
479
+ rescue RestClient::Exception => e
480
+ print_rest_exception(e, options)
481
+ exit 1
482
+ end
483
+ end
511
484
 
512
485
  =begin
513
- def stop(args)
514
- options = {}
515
- optparse = OptionParser.new do|opts|
516
- opts.banner = "Usage: morpheus apps stop [name]"
517
- build_common_options(opts, options, [:json])
518
- end
519
- optparse.parse(args)
520
- if args.count < 1
521
- puts "\n#{optparse.banner}\n\n"
522
- exit 1
523
- end
524
- connect(options)
525
- begin
526
- app_results = @apps_interface.get({name: args[0]})
527
- if app_results['apps'].empty?
528
- print_red_alert "App not found by name #{args[0]}"
529
- exit 1
530
- end
531
- @apps_interface.stop(app_results['apps'][0]['id'])
532
- list([])
533
- rescue RestClient::Exception => e
534
- print_rest_exception(e, options)
535
- exit 1
536
- end
537
- end
538
-
539
- def start(args)
540
- options = {}
541
- optparse = OptionParser.new do|opts|
542
- opts.banner = "Usage: morpheus apps start [name]"
543
- build_common_options(opts, options, [:json])
544
- end
545
- optparse.parse(args)
546
- if args.count < 1
547
- puts "\n#{optparse.banner}\n\n"
548
- exit 1
549
- end
550
- connect(options)
551
- begin
552
- app_results = @apps_interface.get({name: args[0]})
553
- if app_results['apps'].empty?
554
- print_red_alert "App not found by name #{args[0]}"
555
- exit 1
556
- end
557
- @apps_interface.start(app_results['apps'][0]['id'])
558
- list([])
559
- rescue RestClient::Exception => e
560
- print_rest_exception(e, options)
561
- exit 1
562
- end
563
- end
564
-
565
- def restart(args)
566
- options = {}
567
- optparse = OptionParser.new do|opts|
568
- opts.banner = "Usage: morpheus apps restart [name]"
569
- build_common_options(opts, options, [:json])
570
- end
571
- optparse.parse(args)
572
- if args.count < 1
573
- puts "\n#{optparse.banner}\n\n"
574
- exit 1
575
- end
576
- connect(options)
577
- begin
578
- app_results = @apps_interface.get({name: args[0]})
579
- if app_results['apps'].empty?
580
- print_red_alert "App not found by name #{args[0]}"
581
- exit 1
582
- end
583
- @apps_interface.restart(app_results['apps'][0]['id'])
584
- list([])
585
- rescue RestClient::Exception => e
586
- print_rest_exception(e, options)
587
- exit 1
588
- end
589
- end
486
+ def stop(args)
487
+ options = {}
488
+ optparse = OptionParser.new do|opts|
489
+ opts.banner = subcommand_usage("[name]")
490
+ build_common_options(opts, options, [:json, :dry_run])
491
+ end
492
+ optparse.parse!(args)
493
+ if args.count < 1
494
+ puts optparse
495
+ exit 1
496
+ end
497
+ connect(options)
498
+ begin
499
+ app = find_app_by_name_or_id(args[0])
500
+ if options[:dry_run]
501
+ print_dry_run @apps_interface.dry.stop(app['id'])
502
+ return
503
+ end
504
+ @apps_interface.stop(app['id'])
505
+ list([])
506
+ rescue RestClient::Exception => e
507
+ print_rest_exception(e, options)
508
+ exit 1
509
+ end
510
+ end
511
+
512
+ def start(args)
513
+ options = {}
514
+ optparse = OptionParser.new do|opts|
515
+ opts.banner = subcommand_usage("[name]")
516
+ build_common_options(opts, options, [:json, :dry_run])
517
+ end
518
+ optparse.parse!(args)
519
+ if args.count < 1
520
+ puts optparse
521
+ exit 1
522
+ end
523
+ connect(options)
524
+ begin
525
+ app = find_app_by_name_or_id(args[0])
526
+ if options[:dry_run]
527
+ print_dry_run @apps_interface.dry.start(app['id'])
528
+ return
529
+ end
530
+ @apps_interface.start(app['id'])
531
+ list([])
532
+ rescue RestClient::Exception => e
533
+ print_rest_exception(e, options)
534
+ exit 1
535
+ end
536
+ end
537
+
538
+ def restart(args)
539
+ options = {}
540
+ optparse = OptionParser.new do|opts|
541
+ opts.banner = subcommand_usage("[name]")
542
+ build_common_options(opts, options, [:json, :dry_run])
543
+ end
544
+ optparse.parse!(args)
545
+ if args.count < 1
546
+ puts optparse
547
+ exit 1
548
+ end
549
+ connect(options)
550
+ begin
551
+ app = find_app_by_name_or_id(args[0])
552
+ if options[:dry_run]
553
+ print_dry_run @apps_interface.dry.restart(app['id'])
554
+ return
555
+ end
556
+ @apps_interface.restart(app['id'])
557
+ list([])
558
+ rescue RestClient::Exception => e
559
+ print_rest_exception(e, options)
560
+ exit 1
561
+ end
562
+ end
590
563
  =end
591
564
 
592
- def firewall_disable(args)
593
- options = {}
594
- optparse = OptionParser.new do|opts|
595
- opts.banner = "Usage: morpheus apps firewall_disable [name]"
596
- build_common_options(opts, options, [:json])
597
- end
598
- optparse.parse(args)
599
- if args.count < 1
600
- puts "\n#{optparse.banner}\n\n"
601
- exit 1
602
- end
603
- connect(options)
604
-
605
- begin
606
- app_results = @apps_interface.get({name: args[0]})
607
- if app_results['apps'].empty?
608
- print_red_alert "App not found by name #{args[0]}"
609
- exit 1
610
- end
611
- @apps_interface.firewall_disable(app_results['apps'][0]['id'])
612
- security_groups([args[0]])
613
- rescue RestClient::Exception => e
614
- print_rest_exception(e, options)
615
- exit 1
616
- end
617
- end
618
-
619
- def firewall_enable(args)
620
- options = {}
621
- optparse = OptionParser.new do|opts|
622
- opts.banner = "Usage: morpheus apps firewall_enable [name]"
623
- build_common_options(opts, options, [:json])
624
- end
625
- optparse.parse(args)
626
- if args.count < 1
627
- puts "\n#{optparse.banner}\n\n"
628
- exit 1
629
- end
630
- connect(options)
631
-
632
- begin
633
- app_results = @apps_interface.get({name: args[0]})
634
- if app_results['apps'].empty?
635
- print_red_alert "App not found by name #{args[0]}"
636
- exit 1
637
- end
638
- @apps_interface.firewall_enable(app_results['apps'][0]['id'])
639
- security_groups([args[0]])
640
- rescue RestClient::Exception => e
641
- print_rest_exception(e, options)
642
- exit 1
643
- end
644
- end
645
-
646
- def security_groups(args)
647
- options = {}
648
- optparse = OptionParser.new do|opts|
649
- opts.banner = "Usage: morpheus apps security_groups [name]"
650
- build_common_options(opts, options, [:json])
651
- end
652
- optparse.parse(args)
653
- if args.count < 1
654
- puts "\n#{optparse.banner}\n\n"
655
- exit 1
656
- end
657
- connect(options)
658
-
659
- begin
660
- app_results = @apps_interface.get({name: args[0]})
661
- if app_results['apps'].empty?
662
- print_red_alert "App not found by name #{args[0]}"
663
- exit 1
664
- end
665
-
666
- app_id = app_results['apps'][0]['id']
667
- json_response = @apps_interface.security_groups(app_id)
668
-
669
- securityGroups = json_response['securityGroups']
670
- print "\n" ,cyan, bold, "Morpheus Security Groups for App:#{app_id}\n","==================", reset, "\n\n"
671
- print cyan, "Firewall Enabled=#{json_response['firewallEnabled']}\n\n"
672
- if securityGroups.empty?
673
- puts yellow,"No security groups currently applied.",reset
674
- else
675
- securityGroups.each do |securityGroup|
676
- print cyan, "= #{securityGroup['id']} (#{securityGroup['name']}) - (#{securityGroup['description']})\n"
677
- end
678
- end
679
- print reset,"\n\n"
680
-
681
- rescue RestClient::Exception => e
682
- print_rest_exception(e, options)
683
- exit 1
684
- end
685
- end
686
-
687
- def apply_security_groups(args)
688
- usage = <<-EOF
689
- Usage: morpheus apps apply_security_groups [name]
690
- EOF
691
-
692
- options = {}
693
- clear_or_secgroups_specified = false
694
- optparse = OptionParser.new do|opts|
695
- opts.banner = usage
696
- opts.on( '-c', '--clear', "Clear all security groups" ) do
697
- options[:securityGroupIds] = []
698
- clear_or_secgroups_specified = true
699
- end
700
- opts.on( '-s', '--secgroups SECGROUPS', "Apply the specified comma separated security group ids" ) do |secgroups|
701
- options[:securityGroupIds] = secgroups.split(",")
702
- clear_or_secgroups_specified = true
703
- end
704
- opts.on( '-h', '--help', "Prints this help" ) do
705
- puts opts
706
- exit
707
- end
708
- build_common_options(opts, options, [:json])
709
- end
710
- optparse.parse(args)
711
- if args.count < 1
712
- puts "\n#{optparse.banner}\n\n"
713
- exit 1
714
- end
715
- if !clear_or_secgroups_specified
716
- puts usage
717
- exit 1
718
- end
719
-
720
- connect(options)
721
-
722
- begin
723
- app_results = @apps_interface.get({name: args[0]})
724
- if app_results['apps'].empty?
725
- print_red_alert "App not found by name #{args[0]}"
726
- exit 1
727
- end
728
-
729
- @apps_interface.apply_security_groups(app_results['apps'][0]['id'], options)
730
- security_groups([args[0]])
731
- rescue RestClient::Exception => e
732
- print_rest_exception(e, options)
733
- exit 1
734
- end
735
- end
736
-
737
- private
738
-
739
- def find_app_by_id(id)
740
- app_results = @apps_interface.get(id.to_i)
741
- if app_results['app'].empty?
742
- print_red_alert "App not found by id #{id}"
743
- exit 1
744
- end
745
- return app_results['app']
746
- end
747
-
748
- def find_app_by_name(name)
749
- app_results = @apps_interface.get({name: name})
750
- if app_results['apps'].empty?
751
- print_red_alert "App not found by name #{name}"
752
- exit 1
753
- end
754
- return app_results['apps'][0]
755
- end
756
-
757
- def find_app_by_name_or_id(val)
758
- if val.to_s =~ /\A\d{1,}\Z/
759
- return find_app_by_id(val)
760
- else
761
- return find_app_by_name(val)
762
- end
763
- end
764
-
765
- def find_instance_by_id(id)
766
- instance_results = @instances_interface.get(id.to_i)
767
- if instance_results['instance'].empty?
768
- print_red_alert "Instance not found by id #{id}"
769
- exit 1
770
- end
771
- return instance_results['instance']
772
- end
773
-
774
- def find_instance_by_name(name)
775
- instance_results = @instances_interface.get({name: name})
776
- if instance_results['instances'].empty?
777
- print_red_alert "Instance not found by name #{name}"
778
- exit 1
779
- end
780
- return instance_results['instances'][0]
781
- end
782
-
783
- def find_instance_by_name_or_id(val)
784
- if val.to_s =~ /\A\d{1,}\Z/
785
- return find_instance_by_id(val)
786
- else
787
- return find_instance_by_name(val)
788
- end
789
- end
790
-
791
- def print_apps_table(apps, opts={})
565
+ def firewall_disable(args)
566
+ options = {}
567
+ optparse = OptionParser.new do|opts|
568
+ opts.banner = subcommand_usage("[name]")
569
+ build_common_options(opts, options, [:json, :dry_run])
570
+ end
571
+ optparse.parse!(args)
572
+ if args.count < 1
573
+ puts optparse
574
+ exit 1
575
+ end
576
+ connect(options)
577
+
578
+ begin
579
+ app = find_app_by_name_or_id(args[0])
580
+ if options[:dry_run]
581
+ print_dry_run @apps_interface.dry.firewall_disable(app['id'])
582
+ return
583
+ end
584
+ @apps_interface.firewall_disable(app['id'])
585
+ security_groups([args[0]])
586
+ rescue RestClient::Exception => e
587
+ print_rest_exception(e, options)
588
+ exit 1
589
+ end
590
+ end
591
+
592
+ def firewall_enable(args)
593
+ options = {}
594
+ optparse = OptionParser.new do|opts|
595
+ opts.banner = subcommand_usage("[name]")
596
+ build_common_options(opts, options, [:json, :dry_run])
597
+ end
598
+ optparse.parse!(args)
599
+ if args.count < 1
600
+ puts optparse
601
+ exit 1
602
+ end
603
+ connect(options)
604
+
605
+ begin
606
+ app = find_app_by_name_or_id(args[0])
607
+ if options[:dry_run]
608
+ print_dry_run @apps_interface.dry.firewall_enable(app['id'])
609
+ return
610
+ end
611
+ @apps_interface.firewall_enable(app['id'])
612
+ security_groups([args[0]])
613
+ rescue RestClient::Exception => e
614
+ print_rest_exception(e, options)
615
+ exit 1
616
+ end
617
+ end
618
+
619
+ def security_groups(args)
620
+ options = {}
621
+ optparse = OptionParser.new do|opts|
622
+ opts.banner = subcommand_usage("[name]")
623
+ build_common_options(opts, options, [:json, :dry_run])
624
+ end
625
+ optparse.parse!(args)
626
+ if args.count < 1
627
+ puts optparse
628
+ exit 1
629
+ end
630
+ connect(options)
631
+
632
+ begin
633
+ app = find_app_by_name_or_id(args[0])
634
+ if options[:dry_run]
635
+ print_dry_run @apps_interface.dry.security_groups(app['id'])
636
+ return
637
+ end
638
+ json_response = @apps_interface.security_groups(app['id'])
639
+ securityGroups = json_response['securityGroups']
640
+ print "\n" ,cyan, bold, "Morpheus Security Groups for App: #{app['name']}\n","==================", reset, "\n\n"
641
+ print cyan, "Firewall Enabled=#{json_response['firewallEnabled']}\n\n"
642
+ if securityGroups.empty?
643
+ puts yellow,"No security groups currently applied.",reset
644
+ else
645
+ securityGroups.each do |securityGroup|
646
+ print cyan, "= #{securityGroup['id']} (#{securityGroup['name']}) - (#{securityGroup['description']})\n"
647
+ end
648
+ end
649
+ print reset,"\n"
650
+
651
+ rescue RestClient::Exception => e
652
+ print_rest_exception(e, options)
653
+ exit 1
654
+ end
655
+ end
656
+
657
+ def apply_security_groups(args)
658
+ options = {}
659
+ clear_or_secgroups_specified = false
660
+ optparse = OptionParser.new do|opts|
661
+ opts.banner = subcommand_usage("[name] [--clear] [-s]")
662
+ opts.on( '-c', '--clear', "Clear all security groups" ) do
663
+ options[:securityGroupIds] = []
664
+ clear_or_secgroups_specified = true
665
+ end
666
+ opts.on( '-s', '--secgroups SECGROUPS', "Apply the specified comma separated security group ids" ) do |secgroups|
667
+ options[:securityGroupIds] = secgroups.split(",")
668
+ clear_or_secgroups_specified = true
669
+ end
670
+ opts.on( '-h', '--help', "Prints this help" ) do
671
+ puts opts
672
+ exit
673
+ end
674
+ build_common_options(opts, options, [:json, :dry_run])
675
+ end
676
+ optparse.parse!(args)
677
+ if args.count < 1
678
+ puts optparse
679
+ exit 1
680
+ end
681
+ if !clear_or_secgroups_specified
682
+ puts optparse
683
+ exit 1
684
+ end
685
+
686
+ connect(options)
687
+
688
+ begin
689
+ app = find_app_by_name_or_id(args[0])
690
+ if options[:dry_run]
691
+ print_dry_run @apps_interface.dry.apply_security_groups(app['id'], options)
692
+ return
693
+ end
694
+ @apps_interface.apply_security_groups(app['id'], options)
695
+ security_groups([args[0]])
696
+ rescue RestClient::Exception => e
697
+ print_rest_exception(e, options)
698
+ exit 1
699
+ end
700
+ end
701
+
702
+ private
703
+
704
+ def find_app_by_id(id)
705
+ app_results = @apps_interface.get(id.to_i)
706
+ if app_results['app'].empty?
707
+ print_red_alert "App not found by id #{id}"
708
+ exit 1
709
+ end
710
+ return app_results['app']
711
+ end
712
+
713
+ def find_app_by_name(name)
714
+ app_results = @apps_interface.get({name: name})
715
+ if app_results['apps'].empty?
716
+ print_red_alert "App not found by name #{name}"
717
+ exit 1
718
+ end
719
+ return app_results['apps'][0]
720
+ end
721
+
722
+ def find_app_by_name_or_id(val)
723
+ if val.to_s =~ /\A\d{1,}\Z/
724
+ return find_app_by_id(val)
725
+ else
726
+ return find_app_by_name(val)
727
+ end
728
+ end
729
+
730
+ def print_apps_table(apps, opts={})
792
731
  table_color = opts[:color] || cyan
793
732
  rows = apps.collect do |app|
794
- instances_str = (app['instanceCount'].to_i == 1) ? "1 Instance" : "#{app['instanceCount']} Instances"
795
- containers_str = (app['containerCount'].to_i == 1) ? "1 Container" : "#{app['containerCount']} Containers"
796
- status_string = app['status']
797
- if app['instanceCount'].to_i == 0
798
- # show this instead of WARNING
799
- status_string = "#{white}EMPTY#{table_color}"
800
- elsif status_string == 'running'
801
- status_string = "#{green}#{status_string.upcase}#{table_color}"
802
- elsif status_string == 'stopped' or status_string == 'failed'
803
- status_string = "#{red}#{status_string.upcase}#{table_color}"
804
- elsif status_string == 'unknown'
805
- status_string = "#{white}#{status_string.upcase}#{table_color}"
806
- else
807
- status_string = "#{yellow}#{status_string.upcase}#{table_color}"
808
- end
809
-
733
+ instances_str = (app['instanceCount'].to_i == 1) ? "1 Instance" : "#{app['instanceCount']} Instances"
734
+ containers_str = (app['containerCount'].to_i == 1) ? "1 Container" : "#{app['containerCount']} Containers"
735
+ status_string = app['status']
736
+ if app['instanceCount'].to_i == 0
737
+ # show this instead of WARNING
738
+ status_string = "#{white}EMPTY#{table_color}"
739
+ elsif status_string == 'running'
740
+ status_string = "#{green}#{status_string.upcase}#{table_color}"
741
+ elsif status_string == 'stopped' or status_string == 'failed'
742
+ status_string = "#{red}#{status_string.upcase}#{table_color}"
743
+ elsif status_string == 'unknown'
744
+ status_string = "#{white}#{status_string.upcase}#{table_color}"
745
+ else
746
+ status_string = "#{yellow}#{status_string.upcase}#{table_color}"
747
+ end
810
748
  {
811
- id: app['id'],
812
- name: app['name'],
749
+ id: app['id'],
750
+ name: app['name'],
813
751
  instances: instances_str,
814
752
  containers: containers_str,
815
- account: app['account'] ? app['account']['name'] : nil,
816
- status: status_string,
817
- #dateCreated: format_local_dt(app['dateCreated'])
753
+ account: app['account'] ? app['account']['name'] : nil,
754
+ status: status_string,
755
+ #dateCreated: format_local_dt(app['dateCreated'])
818
756
  }
819
757
  end
820
-
821
758
  print table_color
822
759
  tp rows, [
823
- :id,
824
- :name,
760
+ :id,
761
+ :name,
825
762
  :instances,
826
763
  :containers,
827
- #:account,
764
+ #:account,
828
765
  :status,
829
766
  #{:dateCreated => {:display_name => "Date Created"} }
830
767
  ]