Class: Morpheus::Cli::Library
- Inherits:
-
Object
- Object
- Morpheus::Cli::Library
- Includes:
- CliCommand
- Defined in:
- lib/morpheus/cli/library.rb
Instance Attribute Summary
Attributes included from CliCommand
Instance Method Summary collapse
- #add(args) ⇒ Object
- #add_version(args) ⇒ Object
- #connect(opts) ⇒ Object
- #get(args) ⇒ Object
- #handle(args) ⇒ Object
-
#initialize ⇒ Library
constructor
A new instance of Library.
- #list(args) ⇒ Object
- #remove(args) ⇒ Object
- #update(args) ⇒ Object
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
#initialize ⇒ Library
Returns a new instance of Library.
12 13 14 |
# File 'lib/morpheus/cli/library.rb', line 12 def initialize() # @appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance end |
Instance Method Details
#add(args) ⇒ Object
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 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 121 def add(args) = {} optparse = OptionParser.new do|opts| opts. = subcommand_usage() (opts, , [:options, :json, :dry_run]) end optparse.parse!(args) connect() begin params = Morpheus::Cli::OptionTypes.prompt(add_instance_type_option_types, [:options], @api_client, [: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.(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 payload = {instanceType: instance_type_payload} if [:dry_run] print_dry_run @custom_instance_types_interface.dry.create(payload) if logo_file print_dry_run @custom_instance_types_interface.dry.update_logo(":id", logo_file) end return end json_response = @custom_instance_types_interface.create(payload) if json_response['success'] if logo_file begin @custom_instance_types_interface.update_logo(json_response['instanceType']['id'], logo_file) rescue RestClient::Exception => e print_red_alert "Failed to save logo!" print_rest_exception(e, ) end end end if [:json] print JSON.pretty_generate(json_response), "\n" return end print_green_success "Added Instance Type #{instance_type_payload['name']}" unless [:no_prompt] if ::Morpheus::Cli::OptionTypes::confirm("Add first version?", ) puts "\n" add_version(["code:#{json_response['code']}"]) end end #list([]) rescue RestClient::Exception => e print_rest_exception(e, ) exit 1 end end |
#add_version(args) ⇒ Object
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 400 401 402 |
# File 'lib/morpheus/cli/library.rb', line 314 def add_version(args) = {} optparse = OptionParser.new do|opts| opts. = subcommand_usage("[name]") (opts, , [:options, :json]) end optparse.parse!(args) if args.count < 1 puts optparse exit 1 end connect() 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_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]) 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]) payload['containerType']['shortName'] = v_prompt['containerType']['shortName'] end v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldContext' => 'containerType', 'fieldName' => 'provisionTypeCode', 'type' => 'select', 'selectOptions' => , 'fieldLabel' => 'Technology', 'required' => true, 'description' => 'The type of container technology.'}], [: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]) 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],@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(, @api_client) request_payload = payload json_response = @custom_instance_types_interface.create_version(instance_type['id'], request_payload) if [: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, ) exit 1 end end |
#connect(opts) ⇒ Object
16 17 18 19 20 |
# File 'lib/morpheus/cli/library.rb', line 16 def connect(opts) @api_client = establish_remote_appliance_connection(opts) @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 |
#get(args) ⇒ Object
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 |
# File 'lib/morpheus/cli/library.rb', line 73 def get(args) = {} optparse = OptionParser.new do|opts| opts. = subcommand_usage("[name]") (opts, , [:json, :dry_run]) end optparse.parse!(args) if args.count < 1 puts optparse exit 1 end connect() begin if [:dry_run] if args[0] =~ /code:/ print_dry_run @custom_instance_types_interface.dry.list({code: args[0]}) else print_dry_run @custom_instance_types_interface.dry.list({name: args[0]}) end return end instance_type = find_custom_instance_type_by_name_or_code(args[0]) exit 1 if instance_type.nil? if [: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" end rescue RestClient::Exception => e print_rest_exception(e, ) exit 1 end end |
#handle(args) ⇒ Object
22 23 24 |
# File 'lib/morpheus/cli/library.rb', line 22 def handle(args) handle_subcommand(args) end |
#list(args) ⇒ Object
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/morpheus/cli/library.rb', line 27 def list(args) = {} optparse = OptionParser.new do|opts| opts. = subcommand_usage() (opts, , [:list, :dry_run, :json]) end optparse.parse!(args) connect() begin params = {} [:phrase, :offset, :max, :sort, :direction].each do |k| params[k] = [k] unless [k].nil? end if [:dry_run] print_dry_run @custom_instance_types_interface.dry.list(params) return end json_response = @custom_instance_types_interface.list(params) if [: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" rescue RestClient::Exception => e print_rest_exception(e, ) exit 1 end end |
#remove(args) ⇒ Object
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 306 307 308 309 310 311 312 |
# File 'lib/morpheus/cli/library.rb', line 275 def remove(args) = {} optparse = OptionParser.new do|opts| opts. = subcommand_usage("[name]") (opts, , [:auto_confirm, :json, :dry_run]) end optparse.parse!(args) if args.count < 1 puts optparse exit 1 end connect() 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']}?", ) exit end if [:dry_run] print_dry_run @custom_instance_types_interface.dry.destroy(instance_type['id']) return end json_response = @custom_instance_types_interface.destroy(instance_type['id']) if [: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, ) 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 268 269 270 271 272 273 |
# File 'lib/morpheus/cli/library.rb', line 191 def update(args) = {} optparse = OptionParser.new do|opts| opts. = subcommand_usage("[name] [options]") (opts, , [:options, :json, :dry_run]) end optparse.parse!(args) if args.count < 1 puts optparse exit 1 end connect() 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] || {} 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.(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 optparse 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 payload = {instanceType: instance_type_payload} if [:dry_run] print_dry_run @custom_instance_types_interface.dry.update(instance_type['id'], payload) if logo_file print_dry_run @custom_instance_types_interface.dry.update_logo(":id", logo_file) end return end json_response = @custom_instance_types_interface.update(instance_type['id'], payload) if json_response['success'] if logo_file begin @custom_instance_types_interface.update_logo(json_response['instanceType']['id'], logo_file) rescue RestClient::Exception => e print_red_alert "Failed to save logo!" print_rest_exception(e, ) end end end if [: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, ) exit 1 end end |