Class: OneVMHelper
- Inherits:
-
OpenNebulaHelper::OneHelper
- Object
- OpenNebulaHelper::OneHelper
- OneVMHelper
- Defined in:
- lib/one_helper/onevm_helper.rb
Overview
Helper methods for OneVM
Constant Summary collapse
- MULTIPLE =
{ :name => 'multiple', :short => '-m x', :large => '--multiple x', :format => Integer, :description => 'Instance multiple VMs' }
- IMAGE =
{ :name => 'image', :short => '-i id|name', :large => '--image id|name', :description => 'Selects the image', :format => String, :proc => lambda {|o, | OpenNebulaHelper.rname_to_id(o, 'IMAGE') } }
- NETWORK =
{ :name => 'network', :short => '-n id|name', :large => '--network id|name', :description => 'Selects the virtual network', :format => String, :proc => lambda {|o, | OpenNebulaHelper.rname_to_id(o, 'VNET') } }
- IP =
{ :name => 'ip', :short => '-i ip', :large => '--ip ip', :format => String, :description => 'IP address for the new NIC' }
- FILE =
{ :name => 'file', :short => '-f file', :large => '--file file', :description => 'Selects the template file', :format => String, :proc => lambda {|o, | if File.file?(o) [:file] = o else STDERR.puts "File `#{[:file]}` doesn't exist" exit(-1) end } }
- HOLD =
{ :name => 'hold', :large => '--hold', :description => 'Creates the new VM on hold state instead of pending' }
- ALL_TEMPLATE =
{ :name => 'all', :large => '--all', :description => 'Show all template data' }
- LIVE =
{ :name => 'live', :large => '--live', :description => 'Do the action with the VM running' }
- HARD =
{ :name => 'hard', :large => '--hard', :description => 'Does not communicate with the guest OS' }
- POFF =
{ :name => 'poweroff', :large => '--poff', :description => 'Do the migrate by poweringoff the vm' }
- POFFHARD =
{ :name => 'poweroff_hard', :large => '--poff-hard', :description => 'Do the migrate by poweringoff hard the vm' }
- ALIAS =
{ :name => 'alias', :short => '-a alias', :large => '--alias alias', :description => 'Attach the NIC as an ALIAS', :format => String }
- NIC_NAME =
{ :name => 'nic_name', :large => '--nic_name name', :description => 'Name of the NIC', :format => String }
- SEARCH =
{ :name => 'search', :large => '--search search', :description => 'query in KEY=VALUE format', :format => String }
- RECOVER_RETRY_STEPS =
{ :PROLOG_MIGRATE_FAILURE => :migrate, :PROLOG_MIGRATE_POWEROFF_FAILURE => :migrate, :PROLOG_MIGRATE_SUSPEND_FAILURE => :migrate, :PROLOG_MIGRATE_UNKNOWN_FAILURE => :migrate, :PROLOG_FAILURE => :prolog, :PROLOG_RESUME_FAILURE => :resume, :PROLOG_UNDEPLOY_FAILURE => :resume, :EPILOG_FAILURE => :epilog, :EPILOG_STOP_FAILURE => :stop, :EPILOG_UNDEPLOY_FAILURE => :stop }
Instance Attribute Summary
Attributes inherited from OpenNebulaHelper::OneHelper
Class Method Summary collapse
- .conf_file ⇒ Object
-
.ip_str(vm) ⇒ Object
Return the IP or several IPs of a VM.
- .rname ⇒ Object
- .state_to_str(id, lcm_id) ⇒ Object
Instance Method Summary collapse
-
#charters ⇒ Object
Get charters configuration.
- #format_pool(options) ⇒ Object
- #get_backup_mode(vm_id) ⇒ Object
- #get_migration_type(options) ⇒ Object
- #print_tm_action_list ⇒ Object
- #recover_retry_interactive(vm) ⇒ Object
- #retrieve_disk_snapshot_id(vm_id, id) ⇒ Object
- #retrieve_snapshot_id(vm_id, id) ⇒ Object
- #schedule_actions(ids, options, action, warning = nil) ⇒ Object
- #set_backup_mode(vm_ref, backup_mode) ⇒ Object
-
#ssh(args, options) ⇒ Object
SSH into a VM.
-
#update_schedule_action(vm_id, action_id, file, options) ⇒ Object
Update schedule action.
Methods inherited from OpenNebulaHelper::OneHelper
#backup_mode_valid?, #check_orphan, client, #create_resource, #filterflag_to_i, filterflag_to_i_desc, get_client, get_password, #group_name, #initialize, list_layout_help, #list_pool, #list_pool_format, #list_pool_table, #list_pool_top, #list_pool_xml, #list_to_id, list_to_id_desc, name_to_id, #perform_action, #perform_actions, #print_page, #retrieve_resource, #set_client, set_endpoint, set_password, set_user, #show_resource, #start_pager, #stop_pager, table_conf, template_input_help, #to_id, to_id_desc, #user_name
Constructor Details
This class inherits a constructor from OpenNebulaHelper::OneHelper
Class Method Details
.conf_file ⇒ Object
171 172 173 |
# File 'lib/one_helper/onevm_helper.rb', line 171 def self.conf_file 'onevm.yaml' end |
.ip_str(vm) ⇒ Object
Return the IP or several IPs of a VM
190 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 |
# File 'lib/one_helper/onevm_helper.rb', line 190 def self.ip_str(vm) ips = [] vm_nics = [] if !vm['TEMPLATE']['NIC'].nil? vm_nics = [vm['TEMPLATE']['NIC']].flatten end if !vm['TEMPLATE']['PCI'].nil? vm_nics = [vm_nics, vm['TEMPLATE']['PCI']].flatten end vm_nics.each do |nic| ['IP', 'EXTERNAL_IP', 'IP6_GLOBAL', 'IP6_ULA', 'IP6', 'VROUTER_IP', 'VROUTER_IP6_GLOBAL', 'VROUTER_IP6_ULA'].each do |attr| if nic.key?(attr) ips.push(nic[attr]) end end end VirtualMachine::EXTERNAL_IP_ATTRS.each do |attr| external_ip = vm['MONITORING'][attr] if !external_ip.nil? && !ips.include?(external_ip) ips.push(external_ip) end end return '--' if ips.empty? ips.join(',') end |
.rname ⇒ Object
167 168 169 |
# File 'lib/one_helper/onevm_helper.rb', line 167 def self.rname 'VM' end |
.state_to_str(id, lcm_id) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/one_helper/onevm_helper.rb', line 175 def self.state_to_str(id, lcm_id) id = id.to_i state_str = VirtualMachine::VM_STATE[id] short_state_str = VirtualMachine::SHORT_VM_STATES[state_str] if short_state_str == 'actv' lcm_id = lcm_id.to_i lcm_state_str = VirtualMachine::LCM_STATE[lcm_id] return VirtualMachine::SHORT_LCM_STATES[lcm_state_str] end short_state_str end |
Instance Method Details
#charters ⇒ Object
Get charters configuration
@return [Array]
- action
- time
- warning
566 567 568 |
# File 'lib/one_helper/onevm_helper.rb', line 566 def charters YAML.load_file(self.class.table_conf)[:charters] end |
#format_pool(options) ⇒ Object
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 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 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
# File 'lib/one_helper/onevm_helper.rb', line 254 def format_pool() config_file = self.class.table_conf # Get cluster names to use later in list cluster_pool = OpenNebula::ClusterPool.new(@client) rc = cluster_pool.info cluster_names = {} cluster_names['-1'] = 'default' if !OpenNebula.is_error?(rc) cluster_pool.each do |c| cluster_names[c['ID']] = c['NAME'] end end CLIHelper::ShowTable.new(config_file, self) do column :ID, 'ONE identifier for Virtual Machine', :size => 6 do |d| d['ID'] end column :NAME, 'Name of the Virtual Machine', :left, :size => 15 do |d| if d['RESCHED'] == '1' "*#{d['NAME']}" else d['NAME'] end end column :USER, 'Username of the Virtual Machine owner', :left, :size => 8 do |d| helper.user_name(d, ) end column :GROUP, 'Group of the Virtual Machine', :left, :size => 8 do |d| helper.group_name(d, ) end column :STAT, 'Actual status', :size => 4 do |d, _e| OneVMHelper.state_to_str(d['STATE'], d['LCM_STATE']) end column :CPU, 'CPU asigned to the VM', :size => 4 do |d| cpu = d['TEMPLATE']['CPU'] cpu = '0' if cpu.nil? cpu end column :MEM, 'Memory asigned to the VM', :size => 7 do |d| OpenNebulaHelper.unit_to_str(d['TEMPLATE']['MEMORY'].to_i, , 'M') end column :HOST, 'Host where the VM is running', :left, :size => 10 do |d| if d['HISTORY_RECORDS'] && d['HISTORY_RECORDS']['HISTORY'] state_str = VirtualMachine::VM_STATE[d['STATE'].to_i] if ['ACTIVE', 'SUSPENDED', 'POWEROFF'].include? state_str d['HISTORY_RECORDS']['HISTORY']['HOSTNAME'] end end end column :CLUSTER, 'Cluster where the VM is running', :left, :size => 10 do |d| if d['HISTORY_RECORDS']['HISTORY'] history = [d['HISTORY_RECORDS']['HISTORY']].flatten cluster_id = history.last['CID'] cluster = cluster_names[cluster_id] if !cluster cluster_id else cluster end else 'NONE' end end column :TIME, 'Time since the VM was submitted', :size => 10 do |d| stime = d['STIME'].to_i etime = d['ETIME'] == '0' ? Time.now.to_i : d['ETIME'].to_i dtime = etime - stime OpenNebulaHelper.period_to_str(dtime, false) end column :IP, 'VM IP addresses', :left, :adjust, :size => 15 do |d| OneVMHelper.ip_str(d) end default :ID, :USER, :GROUP, :NAME, :STAT, :CPU, :MEM, :HOST, :TIME end end |
#get_backup_mode(vm_id) ⇒ Object
642 643 644 645 646 647 648 649 650 |
# File 'lib/one_helper/onevm_helper.rb', line 642 def get_backup_mode(vm_id) vm = retrieve_resource(vm_id) if vm.has_elements?('/VM/BACKUPS/BACKUP_CONFIG') vm.template_like_str('BACKUPS/BACKUP_CONFIG/MODE') else nil end end |
#get_migration_type(options) ⇒ Object
550 551 552 553 554 555 556 557 558 |
# File 'lib/one_helper/onevm_helper.rb', line 550 def get_migration_type() if [:poweroff] 1 elsif [:poweroff_hard] 2 else 0 end end |
#print_tm_action_list ⇒ Object
540 541 542 543 544 545 546 547 548 |
# File 'lib/one_helper/onevm_helper.rb', line 540 def print_tm_action_list puts 'TM Action list:'.green i = 0 @tm_action_list.lines.each do |line| i += 1 puts "(#{i}) #{line}" end puts end |
#recover_retry_interactive(vm) ⇒ Object
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 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 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 |
# File 'lib/one_helper/onevm_helper.rb', line 436 def recover_retry_interactive(vm) begin require 'one_tm' rescue LoadError STDERR.puts <<-EOT one_tm library not found. Make sure you execute recover --interactive in the frontend machine. EOT exit(-1) end # Disable CTRL-C in the menu trap('SIGINT') {} if !File.readable?(VAR_LOCATION + '/config') STDERR.puts "Error reading #{VAR_LOCATION + '/config'}. The TM " \ 'Debug Interactive Environment must be executed as ' \ 'oneadmin in the frontend.' exit(-1) end rc = vm.info if OpenNebula.is_error?(rc) STDERR.puts rc. exit(-1) end if !RECOVER_RETRY_STEPS.include?(vm.lcm_state_str.to_sym) STDERR.puts "Current LCM STATE '#{vm.lcm_state_str}' not " \ 'compatible with RECOVER RETRY action.' exit(-1) end seq = vm['/VM/HISTORY_RECORDS/HISTORY[last()]/SEQ'] tm_action = RECOVER_RETRY_STEPS[vm.lcm_state_str.to_sym] tm_file = "#{VMS_LOCATION}/#{vm.id}/transfer.#{seq}.#{tm_action}" if !File.readable?(tm_file) STDERR.puts "Cannot read #{tm_file}" exit(-1) end @tm_action_list = File.read(tm_file) puts 'TM Debug Interactive Environment.'.green puts print_tm_action_list @tm = TransferManagerDriver.new(nil) i = 0 @tm_action_list.lines.each do |tm_command| i += 1 success = false until success puts "Current action (#{i}):".green puts tm_command puts puts <<-EOF.gsub(/^\s+/, '') Choose action: (r) Run action (n) Skip to next action (a) Show all actions (q) Quit EOF ans = '' until ['n', 'a', 'r', 'q'].include?(ans) printf '> ' ans = STDIN.gets.strip.downcase puts case ans when 'n' success = true when 'a' print_tm_action_list when 'q' exit(-1) when 'r' result, = @tm.do_transfer_action(@id, tm_command.split) if result == 'SUCCESS' success = true puts result else puts puts "#{result}. Repeat command.".red end puts end end end end puts 'If all the TM actions have been successful and you want to' puts 'recover the Virtual Machine to the RUNNING state execute this ' puts "command $ onevm recover #{vm.id} --success" end |
#retrieve_disk_snapshot_id(vm_id, id) ⇒ Object
241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/one_helper/onevm_helper.rb', line 241 def retrieve_disk_snapshot_id(vm_id, id) return [0, id.to_i] if id =~ /\A\d+\z/ vm = retrieve_resource(vm_id) vm.info ids = vm.retrieve_elements("/VM/SNAPSHOTS/SNAPSHOT[NAME='#{id}']/ID") return [-1, "Snapshot #{id} not found or duplicated"] \ if ids.nil? || ids.size > 1 [0, ids[0].to_i] end |
#retrieve_snapshot_id(vm_id, id) ⇒ Object
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/one_helper/onevm_helper.rb', line 225 def retrieve_snapshot_id(vm_id, id) return [0, id.to_i] if id =~ /\A\d+\z/ vm = retrieve_resource(vm_id) vm.info ids = vm.retrieve_elements( "/VM/TEMPLATE/SNAPSHOT[NAME='#{id}']/SNAPSHOT_ID" ) return [-1, "#{id} not found or duplicated"] \ if ids.nil? || ids.size > 1 [0, ids[0].to_i] end |
#schedule_actions(ids, options, action, warning = nil) ⇒ Object
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 |
# File 'lib/one_helper/onevm_helper.rb', line 353 def schedule_actions(ids, , action, warning = nil) # Verbose by default [:verbose] = true = if [:schedule].class == Integer "#{action} scheduled at #{Time.at([:schedule])}" else "#{action} scheduled after #{[:schedule]}s from start" end tmp_str = OpenNebulaHelper.schedule_action_tmpl(, action, warning) perform_actions(ids, , ) do |vm| rc = vm.sched_action_add(tmp_str) if OpenNebula.is_error?(rc) STDERR.puts rc. return -1 end end end |
#set_backup_mode(vm_ref, backup_mode) ⇒ Object
652 653 654 655 |
# File 'lib/one_helper/onevm_helper.rb', line 652 def set_backup_mode(vm_ref, backup_mode) vm = retrieve_resource(vm_ref) vm.updateconf("BACKUP_CONFIG = [\"MODE\"=\"#{backup_mode}\"]") end |
#ssh(args, options) ⇒ Object
SSH into a VM
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 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 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 |
# File 'lib/one_helper/onevm_helper.rb', line 574 def ssh(args, ) perform_action(args[0], , 'SSH') do |vm| rc = vm.info if OpenNebula.is_error?(rc) STDERR.puts rc. exit(-1) end if vm.lcm_state_str != 'RUNNING' STDERR.puts 'VM is not RUNNING, cannot SSH to it' exit(-1) end # Get user to login username = vm.retrieve_xmlelements('//TEMPLATE/CONTEXT/USERNAME')[0] if !username.nil? login = username.text elsif !args[1].nil? login = args[1] else login = 'root' end # Get CMD to run [:cmd].nil? ? cmd = '' : cmd = [:cmd] # Get NIC to connect if [:nic_id] nic = vm.retrieve_xmlelements( "//TEMPLATE/NIC[NIC_ID=\"#{[:nic_id]}\"]" )[0] else nic = vm.retrieve_xmlelements('//TEMPLATE/NIC[SSH="YES"]')[0] end nic = vm.retrieve_xmlelements('//TEMPLATE/NIC[1]')[0] if nic.nil? if nic.nil? STDERR.puts 'No NIC found' exit(-1) end # If there is node port if nic['EXTERNAL_PORT_RANGE'] ip = vm.to_hash['VM']['HISTORY_RECORDS']['HISTORY'] ip = [ip].flatten[-1]['HOSTNAME'] port = Integer(nic['EXTERNAL_PORT_RANGE'].split(':')[0]) + 21 else ip = nic['IP'] port = 22 end [:ssh_opts].nil? ? opts = '' : opts = [:ssh_opts] if opts.empty? exec('ssh', "#{login}@#{ip}", '-p', port.to_s, cmd.to_s) else exec('ssh', *opts.split, "#{login}@#{ip}", '-p', port.to_s, cmd.to_s) end end # rubocop:disable Style/SpecialGlobalVars $?.exitstatus # rubocop:enable Style/SpecialGlobalVars end |
#update_schedule_action(vm_id, action_id, file, options) ⇒ Object
Update schedule action
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 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 |
# File 'lib/one_helper/onevm_helper.rb', line 381 def update_schedule_action(vm_id, action_id, file, ) perform_action(vm_id, , 'Sched action updated') do |vm| rc = vm.info if OpenNebula.is_error?(rc) STDERR.puts "Error #{rc.}" exit(-1) end xpath = "TEMPLATE/SCHED_ACTION[ID=#{action_id}]" unless vm.retrieve_elements(xpath) STDERR.puts "Sched action #{action_id} not found" exit(-1) end # Get user information if file str = File.read(file) elsif !(stdin = OpenNebulaHelper.read_stdin).empty? str = stdin else str = OpenNebulaHelper.update_template(vm_id, vm, nil, xpath) end # Delete the current sched action vm.delete_element(xpath) # Add the modified sched action tmp_str = "\nSCHED_ACTION = [" tmp_str << str.split("\n").join(',') tmp_str << ']' rc = vm.sched_action_update(action_id, tmp_str) if OpenNebula.is_error?(rc) STDERR.puts "Error updating: #{rc.}" exit(-1) end end end |