Class: Morpheus::Cli::Instances

Inherits:
Object
  • Object
show all
Includes:
Term::ANSIColor
Defined in:
lib/morpheus/cli/instances.rb

Instance Method Summary collapse

Constructor Details

#initialize ⇒ Instances

Returns a new instance of Instances.



11
12
13
14
15
16
17
18
# File 'lib/morpheus/cli/instances.rb', line 11

def initialize() 
	@appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance
	@access_token = Morpheus::Cli::Credentials.new(@appliance_name,@appliance_url).request_credentials()
	@instances_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).instances
	@instance_types_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).instance_types
	@groups_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).groups
	@active_groups = ::Morpheus::Cli::Groups.load_group_file
end

Instance Method Details

#add(args) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/morpheus/cli/instances.rb', line 59

def add(args)
	if args.count < 2
		puts "\nUsage: morpheus instances add NAME TYPE\n\n"
		return
	end

	instance_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"
		return
	end

	groupId = @active_groups[@appliance_name.to_sym]

	options = {
		:servicePlan => nil,
		:instance => {
			:name => instance_name,
			:site => {
				:id => groupId
			},
			:instanceType => {
				:code => instance_type_code
			}
		}
	}

	instance_type['instanceTypeLayouts'].sort! { |x,y| y['sortOrder'] <=> x['sortOrder'] }
	puts "Configurations: "
	instance_type['instanceTypeLayouts'].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['instanceTypeLayouts'][layout_selection-1]['id']
	options[:instance][:layout] = {id: layout}
	print "\n"
	if options[: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
		options[: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"))
					options[opt['name']] = STDIN.noecho(&:gets).chomp!
					print "\n"
				else
					options[opt['name']] = $stdin.gets.chomp!
				end
				
			end
		end
	end
	begin
		@instances_interface.create(options)
	rescue => e
		if e.response.code == 400
			error = JSON.parse(e.response.to_s)
			::Morpheus::Cli::ErrorHandler.new.print_errors(error)
		else
			puts "Error Communicating with the Appliance. Please try again later. #{e}"
		end
		return nil
	end
	list([])
end

#delenv(args) ⇒ Object



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

def delenv(args)
	if args.count < 2
		puts "\nUsage: morpheus instances setenv INSTANCE NAME\n\n"
		return
	end
	begin
		instance_results = @instances_interface.get({name: args[0]})
		if instance_results['instances'].empty?
			puts "Instance not found by name #{args[0]}"
			return
		end
		instance = instance_results['instances'][0]
		instance_id = instance['id']
		name = args[1]

		@instances_interface.del_env(instance_id, name)
		envs([args[0]])
	rescue RestClient::Exception => e
		if e.response.code == 400
			error = JSON.parse(e.response.to_s)
			::Morpheus::Cli::ErrorHandler.new.print_errors(error)
		else
			puts "Error Communicating with the Appliance. Please try again later. #{e}"
		end
		return nil
	end
end

#details(args) ⇒ Object



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

def details(args)
	if args.count < 1
		puts "\nUsage: morpheus instances stats [name]\n\n"
		return
	end
	begin
		instance_results = @instances_interface.get({name: args[0]})
		if instance_results['instances'].empty?
			puts "Instance not found by name #{args[0]}"
			return
		end
		instance = instance_results['instances'][0]
		instance_id = instance['id']
		stats = instance_results['stats'][instance_id.to_s]
		print "\n" ,cyan, bold, "#{instance['name']} (#{instance['instanceType']['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 instance
	rescue RestClient::Exception => e
		if e.response.code == 400
			error = JSON.parse(e.response.to_s)
			::Morpheus::Cli::ErrorHandler.new.print_errors(error)
		else
			puts "Error Communicating with the Appliance. Please try again later. #{e}"
		end
		return nil
	end
end

#envs(args) ⇒ Object



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
239
240
241
242
243
244
245
246
# File 'lib/morpheus/cli/instances.rb', line 212

def envs(args)
	if args.count < 1
		puts "\nUsage: morpheus instances envs [name]\n\n"
		return
	end
	begin
		instance_results = @instances_interface.get({name: args[0]})
		if instance_results['instances'].empty?
			puts "Instance not found by name #{args[0]}"
			return
		end
		instance = instance_results['instances'][0]
		instance_id = instance['id']
		env_results = @instances_interface.get_envs(instance_id)
		print "\n" ,cyan, bold, "#{instance['name']} (#{instance['instanceType']['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, "Imported 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
		if e.response.code == 400
			error = JSON.parse(e.response.to_s)
			::Morpheus::Cli::ErrorHandler.new.print_errors(error)
		else
			puts "Error Communicating with the Appliance. Please try again later. #{e}"
		end
		return nil
	end
end

#handle(args) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/morpheus/cli/instances.rb', line 21

def handle(args) 
	if @access_token.empty?
		print red,bold, "\nInvalid Credentials. Unable to acquire access token. Please verify your credentials and try again.\n\n",reset
		return 1
	end
	if args.empty?
		puts "\nUsage: morpheus instances [list,add,remove,stop,start,restart,resize,upgrade,clone,envs,setenv,delenv] [name]\n\n"
		return 
	end

	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])	
		else
			puts "\nUsage: morpheus instances [list,add,remove,stop,start,restart,resize,upgrade,clone,envs,setenv,delenv] [name]\n\n"
	end
end

#list(args) ⇒ Object



383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# File 'lib/morpheus/cli/instances.rb', line 383

