Class: Morpheus::Cli::Apps

Inherits:
Object
  • Object
show all
Includes:
CliCommand
Defined in:
lib/morpheus/cli/apps.rb

Instance Method Summary collapse

Methods included from CliCommand

#build_common_options, included

Constructor Details

#initializeApps

Returns a new instance of Apps.



12
13
14
# File 'lib/morpheus/cli/apps.rb', line 12

def initialize() 
	@appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance
end

Instance Method Details

#add(args) ⇒ Object



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
158
159
160
161
162
163
# File 'lib/morpheus/cli/apps.rb', line 68

def add(args)
	options = {}
	optparse = OptionParser.new do|opts|
		opts.banner = "Usage: morpheus apps add NAME TYPE"
		build_common_options(opts, options, [])
	end
	optparse.parse(args)
	if args.count < 2
		puts "\n#{optparse.banner}\n\n"
		exit 1
	end
	connect(options)

	app_name = args[0]
	instance_type_code = args[1]
	instance_type = find_instance_type_by_code(instance_type_code)
	if instance_type.nil?
		print reset,"\n\n"
		exit 1
	end

	groupId = @active_groups[@appliance_name.to_sym]

	params = {
		:servicePlan => nil,
		:app => {
			:name => app_name,
			:site => {
				:id => groupId
			},
			:appType => {
				:code => instance_type_code
			}
		}
	}

	instance_type['appTypeLayouts'].sort! { |x,y| y['sortOrder'] <=> x['sortOrder'] }
	puts "Configurations: "
	instance_type['appTypeLayouts'].each_with_index do |layout, index|
		puts "  #{index+1}) #{layout['name']} (#{layout['code']})"
	end
	print "Selection: "
	layout_selection = nil
	layout = nil
	while true do
		layout_selection = $stdin.gets.chomp!
		if layout_selection.to_i.to_s == layout_selection
			layout_selection = layout_selection.to_i
			break
		end
	end
	layout = instance_type['appTypeLayouts'][layout_selection-1]['id']
	params[:app][:layout] = {id: layout}
	print "\n"
	if params[:servicePlan].nil?
		plans = @instance_types_interface.service_plans(layout)
		puts "Select a Plan: "
		plans['servicePlans'].each_with_index do |plan, index|
			puts "  #{index+1}) #{plan['name']}"
		end
		print "Selection: "
		plan_selection = nil
		while true do
			plan_selection = $stdin.gets.chomp!
			if plan_selection.to_i.to_s == plan_selection
				plan_selection = plan_selection.to_i
				break
			end
		end
		params[:servicePlan] = plans['servicePlans'][plan_selection-1]['id']
		print "\n"
	end

	if !instance_type['config'].nil?
		instance_type_config = JSON.parse(instance_type['config'])
		if instance_type_config['options'].nil? == false
			instance_type_config['options'].each do |opt|
				print "#{opt['label']}: "
				if(opt['name'].downcase.include?("password"))
					params[opt['name']] = STDIN.noecho(&:gets).chomp!
					print "\n"
				else
					params[opt['name']] = $stdin.gets.chomp!
				end
				
			end
		end
	end
	begin
		@apps_interface.create(params)
	rescue RestClient::Exception => e
		print_rest_exception(e, options)
		exit 1
	end
	list([])
end

#apply_security_groups(args) ⇒ Object



619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
# File 'lib/morpheus/cli/apps.rb', line 619

def apply_security_groups(args)
	usage = <<-EOF
Usage: morpheus apps apply_security_groups [name] [options]
EOF

	options = {}
	clear_or_secgroups_specified = false
	optparse = OptionParser.new do|opts|
		opts.banner = usage
		opts.on( '-c', '--clear', "Clear all security groups" ) do
			options[:securityGroupIds] = []
			clear_or_secgroups_specified = true
		end
		opts.on( '-s', '--secgroups SECGROUPS', "Apply the specified comma separated security group ids" ) do |secgroups|
			options[:securityGroupIds] = secgroups.split(",")
			clear_or_secgroups_specified = true
		end
		opts.on( '-h', '--help', "Prints this help" ) do
			puts opts
			exit
		end
		build_common_options(opts, options, [:json])
	end
	optparse.parse(args)
	if args.count < 1
		puts "\n#{optparse.banner}\n\n"
		exit 1
	end
	if !clear_or_secgroups_specified 
		puts usage
		exit 1
	end

	connect(options)

	begin
		app_results = @apps_interface.get({name: args[0]})
		if app_results['apps'].empty?
			print_red_alert "App not found by name #{args[0]}"
			exit 1
		end

		@apps_interface.apply_security_groups(app_results['apps'][0]['id'], options)
		security_groups([args[0]])
	rescue RestClient::Exception => e
		print_rest_exception(e, options)
		exit 1
	end
