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' }
- SCHEDULE =
{ :name => 'schedule', :large => '--schedule TIME', :description => 'Schedules this action to be executed after' \ 'the given time. For example: onevm resume 0 --schedule "09/23 14:15"', :format => String, :proc => lambda {|o, | if o[0] == '+' [:schedule] = o elsif o == 'now' [:schedule] = Time.now.to_i else begin [:schedule] = Time.parse(o).to_i rescue StandardError STDERR.puts "Error parsing time spec: #{o}" exit(-1) end end } }
- WEEKLY =
{ :name => 'weekly', :large => '--weekly days', :description => 'Repeats the schedule action the days of the week ' \ 'specified, it can be a number between 0 (Sunday) to 6 (Saturday) ' \ 'separated with commas. ' \ 'For example: onevm resume 0 --schedule "09/23 14:15" --weekly 0,2,4', :format => String }
- MONTHLY =
{ :name => 'monthly', :large => '--monthly days', :description => 'Repeats the schedule action the days of the month ' \ 'specified, it can be a number between 1,31 separated with commas. ' \ 'For example: onevm resume 0 --schedule "09/23 14:15" --monthly 1,14', :format => String }
- YEARLY =
{ :name => 'yearly', :large => '--yearly days', :description => 'Repeats the schedule action the days of the year ' \ 'specified, it can be a number between 0,365 separated with commas. ' \ 'For example: onevm resume 0 --schedule "09/23 14:15" --yearly 30,60', :format => String }
- HOURLY =
{ :name => 'hourly', :large => '--hourly hour', :description => 'Repeats the schedule action each hours specified,' \ 'it can be a number between 0,168 separated with commas. ' \ 'For example: onevm resume 0 --schedule "09/23 14:15" --hourly 1,5', :format => Numeric }
- END_TIME =
{ :name => 'end', :large => '--end number|TIME', :description => '----', :format => String }
- 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
- #format_pool(options) ⇒ Object
-
#get_charters ⇒ Object
Get charters configuration.
- #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
-
#ssh(args, options) ⇒ Object
SSH into a VM.
-
#update_schedule_action(vm_id, action_id, file) ⇒ Object
Update schedule action.
Methods inherited from OpenNebulaHelper::OneHelper
#check_orphan, client, #create_resource, #filterflag_to_i, filterflag_to_i_desc, get_client, get_password, #group_name, #initialize, #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, #to_id, to_id_desc, #user_name
Constructor Details
This class inherits a constructor from OpenNebulaHelper::OneHelper
Class Method Details
.conf_file ⇒ Object
237 238 239 |
# File 'lib/one_helper/onevm_helper.rb', line 237 def self.conf_file 'onevm.yaml' end |
.ip_str(vm) ⇒ Object
Return the IP or several IPs of a VM
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 |
# File 'lib/one_helper/onevm_helper.rb', line 256 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| %w[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
233 234 235 |
# File 'lib/one_helper/onevm_helper.rb', line 233 def self.rname 'VM' end |
.state_to_str(id, lcm_id) ⇒ Object
241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/one_helper/onevm_helper.rb', line 241 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
#format_pool(options) ⇒ Object
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 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 |
# File 'lib/one_helper/onevm_helper.rb', line 320 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 table = 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 %w[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 table end |
#get_charters ⇒ Object
Get charters configuration
@return [Array]
- action
- time
- warning
659 660 661 |
# File 'lib/one_helper/onevm_helper.rb', line 659 def get_charters YAML.load_file(self.class.table_conf)[:charters] end |
#get_migration_type(options) ⇒ Object
643 644 645 646 647 648 649 650 651 |
# File 'lib/one_helper/onevm_helper.rb', line 643 def get_migration_type() if [:poweroff] 1 elsif [:poweroff_hard] 2 else 0 end end |
#print_tm_action_list ⇒ Object
633 634 635 636 637 638 639 640 641 |
# File 'lib/one_helper/onevm_helper.rb', line 633 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
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 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 |
# File 'lib/one_helper/onevm_helper.rb', line 529 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 %w[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.to_s 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
307 308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/one_helper/onevm_helper.rb', line 307 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
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
# File 'lib/one_helper/onevm_helper.rb', line 291 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
421 422 423 424 425 426 427 428 429 430 431 432 433 434 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 |
# File 'lib/one_helper/onevm_helper.rb', line 421 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 perform_actions( ids, , ) do |vm| str_periodic = '' if .key?(:weekly) str_periodic << ", REPEAT = 0, DAYS = \"#{[:weekly]}\"" elsif .key?(:monthly) str_periodic << ", REPEAT = 1, DAYS = \"#{[:monthly]}\"" elsif .key?(:yearly) str_periodic << ", REPEAT = 2, DAYS = \"#{[:yearly]}\"" elsif .key?(:hourly) str_periodic << ", REPEAT = 3, DAYS = \"#{[:hourly]}\"" end if .key?(:end) begin end_date = Date.parse([:end]) str_periodic << ', END_TYPE = 2, ' \ "END_VALUE = #{end_date.to_time.to_i}" rescue ArgumentError if [:end].to_i > 0 str_periodic << ', END_TYPE = 1, ' \ "END_VALUE = #{[:end].to_i}" end end elsif str_periodic != '' str_periodic << ', END_TYPE = 0' end tmp_str = "SCHED_ACTION = [" tmp_str << "ACTION = #{action}, " tmp_str << "WARNING = #{warning}," if warning tmp_str << "ARGS = \"#{[:args]}\"," if [:args] tmp_str << "TIME = #{[:schedule]}" tmp_str << str_periodic << ']' vm.sched_action_add(tmp_str) end end |
#ssh(args, options) ⇒ Object
SSH into a VM
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 |
# File 'lib/one_helper/onevm_helper.rb', line 667 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(*%W[ssh #{login}@#{ip} -p #{port} #{cmd}]) else exec('ssh', *opts.split, *%W[#{login}@#{ip} -p #{port} #{cmd}]) end end # rubocop:disable Style/SpecialGlobalVars $?.exitstatus # rubocop:enable Style/SpecialGlobalVars end |
#update_schedule_action(vm_id, action_id, file) ⇒ Object
Update schedule action
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 |
# File 'lib/one_helper/onevm_helper.rb', line 476 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) 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 |