Class: Morpheus::Cli::Instances
Instance Attribute Summary
Attributes included from CliCommand
#no_prompt
Instance Method Summary
collapse
#api_client, #find_cloud_by_id_for_provisioning, #find_cloud_by_name_for_provisioning, #find_cloud_by_name_or_id_for_provisioning, #find_group_by_id_for_provisioning, #find_group_by_name_for_provisioning, #find_group_by_name_or_id_for_provisioning, #find_instance_by_id, #find_instance_by_name, #find_instance_by_name_or_id, #find_instance_type_by_code, #find_instance_type_by_name, #get_available_clouds, #get_available_groups, included, #instance_context_options, #instance_types_interface, #instances_interface, #options_interface, #prompt_evars, #prompt_network_interfaces, #prompt_new_instance, #prompt_resize_volumes, #prompt_volumes, #reject_networking_option_types, #reject_service_plan_option_types, #reject_volume_option_types
Methods included from CliCommand
#build_common_options, #build_option_type_options, #command_name, #default_subcommand, #establish_remote_appliance_connection, #handle_subcommand, included, #interactive?, #noninteractive, #print_usage, #subcommand_aliases, #subcommand_usage, #subcommands, #usage, #verify_access_token!
Constructor Details
Returns a new instance of Instances.
18
19
20
|
# File 'lib/morpheus/cli/instances.rb', line 18
def initialize()
end
|
Instance Method Details
#add(args) ⇒ Object
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
|
# File 'lib/morpheus/cli/instances.rb', line 39
def add(args)
options = {}
optparse = OptionParser.new do|opts|
opts.banner = subcommand_usage("[type] [name]")
opts.on( '-g', '--group GROUP', "Group Name or ID" ) do |val|
options[:group] = val
end
opts.on( '-c', '--cloud CLOUD', "Cloud Name or ID" ) do |val|
options[:cloud] = val
end
opts.on( '-t', '--type CODE', "Instance Type" ) do |val|
options[:instance_type_code] = val
end
opts.on("--copies NUMBER", Integer, "Number of copies to provision") do |val|
options[:copies] = val.to_i
end
opts.on("--layout-size NUMBER", Integer, "Apply a multiply factor of containers/vms within the instance") do |val|
options[:layout_size] = val.to_i
end
build_common_options(opts, options, [:options, :json, :dry_run, :remote])
end
optparse.parse!(args)
connect(options)
if args[0]
options[:instance_type_code] = args[0]
end
if args[1]
options[:instance_name] = args[1]
end
options[:group] ||= @active_group_id
options[:name_required] = true
begin
payload = prompt_new_instance(options)
payload[:copies] = options[:copies] if options[:copies] && options[:copies] > 0
payload[:layoutSize] = options[:layout_size] if options[:layout_size] && options[:layout_size] > 0
if options[:dry_run]
print_dry_run @instances_interface.dry.create(payload)
return
end
json_response = @instances_interface.create(payload)
if options[:json]
print JSON.pretty_generate(json_response)
print "\n"
else
instance_name = json_response["instance"]["name"]
print_green_success "Provisioning instance #{instance_name}"
list([])
end
rescue RestClient::Exception => e
print_rest_exception(e, options)
exit 1
end
end
|
#apply_security_groups(args) ⇒ Object
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
|
# File 'lib/morpheus/cli/instances.rb', line 1213
def apply_security_groups(args)
options = {}
security_group_ids = nil
clear_or_secgroups_specified = false
optparse = OptionParser.new do|opts|
opts.banner = subcommand_usage("[name] [-S] [-c]")
opts.on( '-S', '--secgroups SECGROUPS', "Apply the specified comma separated security group ids" ) do |secgroups|
security_group_ids = secgroups.split(",")
clear_or_secgroups_specified = true
end
opts.on( '-c', '--clear', "Clear all security groups" ) do
security_group_ids = []
clear_or_secgroups_specified = true
end
build_common_options(opts, options, [:json, :dry_run, :quiet, :remote])
end
optparse.parse!(args)
if args.count < 1
puts optparse
exit 1
end
if !clear_or_secgroups_specified
puts optparse
exit
end
connect(options)
begin
instance = find_instance_by_name_or_id(args[0])
payload = {securityGroupIds: security_group_ids}
if options[:dry_run]
print_dry_run @instances_interface.dry.apply_security_groups(instance['id'], payload)
return
end
json_response = @instances_interface.apply_security_groups(instance['id'], payload)
if options[:json]
print JSON.pretty_generate(json_response)
print "\n"
return
end
if !options[:quiet]
security_groups([args[0]])
end
rescue RestClient::Exception => e
print_rest_exception(e, options)
exit 1
end
end
|
#backup(args) ⇒ Object
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
|
# File 'lib/morpheus/cli/instances.rb', line 962
def backup(args)
options = {}
optparse = OptionParser.new do|opts|
opts.banner = subcommand_usage("[name]")
build_common_options(opts, options, [:auto_confirm, :json, :dry_run, :remote])
end
optparse.parse!(args)
if args.count < 1
puts optparse
exit 1
end
connect(options)
begin
instance = find_instance_by_name_or_id(args[0])
unless options[:yes] || ::Morpheus::Cli::OptionTypes::confirm("Are you sure you would like to backup the instance '#{instance['name']}'?", options)
exit 1
end
if options[:dry_run]
print_dry_run @instances_interface.dry.backup(instance['id'])
return
end
json_response = @instances_interface.backup(instance['id'])
if options[:json]
print JSON.pretty_generate(json_response)
print "\n"
else
puts "Backup initiated."
end
return
rescue RestClient::Exception => e
print_rest_exception(e, options)
exit 1
end
end
|
#backups(args) ⇒ Object
401
402
403
404
405
406
407
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
|
# File 'lib/morpheus/cli/instances.rb', line 401
def backups(args)
options = {}
optparse = OptionParser.new do|opts|
opts.banner = subcommand_usage("[name]")
build_common_options(opts, options, [:auto_confirm, :json, :dry_run, :remote])
end
optparse.parse!(args)
if args.count < 1
puts optparse
exit 1
end
connect(options)
begin
instance = find_instance_by_name_or_id(args[0])
params = {}
if options[:dry_run]
print_dry_run @instances_interface.dry.backups(instance['id'], params)
return
end
json_response = @instances_interface.backups(instance['id'], params)
if options[:json]
print JSON.pretty_generate(json_response), "\n"
return
end
backups = json_response['backups']
stats = json_response['stats'] || {}
print "\n" ,cyan, bold, "Instance Backups\n","==================", reset, "\n\n"
print cyan
puts "ID: #{instance['id']}"
puts "Name: #{instance['name']}"
print "\n"
puts "Backups:"
backup_rows = backups.collect {|it| {id: it['id'], name: it['name'], dateCreated: it['dateCreated']} }
print cyan
tp backup_rows, [
:id,
:name,
{:dateCreated => {:display_name => "Date Created"} }
]
print reset, "\n"
rescue RestClient::Exception => e
print_rest_exception(e, options)
exit 1
end
end
|
#check_status ⇒ Object
1374
1375
|
# File 'lib/morpheus/cli/instances.rb', line 1374
def check_status
end
|
#clone(args) ⇒ Object
449
450
451
452
453
454
455
456
457
458
459
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
496
497
498
499
500
501
502
503
504
505
506
507
|
# File 'lib/morpheus/cli/instances.rb', line 449
def clone(args)
options = {}
optparse = OptionParser.new do|opts|
opts.banner = subcommand_usage("[name] -g GROUP")
build_option_type_options(opts, options, clone_instance_option_types(false))
opts.on( '-g', '--group GROUP', "Group Name or ID for the new instance" ) do |val|
options[:group] = val
end
build_common_options(opts, options, [:auto_confirm, :json, :dry_run, :remote])
end
optparse.parse!(args)
if args.count < 1
puts optparse
exit 1
end
if !options[:group]
print_red_alert "GROUP is required."
puts optparse
exit 1
end
connect(options)
begin
options[:options] ||= {}
options[:options]['group'] ||= options[:group] payload = {
}
params = Morpheus::Cli::OptionTypes.prompt(clone_instance_option_types, options[:options], @api_client, options[:params])
group = find_group_by_name_or_id_for_provisioning(params.delete('group'))
payload.merge!(params)
payload['group'] = {id: group['id']}
instance = find_instance_by_name_or_id(args[0])
unless options[:yes] || ::Morpheus::Cli::OptionTypes::confirm("Are you sure you would like to clone the instance '#{instance['name']}'?", options)
exit 1
end
if options[:dry_run]
print_dry_run @instances_interface.dry.clone(instance['id'], payload)
return
end
json_response = @instances_interface.clone(instance['id'], payload)
if options[:json]
print JSON.pretty_generate(json_response)
print "\n"
else
print_green_success "Cloning instance #{instance['name']} to '#{payload['name']}'"
end
return
rescue RestClient::Exception => e
print_rest_exception(e, options)
exit 1
end
end
|
#connect(opts) ⇒ Object
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/morpheus/cli/instances.rb', line 22
def connect(opts)
@api_client = establish_remote_appliance_connection(opts)
@instances_interface = @api_client.instances
@task_sets_interface = @api_client.task_sets
@logs_interface = @api_client.logs
@tasks_interface = @api_client.tasks
@instance_types_interface = @api_client.instance_types
@clouds_interface = @api_client.clouds
@provision_types_interface = @api_client.provision_types
@options_interface = @api_client.options
@active_group_id = Morpheus::Cli::Groups.active_group
end
|
#console(args) ⇒ Object
240
241
242
243
244
245
246
247
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
|
# File 'lib/morpheus/cli/instances.rb', line 240
def console(args)
options = {}
optparse = OptionParser.new do|opts|
opts.banner = subcommand_usage("[name]")
opts.on( '-n', '--node NODE_ID', "Scope console to specific Container or VM" ) do |node_id|
options[:node_id] = node_id.to_i
end
end
optparse.parse!(args)
if args.count < 1
puts optparse
exit 1
end
connect(options)
begin
instance = find_instance_by_name_or_id(args[0])
link = "#{@appliance_url}/login/oauth-redirect?access_token=#{@access_token}\\&redirectUri=/terminal/#{instance['id']}"
container_ids = instance['containers']
if options[:node_id] && container_ids.include?(options[:node_id])
link += "?containerId=#{options[:node_id]}"
end
if RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/
system "start #{link}"
elsif RbConfig::CONFIG['host_os'] =~ /darwin/
system "open #{link}"
elsif RbConfig::CONFIG['host_os'] =~ /linux|bsd/
system "xdg-open #{link}"
end
rescue RestClient::Exception => e
print_rest_exception(e, options)
exit 1
end
end
|
#delenv(args) ⇒ Object
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
618
619
620
|
# File 'lib/morpheus/cli/instances.rb', line 590
def delenv(args)
options = {}
optparse = OptionParser.new do|opts|
opts.banner = subcommand_usage("[name] VAR")
build_common_options(opts, options, [:json, :dry_run, :remote])
end
optparse.parse!(args)
if args.count < 2
puts optparse
exit 1
end
connect(options)
begin
instance = find_instance_by_name_or_id(args[0])
if options[:dry_run]
print_dry_run @instances_interface.dry.del_env(instance['id'], args[1])
return
end
json_response = @instances_interface.del_env(instance['id'], args[1])
if options[:json]
print JSON.pretty_generate(json_response), "\n"
return
end
if !options[:quiet]
envs([args[0]])
end
rescue RestClient::Exception => e
print_rest_exception(e, options)
exit 1
end
end
|
#eject(args) ⇒ Object
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
|
# File 'lib/morpheus/cli/instances.rb', line 751
def eject(args)
options = {}
optparse = OptionParser.new do|opts|
opts.banner = subcommand_usage("restart [name]")
build_common_options(opts, options, [:auto_confirm, :json, :dry_run, :remote])
end
optparse.parse!(args)
if args.count < 1
puts optparse
exit 1
end
connect(options)
begin
instance = find_instance_by_name_or_id(args[0])
unless options[:yes] || ::Morpheus::Cli::OptionTypes::confirm("Are you sure you would like to eject this instance?", options)
exit 1
end
if options[:dry_run]
print_dry_run @instances_interface.dry.eject(instance['id'])
return
end
json_response = @instances_interface.eject(instance['id'])
if options[:json]
print JSON.pretty_generate(json_response)
print "\n"
end
return
rescue RestClient::Exception => e
print_rest_exception(e, options)
exit 1
end
end
|
#envs(args) ⇒ Object
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
|
# File 'lib/morpheus/cli/instances.rb', line 510
def envs(args)
options = {}
optparse = OptionParser.new do|opts|
opts.banner = subcommand_usage("[name]")
build_common_options(opts, options, [:json, :dry_run, :remote])
end
optparse.parse!(args)
if args.count < 1
puts optparse
exit 1
end
connect(options)
begin
instance = find_instance_by_name_or_id(args[0])
if options[:dry_run]
print_dry_run @instances_interface.dry.get_envs(instance['id'])
return
end
json_response = @instances_interface.get_envs(instance['id'])
if options[:json]
print JSON.pretty_generate(json_response), "\n"
return
end
print "\n" ,cyan, bold, "#{instance['name']} (#{instance['instanceType']['name']})\n","==================", "\n\n", reset, cyan
envs = json_response['envs'] || {}
if json_response['readOnlyEnvs']
envs += json_response['readOnlyEnvs'].map { |k,v| {:name => k, :value => k.downcase.include?("password") || v['masked'] ? "********" : v['value'], :export => true}}
end
tp envs, :name, :value, :export
print "\n" ,cyan, bold, "Imported Envs\n","==================", "\n\n", reset, cyan
imported_envs = json_response['importedEnvs'].map { |k,v| {:name => k, :value => k.downcase.include?("password") || v['masked'] ? "********" : v['value']}}
tp imported_envs
print reset, "\n"
rescue RestClient::Exception => e
print_rest_exception(e, options)
exit 1
end
end
|
#firewall_disable(args) ⇒ Object
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
|
# File 'lib/morpheus/cli/instances.rb', line 1109
def firewall_disable(args)
options = {}
optparse = OptionParser.new do|opts|
opts.banner = subcommand_usage("[name]")
build_common_options(opts, options, [:json, :dry_run, :quiet, :remote])
end
optparse.parse!(args)
if args.count < 1
puts optparse
exit 1
end
connect(options)
begin
instance = find_instance_by_name_or_id(args[0])
if options[:dry_run]
print_dry_run @instances_interface.dry.firewall_disable(instance['id'])
return
end
json_response = @instances_interface.firewall_disable(instance['id'])
if options[:json]
print JSON.pretty_generate(json_response)
print "\n"
elsif !options[:quiet]
security_groups([args[0]])
end
rescue RestClient::Exception => e
print_rest_exception(e, options)
exit 1
end
end
|
#firewall_enable(args) ⇒ Object
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
|
# File 'lib/morpheus/cli/instances.rb', line 1140
def firewall_enable(args)
options = {}
optparse = OptionParser.new do|opts|
opts.banner = subcommand_usage("[name]")
build_common_options(opts, options, [:json, :dry_run, :quiet, :remote])
end
optparse.parse!(args)
if args.count < 1
puts optparse
exit 1
end
connect(options)
begin
instance = find_instance_by_name_or_id(args[0])
if options[:dry_run]
print_dry_run @instances_interface.dry.firewall_enable(instance['id'])
return
end
json_response = @instances_interface.firewall_enable(instance['id'])
if options[:json]
print JSON.pretty_generate(json_response)
print "\n"
elsif !options[:quiet]
security_groups([args[0]])
end
rescue RestClient::Exception => e
print_rest_exception(e, options)
exit 1
end
end
|
#get(args) ⇒ Object
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
|
# File 'lib/morpheus/cli/instances.rb', line 331
def get(args)
options = {}
optparse = OptionParser.new do|opts|
opts.banner = subcommand_usage("[name]")
build_common_options(opts, options, [:json, :dry_run, :remote])
end
optparse.parse!(args)
if args.count < 1
puts optparse
exit 1
end
connect(options)
begin
if options[:dry_run]
if args[0].to_s =~ /\A\d{1,}\Z/
print_dry_run @instances_interface.dry.get(args[0].to_i)
else
print_dry_run @instances_interface.dry.get({name:args[0]})
end
return
end
instance = find_instance_by_name_or_id(args[0])
json_response = @instances_interface.get(instance['id'])
if options[:json]
print JSON.pretty_generate(json_response), "\n"
return
end
instance = json_response['instance']
stats = json_response['stats'] || {}
connection_string = ''
if !instance['connectionInfo'].nil? && instance['connectionInfo'].empty? == false
connection_string = "#{instance['connectionInfo'][0]['ip']}:#{instance['connectionInfo'][0]['port']}"
end
print "\n" ,cyan, bold, "Instance Details\n","==================", reset, "\n\n"
print cyan
puts "ID: #{instance['id']}"
puts "Name: #{instance['name']}"
puts "Description: #{instance['description']}"
puts "Group: #{instance['group'] ? instance['group']['name'] : ''}"
puts "Cloud: #{instance['cloud'] ? instance['cloud']['name'] : ''}"
puts "Type: #{instance['instanceType']['name']}"
puts "Plan: #{instance['plan'] ? instance['plan']['name'] : ''}"
puts "Environment: #{instance['instanceContext']}"
puts "Nodes: #{instance['containers'] ? instance['containers'].count : 0}"
puts "Connection: #{connection_string}"
puts "Status: #{format_instance_status(instance)}"
if ((stats['maxMemory'].to_i != 0) || (stats['maxStorage'].to_i != 0))
print "\n"
print_stats_usage(stats)
else
end
print reset, "\n"
rescue RestClient::Exception => e
print_rest_exception(e, options)
exit 1
end
end
|
#handle(args) ⇒ Object
35
36
37
|
# File 'lib/morpheus/cli/instances.rb', line 35
def handle(args)
handle_subcommand(args)
end
|
#import_snapshot(args) ⇒ Object
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
|
# File 'lib/morpheus/cli/instances.rb', line 1317
def import_snapshot(args)
options = {}
storage_provider_id = nil
optparse = OptionParser.new do|opts|
opts.banner = subcommand_usage("[name]")
opts.on("--storage-provider ID", String, "Optional storage provider") do |val|
storage_provider_id = val
end
build_common_options(opts, options, [:auto_confirm, :json, :dry_run, :remote])
end
optparse.parse!(args)
if args.count < 1
puts optparse
exit 1
end
connect(options)
begin
instance = find_instance_by_name_or_id(args[0])
unless options[:yes] || ::Morpheus::Cli::OptionTypes::confirm("Are you sure you would like to import a snapshot of the instance '#{instance['name']}'?", options)
exit 1
end
payload = {}
begin
options[:options] ||= {}
options[:options]['storageProviderId'] = storage_provider_id if storage_provider_id
storage_provider_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'storageProviderId', 'type' => 'select', 'fieldLabel' => 'Storage Provider', 'optionSource' => 'storageProviders', 'required' => false, 'description' => 'Select Storage Provider.'}], options[:options], @api_client, {})
if !storage_provider_prompt['storageProviderId'].empty?
payload['storageProviderId'] = storage_provider_prompt['storageProviderId']
end
rescue RestClient::Exception => e
puts "Failed to load storage providers"
exit 1
end
if options[:dry_run]
print_dry_run @instances_interface.dry.import_snapshot(instance['id'], payload)
return
end
json_response = @instances_interface.import_snapshot(instance['id'], payload)
if options[:json]
print JSON.pretty_generate(json_response)
print "\n"
else
puts "Snapshot import initiated."
end
return
rescue RestClient::Exception => e
print_rest_exception(e, options)
exit 1
end
end
|
#list(args) ⇒ Object
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
|
# File 'lib/morpheus/cli/instances.rb', line 997
def list(args)
options = {}
optparse = OptionParser.new do|opts|
opts.banner = subcommand_usage()
opts.on( '-g', '--group GROUP', "Group Name or ID" ) do |val|
options[:group] = val
end
opts.on( '-c', '--cloud CLOUD', "Cloud Name or ID" ) do |val|
options[:cloud] = val
end
build_common_options(opts, options, [:list, :json, :dry_run, :remote])
end
optparse.parse!(args)
connect(options)
begin
params = {}
group = options[:group] ? find_group_by_name_or_id_for_provisioning(options[:group]) : nil
if group
params['siteId'] = group['id']
end
cloud = options[:cloud] ? find_zone_by_name_or_id(nil, options[:cloud]) : nil
if cloud
params['zoneId'] = cloud['id']
end
[:phrase, :offset, :max, :sort, :direction].each do |k|
params[k] = options[k] unless options[k].nil?
end
if options[:dry_run]
print_dry_run @instances_interface.dry.list(params)
return
end
json_response = @instances_interface.get(params)
if options[:json]
print JSON.pretty_generate(json_response)
print "\n"
else
instances = json_response['instances']
title = "Morpheus Instances"
subtitles = []
if group
subtitles << "Group: #{group['name']}".strip
end
if cloud
subtitles << "Cloud: #{cloud['name']}".strip
end
if params[:phrase]
subtitles << "Search: #{params[:phrase]}".strip
end
subtitle = subtitles.join(', ')
print "\n" ,cyan, bold, title, (subtitle.empty? ? "" : " - #{subtitle}"), "\n", "==================", reset, "\n\n"
if instances.empty?
puts yellow,"No instances found.",reset
else
print_instances_table(instances)
(json_response)
end
print reset,"\n"
end
rescue RestClient::Exception => e
print_rest_exception(e, options)
exit 1
end
end
|
#logs(args) ⇒ Object
277
278
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
|
# File 'lib/morpheus/cli/instances.rb', line 277
def logs(args)
options = {}
optparse = OptionParser.new do|opts|
opts.banner = subcommand_usage("[name]")
opts.on( '-n', '--node NODE_ID', "Scope logs to specific Container or VM" ) do |node_id|
options[:node_id] = node_id.to_i
end
build_common_options(opts, options, [:list, :json, :dry_run, :remote])
end
optparse.parse!(args)
if args.count < 1
puts optparse
exit 1
end
connect(options)
begin
instance = find_instance_by_name_or_id(args[0])
container_ids = instance['containers']
if options[:node_id] && container_ids.include?(options[:node_id])
container_ids = [options[:node_id]]
end
query_params = { max: options[:max] || 100, offset: options[:offset] || 0, query: options[:phrase]}
if options[:dry_run]
print_dry_run @logs_interface.dry.container_logs(container_ids, query_params)
return
end
logs = @logs_interface.container_logs(container_ids, query_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
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
|
# File 'lib/morpheus/cli/instances.rb', line 1067
def remove(args)
options = {}
query_params = {keepBackups: 'off', force: 'off'}
optparse = OptionParser.new do|opts|
opts.banner = subcommand_usage("[name] [-fB]")
opts.on( '-f', '--force', "Force Remove" ) do
query_params[:force] = 'on'
end
opts.on( '-B', '--keep-backups', "Preserve copy of backups" ) do
query_params[:keepBackups] = 'on'
end
build_common_options(opts, options, [:auto_confirm, :json, :dry_run, :quiet, :remote])
end
optparse.parse!(args)
if args.count < 1
puts "\n#{optparse}\n\n"
exit 1
end
connect(options)
begin
instance = find_instance_by_name_or_id(args[0])
unless options[:yes] || ::Morpheus::Cli::OptionTypes::confirm("Are you sure you would like to remove the instance '#{instance['name']}'?", options)
exit 1
end
if options[:dry_run]
print_dry_run @instances_interface.dry.destroy(instance['id'],query_params)
return
end
json_response = @instances_interface.destroy(instance['id'],query_params)
if options[:json]
print JSON.pretty_generate(json_response)
print "\n"
elsif !options[:quiet]
list([])
end
rescue RestClient::Exception => e
print_rest_exception(e, options)
exit 1
end
end
|
#resize(args) ⇒ Object
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
|
# File 'lib/morpheus/cli/instances.rb', line 886
def resize(args)
options = {}
optparse = OptionParser.new do|opts|
opts.banner = subcommand_usage("[name]")
build_common_options(opts, options, [:options, :json, :dry_run, :remote])
end
optparse.parse!(args)
if args.count < 1
puts optparse
exit 1
end
connect(options)
begin
instance = find_instance_by_name_or_id(args[0])
group_id = instance['group']['id']
cloud_id = instance['cloud']['id']
layout_id = instance['layout']['id']
plan_id = instance['plan']['id']
payload = {
:instance => {:id => instance["id"]}
}
puts "\nDue to limitations by most Guest Operating Systems, Disk sizes can only be expanded and not reduced.\nIf a smaller plan is selected, memory and CPU (if relevant) will be reduced but storage will not.\n\n"
service_plans_json = @instances_interface.service_plans({zoneId: cloud_id, layoutId: layout_id})
service_plans = service_plans_json["plans"]
service_plans_dropdown = service_plans.collect {|sp| {'name' => sp["name"], 'value' => sp["id"]} } service_plans_dropdown.each do |plan|
if plan['value'] && plan['value'].to_i == plan_id.to_i
plan['name'] = "#{plan['name']} (current)"
end
end
plan_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'servicePlan', 'type' => 'select', 'fieldLabel' => 'Plan', 'selectOptions' => service_plans_dropdown, 'required' => true, 'description' => 'Choose the appropriately sized plan for this instance'}],options[:options])
service_plan = service_plans.find {|sp| sp["id"] == plan_prompt['servicePlan'].to_i }
new_plan_id = service_plan["id"]
payload[:instance][:plan] = {id: service_plan["id"]}
volumes_response = @instances_interface.volumes(instance['id'])
current_volumes = volumes_response['volumes'].sort {|x,y| x['displayOrder'] <=> y['displayOrder'] }
volumes = prompt_resize_volumes(current_volumes, service_plan, options)
if !volumes.empty?
payload[:volumes] = volumes
end
payload[:deleteOriginalVolumes] = true
if options[:dry_run]
print_dry_run @instances_interface.dry.resize(instance['id'], payload)
return
end
json_response = @instances_interface.resize(instance['id'], payload)
if options[:json]
print JSON.pretty_generate(json_response)
print "\n"
else
print_green_success "Resizing instance #{instance['name']}"
list([])
end
rescue RestClient::Exception => e
print_rest_exception(e, options)
exit 1
end
end
|
#restart(args) ⇒ Object
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
|
# File 'lib/morpheus/cli/instances.rb', line 685
def restart(args)
options = {}
optparse = OptionParser.new do|opts|
opts.banner = subcommand_usage("[name]")
build_common_options(opts, options, [:auto_confirm, :json, :dry_run, :remote])
end
optparse.parse!(args)
if args.count < 1
puts optparse
exit 1
end
connect(options)
begin
instance = find_instance_by_name_or_id(args[0])
unless options[:yes] || ::Morpheus::Cli::OptionTypes::confirm("Are you sure you would like to restart this instance?", options)
exit 1
end
if options[:dry_run]
print_dry_run @instances_interface.dry.restart(instance['id'])
return
end
json_response = @instances_interface.restart(instance['id'])
if options[:json]
print JSON.pretty_generate(json_response)
print "\n"
end
return
rescue RestClient::Exception => e
print_rest_exception(e, options)
exit 1
end
end
|
#restart_service(args) ⇒ Object
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
|
# File 'lib/morpheus/cli/instances.rb', line 851
def restart_service(args)
options = {}
optparse = OptionParser.new do|opts|
opts.banner = subcommand_usage("[name]")
build_common_options(opts, options, [:auto_confirm, :json, :dry_run, :remote])
end
optparse.parse!(args)
if args.count < 1
puts optparse
exit 1
end
connect(options)
begin
instance = find_instance_by_name_or_id(args[0])
unless options[:yes] || ::Morpheus::Cli::OptionTypes::confirm("Are you sure you would like to restart this instance?", options)
exit 1
end
if options[:dry_run]
print_dry_run @instances_interface.dry.restart(instance['id'],false)
return
end
json_response = @instances_interface.restart(instance['id'],false)
if options[:json]
print JSON.pretty_generate(json_response)
print "\n"
else
puts "Restarting service on instance #{args[0]}"
end
return
rescue RestClient::Exception => e
print_rest_exception(e, options)
exit 1
end
end
|
#run_workflow(args) ⇒ Object
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
|
# File 'lib/morpheus/cli/instances.rb', line 1262
def run_workflow(args)
options = {}
optparse = OptionParser.new do|opts|
opts.banner = subcommand_usage("[name] [workflow] [options]")
build_common_options(opts, options, [:options, :json, :dry_run, :remote])
end
optparse.parse!(args)
if args.count < 2
puts "\n#{optparse}\n\n"
exit 1
end
connect(options)
instance = find_instance_by_name_or_id(args[0])
workflow = find_workflow_by_name(args[1])
task_types = @tasks_interface.task_types()
editable_options = []
workflow['taskSetTasks'].sort{|a,b| a['taskOrder'] <=> b['taskOrder']}.each do |task_set_task|
task_type_id = task_set_task['task']['taskType']['id']
task_type = task_types['taskTypes'].find{ |current_task_type| current_task_type['id'] == task_type_id}
task_opts = task_type['optionTypes'].select { |otype| otype['editable']}
if !task_opts.nil? && !task_opts.empty?
editable_options += task_opts.collect do |task_opt|
new_task_opt = task_opt.clone
new_task_opt['fieldContext'] = "#{task_set_task['id']}.#{new_task_opt['fieldContext']}"
end
end
end
params = options[:options] || {}
if params.empty? && !editable_options.empty?
puts optparse
option_lines = editable_options.collect {|it| "\t-O #{it['fieldContext'] ? (it['fieldContext'] + '.') : ''}#{it['fieldName']}=\"value\"" }.join("\n")
puts "\nAvailable Options:\n#{option_lines}\n\n"
exit 1
end
workflow_payload = {taskSet: {"#{workflow['id']}" => params }}
begin
if options[:dry_run]
print_dry_run @instances_interface.dry.workflow(instance['id'],workflow['id'], workflow_payload)
return
end
json_response = @instances_interface.workflow(instance['id'],workflow['id'], workflow_payload)
if options[:json]
print JSON.pretty_generate(json_response)
print "\n"
else
puts "Running workflow..."
end
rescue RestClient::Exception => e
print_rest_exception(e, options)
exit 1
end
end
|
#security_groups(args) ⇒ Object
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
|
# File 'lib/morpheus/cli/instances.rb', line 1171
def security_groups(args)
options = {}
optparse = OptionParser.new do|opts|
opts.banner = subcommand_usage("[name]")
build_common_options(opts, options, [:json, :dry_run, :remote])
end
optparse.parse!(args)
if args.count < 1
puts optparse
exit 1
end
connect(options)
begin
instance = find_instance_by_name_or_id(args[0])
if options[:dry_run]
print_dry_run @instances_interface.dry.security_groups(instance['id'])
return
end
json_response = @instances_interface.security_groups(instance['id'])
if options[:json]
print JSON.pretty_generate(json_response)
print "\n"
return
end
securityGroups = json_response['securityGroups']
print "\n" ,cyan, bold, "Morpheus Security Groups for Instance: #{instance['name']}\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"
rescue RestClient::Exception => e
print_rest_exception(e, options)
exit 1
end
end
|
#setenv(args) ⇒ Object
549
550
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
577
578
579
580
581
582
583
584
585
586
587
588
|
# File 'lib/morpheus/cli/instances.rb', line 549
def setenv(args)
options = {}
optparse = OptionParser.new do|opts|
opts.banner = subcommand_usage("[name] VAR VALUE [-e]")
opts.on( '-e', "Exportable" ) do |exportable|
options[:export] = exportable
end
opts.on( '-M', "Masked" ) do |masked|
options[:masked] = masked
end
build_common_options(opts, options, [:json, :dry_run, :remote, :quiet])
end
optparse.parse!(args)
if args.count < 3
puts optparse
exit 1
end
connect(options)
begin
instance = find_instance_by_name_or_id(args[0])
evar = {name: args[1], value: args[2], export: options[:export], masked: options[:masked]}
payload = {envs: [evar]}
if options[:dry_run]
print_dry_run @instances_interface.dry.create_env(instance['id'], payload)
return
end
json_response = @instances_interface.create_env(instance['id'], payload)
if options[:json]
print JSON.pretty_generate(json_response), "\n"
return
end
if !options[:quiet]
envs([args[0]])
end
rescue RestClient::Exception => e
print_rest_exception(e, options)
exit 1
end
end
|
#start(args) ⇒ Object
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
|
# File 'lib/morpheus/cli/instances.rb', line 655
def start(args)
options = {}
optparse = OptionParser.new do|opts|
opts.banner = subcommand_usage("[name]")
build_common_options(opts, options, [:json, :dry_run, :remote])
end
optparse.parse!(args)
if args.count < 1
puts optparse
exit 1
end
connect(options)
begin
instance = find_instance_by_name_or_id(args[0])
if options[:dry_run]
print_dry_run @instances_interface.dry.start(instance['id'])
return
end
json_response = @instances_interface.start(instance['id'])
if options[:json]
print JSON.pretty_generate(json_response)
print "\n"
end
return
rescue RestClient::Exception => e
print_rest_exception(e, options)
exit 1
end
end
|
#start_service(args) ⇒ Object
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
|
# File 'lib/morpheus/cli/instances.rb', line 819
def start_service(args)
options = {}
optparse = OptionParser.new do|opts|
opts.banner = subcommand_usage("[name]")
build_common_options(opts, options, [:json, :dry_run, :remote])
end
optparse.parse!(args)
if args.count < 1
puts optparse
exit 1
end
connect(options)
begin
instance = find_instance_by_name_or_id(args[0])
if options[:dry_run]
print_dry_run @instances_interface.dry.start(instance['id'], false)
return
end
json_response = @instances_interface.start(instance['id'],false)
if options[:json]
print JSON.pretty_generate(json_response)
print "\n"
else
puts "Starting service on #{args[0]}"
end
return
rescue RestClient::Exception => e
print_rest_exception(e, options)
exit 1
end
end
|
#stats(args) ⇒ Object
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
# File 'lib/morpheus/cli/instances.rb', line 194
def stats(args)
options = {}
optparse = OptionParser.new do|opts|
opts.banner = subcommand_usage("[name]")
build_common_options(opts, options, [:json, :dry_run, :remote])
end
optparse.parse!(args)
if args.count < 1
puts optparse
exit 1
end
connect(options)
begin
instance = find_instance_by_name_or_id(args[0])
if options[:dry_run]
print_dry_run @instances_interface.dry.get(instance['id'])
return
end
json_response = @instances_interface.get(instance['id'])
if options[:json]
print JSON.pretty_generate(json_response), "\n"
return
end
instance = json_response['instance']
stats = json_response['stats'] || {}
print "\n" ,cyan, bold, "Instance Stats: #{instance['name']} (#{instance['instanceType']['name']})\n","==================", "\n\n", reset, cyan
puts "Status: #{format_instance_status(instance)}"
print "\n"
if ((stats['maxMemory'].to_i != 0) || (stats['maxStorage'].to_i != 0))
print_stats_usage(stats)
else
print cyan, "No data.", "\n", reset
end
print reset, "\n"
rescue RestClient::Exception => e
print_rest_exception(e, options)
exit 1
end
end
|
#status_check(args) ⇒ Object
161
162
163
164
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
|
# File 'lib/morpheus/cli/instances.rb', line 161
def status_check(args)
out = ""
options = {}
optparse = OptionParser.new do|opts|
opts.banner = subcommand_usage("[name]")
build_common_options(opts, options, [:quiet, :json, :remote]) end
optparse.parse!(args)
if args.count < 1
puts optparse
exit 1
end
connect(options)
instance = find_instance_by_name_or_id(args[0])
exit_code = 0
if instance['status'].to_s.downcase != (args[1] || "running").to_s.downcase
exit_code = 1
end
if options[:json]
mock_json = {status: instance['status'], exit: exit_code}
out << JSON.pretty_generate(mock_json)
out << "\n"
elsif !options[:quiet]
out << cyan
out << "Status: #{format_instance_status(instance)}"
out << reset
out << "\n"
end
print out unless options[:quiet]
exit exit_code end
|
#stop(args) ⇒ Object
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
|
# File 'lib/morpheus/cli/instances.rb', line 622
def stop(args)
options = {}
optparse = OptionParser.new do|opts|
opts.banner = subcommand_usage("[name]")
build_common_options(opts, options, [:auto_confirm, :json, :dry_run, :remote])
end
optparse.parse!(args)
if args.count < 1
puts optparse
exit 1
end
connect(options)
begin
instance = find_instance_by_name_or_id(args[0])
unless options[:yes] || ::Morpheus::Cli::OptionTypes::confirm("Are you sure you would like to stop this instance?", options)
exit 1
end
if options[:dry_run]
print_dry_run @instances_interface.dry.stop(instance['id'])
return
end
json_response = @instances_interface.stop(instance['id'])
if options[:json]
print JSON.pretty_generate(json_response)
print "\n"
end
return
rescue RestClient::Exception => e
print_rest_exception(e, options)
exit 1
end
end
|
#stop_service(args) ⇒ Object
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
|
# File 'lib/morpheus/cli/instances.rb', line 784
def stop_service(args)
options = {}
optparse = OptionParser.new do|opts|
opts.banner = subcommand_usage("[name]")
build_common_options(opts, options, [:auto_confirm, :json, :dry_run, :remote])
end
optparse.parse!(args)
if args.count < 1
puts optparse
exit 1
end
connect(options)
begin
instance = find_instance_by_name_or_id(args[0])
unless options[:yes] || ::Morpheus::Cli::OptionTypes::confirm("Are you sure you would like to stop this instance?", options)
exit 1
end
if options[:dry_run]
print_dry_run @instances_interface.dry.stop(instance['id'],false)
return
end
json_response = @instances_interface.stop(instance['id'],false)
if options[:json]
print JSON.pretty_generate(json_response)
print "\n"
else
puts "Stopping service on #{args[0]}"
end
return
rescue RestClient::Exception => e
print_rest_exception(e, options)
exit 1
end
end
|
#suspend(args) ⇒ Object
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
|
# File 'lib/morpheus/cli/instances.rb', line 718
def suspend(args)
options = {}
optparse = OptionParser.new do|opts|
opts.banner = subcommand_usage("[name]")
build_common_options(opts, options, [:auto_confirm, :json, :dry_run, :remote])
end
optparse.parse!(args)
if args.count < 1
puts optparse
exit 1
end
connect(options)
begin
instance = find_instance_by_name_or_id(args[0])
unless options[:yes] || ::Morpheus::Cli::OptionTypes::confirm("Are you sure you would like to suspend this instance?", options)
exit 1
end
if options[:dry_run]
print_dry_run @instances_interface.dry.suspend(instance['id'])
return
end
json_response = @instances_interface.suspend(instance['id'])
if options[:json]
print JSON.pretty_generate(json_response)
print "\n"
end
return
rescue RestClient::Exception => e
print_rest_exception(e, options)
exit 1
end
end
|
#update(args) ⇒ Object
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
|
# File 'lib/morpheus/cli/instances.rb', line 99
def update(args)
usage = "Usage: morpheus instances update [name] [options]"
options = {}
optparse = OptionParser.new do|opts|
opts.banner = subcommand_usage("[name]")
build_common_options(opts, options, [:options, :json, :dry_run, :remote])
end
optparse.parse!(args)
if args.count < 1
puts optparse
exit 1
end
connect(options)
begin
instance = find_instance_by_name_or_id(args[0])
payload = {
'instance' => {id: instance["id"]}
}
update_instance_option_types = [
{'fieldName' => 'name', 'fieldLabel' => 'Name', 'type' => 'text', 'required' => true, 'description' => 'Enter a name for this instance'},
{'fieldName' => 'description', 'fieldLabel' => 'Description', 'type' => 'text', 'required' => false},
{'fieldName' => 'instanceContext', 'fieldLabel' => 'Environment', 'type' => 'select', 'required' => false, 'selectOptions' => instance_context_options()},
{'fieldName' => 'tags', 'fieldLabel' => 'Tags', 'type' => 'text', 'required' => false}
]
params = options[:options] || {}
if params.empty?
puts "\n#{usage}\n"
option_lines = update_instance_option_types.collect {|it| "\t-O #{it['fieldName']}=\"value\"" }.join("\n")
puts "\nAvailable Options:\n#{option_lines}\n\n"
exit 1
end
instance_keys = ['name', 'description', 'instanceContext', 'tags']
params = params.select {|k,v| instance_keys.include?(k) }
params['tags'] = params['tags'].split(',').collect {|it| it.to_s.strip }.compact.uniq if params['tags']
payload['instance'].merge!(params)
if options[:dry_run]
print_dry_run @instances_interface.dry.update(instance["id"], payload)
return
end
json_response = @instances_interface.update(instance["id"], payload)
if options[:json]
print JSON.pretty_generate(json_response)
print "\n"
else
print_green_success "Updated instance #{instance['name']}"
list([])
end
rescue RestClient::Exception => e
print_rest_exception(e, options)
exit 1
end
end
|