end

#connect(opts) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/morpheus/cli/apps.rb', line 16

def connect(opts)
	@access_token = Morpheus::Cli::Credentials.new(@appliance_name,@appliance_url).request_credentials()
	if @access_token.empty?
		print_red_alert "Invalid Credentials. Unable to acquire access token. Please verify your credentials and try again."
		exit 1
	end
	@apps_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).apps
	@instance_types_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).instance_types
	@apps_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).apps
	@groups_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).groups
	@active_groups = ::Morpheus::Cli::Groups.load_group_file
end

#delenv(args) ⇒ Object



351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'lib/morpheus/cli/apps.rb', line 351

def delenv(args)
	options = {}
	optparse = OptionParser.new do|opts|
		opts.banner = "Usage: morpheus apps setenv INSTANCE NAME"
		build_common_options(opts, options, [:json])
	end
	optparse.parse(args)
	if args.count < 2
		puts "\n#{optparse.banner}\n\n"
		exit 1
	end
	connect(options)

	begin
		app_results = @apps_interface.get({name: args[0]})
		if app_results['apps'].empty?
			print_red_alert "App not found by name #{args[0]}"
			exit 1
		end
		app = app_results['apps'][0]
		app_id = app['id']
		name = args[1]

		@apps_interface.del_env(app_id, name)
		envs([args[0]])
	rescue RestClient::Exception => e
		print_rest_exception(e, options)
		exit 1
	end
end

#details(args) ⇒ Object



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/morpheus/cli/apps.rb', line 248

def details(args)
	options = {}
	optparse = OptionParser.new do|opts|
		opts.banner = "Usage: morpheus apps details [name]"
		build_common_options(opts, options, [:json])
	end
	optparse.parse(args)
	if args.count < 1
		puts "\n#{optparse.banner}\n\n"
		exit 1
	end
	connect(options)
	begin
		app_results = @apps_interface.get({name: args[0]})
		if app_results['apps'].empty?
			print_red_alert "App not found by name #{args[0]}"
			exit 1
		end
		app = app_results['apps'][0]
		app_id = app['id']
		stats = app_results['stats'][app_id.to_s]
		print "\n" ,cyan, bold, "#{app['name']} (#{app['appType']['name']})\n","==================", reset, "\n\n"
		print cyan, "Memory: \t#{Filesize.from("#{stats['usedMemory']} B").pretty} / #{Filesize.from("#{stats['maxMemory']} B").pretty}\n"
		print cyan, "Storage: \t#{Filesize.from("#{stats['usedStorage']} B").pretty} / #{Filesize.from("#{stats['maxStorage']} B").pretty}\n\n",reset
		puts app
	rescue RestClient::Exception => e
		print_rest_exception(e, options)
		exit 1
	end
end

#envs(args) ⇒ Object



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/morpheus/cli/apps.rb', line 279

def envs(args)
	options = {}
	optparse = OptionParser.new do|opts|
		opts.banner = "Usage: morpheus apps envs [name]"
		build_common_options(opts, options, [:json])
	end
	optparse.parse(args)
	if args.count < 1
		puts "\n#{optparse.banner}\n\n"
		exit 1
	end
	connect(options)
	begin
		app_results = @apps_interface.get({name: args[0]})
		if app_results['apps'].empty?
			print_red_alert "App not found by name #{args[0]}"
			return
		end
		app = app_results['apps'][0]
		app_id = app['id']
		env_results = @apps_interface.get_envs(app_id)
		print "\n" ,cyan, bold, "#{app['name']} (#{app['appType']['name']})\n","==================", "\n\n", reset, cyan
		envs = env_results['envs'] || {}
		if env_results['readOnlyEnvs']
			envs += env_results['readOnlyEnvs'].map { |k,v| {:name => k, :value => k.downcase.include?("password") ? "********" : v, :export => true}}
		end
		tp envs, :name, :value, :export
		print "\n" ,cyan, bold, "Importad Envs\n","==================", "\n\n", reset, cyan
		 imported_envs = env_results['importedEnvs'].map { |k,v| {:name => k, :value => k.downcase.include?("password") ? "********" : v}}
		 tp imported_envs
		print reset, "\n"
		
	rescue RestClient::Exception => e
		print_rest_exception(e, options)
		exit 1
	end