def list(args)
	options = {}
	optparse = OptionParser.new do|opts|
		opts.on( '-g', '--group GROUP', "Group Name" ) do |group|
			options[:group] = group
		end
	end
	optparse.parse(args)
	begin
		params = {}
		if !options[:group].nil?
			group = find_group_by_name(options[:group])
			if !group.nil?
				params['site'] = group['id']
			end
		end

		json_response = @instances_interface.get(params)
		instances = json_response['instances']
		print "\n" ,cyan, bold, "Morpheus Instances\n","==================", reset, "\n\n"
		if instances.empty?
			puts yellow,"No instances currently configured.",reset
		else
			instances.each do |instance|
				print cyan, "=  #{instance['name']} (#{instance['instanceType']['name']}) - #{instance['status']['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

#remove(args) ⇒ Object



418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# File 'lib/morpheus/cli/instances.rb', line 418

def remove(args)
	if args.count < 1
		puts "\nUsage: morpheus instances remove [name]\n\n"
		return
	end
	begin
		instance_results = @instances_interface.get({name: args[0]})
		if instance_results['instances'].empty?
			puts "Instance not found by name #{args[0]}"
			return
		end
		@instances_interface.destroy(instance_results['instances'][0]['id'])
		list([])
	rescue RestClient::Exception => e
		if e.response.code == 400
			error = JSON.parse(e.response.to_s)
			::Morpheus::Cli::ErrorHandler.new.print_errors(error)
		else
			puts "Error Communicating with the Appliance. Please try again later. #{e}"
		end
		return nil
	end
end

#restart(args) ⇒ Object



359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/morpheus/cli/instances.rb', line 359

def restart(args)
	if args.count < 1
		puts "\nUsage: morpheus instances restart [name]\n\n"
		return
	end
	begin
		instance_results = @instances_interface.get({name: args[0]})
		if instance_results['instances'].empty?
			puts "Instance not found by name #{args[0]}"
			return
		end
		@instances_interface.restart(instance_results['instances'][0]['id'])
		list([])
	rescue RestClient::Exception => e
		if e.response.code == 400
			error = JSON.parse(e.response.to_s)
			::Morpheus::Cli::ErrorHandler.new.print_errors(error)
		else
			puts "Error Communicating with the Appliance. Please try again later. #{e}"
		end
		return nil
	end
end

#setenv(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
278
279
280
281
# File 'lib/morpheus/cli/instances.rb', line 248

def setenv(args)
	if args.count < 3
		puts "\nUsage: morpheus instances setenv INSTANCE NAME VALUE [-e]\n\n"
		return
	end
	begin
		instance_results = @instances_interface.get({name: args[0]})
		if instance_results['instances'].empty?
			puts "Instance not found by name #{args[0]}"
			return
		end
		instance = instance_results['instances'][0]
		instance_id = instance['id']
		evar = {name: args[1], value: args[2], export: false}
		params = {}
		optparse = OptionParser.new do|opts|
			opts.on( '-e', "Exportable" ) do |exportable|
				evar[:export] = exportable
			end
		end
		optparse.parse(args)

		@instances_interface.create_env(instance_id, [evar])
		envs([args[0]])
	rescue RestClient::Exception => e
		if e.response.code == 400
			error = JSON.parse(e.response.to_s)
			::Morpheus::Cli::ErrorHandler.new.print_errors(error)
		else
			puts "Error Communicating with the Appliance. Please try again later. #{e}"
		end
		return nil
	end
end

#start(args) ⇒ Object



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/morpheus/cli/instances.rb', line 335

def start(args)
	if args.count < 1
		puts "\nUsage: morpheus instances start [name]\n\n"
		return
	end
	begin
		instance_results = @instances_interface.get({name: args[0]})
		if instance_results['instances'].empty?
			puts "Instance not found by name #{args[0]}"
			return
		end
		@instances_interface.start(instance_results['instances'][0]['id'])
		list([])
	rescue RestClient::Exception => e
		if e.response.code == 400
			error = JSON.parse(e.response.to_s)
			::Morpheus::Cli::ErrorHandler.new.print_errors(error)
		else
			puts "Error Communicating with the Appliance. Please try again later. #{e}"
		end
		return nil
	end
end

#stats(args) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/morpheus/cli/instances.rb', line 154

def stats(args)
	if args.count < 1
		puts "\nUsage: morpheus instances stats [name]\n\n"
		return
	end
	begin
		instance_results = @instances_interface.get({name: args[0]})
		if instance_results['instances'].empty?
			puts "Instance not found by name #{args[0]}"
			return
		end
		instance = instance_results['instances'][0]
		instance_id = instance['id']
		stats = instance_results['stats'][instance_id.to_s]
		print "\n" ,cyan, bold, "#{instance['name']} (#{instance['instanceType']['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
		if e.response.code == 400
			error = JSON.parse(e.response.to_s)
			::Morpheus::Cli::ErrorHandler.new.print_errors(error)
		else
			puts "Error Communicating with the Appliance. Please try again later. #{e}"
		end
		return nil
	end
end

#stop(args) ⇒ Object



311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/morpheus/cli/instances.rb', line 311

def stop(args)
	if args.count < 1
		puts "\nUsage: morpheus instances stop [name]\n\n"
		return
	end
	begin
		instance_results = @instances_interface.get({name: args[0]})
		if instance_results['instances'].empty?
			puts "Instance not found by name #{args[0]}"
			return
		end
		@instances_interface.stop(instance_results['instances'][0]['id'])
		list([])
	rescue RestClient::Exception => e
		if e.response.code == 400
			error = JSON.parse(e.response.to_s)
			::Morpheus::Cli::ErrorHandler.new.print_errors(error)
		else
			puts "Error Communicating with the Appliance. Please try again later. #{e}"
		end
		return nil
	end
end