Class: Morpheus::Cli::Library

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

Instance Method Summary collapse

Methods included from CliCommand

#build_common_options, included

Constructor Details

#initialize ⇒ Library

Returns a new instance of Library.



9
10
11
# File 'lib/morpheus/cli/library.rb', line 9

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

Instance Method Details

#add(args) ⇒ Object



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

def add(args)
	options = {}
	optparse = OptionParser.new do|opts|
		opts.banner = "Usage: morpheus library add"
		build_common_options(opts, options, [:options, :json])
	end
	optparse.parse(args)
	connect(options)
	begin
		params = Morpheus::Cli::OptionTypes.prompt(add_instance_type_option_types, options[:options], @api_client, options[:params])
		instance_type_keys = ['name', 'description', 'category', 'visibility', 'environmentPrefix']
		instance_type_payload = params.select {|k,v| instance_type_keys.include?(k) }
		logo_file = nil
		if params['logo']
			filename = File.expand_path(params['logo'])
			if !File.exists?(filename)
				print_red_alert "File not found: #{filename}"
         exit 1
			end
			#instance_type_payload['logo'] = File.new(filename, 'rb')
			logo_file = File.new(filename, 'rb')
		end
		if params['hasAutoScale'] == 'on'
			instance_type_payload['hasAutoScale'] = true
		end
		if params['hasDeployment'] == 'on'
			instance_type_payload['hasDeployment'] = true
		end
		request_payload = {instanceType: instance_type_payload}
		json_response = @custom_instance_types_interface.create(request_payload)

		if json_response['success']
			if logo_file
				begin
					@custom_instance_types_interface.(json_response['instanceType']['id'], logo_file)
				rescue RestClient::Exception => e
					print_red_alert "Failed to save logo!"
					print_rest_exception(e, options)
				end
			end
		end

		if options[:json]
			print JSON.pretty_generate(json_response), "\n"
			return
		end

		print_green_success "Added Instance Type #{instance_type_payload['name']}"
		
		unless options[:no_prompt]
			if ::Morpheus::Cli::OptionTypes::confirm("Add first version?", options)
				puts "\n"
				add_version(["code:#{json_response['code']}"])
			end
		end

		#list([])

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

#add_version(args) ⇒ Object



307
308
309
310
311
312
313
314
315
316
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
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/library.rb', line 307