end

#firewall_disable(args) ⇒ Object



524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
# File 'lib/morpheus/cli/apps.rb', line 524

def firewall_disable(args)
	options = {}
	optparse = OptionParser.new do|opts|
		opts.banner = "Usage: morpheus apps firewall_disable [name]"
		build_common_options(opts, options, [:json])
	end
	optparse.parse(args)
	if args.count < 1
		puts "\n#{optparse.banner}\n\n"
		exit 1
	end
	connect(options)

	begin
		app_results = @apps_interface.get({name: args[0]})
		if app_results['apps'].empty?
			print_red_alert "App not found by name #{args[0]}"
			exit 1
		end
		@apps_interface.firewall_disable(app_results['apps'][0]['id'])
		security_groups([args[0]])
	rescue RestClient::Exception => e
		print_rest_exception(e, options)
		exit 1
	end
end

#firewall_enable(args) ⇒ Object



551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
# File 'lib/morpheus/cli/apps.rb', line 551

def firewall_enable(args)
	options = {}
	optparse = OptionParser.new do|opts|
		opts.banner = "Usage: morpheus apps firewall_enable [name]"
		build_common_options(opts, options, [:json])
	end
	optparse.parse(args)
	if args.count < 1
		puts "\n#{optparse.banner}\n\n"
		exit 1
	end
	connect(options)

	begin
		app_results = @apps_interface.get({name: args[0]})
		if app_results['apps'].empty?
			print_red_alert "App not found by name #{args[0]}"
			exit 1
		end
		@apps_interface.firewall_enable(app_results['apps'][0]['id'])
		security_groups([args[0]])
	rescue RestClient::Exception => e
		print_rest_exception(e, options)
		exit 1
	end
end

#handle(args) ⇒ Object



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
# File 'lib/morpheus/cli/apps.rb', line 29

def handle(args) 
	usage = "Usage: morpheus apps [list,add,remove,stop,start,restart,resize,upgrade,clone,envs,setenv,delenv,firewall_disable,firewall_enable,security_groups,apply_security_groups] [name]"
	case args[0]
		when 'list'
			list(args[1..-1])
		when 'add'
			add(args[1..-1])
		when 'remove'
			remove(args[1..-1])
		when 'stop'
			stop(args[1..-1])
		when 'start'
			start(args[1..-1])
		when 'restart'
			restart(args[1..-1])
		when 'stats'
			stats(args[1..-1])
		when 'details'
			details(args[1..-1])
		when 'envs'
			envs(args[1..-1])
		when 'setenv'
			setenv(args[1..-1])	
		when 'delenv'
			delenv(args[1..-1])
		when 'firewall_disable'
			firewall_disable(args[1..-1])	
		when 'firewall_enable'
			firewall_enable(args[1..-1])	
		when 'security_groups'	
			security_groups(args[1..-1])	
		when 'apply_security_groups'	
			apply_security_groups(args[1..-1])		
		else
			puts "\n#{usage}\n\n"
			exit 127
	end
end

#list(args) ⇒ Object



460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
# File 'lib/morpheus/cli/apps.rb', line 460

def list(args)
	options = {}
	optparse = OptionParser.new do|opts|
		opts.banner = "Usage: morpheus apps list"
		opts.on( '-g', '--group GROUP', "Group Name" ) do |group|
			options[:group] = group
		end
		build_common_options(opts, options, [:json])
	end
	optparse.parse(args)

	connect(options)
	
	begin
		params = {}
		[:phrase, :offset, :max, :sort, :direction].each do |k|
			params[k] = options[k] unless options[k].nil?
		end
		# todo: include options[:group] in params?
		json_response = @apps_interface.get(params)
		apps = json_response['apps']
		print "\n" ,cyan, bold, "Morpheus Apps\n","==================", reset, "\n\n"
		if apps.empty?
			puts yellow,"No apps currently configured.",reset
		else
			apps.each do |app|
				print cyan, "=  #{app['name']}\n"
			end
		end
		print reset,"\n\n"
		
	rescue => e
		puts "Error Communicating with the Appliance. Please try again later. #{e}"
		return nil
	end
