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 => 'poff', :large => '--poff', :description => 'Do the migrate by poweringoff the vm' }
- POFFHARD =
{ :name => 'poff-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 PATH=VALUE format. For example: ' \ 'onevm list --search "VM.NAME=abc&VM.TEMPLATE.DISK[*].IMAGE=db1"', :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
- #format_snapshots(vm) ⇒ Object
- #format_template_snapshots(vm) ⇒ 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_nic_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
176 177 178 |
# File 'lib/one_helper/onevm_helper.rb', line 176 def self.conf_file 'onevm.yaml' end |
.ip_str(vm) ⇒ Object
Return the IP or several IPs of a VM
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 |
# File 'lib/one_helper/onevm_helper.rb', line 195 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 return '--' if ips.empty? ips.join(',') end |
.rname ⇒ Object
172 173 174 |
# File 'lib/one_helper/onevm_helper.rb', line 172 def self.rname 'VM' end |
.state_to_str(id, lcm_id) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/one_helper/onevm_helper.rb', line 180 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
576 577 578 |
# File 'lib/one_helper/onevm_helper.rb', line 576 def charters YAML.load_file(self.class.table_conf)[:charters] end |
#format_pool(options) ⇒ Object
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 352 353 354 355 356 357 358 359 360 361 |
# File 'lib/one_helper/onevm_helper.rb', line 264 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 |
#format_snapshots(vm) ⇒ Object
1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 |
# File 'lib/one_helper/onevm_helper.rb', line 1419 def format_snapshots(vm) table = CLIHelper::ShowTable.new(nil, self) do column :AC, 'Is active', :left, :size => 2 do |d| if d['ACTIVE'] == 'YES' '=>' else '' end end column :ID, 'Snapshot ID', :size => 3 do |d| d['ID'] end column :DISK, 'Disk ID', :size => 4 do |d| d['DISK_ID'] end column :PARENT, 'Snapshot Parent ID', :size => 6 do |d| d['PARENT'] end column :CHILDREN, 'Snapshot Children IDs', :size => 10 do |d| d['CHILDREN'] end column :SIZE, '', :left, :size => 12 do |d| if d['SIZE'] size = OpenNebulaHelper.unit_to_str( d['SIZE'].to_i, {}, 'M' ) else size = '-' end if d['MONITOR_SIZE'] monitor_size = OpenNebulaHelper.unit_to_str( d['MONITOR_SIZE'].to_i, {}, 'M' ) else monitor_size = '-' end "#{monitor_size}/#{size}" end column :NAME, 'Snapshot Name', :left, :size => 32 do |d| d['NAME'] end column :DATE, 'Snapshot creation date', :size => 15 do |d| OpenNebulaHelper.time_to_str(d['DATE']) end default :AC, :ID, :DISK, :PARENT, :DATE, :SIZE, :NAME end # Convert snapshot data to an array vm_hash = vm.to_hash vm_snapshots = [vm_hash['VM']['SNAPSHOTS']].flatten snapshots = [] vm_snapshots.each do |disk| disk_id = disk['DISK_ID'] sshots = [disk['SNAPSHOT'] || []].flatten sshots.each do |snapshot| data = snapshot.merge('DISK_ID' => disk_id) snapshots << data end end # get monitoring data snapshots.each do |snapshot| disk_id = snapshot['DISK_ID'] snap_id = snapshot['ID'] xpath = "MONITORING/SNAPSHOT_SIZE[ID='#{snap_id}' " \ "and DISK_ID='#{disk_id}']/SIZE" snapshot['MONITOR_SIZE'] = vm[xpath] end table.show(snapshots) end |
#format_template_snapshots(vm) ⇒ Object
1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 |
# File 'lib/one_helper/onevm_helper.rb', line 1507 def format_template_snapshots(vm) table = CLIHelper::ShowTable.new(nil, self) do column :ID, '', :size => 4 do |d| d['SNAPSHOT_ID'] unless d.nil? end column :TIME, '', :size => 12 do |d| OpenNebulaHelper.time_to_str(d['TIME'], false) unless d.nil? end column :NAME, '', :left, :size => 46 do |d| d['NAME'] unless d.nil? end column :HYPERVISOR_ID, '', :left, :size => 15 do |d| d['HYPERVISOR_ID'] unless d.nil? end end vm_hash = vm.to_hash table.show([vm_hash['VM']['TEMPLATE']['SNAPSHOT']].flatten, {}) end |
#get_backup_mode(vm_id) ⇒ Object
650 651 652 653 654 655 656 657 658 |
# File 'lib/one_helper/onevm_helper.rb', line 650 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
560 561 562 563 564 565 566 567 568 |
# File 'lib/one_helper/onevm_helper.rb', line 560 def get_migration_type() if [:poff] 1 elsif [:'poff-hard'] 2 else 0 end end |
#print_tm_action_list ⇒ Object
550 551 552 553 554 555 556 557 558 |
# File 'lib/one_helper/onevm_helper.rb', line 550 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
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 539 540 541 542 543 544 545 546 547 548 |
# File 'lib/one_helper/onevm_helper.rb', line 446 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
238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/one_helper/onevm_helper.rb', line 238 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_nic_id(vm_id, id) ⇒ Object
251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/one_helper/onevm_helper.rb', line 251 def retrieve_nic_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/NIC[NAME='#{id}']/NIC_ID") return [-1, "NIC #{id} not found or duplicated"] \ if ids.nil? || ids.size > 1 [0, ids[0].to_i] end |
#retrieve_snapshot_id(vm_id, id) ⇒ Object
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/one_helper/onevm_helper.rb', line 222 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
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 |
# File 'lib/one_helper/onevm_helper.rb', line 363 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
660 661 662 663 |
# File 'lib/one_helper/onevm_helper.rb', line 660 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
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 641 642 643 644 645 646 647 648 |
# File 'lib/one_helper/onevm_helper.rb', line 584 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 $CHILD_STATUS.exitstatus end |
#update_schedule_action(vm_id, action_id, file, options) ⇒ Object
Update schedule action
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 422 423 424 425 426 427 428 429 430 431 |
# File 'lib/one_helper/onevm_helper.rb', line 391 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 |