def add_version(args)
	options = {}
	optparse = OptionParser.new do|opts|
		opts.banner = "Usage: morpheus library add-version [name]"
		build_common_options(opts, options, [:options, :json])
	end
	optparse.parse(args)
	if args.count < 1
		puts "\n#{optparse.banner}\n\n"
		exit 1
	end
	connect(options)
	begin
		instance_type = find_custom_instance_type_by_name_or_code(args[0])
		exit 1 if instance_type.nil?

		#params = Morpheus::Cli::OptionTypes.prompt(add_version_option_types, options[:options], @api_client, options[:params])
		
		provision_types = @provision_types_interface.get({customSupported: true})['provisionTypes']
		if provision_types.empty?
			print_red_alert "No available provision types found!"
			exit 1
		end
		provision_type_options = provision_types.collect {|it| { 'name' => it['name'], 'value' => it['code']} }
		
		payload = {'containerType' => {}, 'instanceTypeLayout' => {}, 'instanceType' => {}}

		v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldContext' => 'instanceTypeLayout', 'fieldName' => 'name', 'type' => 'text', 'fieldLabel' => 'Name', 'required' => true, 'description' => 'A name for this layout.'}], options[:options])
		payload['instanceTypeLayout']['name'] = v_prompt['instanceTypeLayout']['name']

		# shortName is only available for the first new version
		if !instance_type['versions'] || instance_type['versions'].size == 0
			v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldContext' => 'containerType', 'fieldName' => 'shortName', 'type' => 'text', 'fieldLabel' => 'Short Name', 'required' => true, 'description' => 'The short name is a lowercase name with no spaces used for display in your container list.'}], options[:options])
			payload['containerType']['shortName'] = v_prompt['containerType']['shortName']
		end

		v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldContext' => 'containerType', 'fieldName' => 'provisionTypeCode', 'type' => 'select', 'selectOptions' => provision_type_options, 'fieldLabel' => 'Technology', 'required' => true, 'description' => 'The type of container technology.'}], options[:options])
		payload['containerType']['provisionTypeCode'] = v_prompt['containerType']['provisionTypeCode']
		provision_type = provision_types.find {|it| it['code'] == payload['containerType']['provisionTypeCode'] }

		v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldContext' => 'instanceTypeLayout', 'fieldName' => 'instanceVersion', 'type' => 'text', 'fieldLabel' => 'Version', 'required' => true, 'description' => 'A Version Number eg. 0.0.1'}], options[:options])
		payload['instanceTypeLayout']['instanceVersion'] = v_prompt['instanceTypeLayout']['instanceVersion']

		custom_option_types = provision_type['customOptionTypes']

		if (!custom_option_types || custom_option_types.empty?)
			puts yellow,"Sorry, no options were found for #{provision_type['name']}.",reset
			exit 1
		end
		
		# prompt custom options for the selected provision type
		field_group_name = custom_option_types.first['fieldGroup'] || "#{provision_type['name']} Options"
		puts field_group_name
		puts "==============="
		v_prompt = Morpheus::Cli::OptionTypes.prompt(custom_option_types,options[:options],@api_client, {provisionTypCode: payload['provisionTypeCode']})

		if v_prompt['containerType']
			payload['containerType'].merge!(v_prompt['containerType'])
		end
		if v_prompt['containerType.config']
			payload['containerType']['config'] = v_prompt['containerType.config']
		end
		if v_prompt['instanceTypeLayout']
			payload['instanceTypeLayout'].merge!(v_prompt['instanceTypeLayout'])
		end
		# instanceType.backupType, which is not even persisted on the server?
		if v_prompt['instanceType']
			payload['instanceType'].merge!(v_prompt['instanceType'])
		end

		# puts "PAYLOAD:"
		# puts JSON.pretty_generate(payload)
		
		# puts "\nexiting early"
		# exit 0

		payload['exposedPorts'] = prompt_exposed_ports(options, @api_client)

		request_payload = payload
		json_response = @custom_instance_types_interface.create_version(instance_type['id'], request_payload)

		if options[:json]
			print JSON.pretty_generate(json_response), "\n"
			return
		end

		print_green_success "Added Instance Type Version #{instance_type['name']} - #{payload['instanceTypeLayout']['instanceVersion']}"
		#list([])
	rescue RestClient::Exception => e
		print_rest_exception(e, options)
		exit 1
	end
end

#connect(opts) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/morpheus/cli/library.rb', line 13

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
	@api_client = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url)
	@custom_instance_types_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).custom_instance_types
	@provision_types_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).provision_types
end

#details(args) ⇒ Object



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

def details(args)
	options = {}
	optparse = OptionParser.new do|opts|
		opts.banner = "Usage: morpheus library 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
		instance_type = find_custom_instance_type_by_name_or_code(args[0])
		exit 1 if instance_type.nil?

		if options[:json]
			print JSON.pretty_generate({instanceType: instance_type}), "\n"
			return
		end

		if instance_type.nil?
			puts yellow,"No custom instance type found by name #{name}.",reset
		else
			print "\n" ,cyan, bold, "Custom Instance Type Details\n","==================", reset, "\n\n"
			versions = instance_type['versions'].join(', ')
			print cyan, "=  #{instance_type['name']} (#{instance_type['code']}) - #{versions}\n"
			instance_type['instanceTypeLayouts'].each do |layout|
				print green, "     - #{layout['name']}\n",reset
			end
			print reset,"\n\n"
		end

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

#handle(args) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/morpheus/cli/library.rb', line 24

def handle(args) 
	usage = "Usage: morpheus library [list,details,add,update,remove,add-version] [name]"
	case args[0]
		when 'list'
			list(args[1..-1])
		when 'details'
			details(args[1..-1])
		when 'add'
			add(args[1..-1])
		when 'update'
			update(args[1..-1])
		when 'remove'
			remove(args[1..-1])
		when 'add-version'
			add_version(args[1..-1])
		else
			puts "\n#{usage}\n\n"
			exit 127
	end
end

#list(args) ⇒ Object



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