end

#logs(args) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/morpheus/cli/apps.rb', line 165

def logs(args) 
	options = {}
	optparse = OptionParser.new do|opts|
		opts.banner = "Usage: morpheus apps logs [name] [options]"
		build_common_options(opts, options, [:list, :json])
	end
	optparse.parse(args)
	if args.count < 1
		puts "\n#{optparse.banner}\n\n"
		exit 1
	end
	connect(options)
	begin
		app = find_app_by_name(args[0])
		containers = []
		app['appTiers'].each do |app_tier|
			app_tier['appInstances'].each do |app_instance|
				containers += app_instance['instance']['containers']
			end
		end
		params = {}
		[:phrase, :offset, :max, :sort, :direction].each do |k|
			params[k] = options[k] unless options[k].nil?
		end
		logs = @logs_interface.container_logs(containers, params)
		if options[:json]
			puts logs
		else
			logs['data'].reverse.each do |log_entry|
				log_level = ''
				case log_entry['level']
					when 'INFO'
						log_level = "#{blue}#{bold}INFO#{reset}"
					when 'DEBUG'
						log_level = "#{white}#{bold}DEBUG#{reset}"
					when 'WARN'
						log_level = "#{yellow}#{bold}WARN#{reset}"
					when 'ERROR'
						log_level = "#{red}#{bold}ERROR#{reset}"
					when 'FATAL'
						log_level = "#{red}#{bold}FATAL#{reset}"
				end
				puts "[#{log_entry['ts']}] #{log_level} - #{log_entry['message']}"
			end
			print reset,"\n"
		end
	rescue RestClient::Exception => e
		print_rest_exception(e, options)
		exit 1
	end
end

#remove(args) ⇒ Object



497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
# File 'lib/morpheus/cli/apps.rb', line 497

def remove(args)
	options = {}
	optparse = OptionParser.new do|opts|
		opts.banner = "Usage: morpheus apps remove [name]"
		build_common_options(opts, options, [:json])
	end
	optparse.parse(args)
	if args.count < 1
		puts "\n#{optparse.banner}\n\n"
		exit 1
	end
	connect(options)

	begin
		app_results = @apps_interface.get({name: args[0]})
		if app_results['apps'].empty?
			print_red_alert "App not found by name #{args[0]}"
			exit 1
		end
		@apps_interface.destroy(app_results['apps'][0]['id'])
		list([])
	rescue RestClient::Exception => e
		print_rest_exception(e, options)
		exit 1
	end
end

#restart(args) ⇒ Object



434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
# File 'lib/morpheus/cli/apps.rb', line 434

def restart(args)
	options = {}
	optparse = OptionParser.new do|opts|
		opts.banner = "Usage: morpheus apps restart [name]"
		build_common_options(opts, options, [:json])
	end
	optparse.parse(args)
	if args.count < 1
		puts "\n#{optparse.banner}\n\n"
		exit 1
	end
	connect(options)
	begin
		app_results = @apps_interface.get({name: args[0]})
		if app_results['apps'].empty?
			print_red_alert "App not found by name #{args[0]}"
			exit 1
		end
		@apps_interface.restart(app_results['apps'][0]['id'])
		list([])
	rescue RestClient::Exception => e
		print_rest_exception(e, options)
		exit 1
	end
end

#security_groups(args) ⇒ Object



578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
# File 'lib/morpheus/cli/apps.rb', line 578

def security_groups(args)
	options = {}
	optparse = OptionParser.new do|opts|
		opts.banner = "Usage: morpheus apps security_groups [name]"
		build_common_options(opts, options, [:json])
	end
	optparse.parse(args)
	if args.count < 1
		puts "\n#{optparse.banner}\n\n"
		exit 1
	end
	connect(options)

	begin
		app_results = @apps_interface.get({name: args[0]})
		if app_results['apps'].empty?
			print_red_alert "App not found by name #{args[0]}"
			exit 1
		end

		app_id = app_results['apps'][0]['id']
		json_response = @apps_interface.security_groups(app_id)

		securityGroups = json_response['securityGroups']
		print "\n" ,cyan, bold, "Morpheus Security Groups for App:#{app_id}\n","==================", reset, "\n\n"
		print cyan, "Firewall Enabled=#{json_response['firewallEnabled']}\n\n"
		if securityGroups.empty?
			puts yellow,"No security groups currently applied.",reset
		else
			securityGroups.each do |securityGroup|
				print cyan, "=  #{securityGroup['id']} (#{securityGroup['name']}) - (#{securityGroup['description']})\n"
			end
		end
		print reset,"\n\n"

	rescue RestClient::Exception => e
		print_rest_exception(e, options)
		exit 1
	end
