Class: Morpheus::Cli::Hosts
- Inherits:
-
Object
- Object
- Morpheus::Cli::Hosts
- Includes:
- CliCommand, Term::ANSIColor
- Defined in:
- lib/morpheus/cli/hosts.rb
Instance Method Summary collapse
- #add(args) ⇒ Object
- #connect(opts) ⇒ Object
- #handle(args) ⇒ Object
-
#initialize ⇒ Hosts
constructor
A new instance of Hosts.
- #list(args) ⇒ Object
- #logs(args) ⇒ Object
- #remove(args) ⇒ Object
- #run_workflow(args) ⇒ Object
- #server_types(args) ⇒ Object
- #start(args) ⇒ Object
- #stop(args) ⇒ Object
- #upgrade(args) ⇒ Object
Methods included from CliCommand
accountScopeOptions, genericOptions, included
Constructor Details
#initialize ⇒ Hosts
Returns a new instance of Hosts.
14 15 16 17 |
# File 'lib/morpheus/cli/hosts.rb', line 14 def initialize() @appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance @active_groups = ::Morpheus::Cli::Groups.load_group_file end |
Instance Method Details
#add(args) ⇒ Object
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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/morpheus/cli/hosts.rb', line 164 def add(args) if args.count < 2 puts "\nUsage: morpheus hosts add CLOUD [name]\n\n" return end = {zone: args[0], params:{}} name = args[1] optparse = OptionParser.new do|opts| opts. = "Usage: morpheus server add CLOUD NAME -t HOST_TYPE [options]" opts.on( '-t', '--type TYPE', "Host Type" ) do |server_type| [:server_type] = server_type end Morpheus::Cli::CliCommand.genericOptions(opts,) end optparse.parse(args) connect() params = {} zone=nil if ![:zone].nil? zone = find_zone_by_name(nil, [:zone]) [:params][:zoneId] = zone['id'] end if zone.nil? puts red,bold,"\nEither the cloud was not specified or was not found. Please make sure a cloud is specied at the beginning of the argument\n\n",reset return else zone_type = cloud_type_for_id(zone['zoneTypeId']) end server_type = zone_type['serverTypes'].find{|b| b['creatable'] == true && (b['code'] == [:server_type] || b['name'] == [:server_type])} params = Morpheus::Cli::OptionTypes.prompt(server_type['optionTypes'],[:options],@api_client, [:params]) begin server_payload = {server: {name: name, zone: {id: zone['id']}, computeServerType: [id: server_type['id']]}.merge(params['server']), config: params['config'], network: params['network']} response = @servers_interface.create(server_payload) 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 exit 1 end end |
#connect(opts) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/morpheus/cli/hosts.rb', line 19 def connect(opts) if opts[:remote] @appliance_url = opts[:remote] @appliance_name = opts[:remote] @access_token = Morpheus::Cli::Credentials.new(@appliance_name,@appliance_url).request_credentials(opts) else @access_token = Morpheus::Cli::Credentials.new(@appliance_name,@appliance_url).request_credentials(opts) end @api_client = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url) @clouds_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).clouds @groups_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).groups @tasks_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).tasks @task_sets_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).task_sets @servers_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).servers @logs_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).logs @cloud_types = @clouds_interface.cloud_types['zoneTypes'] if @access_token.empty? print red,bold, "\nInvalid Credentials. Unable to acquire access token. Please verify your credentials and try again.\n\n",reset exit 1 end end |
#handle(args) ⇒ Object
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 |
# File 'lib/morpheus/cli/hosts.rb', line 41 def handle(args) if args.empty? puts "\nUsage: morpheus hosts [list,add,remove,logs,start,stop,run-workflow,make-managed,upgrade-agent] [name]\n\n" exit 1 end case args[0] when 'list' list(args[1..-1]) when 'add' add(args[1..-1]) when 'remove' remove(args[1..-1]) when 'start' start(args[1..-1]) when 'stop' start(args[1..-1]) when 'run-workflow' run_workflow(args[1..-1]) when 'upgrade-agent' upgrade(args[1..-1]) when 'logs' logs(args[1..-1]) when 'server-types' server_types(args[1..-1]) else puts "\nUsage: morpheus hosts [list,add,remove,logs,start,stop,run-workflow,make-managed,upgrade-agent] [name]\n\n" exit 127 #Command now foud exit code end end |
#list(args) ⇒ Object
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 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 |
# File 'lib/morpheus/cli/hosts.rb', line 286 def list(args) = {} params = {} optparse = OptionParser.new do|opts| opts.on( '-g', '--group GROUP', "Group Name" ) do |group| [:group] = group end Morpheus::Cli::CliCommand.genericOptions(opts,) end optparse.parse(args) connect() begin if ![:group].nil? group = find_group_by_name([:group]) if !group.nil? params['site'] = group['id'] end end if ![:max].nil? params['max'] = [:max] end if ![:offset].nil? params['offset'] = [:offset] end if ![:phrase].nil? params['phrase'] = [:phrase] end json_response = @servers_interface.get(params) servers = json_response['servers'] if [:json] print JSON.pretty_generate(json_response) print "\n" else print "\n" ,red, bold, "Morpheus Hosts\n","==================", reset, "\n\n" if servers.empty? puts yellow,"No hosts currently configured.",reset else server_table =servers.collect do |server| power_state = nil if server['powerState'] == 'on' power_state = "#{green}ON#{red}" elsif server['powerState'] == 'off' power_state = "#{red}OFF#{red}" else power_state = "#{white}#{server['powerState'].upcase}#{red}" end {id: server['id'], name: server['name'], platform: server['serverOs'] ? server['serverOs']['name'].upcase : 'N/A', type: server['computeServerType'] ? server['computeServerType']['name'] : 'unmanaged', status: server['status'], power: power_state} # print red, "= [#{server['id']}] #{server['name']} - #{server['computeServerType'] ? server['computeServerType']['name'] : 'unmanaged'} (#{server['status']}) Power: ", power_state, "\n" end end print red tp server_table, :id, :name, :type, :platform, :status, :power print reset,"\n\n" end rescue => e puts "Error Communicating with the Appliance. Please try again later. #{e}" return nil end end |
#logs(args) ⇒ Object
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 |
# File 'lib/morpheus/cli/hosts.rb', line 72 def logs(args) = {} optparse = OptionParser.new do|opts| opts. = "Usage: morpheus hosts logs [name]" Morpheus::Cli::CliCommand.genericOptions(opts,) end if args.count < 1 puts "\n#{optparse.}\n\n" return end optparse.parse(args) connect() begin host = find_host_by_name(args[0]) logs = @logs_interface.server_logs([host['id']], { max: [:max] || 100, offset: [:offset] || 0, query: [:phrase]}) if [: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 ::Morpheus::Cli::ErrorHandler.new.print_rest_exception(e) exit 1 end end |
#remove(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 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 276 277 278 279 280 281 282 283 284 |
# File 'lib/morpheus/cli/hosts.rb', line 212 def remove(args) if args.count < 1 puts "\nUsage: morpheus hosts remove [name] [-c CLOUD] [-f] [-S]\n\n" return end = {} query_params = {name: args[0], removeResources: 'on', force: 'off'} optparse = OptionParser.new do|opts| opts.on( '-c', '--cloud CLOUD', "Cloud" ) do |cloud| [:zone] = cloud end opts.on( '-f', '--force', "Force Remove" ) do query_params[:force] = 'on' end opts.on( '-S', '--skip-remove-infrastructure', "Skip removal of underlying cloud infrastructure" ) do query_params[:removeResources] = 'off' end Morpheus::Cli::CliCommand.genericOptions(opts,) end optparse.parse(args) connect() zone=nil if ![:zone].nil? zone = find_zone_by_name(nil, [:zone]) end begin if zone query_params[:zoneId] = zone['id'] end server = nil server_results = @servers_interface.get(query_params) if server_results['servers'].nil? || server_results['servers'].empty? server_results = @servers_interface.get(args[0].to_i) server = server_results['server'] else if !server_results['servers'].empty? && server_results['servers'].count > 1 puts "Multiple Servers exist with the same name. Try scoping by cloud or using id to confirm" exit 1 end server = server_results['servers'][0] unless server_results['servers'].empty? end if server.nil? puts "Server not found by name #{args[0]}" exit 1 else end if !::Morpheus::Cli::OptionTypes::confirm("Are you sure you would like to remove this server?", ) exit 1 end json_response = @servers_interface.destroy(server['id']) if [:json] print JSON.pretty_generate(json_response) print "\n" else puts "Removing Server..." end 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 exit 1 end end |
#run_workflow(args) ⇒ Object
435 436 437 438 439 440 441 442 443 444 445 446 447 448 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 |
# File 'lib/morpheus/cli/hosts.rb', line 435 def run_workflow(args) = {} optparse = OptionParser.new do|opts| opts. = "Usage: morpheus hosts run-workflow [HOST] [name] [options]" Morpheus::Cli::CliCommand.genericOptions(opts,) end if args.count < 2 puts "\n#{optparse}\n\n" exit 1 end optparse.parse(args) connect() host = find_host_by_name(args[0]) workflow = find_workflow_by_name(args[1]) task_types = @tasks_interface.task_types() = [] 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? += 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] || {} if params.empty? && !.empty? puts "\n#{optparse.}\n\n" option_lines = .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 json_response = @servers_interface.workflow(host['id'],workflow['id'], workflow_payload) if [:json] print JSON.pretty_generate(json_response) print "\n" else puts "Running workflow..." end rescue RestClient::Exception => e ::Morpheus::Cli::ErrorHandler.new.print_rest_exception(e) exit 1 end end |
#server_types(args) ⇒ Object
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 |
# File 'lib/morpheus/cli/hosts.rb', line 114 def server_types(args) if args.count < 1 puts "\nUsage: morpheus hosts server-types CLOUD\n\n" exit 1 end = {zone: args[0]} optparse = OptionParser.new do|opts| opts. = "Usage: morpheus server add CLOUD NAME -t HOST_TYPE [options]" opts.on( '-t', '--type TYPE', "Host Type" ) do |server_type| [:server_type] = server_type end Morpheus::Cli::CliCommand.genericOptions(opts,) end optparse.parse(args) connect() params = {} zone=nil if ![:zone].nil? zone = find_zone_by_name(nil, [:zone]) end if zone.nil? puts "Cloud not found" exit 1 else zone_type = cloud_type_for_id(zone['zoneTypeId']) end server_types = zone_type['serverTypes'].select{|b| b['creatable'] == true} if [:json] print JSON.pretty_generate(server_types) print "\n" else print "\n" ,red, bold, "Morpheus Server Types\n","==================", reset, "\n\n" if server_types.nil? || server_types.empty? puts yellow,"No server types found for the selected cloud.",reset else server_types.each do |server_type| print red, "= #{server_type['code']} - #{server_type['name']}\n" end end print reset,"\n\n" end end |
#start(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 |
# File 'lib/morpheus/cli/hosts.rb', line 351 def start(args) = {} optparse = OptionParser.new do|opts| opts. = "Usage: morpheus hosts start [name]" Morpheus::Cli::CliCommand.genericOptions(opts,) end if args.count < 1 puts "\n#{optparse.}\n\n" exit 1 end optparse.parse(args) connect() begin host = find_host_by_name(args[0]) json_response = @servers_interface.start(host['id']) if [:json] print JSON.pretty_generate(json_response) print "\n" else puts "Host #{host[name]} started." end return rescue RestClient::Exception => e ::Morpheus::Cli::ErrorHandler.new.print_rest_exception(e) exit 1 end end |
#stop(args) ⇒ Object
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 |
# File 'lib/morpheus/cli/hosts.rb', line 379 def stop(args) = {} optparse = OptionParser.new do|opts| opts. = "Usage: morpheus hosts stop [name]" Morpheus::Cli::CliCommand.genericOptions(opts,) end if args.count < 1 puts "\n#{optparse.}\n\n" exit 1 end optparse.parse(args) connect() begin host = find_host_by_name(args[0]) json_response = @servers_interface.stop(host['id']) if [:json] print JSON.pretty_generate(json_response) print "\n" else puts "Host #{host[name]} stopped." end return rescue RestClient::Exception => e ::Morpheus::Cli::ErrorHandler.new.print_rest_exception(e) exit 1 end end |
#upgrade(args) ⇒ Object
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 |
# File 'lib/morpheus/cli/hosts.rb', line 407 def upgrade(args) = {} optparse = OptionParser.new do|opts| opts. = "Usage: morpheus hosts upgrade [name]" Morpheus::Cli::CliCommand.genericOptions(opts,) end if args.count < 1 puts "\n#{optparse.}\n\n" exit 1 end optparse.parse(args) connect() begin host = find_host_by_name(args[0]) json_response = @servers_interface.upgrade(host['id']) if [:json] print JSON.pretty_generate(json_response) print "\n" else puts "Host #{host[name]} upgrading..." end return rescue RestClient::Exception => e ::Morpheus::Cli::ErrorHandler.new.print_rest_exception(e) exit 1 end end |