def list(args)
	options = {}
	optparse = OptionParser.new do|opts|
		opts.banner = "Usage: morpheus library list"
		build_common_options(opts, options, [:list, :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

		json_response = @custom_instance_types_interface.list(params)

		if options[:json]
			print JSON.pretty_generate(json_response), "\n"
			return
		end

		instance_types = json_response['instanceTypes']
		print "\n" ,cyan, bold, "Morpheus Custom Instance Types\n","==================", reset, "\n\n"
		if instance_types.empty?
			puts yellow,"No instance types currently configured.",reset
		else
			instance_types.each do |instance_type|
				versions = instance_type['versions'].join(', ')
				print cyan, "=  #{instance_type['name']} (#{instance_type['code']}) - #{versions}\n"
				instance_type['instanceTypeLayouts'].each do |layout|
					print green, "     - #{layout['name']}\n",reset
				end
			end
		end
		print reset,"\n\n"
		
	rescue RestClient::Exception => e
		print_rest_exception(e, options)
		exit 1
	end
end

#remove(args) ⇒ Object



269
270
271
272
273
274
275
276
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
# File 'lib/morpheus/cli/library.rb', line 269

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

	begin
		
		instance_type = find_custom_instance_type_by_name_or_code(args[0])
		exit 1 if instance_type.nil?

		unless Morpheus::Cli::OptionTypes.confirm("Are you sure you want to delete the instance type #{instance_type['name']}?", options)
			exit
		end

		json_response = @custom_instance_types_interface.destroy(instance_type['id'])

		if options[:json]
			print JSON.pretty_generate(json_response), "\n"
			return
		end

		print_green_success "Removed Instance Type #{instance_type['name']}"
		#list([])
	rescue RestClient::Exception => e
		print_rest_exception(e, options)
		exit 1
	end
end

#update(args) ⇒ Object



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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/morpheus/cli/library.rb', line 191

def update(args)
	options = {}
	optparse = OptionParser.new do|opts|
		opts.banner = "Usage: morpheus library update [name] [options]"
		build_common_options(opts, options, [:options, :json])
	end
	optparse.parse(args)
	if args.count < 1
		puts "\n#{optparse.banner}\n\n"
		exit 1
	end
	connect(options)
	begin
		instance_type = find_custom_instance_type_by_name_or_code(args[0])
		exit 1 if instance_type.nil?
		# option_types = update_instance_type_option_types(instance_type)
		# params = Morpheus::Cli::OptionTypes.prompt(option_types, options[:options], @api_client, options[:params])
		params = options[:options] || {}
		
		instance_type_keys = ['name', 'description', 'category', 'visibility', 'environmentPrefix']
		instance_type_payload = params.select {|k,v| instance_type_keys.include?(k) }
		logo_file = nil
		if params['logo']
			filename = File.expand_path(params['logo'])
			if !File.exists?(filename)
				print_red_alert "File not found: #{filename}"
         exit 1
			end
			#instance_type_payload['logo'] = File.new(filename, 'rb')
			logo_file = File.new(filename, 'rb')
		end
		if params['hasAutoScale'] == 'on'
			instance_type_payload['hasAutoScale'] = true
		elsif params['hasAutoScale'] == 'off'
			instance_type_payload['hasAutoScale'] = false
		end
		if params['hasDeployment'] == 'on'
			instance_type_payload['hasDeployment'] = true
		elsif params['hasDeployment'] == 'off'
			instance_type_payload['hasDeployment'] = false
		end
		if instance_type_payload.empty? && logo_file.nil?
			puts "\n#{optparse.banner}\n\n"
			option_lines = update_instance_type_option_types.collect {|it| "\t-O #{it['fieldName']}=\"value\"" }.join("\n")
			puts "\nAvailable Options:\n#{option_lines}\n\n"
			exit 1
		end
		if instance_type_payload.empty?
			# just updating logo (separate request)
			instance_type_payload['name'] = instance_type['name']
		end
		request_payload = {instanceType: instance_type_payload}
		json_response = @custom_instance_types_interface.update(instance_type['id'], request_payload)

		if json_response['success']
			if logo_file
				begin
					@custom_instance_types_interface.(json_response['instanceType']['id'], logo_file)
				rescue RestClient::Exception => e
					print_red_alert "Failed to save logo!"
					print_rest_exception(e, options)
				end
			end
		end

		if options[:json]
			print JSON.pretty_generate(json_response), "\n"
			return
		end

		print_green_success "Updated Instance Type #{instance_type_payload['name']}"
		#list([])
	rescue RestClient::Exception => e
		print_rest_exception(e, options)
		exit 1
	end
end