end

#setenv(args) ⇒ Object



317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/morpheus/cli/apps.rb', line 317

def setenv(args)
	options = {}
	evar = {name: args[1], value: args[2], export: false}
	optparse = OptionParser.new do|opts|
		opts.banner = "Usage: morpheus apps setenv INSTANCE NAME VALUE [-e]"
		opts.on( '-e', "Exportable" ) do |exportable|
			evar[:export] = exportable
		end
		build_common_options(opts, options, [:json])
	end
	optparse.parse(args)
	if args.count < 3
		puts "\n#{optparse.banner}\n\n"
		exit 1
	end
	connect(options)
	begin
		app_results = @apps_interface.get({name: args[0]})
		if app_results['apps'].empty?
			print_red_alert "App not found by name #{args[0]}"
			exit 1
		end
		app = app_results['apps'][0]
		app_id = app['id']
		params = {}

		@apps_interface.create_env(app_id, [evar])
		envs([args[0]])
	rescue RestClient::Exception => e
		print_rest_exception(e, options)
		exit 1
	end
end

#start(args) ⇒ Object



408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
# File 'lib/morpheus/cli/apps.rb', line 408

def start(args)
	options = {}
	optparse = OptionParser.new do|opts|
		opts.banner = "Usage: morpheus apps start [name]"
		build_common_options(opts, options, [:json])
	end
	optparse.parse(args)
	if args.count < 1
		puts "\n#{optparse.banner}\n\n"
		exit 1
	end
	connect(options)
	begin
		app_results = @apps_interface.get({name: args[0]})
		if app_results['apps'].empty?
			print_red_alert "App not found by name #{args[0]}"
			exit 1
		end
		@apps_interface.start(app_results['apps'][0]['id'])
		list([])
	rescue RestClient::Exception => e
		print_rest_exception(e, options)
		exit 1
	end
end

#stats(args) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/morpheus/cli/apps.rb', line 217

def stats(args)
	options = {}
	optparse = OptionParser.new do|opts|
		opts.banner = "Usage: morpheus apps stats [name]"
		build_common_options(opts, options, [:json])
	end
	optparse.parse(args)
	if args.count < 1
		puts "\n#{optparse.banner}\n\n"
		exit 1
	end
	connect(options)
	begin
		app_results = @apps_interface.get({name: args[0]})
		if app_results['apps'].empty?
			print_red_alert "App not found by name #{args[0]}"
			exit 1
		end
		app = app_results['apps'][0]
		app_id = app['id']
		stats = app_results['stats'][app_id.to_s]
		print "\n" ,cyan, bold, "#{app['name']} (#{app['appType']['name']})\n","==================", reset, "\n\n"
		print cyan, "Memory: \t#{Filesize.from("#{stats['usedMemory']} B").pretty} / #{Filesize.from("#{stats['maxMemory']} B").pretty}\n"
		print cyan, "Storage: \t#{Filesize.from("#{stats['usedStorage']} B").pretty} / #{Filesize.from("#{stats['maxStorage']} B").pretty}\n\n",reset
		puts 
	rescue RestClient::Exception => e
		print_rest_exception(e, options)
		exit 1
	end
end

#stop(args) ⇒ Object



382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'lib/morpheus/cli/apps.rb', line 382

def stop(args)
	options = {}
	optparse = OptionParser.new do|opts|
		opts.banner = "Usage: morpheus apps stop [name]"
		build_common_options(opts, options, [:json])
	end
	optparse.parse(args)
	if args.count < 1
		puts "\n#{optparse.banner}\n\n"
		exit 1
	end
	connect(options)
	begin
		app_results = @apps_interface.get({name: args[0]})
		if app_results['apps'].empty?
			print_red_alert "App not found by name #{args[0]}"
			exit 1
		end
		@apps_interface.stop(app_results['apps'][0]['id'])
		list([])
	rescue RestClient::Exception => e
		print_rest_exception(e, options)
		exit 1
	end
end