Class: Vmpooler::PoolManager::Provider::VSphere
- Inherits:
-
Base
- Object
- Base
- Vmpooler::PoolManager::Provider::VSphere
- Defined in:
- lib/vmpooler/providers/vsphere.rb
Constant Summary collapse
- ADAPTER_TYPE =
vSphere helper methods
'lsiLogic'- DISK_TYPE =
'thin'- DISK_MODE =
'persistent'
Instance Attribute Summary collapse
-
#connection_pool ⇒ Object
readonly
The connection_pool method is normally used only for testing.
Instance Method Summary collapse
- #add_disk(vm, size, datastore, connection, datacentername) ⇒ Object
- #build_compatible_hosts_lists(hosts, percentage) ⇒ Object
-
#build_propSpecs(datacenter, folder, vmname) ⇒ Object
rubocop:disable Naming/MethodName.
- #connect_to_vsphere ⇒ Object
- #cpu_utilization_for(host) ⇒ Object
- #create_clone_spec(relocate_spec, config_spec) ⇒ Object
- #create_config_spec(vm_name, template_name, extra_config) ⇒ Object
- #create_disk(pool_name, vm_name, disk_size) ⇒ Object
- #create_folder(connection, new_folder, datacenter) ⇒ Object
- #create_relocate_spec(target_datastore, target_datacenter_name, pool_name, connection) ⇒ Object
- #create_snapshot(pool_name, vm_name, new_snapshot_name) ⇒ Object
- #create_template_delta_disks(pool) ⇒ Object
- #create_vm(pool_name, new_vmname) ⇒ Object
- #destroy_folder(folder_object) ⇒ Object
- #destroy_folder_and_children(folder_object) ⇒ Object
- #destroy_vm(pool_name, vm_name) ⇒ Object
- #destroy_vm_and_log(vm_name, vm_object, pool, data_ttl) ⇒ Object
- #domain(pool_name) ⇒ Object
- #ensured_vsphere_connection(connection_pool_object) ⇒ Object
- #find_cluster(cluster, connection, datacentername) ⇒ Object
- #find_datastore(datastorename, connection, datacentername) ⇒ Object
- #find_device(vm, device_name) ⇒ Object
- #find_disk_controller(vm) ⇒ Object
- #find_disk_devices(vm) ⇒ Object
- #find_disk_unit_number(vm, controller) ⇒ Object
- #find_host_by_dnsname(connection, dnsname) ⇒ Object
- #find_least_used_host(cluster, connection, datacentername) ⇒ Object
- #find_least_used_hosts(cluster, datacentername, percentage) ⇒ Object
- #find_least_used_vpshere_compatible_host(vm) ⇒ Object
- #find_snapshot(vm, snapshotname) ⇒ Object
- #find_template_vm(pool, connection) ⇒ Object
- #find_vm(pool_name, vmname, connection) ⇒ Object
-
#find_vm_folder(pool_name, connection) ⇒ Object
Finds a folder object by inventory path Params:
pool_name:: the pool to find the folder forconnection:: the vsphere connection object returns a ManagedObjectReference for the folder found or nil if not found. - #folder_configured?(folder_title, base_folder, configured_folders, allowlist) ⇒ Boolean
-
#generate_vm_hash(vm_object, pool_name) ⇒ Object
Return a hash of VM data Provides vmname, hostname, template, poolname, boottime and powerstate information.
- #get_average_cluster_utilization(hosts) ⇒ Object
- #get_base_folders(folders) ⇒ Object
- #get_base_vm_container_from(connection) ⇒ Object
- #get_cluster_host_utilization(cluster, model = nil) ⇒ Object
- #get_disk_backing(pool) ⇒ Object
- #get_folder_children(folder_name, connection) ⇒ Object
- #get_host_cpu_arch_version(host) ⇒ Object
-
#get_host_utilization(host, model = nil, limit = 90) ⇒ Object
Returns an array containing cumulative CPU and memory utilization of a host, and its object reference Params:
model:: CPU arch version to match onlimit:: Hard limit for CPU or memory utilization beyond which a host is excluded for deployments returns nil if one on these conditions is true: the model param is defined and cannot be found the host is in maintenance mode the host status is not 'green' the cpu or memory utilization is bigger than the limit param. - #get_snapshot_list(tree, snapshotname) ⇒ Object
-
#get_target_cluster_from_config(pool_name) ⇒ Object
VSphere Helper methods.
- #get_target_datacenter_from_config(pool_name) ⇒ Object
- #get_vm(pool_name, vm_name) ⇒ Object
- #get_vm_details(pool_name, vm_name, connection) ⇒ Object
- #get_vm_folder_path(vm_object) ⇒ Object
-
#get_vm_ip_address(vm_name, pool_name) ⇒ Object
The inner method requires vmware tools running in the guest os.
- #host_has_cpu_model?(host, model) ⇒ Boolean
-
#initialize(config, logger, metrics, redis_connection_pool, name, options) ⇒ VSphere
constructor
A new instance of VSphere.
- #linked_clone?(pool) ⇒ Boolean
- #memory_utilization_for(host) ⇒ Object
- #migrate_vm(pool_name, vm_name) ⇒ Object
- #migrate_vm_and_record_timing(pool_name, vm_name, vm_hash, target_host_object, dest_host_name) ⇒ Object
- #migrate_vm_host(vm_object, host) ⇒ Object
- #migrate_vm_to_new_host(pool_name, vm_name, vm_hash, connection) ⇒ Object
- #migration_enabled?(config) ⇒ Boolean
-
#name ⇒ Object
name of the provider class.
-
#open_socket(host, domain = nil, timeout = 5, port = 22, &_block) ⇒ Object
This should supercede the open_socket method in the Pool Manager.
-
#pool_folders(provider_name) ⇒ Object
Return a list of pool folders.
- #purge_unconfigured_resources(allowlist) ⇒ Object
- #revert_snapshot(pool_name, vm_name, snapshot_name) ⇒ Object
- #run_select_hosts(pool_name, target) ⇒ Object
- #select_least_used_hosts(hosts, percentage) ⇒ Object
- #select_next_host(pool_name, target, architecture = nil) ⇒ Object
- #select_target_hosts(target, cluster, datacenter) ⇒ Object
- #set_network_device(datacenter_name, template_vm_network_device, network_name, connection) ⇒ Object
- #valid_template_path?(template) ⇒ Boolean
- #vm_in_target?(pool_name, parent_host, architecture, target) ⇒ Boolean
- #vm_ready?(pool_name, vm_name, redis) ⇒ Boolean
- #vms_in_pool(pool_name) ⇒ Object
- #vsphere_connection_ok?(connection) ⇒ Boolean
- #vsphere_connection_timeout ⇒ Object
- #wait_for_host_selection(dc, target, maxloop = 0, loop_delay = 1, max_age = 60) ⇒ Object
- #with_circuit_breaker(&block) ⇒ Object
Constructor Details
#initialize(config, logger, metrics, redis_connection_pool, name, options) ⇒ VSphere
Returns a new instance of VSphere.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/vmpooler/providers/vsphere.rb', line 16 def initialize(config, logger, metrics, redis_connection_pool, name, ) super(config, logger, metrics, redis_connection_pool, name, ) task_limit = global_config[:config].nil? || global_config[:config]['task_limit'].nil? ? 10 : global_config[:config]['task_limit'].to_i # The default connection pool size is: # Whatever is biggest from: # - How many pools this provider services # - Maximum number of cloning tasks allowed # - Need at least 2 connections so that a pool can have inventory functions performed while cloning etc. default_connpool_size = [provided_pools.count, task_limit, 2].max connpool_size = provider_config['connection_pool_size'].nil? ? default_connpool_size : provider_config['connection_pool_size'].to_i # The default connection pool timeout should be quite large - 60 seconds connpool_timeout = provider_config['connection_pool_timeout'].nil? ? 60 : provider_config['connection_pool_timeout'].to_i logger.log('d', "[#{name}] ConnPool - Creating a connection pool of size #{connpool_size} with timeout #{connpool_timeout}") @connection_pool = Vmpooler::PoolManager::GenericConnectionPool.new( metrics: metrics, connpool_type: 'provider_connection_pool', connpool_provider: name, size: connpool_size, timeout: connpool_timeout ) do logger.log('d', "[#{name}] Connection Pool - Creating a connection object") # Need to wrap the vSphere connection object in another object. The generic connection pooler will preserve # the object reference for the connection, which means it cannot "reconnect" by creating an entirely new connection # object. Instead by wrapping it in a Hash, the Hash object reference itself never changes but the content of the # Hash can change, and is preserved across invocations. new_conn = connect_to_vsphere { connection: new_conn } end @provider_hosts = {} @provider_hosts_lock = Mutex.new @redis = redis_connection_pool end |
Instance Attribute Details
#connection_pool ⇒ Object (readonly)
The connection_pool method is normally used only for testing
14 15 16 |
# File 'lib/vmpooler/providers/vsphere.rb', line 14 def connection_pool @connection_pool end |
Instance Method Details
#add_disk(vm, size, datastore, connection, datacentername) ⇒ Object
738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 |
# File 'lib/vmpooler/providers/vsphere.rb', line 738 def add_disk(vm, size, datastore, connection, datacentername) return false unless size.to_i > 0 vmdk_datastore = find_datastore(datastore, connection, datacentername) raise("Datastore '#{datastore}' does not exist in datacenter '#{datacentername}'") if vmdk_datastore.nil? datacenter = connection.serviceInstance.find_datacenter(datacentername) controller = find_disk_controller(vm) disk_unit_number = find_disk_unit_number(vm, controller) disk_count = vm.config.hardware.device.grep(RbVmomi::VIM::VirtualDisk).count vmdk_file_name = "#{vm['name']}/#{vm['name']}_#{disk_count}.vmdk" vmdk_spec = RbVmomi::VIM::FileBackedVirtualDiskSpec( capacityKb: size.to_i * 1024 * 1024, adapterType: ADAPTER_TYPE, diskType: DISK_TYPE ) vmdk_backing = RbVmomi::VIM::VirtualDiskFlatVer2BackingInfo( datastore: vmdk_datastore, diskMode: DISK_MODE, fileName: "[#{datastore}] #{vmdk_file_name}" ) device = RbVmomi::VIM::VirtualDisk( backing: vmdk_backing, capacityInKB: size.to_i * 1024 * 1024, controllerKey: controller.key, key: -1, unitNumber: disk_unit_number ) device_config_spec = RbVmomi::VIM::VirtualDeviceConfigSpec( device: device, operation: RbVmomi::VIM::VirtualDeviceConfigSpecOperation('add') ) vm_config_spec = RbVmomi::VIM::VirtualMachineConfigSpec( deviceChange: [device_config_spec] ) connection.serviceContent.virtualDiskManager.CreateVirtualDisk_Task( datacenter: datacenter, name: "[#{datastore}] #{vmdk_file_name}", spec: vmdk_spec ).wait_for_completion vm.ReconfigVM_Task(spec: vm_config_spec).wait_for_completion true end |
#build_compatible_hosts_lists(hosts, percentage) ⇒ Object
950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 |
# File 'lib/vmpooler/providers/vsphere.rb', line 950 def build_compatible_hosts_lists(hosts, percentage) hosts_with_arch_versions = hosts.map do |h| { 'utilization' => h[0], 'host_object' => h[1], 'architecture' => get_host_cpu_arch_version(h[1]) } end versions = hosts_with_arch_versions.map { |host| host['architecture'] }.uniq architectures = {} versions.each do |version| architectures[version] = [] end hosts_with_arch_versions.each do |h| architectures[h['architecture']] << [h['utilization'], h['host_object'], h['architecture']] end versions.each do |version| targets = select_least_used_hosts(architectures[version], percentage) architectures[version] = targets end architectures end |
#build_propSpecs(datacenter, folder, vmname) ⇒ Object
rubocop:disable Naming/MethodName
1064 1065 1066 1067 1068 1069 |
# File 'lib/vmpooler/providers/vsphere.rb', line 1064 def build_propSpecs(datacenter, folder, vmname) # rubocop:disable Naming/MethodName { entity => self, :inventoryPath => "#{datacenter}/vm/#{folder}/#{vmname}" } end |
#connect_to_vsphere ⇒ Object
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 |
# File 'lib/vmpooler/providers/vsphere.rb', line 673 def connect_to_vsphere max_tries = global_config[:config]['max_tries'] || 3 retry_factor = global_config[:config]['retry_factor'] || 10 try = 1 begin connection = RbVmomi::VIM.connect host: provider_config['server'], user: provider_config['username'], password: provider_config['password'], insecure: provider_config['insecure'] || false, read_timeout: vsphere_connection_timeout, open_timeout: vsphere_connection_timeout metrics.increment('connect.open') connection rescue StandardError => e metrics.increment('connect.fail') raise e if try >= max_tries sleep(try * retry_factor) try += 1 retry end end |
#cpu_utilization_for(host) ⇒ Object
929 930 931 932 933 934 935 |
# File 'lib/vmpooler/providers/vsphere.rb', line 929 def cpu_utilization_for(host) cpu_usage = host.summary.quickStats.overallCpuUsage return nil if cpu_usage.nil? cpu_size = host.summary.hardware.cpuMhz * host.summary.hardware.numCpuCores cpu_usage.fdiv(cpu_size) * 100 end |
#create_clone_spec(relocate_spec, config_spec) ⇒ Object
456 457 458 459 460 461 462 463 |
# File 'lib/vmpooler/providers/vsphere.rb', line 456 def create_clone_spec(relocate_spec, config_spec) RbVmomi::VIM.VirtualMachineCloneSpec( location: relocate_spec, config: config_spec, powerOn: true, template: false ) end |
#create_config_spec(vm_name, template_name, extra_config) ⇒ Object
422 423 424 425 426 427 428 429 430 431 432 |
# File 'lib/vmpooler/providers/vsphere.rb', line 422 def create_config_spec(vm_name, template_name, extra_config) RbVmomi::VIM.VirtualMachineConfigSpec( annotation: JSON.pretty_generate( name: vm_name, created_by: provider_config['username'], base_template: template_name, creation_timestamp: Time.now.utc ), extraConfig: extra_config ) end |
#create_disk(pool_name, vm_name, disk_size) ⇒ Object
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 |
# File 'lib/vmpooler/providers/vsphere.rb', line 509 def create_disk(pool_name, vm_name, disk_size) pool = pool_config(pool_name) raise("Pool #{pool_name} does not exist for the provider #{name}") if pool.nil? datastore_name = pool['datastore'] raise("Pool #{pool_name} does not have a datastore defined for the provider #{name}") if datastore_name.nil? @connection_pool.with_metrics do |pool_object| connection = ensured_vsphere_connection(pool_object) vm_object = find_vm(pool_name, vm_name, connection) raise("VM #{vm_name} in pool #{pool_name} does not exist for the provider #{name}") if vm_object.nil? add_disk(vm_object, disk_size, datastore_name, connection, get_target_datacenter_from_config(pool_name)) end true end |
#create_folder(connection, new_folder, datacenter) ⇒ Object
1212 1213 1214 1215 1216 1217 1218 |
# File 'lib/vmpooler/providers/vsphere.rb', line 1212 def create_folder(connection, new_folder, datacenter) dc = connection.serviceInstance.find_datacenter(datacenter) folder_object = dc.vmFolder.traverse(new_folder, RbVmomi::VIM::Folder, true) raise("Cannot create folder #{new_folder}") if folder_object.nil? folder_object end |
#create_relocate_spec(target_datastore, target_datacenter_name, pool_name, connection) ⇒ Object
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 |
# File 'lib/vmpooler/providers/vsphere.rb', line 434 def create_relocate_spec(target_datastore, target_datacenter_name, pool_name, connection) pool = pool_config(pool_name) target_cluster_name = get_target_cluster_from_config(pool_name) relocate_spec = RbVmomi::VIM.VirtualMachineRelocateSpec( datastore: find_datastore(target_datastore, connection, target_datacenter_name), diskMoveType: get_disk_backing(pool) ) manage_host_selection = @config[:config]['manage_host_selection'] if @config[:config].key?('manage_host_selection') if manage_host_selection run_select_hosts(pool_name, @provider_hosts) target_host = select_next_host(pool_name, @provider_hosts) host_object = find_host_by_dnsname(connection, target_host) relocate_spec.host = host_object else # Choose a cluster/host to place the new VM on target_cluster_object = find_cluster(target_cluster_name, connection, target_datacenter_name) relocate_spec.pool = target_cluster_object.resourcePool end relocate_spec end |
#create_snapshot(pool_name, vm_name, new_snapshot_name) ⇒ Object
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 |
# File 'lib/vmpooler/providers/vsphere.rb', line 526 def create_snapshot(pool_name, vm_name, new_snapshot_name) @connection_pool.with_metrics do |pool_object| connection = ensured_vsphere_connection(pool_object) vm_object = find_vm(pool_name, vm_name, connection) raise("VM #{vm_name} in pool #{pool_name} does not exist for the provider #{name}") if vm_object.nil? old_snap = find_snapshot(vm_object, new_snapshot_name) raise("Snapshot #{new_snapshot_name} for VM #{vm_name} in pool #{pool_name} already exists for the provider #{name}") unless old_snap.nil? vm_object.CreateSnapshot_Task( name: new_snapshot_name, description: 'vmpooler', memory: true, quiesce: true ).wait_for_completion end true end |
#create_template_delta_disks(pool) ⇒ Object
1235 1236 1237 1238 1239 1240 1241 1242 |
# File 'lib/vmpooler/providers/vsphere.rb', line 1235 def create_template_delta_disks(pool) @connection_pool.with_metrics do |pool_object| connection = ensured_vsphere_connection(pool_object) template_vm_object = find_template_vm(pool, connection) template_vm_object.add_delta_disk_layer_on_all_disks end end |
#create_vm(pool_name, new_vmname) ⇒ Object
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 |
# File 'lib/vmpooler/providers/vsphere.rb', line 327 def create_vm(pool_name, new_vmname) pool = pool_config(pool_name) raise("Pool #{pool_name} does not exist for the provider #{name}") if pool.nil? vm_hash = nil with_circuit_breaker do @connection_pool.with_metrics do |pool_object| connection = ensured_vsphere_connection(pool_object) # Assume all pool config is valid i.e. not missing template_path = pool['template'] target_folder_path = pool['folder'] target_datastore = pool['datastore'] target_datacenter_name = get_target_datacenter_from_config(pool_name) # Get the template VM object raise("Pool #{pool_name} did not specify a full path for the template for the provider #{name}") unless valid_template_path? template_path template_vm_object = find_template_vm(pool, connection) extra_config = [ { key: 'guestinfo.hostname', value: new_vmname } ] if pool.key?('snapshot_mainMem_ioBlockPages') ioblockpages = pool['snapshot_mainMem_ioBlockPages'] extra_config.push( { key: 'mainMem.ioBlockPages', value: ioblockpages } ) end if pool.key?('snapshot_mainMem_iowait') iowait = pool['snapshot_mainMem_iowait'] extra_config.push( { key: 'mainMem.iowait', value: iowait } ) end # Annotate with creation time, origin template, etc. # Add extraconfig options that can be queried by vmtools config_spec = create_config_spec(new_vmname, template_path, extra_config) # Check if alternate network configuration is specified and add configuration if pool.key?('network') template_vm_network_device = template_vm_object.config.hardware.device.grep(RbVmomi::VIM::VirtualEthernetCard).first network_name = pool['network'] network_device = set_network_device(target_datacenter_name, template_vm_network_device, network_name, connection) config_spec.deviceChange = [{ operation: 'edit', device: network_device }] end # Put the VM in the specified folder and resource pool relocate_spec = create_relocate_spec(target_datastore, target_datacenter_name, pool_name, connection) # Create a clone spec clone_spec = create_clone_spec(relocate_spec, config_spec) begin vm_target_folder = find_vm_folder(pool_name, connection) vm_target_folder ||= create_folder(connection, target_folder_path, target_datacenter_name) if @config[:config].key?('create_folders') && (@config[:config]['create_folders'] == true) rescue StandardError if @config[:config].key?('create_folders') && (@config[:config]['create_folders'] == true) vm_target_folder = create_folder(connection, target_folder_path, target_datacenter_name) else raise end end raise ArgumentError, "Cannot find the configured folder for #{pool_name} #{target_folder_path}" unless vm_target_folder # Create the new VM new_vm_object = template_vm_object.CloneVM_Task( folder: vm_target_folder, name: new_vmname, spec: clone_spec ).wait_for_completion vm_hash = generate_vm_hash(new_vm_object, pool_name) end end vm_hash end |
#destroy_folder(folder_object) ⇒ Object
118 119 120 121 122 123 124 125 126 |
# File 'lib/vmpooler/providers/vsphere.rb', line 118 def destroy_folder(folder_object) try = 0 if try.nil? max_tries = 3 logger.log('s', "[-] [#{folder_object.name}] removing unconfigured folder") folder_object.Destroy_Task.wait_for_completion rescue StandardError try += 1 try >= max_tries ? raise : retry end |
#destroy_folder_and_children(folder_object) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/vmpooler/providers/vsphere.rb', line 102 def destroy_folder_and_children(folder_object) vms = {} data_ttl = $config[:redis]['data_ttl'].to_i folder_name = folder_object.name unless folder_object.childEntity.count == 0 folder_object.childEntity.each do |vm| vms[vm.name] = vm end vms.each do |vm_name, vm_object| destroy_vm_and_log(vm_name, vm_object, folder_name, data_ttl) end end destroy_folder(folder_object) end |
#destroy_vm(pool_name, vm_name) ⇒ Object
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 |
# File 'lib/vmpooler/providers/vsphere.rb', line 559 def destroy_vm(pool_name, vm_name) with_circuit_breaker do @connection_pool.with_metrics do |pool_object| connection = ensured_vsphere_connection(pool_object) vm_object = find_vm(pool_name, vm_name, connection) # If a VM doesn't exist then it is effectively deleted next if vm_object.nil? # Poweroff the VM if it's running vm_object.PowerOffVM_Task.wait_for_completion if vm_object.runtime&.powerState && vm_object.runtime.powerState == 'poweredOn' # Kill it with fire vm_object.Destroy_Task.wait_for_completion end end true end |
#destroy_vm_and_log(vm_name, vm_object, pool, data_ttl) ⇒ Object
68 69 70 71 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 |
# File 'lib/vmpooler/providers/vsphere.rb', line 68 def destroy_vm_and_log(vm_name, vm_object, pool, data_ttl) try = 0 if try.nil? max_tries = 3 @redis.with_metrics do |redis| redis.multi do |transaction| transaction.srem("vmpooler__completed__#{pool}", vm_name) transaction.hdel("vmpooler__active__#{pool}", vm_name) transaction.hset("vmpooler__vm__#{vm_name}", 'destroy', Time.now.to_s) # Auto-expire metadata key transaction.expire("vmpooler__vm__#{vm_name}", data_ttl * 60 * 60) end end start = Time.now if vm_object.is_a? RbVmomi::VIM::Folder logger.log('s', "[!] [#{pool}] '#{vm_name}' is a folder, bailing on destroying") raise('Expected VM, but received a folder object') end vm_object.PowerOffVM_Task.wait_for_completion if vm_object.runtime&.powerState && vm_object.runtime.powerState == 'poweredOn' vm_object.Destroy_Task.wait_for_completion finish = format('%<time>.2f', time: Time.now - start) logger.log('s', "[-] [#{pool}] '#{vm_name}' destroyed in #{finish} seconds") metrics.timing("destroy.#{pool}", finish) rescue RuntimeError raise rescue StandardError => e try += 1 logger.log('s', "[!] [#{pool}] failed to destroy '#{vm_name}' with an error: #{e}") try >= max_tries ? raise : retry end |
#domain(pool_name) ⇒ Object
55 56 57 58 |
# File 'lib/vmpooler/providers/vsphere.rb', line 55 def domain(pool_name) dns_plugin_name = pool_config(pool_name)['dns_plugin'] dns_config(dns_plugin_name) end |
#ensured_vsphere_connection(connection_pool_object) ⇒ Object
661 662 663 664 |
# File 'lib/vmpooler/providers/vsphere.rb', line 661 def ensured_vsphere_connection(connection_pool_object) connection_pool_object[:connection] = connect_to_vsphere unless vsphere_connection_ok?(connection_pool_object[:connection]) connection_pool_object[:connection] end |
#find_cluster(cluster, connection, datacentername) ⇒ Object
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 |
# File 'lib/vmpooler/providers/vsphere.rb', line 1021 def find_cluster(cluster, connection, datacentername) datacenter = connection.serviceInstance.find_datacenter(datacentername) raise("Datacenter #{datacentername} does not exist") if datacenter.nil? # In the event the cluster is not a direct descendent of the # datacenter, we use a ContainerView to leverage its recursive # search. This will find clusters which are, for example, in # folders under the datacenter. This will also find standalone # hosts which are not part of a cluster. cv = connection.serviceContent.viewManager.CreateContainerView( container: datacenter.hostFolder, type: ['ComputeResource', 'ClusterComputeResource'], recursive: true ) cluster = cv.view.find { |cluster_object| cluster_object.name == cluster } cv.DestroyView cluster end |
#find_datastore(datastorename, connection, datacentername) ⇒ Object
790 791 792 793 794 795 |
# File 'lib/vmpooler/providers/vsphere.rb', line 790 def find_datastore(datastorename, connection, datacentername) datacenter = connection.serviceInstance.find_datacenter(datacentername) raise("Datacenter #{datacentername} does not exist") if datacenter.nil? datacenter.find_datastore(datastorename) end |
#find_device(vm, device_name) ⇒ Object
797 798 799 800 801 802 803 |
# File 'lib/vmpooler/providers/vsphere.rb', line 797 def find_device(vm, device_name) vm.config.hardware.device.each do |device| return device if device.deviceInfo.label == device_name end nil end |
#find_disk_controller(vm) ⇒ Object
805 806 807 808 809 810 811 812 813 |
# File 'lib/vmpooler/providers/vsphere.rb', line 805 def find_disk_controller(vm) devices = find_disk_devices(vm) devices.keys.sort.each do |device| return find_device(vm, devices[device]['device'].deviceInfo.label) if devices[device]['children'].length < 15 end nil end |
#find_disk_devices(vm) ⇒ Object
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 |
# File 'lib/vmpooler/providers/vsphere.rb', line 815 def find_disk_devices(vm) devices = {} vm.config.hardware.device.each do |device| if device.is_a? RbVmomi::VIM::VirtualSCSIController if devices[device.controllerKey].nil? devices[device.key] = {} devices[device.key]['children'] = [] end devices[device.key]['device'] = device end if device.is_a? RbVmomi::VIM::VirtualDisk if devices[device.controllerKey].nil? devices[device.controllerKey] = {} devices[device.controllerKey]['children'] = [] end devices[device.controllerKey]['children'].push(device) end end devices end |
#find_disk_unit_number(vm, controller) ⇒ Object
841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 |
# File 'lib/vmpooler/providers/vsphere.rb', line 841 def find_disk_unit_number(vm, controller) used_unit_numbers = [] available_unit_numbers = [] devices = find_disk_devices(vm) devices.keys.sort.each do |c| next unless controller.key == devices[c]['device'].key used_unit_numbers.push(devices[c]['device'].scsiCtlrUnitNumber) devices[c]['children'].each do |disk| used_unit_numbers.push(disk.unitNumber) end end (0..15).each do |scsi_id| available_unit_numbers.push(scsi_id) if used_unit_numbers.grep(scsi_id).length <= 0 end available_unit_numbers.min end |
#find_host_by_dnsname(connection, dnsname) ⇒ Object
1006 1007 1008 1009 1010 1011 |
# File 'lib/vmpooler/providers/vsphere.rb', line 1006 def find_host_by_dnsname(connection, dnsname) host_object = connection.searchIndex.FindByDnsName(dnsName: dnsname, vmSearch: false) return nil if host_object.nil? host_object end |
#find_least_used_host(cluster, connection, datacentername) ⇒ Object
1013 1014 1015 1016 1017 1018 1019 |
# File 'lib/vmpooler/providers/vsphere.rb', line 1013 def find_least_used_host(cluster, connection, datacentername) cluster_object = find_cluster(cluster, connection, datacentername) target_hosts = get_cluster_host_utilization(cluster_object) raise("There is no host candidate in vcenter that meets all the required conditions, check that the cluster has available hosts in a 'green' status, not in maintenance mode and not overloaded CPU and memory'") if target_hosts.empty? target_hosts.min[1] end |
#find_least_used_hosts(cluster, datacentername, percentage) ⇒ Object
988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 |
# File 'lib/vmpooler/providers/vsphere.rb', line 988 def find_least_used_hosts(cluster, datacentername, percentage) @connection_pool.with_metrics do |pool_object| connection = ensured_vsphere_connection(pool_object) cluster_object = find_cluster(cluster, connection, datacentername) raise("Cluster #{cluster} cannot be found") if cluster_object.nil? target_hosts = get_cluster_host_utilization(cluster_object) raise("there is no candidate in vcenter that meets all the required conditions, that the cluster has available hosts in a 'green' status, not in maintenance mode and not overloaded CPU and memory'") if target_hosts.empty? architectures = build_compatible_hosts_lists(target_hosts, percentage) least_used_hosts = select_least_used_hosts(target_hosts, percentage) { 'hosts' => least_used_hosts, 'architectures' => architectures } end end |
#find_least_used_vpshere_compatible_host(vm) ⇒ Object
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 |
# File 'lib/vmpooler/providers/vsphere.rb', line 1049 def find_least_used_vpshere_compatible_host(vm) source_host = vm.summary.runtime.host model = get_host_cpu_arch_version(source_host) cluster = source_host.parent target_hosts = get_cluster_host_utilization(cluster, model) raise("There is no host candidate in vcenter that meets all the required conditions, check that the cluster has available hosts in a 'green' status, not in maintenance mode and not overloaded CPU and memory'") if target_hosts.empty? target_host = target_hosts.min[1] [target_host, target_host.name] end |
#find_snapshot(vm, snapshotname) ⇒ Object
1060 1061 1062 |
# File 'lib/vmpooler/providers/vsphere.rb', line 1060 def find_snapshot(vm, snapshotname) get_snapshot_list(vm.snapshot.rootSnapshotList, snapshotname) if vm.snapshot end |
#find_template_vm(pool, connection) ⇒ Object
1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 |
# File 'lib/vmpooler/providers/vsphere.rb', line 1220 def find_template_vm(pool, connection) datacenter = get_target_datacenter_from_config(pool['name']) raise('cannot find datacenter') if datacenter.nil? propSpecs = { # rubocop:disable Naming/VariableName entity: self, inventoryPath: "#{datacenter}/vm/#{pool['template']}" } template_vm_object = connection.searchIndex.FindByInventoryPath(propSpecs) # rubocop:disable Naming/VariableName raise("Pool #{pool['name']} specifies a template VM of #{pool['template']} which does not exist for the provider #{name}") if template_vm_object.nil? template_vm_object end |
#find_vm(pool_name, vmname, connection) ⇒ Object
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 |
# File 'lib/vmpooler/providers/vsphere.rb', line 1071 def find_vm(pool_name, vmname, connection) # Find a VM by its inventory path and return the VM object # Returns nil when a VM, or pool configuration, cannot be found pool_configuration = pool_config(pool_name) return nil if pool_configuration.nil? folder = pool_configuration['folder'] datacenter = get_target_datacenter_from_config(pool_name) return nil if datacenter.nil? propSpecs = { # rubocop:disable Naming/VariableName entity: self, inventoryPath: "#{datacenter}/vm/#{folder}/#{vmname}" } connection.searchIndex.FindByInventoryPath(propSpecs) # rubocop:disable Naming/VariableName end |
#find_vm_folder(pool_name, connection) ⇒ Object
Finds a folder object by inventory path Params:
pool_namethe pool to find the folder for
connectionthe vsphere connection object
returns a ManagedObjectReference for the folder found or nil if not found
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 |
# File 'lib/vmpooler/providers/vsphere.rb', line 868 def find_vm_folder(pool_name, connection) # Find a folder by its inventory path and return the object # Returns nil when the object found is not a folder pool_configuration = pool_config(pool_name) return nil if pool_configuration.nil? folder = pool_configuration['folder'] datacenter = get_target_datacenter_from_config(pool_name) return nil if datacenter.nil? propSpecs = { # rubocop:disable Naming/VariableName entity: self, inventoryPath: "#{datacenter}/vm/#{folder}" } folder_object = connection.searchIndex.FindByInventoryPath(propSpecs) # rubocop:disable Naming/VariableName return nil unless folder_object.instance_of? RbVmomi::VIM::Folder folder_object end |
#folder_configured?(folder_title, base_folder, configured_folders, allowlist) ⇒ Boolean
60 61 62 63 64 65 66 |
# File 'lib/vmpooler/providers/vsphere.rb', line 60 def folder_configured?(folder_title, base_folder, configured_folders, allowlist) return true if allowlist&.include?(folder_title) return false unless configured_folders.keys.include?(folder_title) return false unless configured_folders[folder_title] == base_folder true end |
#generate_vm_hash(vm_object, pool_name) ⇒ Object
Return a hash of VM data Provides vmname, hostname, template, poolname, boottime and powerstate information
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 |
# File 'lib/vmpooler/providers/vsphere.rb', line 613 def generate_vm_hash(vm_object, pool_name) pool_configuration = pool_config(pool_name) return nil if pool_configuration.nil? hostname = vm_object.summary.guest.hostName if vm_object.summary&.guest&.hostName boottime = vm_object.runtime.bootTime if vm_object.runtime&.bootTime powerstate = vm_object.runtime.powerState if vm_object.runtime&.powerState ip_maxloop = 240 ip_loop_delay = 1 ip_loop_count = 1 ip = nil invalid_addresses = /(0|169)\.(0|254)\.\d+\.\d+/ while ip.nil? sleep(ip_loop_delay) ip = vm_object.guest_ip ip = nil if !ip.nil? && ip.match?(invalid_addresses) unless ip_maxloop == 0 break if ip_loop_count >= ip_maxloop ip_loop_count += 1 end end { 'name' => vm_object.name, 'hostname' => hostname, 'template' => pool_configuration['template'], 'poolname' => pool_name, 'boottime' => boottime, 'powerstate' => powerstate, 'ip' => ip } end |
#get_average_cluster_utilization(hosts) ⇒ Object
945 946 947 948 |
# File 'lib/vmpooler/providers/vsphere.rb', line 945 def get_average_cluster_utilization(hosts) utilization_counts = hosts.map { |host| host[0] } utilization_counts.inject(:+) / hosts.count end |
#get_base_folders(folders) ⇒ Object
141 142 143 144 145 146 147 |
# File 'lib/vmpooler/providers/vsphere.rb', line 141 def get_base_folders(folders) base = [] folders.each do |_key, value| base << value end base.uniq end |
#get_base_vm_container_from(connection) ⇒ Object
1089 1090 1091 1092 1093 1094 1095 1096 |
# File 'lib/vmpooler/providers/vsphere.rb', line 1089 def get_base_vm_container_from(connection) view_manager = connection.serviceContent.viewManager view_manager.CreateContainerView( container: connection.serviceContent.rootFolder, recursive: true, type: ['VirtualMachine'] ) end |
#get_cluster_host_utilization(cluster, model = nil) ⇒ Object
1040 1041 1042 1043 1044 1045 1046 1047 |
# File 'lib/vmpooler/providers/vsphere.rb', line 1040 def get_cluster_host_utilization(cluster, model = nil) cluster_hosts = [] cluster.host.each do |host| host_usage = get_host_utilization(host, model) cluster_hosts << host_usage if host_usage end cluster_hosts end |
#get_disk_backing(pool) ⇒ Object
1252 1253 1254 1255 1256 |
# File 'lib/vmpooler/providers/vsphere.rb', line 1252 def get_disk_backing(pool) return :moveChildMostDiskBacking if linked_clone?(pool) :moveAllDiskBackingsAndConsolidate end |
#get_folder_children(folder_name, connection) ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/vmpooler/providers/vsphere.rb', line 168 def get_folder_children(folder_name, connection) folders = [] propSpecs = { # rubocop:disable Naming/VariableName entity: self, inventoryPath: folder_name } folder_object = connection.searchIndex.FindByInventoryPath(propSpecs) # rubocop:disable Naming/VariableName return folders if folder_object.nil? folder_object.childEntity.each do |folder| next unless folder.is_a? RbVmomi::VIM::Folder folders << { folder.name => folder } end folders end |
#get_host_cpu_arch_version(host) ⇒ Object
923 924 925 926 927 |
# File 'lib/vmpooler/providers/vsphere.rb', line 923 def get_host_cpu_arch_version(host) cpu_model = host.hardware.cpuPkg[0].description cpu_model_parts = cpu_model.split cpu_model_parts[4] end |
#get_host_utilization(host, model = nil, limit = 90) ⇒ Object
Returns an array containing cumulative CPU and memory utilization of a host, and its object reference Params:
modelCPU arch version to match on
limitHard limit for CPU or memory utilization beyond which a host is excluded for deployments
returns nil if one on these conditions is true:
the model param is defined and cannot be found
the host is in maintenance mode
the host status is not 'green'
the cpu or memory utilization is bigger than the limit param
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 |
# File 'lib/vmpooler/providers/vsphere.rb', line 898 def get_host_utilization(host, model = nil, limit = 90) limit = @config[:config]['utilization_limit'] if @config[:config].key?('utilization_limit') return nil if model && !host_has_cpu_model?(host, model) return nil if host.runtime.inMaintenanceMode return nil unless host.overallStatus == 'green' return nil unless host.configIssue.empty? cpu_utilization = cpu_utilization_for host memory_utilization = memory_utilization_for host return nil if cpu_utilization.nil? return nil if cpu_utilization.to_d == 0.0.to_d return nil if memory_utilization.nil? return nil if memory_utilization.to_d == 0.0.to_d return nil if cpu_utilization > limit return nil if memory_utilization > limit [cpu_utilization, host] end |
#get_snapshot_list(tree, snapshotname) ⇒ Object
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 |
# File 'lib/vmpooler/providers/vsphere.rb', line 1098 def get_snapshot_list(tree, snapshotname) snapshot = nil tree.each do |child| if child.name == snapshotname snapshot ||= child.snapshot else snapshot ||= get_snapshot_list(child.childSnapshotList, snapshotname) end end snapshot end |
#get_target_cluster_from_config(pool_name) ⇒ Object
VSphere Helper methods
591 592 593 594 595 596 597 598 599 |
# File 'lib/vmpooler/providers/vsphere.rb', line 591 def get_target_cluster_from_config(pool_name) pool = pool_config(pool_name) return nil if pool.nil? return pool['clone_target'] unless pool['clone_target'].nil? return global_config[:config]['clone_target'] unless global_config[:config]['clone_target'].nil? nil end |
#get_target_datacenter_from_config(pool_name) ⇒ Object
601 602 603 604 605 606 607 608 609 |
# File 'lib/vmpooler/providers/vsphere.rb', line 601 def get_target_datacenter_from_config(pool_name) pool = pool_config(pool_name) return nil if pool.nil? return pool['datacenter'] unless pool['datacenter'].nil? return provider_config['datacenter'] unless provider_config['datacenter'].nil? nil end |
#get_vm(pool_name, vm_name) ⇒ Object
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
# File 'lib/vmpooler/providers/vsphere.rb', line 311 def get_vm(pool_name, vm_name) vm_hash = nil with_circuit_breaker do Timeout.timeout(vsphere_connection_timeout) do @connection_pool.with_metrics do |pool_object| connection = ensured_vsphere_connection(pool_object) vm_object = find_vm(pool_name, vm_name, connection) next if vm_object.nil? vm_hash = generate_vm_hash(vm_object, pool_name) end end end vm_hash end |
#get_vm_details(pool_name, vm_name, connection) ⇒ Object
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 |
# File 'lib/vmpooler/providers/vsphere.rb', line 1112 def get_vm_details(pool_name, vm_name, connection) vm_object = find_vm(pool_name, vm_name, connection) return nil if vm_object.nil? parent_host_object = vm_object.summary.runtime.host if vm_object.summary&.runtime&.host raise('Unable to determine which host the VM is running on') if parent_host_object.nil? parent_host = parent_host_object.name architecture = get_host_cpu_arch_version(parent_host_object) { 'host_name' => parent_host, 'object' => vm_object, 'architecture' => architecture } end |
#get_vm_folder_path(vm_object) ⇒ Object
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 |
# File 'lib/vmpooler/providers/vsphere.rb', line 714 def get_vm_folder_path(vm_object) # This gives an array starting from the root Datacenters folder all the way to the VM # [ [Object, String], [Object, String ] ... ] # It's then reversed so that it now goes from the VM to the Datacenter full_path = vm_object.path.reverse # Find the Datacenter object dc_index = full_path.index { |p| p[0].is_a?(RbVmomi::VIM::Datacenter) } return nil if dc_index.nil? # The Datacenter should be at least 2 otherwise there's something # wrong with the array passed in # This is the minimum: # [ VM (0), VM ROOT FOLDER (1), DC (2)] return nil if dc_index <= 1 # Remove the VM name (Starting position of 1 in the slice) # Up until the Root VM Folder of DataCenter Node (dc_index - 2) full_path = full_path.slice(1..dc_index - 2) # Reverse the array back to normal and # then convert the array of paths into a '/' seperated string full_path.reverse.map { |p| p[1] }.join('/') end |
#get_vm_ip_address(vm_name, pool_name) ⇒ Object
The inner method requires vmware tools running in the guest os
407 408 409 410 411 412 413 414 415 416 417 418 419 420 |
# File 'lib/vmpooler/providers/vsphere.rb', line 407 def get_vm_ip_address(vm_name, pool_name) ip = nil with_circuit_breaker do Timeout.timeout(vsphere_connection_timeout) do @connection_pool.with_metrics do |pool_object| connection = ensured_vsphere_connection(pool_object) vm_object = find_vm(pool_name, vm_name, connection) vm_hash = generate_vm_hash(vm_object, pool_name) ip = vm_hash['ip'] end end end ip end |
#host_has_cpu_model?(host, model) ⇒ Boolean
919 920 921 |
# File 'lib/vmpooler/providers/vsphere.rb', line 919 def host_has_cpu_model?(host, model) get_host_cpu_arch_version(host) == model end |
#linked_clone?(pool) ⇒ Boolean
1258 1259 1260 1261 1262 1263 |
# File 'lib/vmpooler/providers/vsphere.rb', line 1258 def linked_clone?(pool) return if pool['create_linked_clone'] == false return true if pool['create_linked_clone'] true if @config[:config]['create_linked_clones'] end |
#memory_utilization_for(host) ⇒ Object
937 938 939 940 941 942 943 |
# File 'lib/vmpooler/providers/vsphere.rb', line 937 def memory_utilization_for(host) memory_usage = host.summary.quickStats.overallMemoryUsage return nil if memory_usage.nil? memory_size = host.summary.hardware.memorySize / 1024 / 1024 memory_usage.fdiv(memory_size) * 100 end |
#migrate_vm(pool_name, vm_name) ⇒ Object
1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 |
# File 'lib/vmpooler/providers/vsphere.rb', line 1136 def migrate_vm(pool_name, vm_name) @connection_pool.with_metrics do |pool_object| begin connection = ensured_vsphere_connection(pool_object) vm_hash = get_vm_details(pool_name, vm_name, connection) raise StandardError, 'failed to get vm details. vm is unreachable or no longer exists' if vm_hash.nil? @redis.with_metrics do |redis| redis.hset("vmpooler__vm__#{vm_name}", 'host', vm_hash['host_name']) migration_count = redis.scard('vmpooler__migration') migration_limit = @config[:config]['migration_limit'] if @config[:config].key?('migration_limit') if migration_enabled? @config if migration_count >= migration_limit logger.log('s', "[ ] [#{pool_name}] '#{vm_name}' is running on #{vm_hash['host_name']}. No migration will be evaluated since the migration_limit has been reached") break end run_select_hosts(pool_name, @provider_hosts) if vm_in_target?(pool_name, vm_hash['host_name'], vm_hash['architecture'], @provider_hosts) logger.log('s', "[ ] [#{pool_name}] No migration required for '#{vm_name}' running on #{vm_hash['host_name']}") else migrate_vm_to_new_host(pool_name, vm_name, vm_hash, connection) end else logger.log('s', "[ ] [#{pool_name}] '#{vm_name}' is running on #{vm_hash['host_name']}") end end rescue StandardError logger.log('s', "[!] [#{pool_name}] '#{vm_name}' is running on #{vm_hash['host_name']}") raise end end end |
#migrate_vm_and_record_timing(pool_name, vm_name, vm_hash, target_host_object, dest_host_name) ⇒ Object
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 |
# File 'lib/vmpooler/providers/vsphere.rb', line 1190 def migrate_vm_and_record_timing(pool_name, vm_name, vm_hash, target_host_object, dest_host_name) start = Time.now migrate_vm_host(vm_hash['object'], target_host_object) finish = format('%<time>.2f', time: Time.now - start) metrics.timing("migrate.#{pool_name}", finish) metrics.increment("migrate_from.#{vm_hash['host_name']}") metrics.increment("migrate_to.#{dest_host_name}") @redis.with_metrics do |redis| checkout_to_migration = format('%<time>.2f', time: Time.now - Time.parse(redis.hget("vmpooler__vm__#{vm_name}", 'checkout'))) redis.multi do |transaction| transaction.hset("vmpooler__vm__#{vm_name}", 'migration_time', finish) transaction.hset("vmpooler__vm__#{vm_name}", 'checkout_to_migration', checkout_to_migration) end end finish end |
#migrate_vm_host(vm_object, host) ⇒ Object
1207 1208 1209 1210 |
# File 'lib/vmpooler/providers/vsphere.rb', line 1207 def migrate_vm_host(vm_object, host) relospec = RbVmomi::VIM.VirtualMachineRelocateSpec(host: host) vm_object.RelocateVM_Task(spec: relospec).wait_for_completion end |
#migrate_vm_to_new_host(pool_name, vm_name, vm_hash, connection) ⇒ Object
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 |
# File 'lib/vmpooler/providers/vsphere.rb', line 1170 def migrate_vm_to_new_host(pool_name, vm_name, vm_hash, connection) @redis.with_metrics do |redis| redis.sadd('vmpooler__migration', vm_name) end target_host_name = select_next_host(pool_name, @provider_hosts, vm_hash['architecture']) target_host_object = find_host_by_dnsname(connection, target_host_name) finish = migrate_vm_and_record_timing(pool_name, vm_name, vm_hash, target_host_object, target_host_name) @redis.with_metrics do |redis| redis.multi do |transaction| transaction.hset("vmpooler__vm__#{vm_name}", 'host', target_host_name) transaction.hset("vmpooler__vm__#{vm_name}", 'migrated', 'true') end end logger.log('s', "[>] [#{pool_name}] '#{vm_name}' migrated from #{vm_hash['host_name']} to #{target_host_name} in #{finish} seconds") ensure @redis.with_metrics do |redis| redis.srem('vmpooler__migration', vm_name) end end |
#migration_enabled?(config) ⇒ Boolean
1128 1129 1130 1131 1132 1133 1134 |
# File 'lib/vmpooler/providers/vsphere.rb', line 1128 def migration_enabled?(config) migration_limit = config[:config]['migration_limit'] return false unless migration_limit.is_a? Integer return true if migration_limit > 0 false end |
#name ⇒ Object
name of the provider class
51 52 53 |
# File 'lib/vmpooler/providers/vsphere.rb', line 51 def name 'vsphere' end |
#open_socket(host, domain = nil, timeout = 5, port = 22, &_block) ⇒ Object
This should supercede the open_socket method in the Pool Manager
703 704 705 706 707 708 709 710 711 712 |
# File 'lib/vmpooler/providers/vsphere.rb', line 703 def open_socket(host, domain = nil, timeout = 5, port = 22, &_block) target_host = host target_host = "#{host}.#{domain}" if domain sock = TCPSocket.new(target_host, port, connect_timeout: timeout) begin yield sock if block_given? ensure sock.close end end |
#pool_folders(provider_name) ⇒ Object
Return a list of pool folders
129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/vmpooler/providers/vsphere.rb', line 129 def pool_folders(provider_name) folders = {} $config[:pools].each do |pool| next unless pool['provider'] == provider_name.to_s folder_parts = pool['folder'].split('/') datacenter = get_target_datacenter_from_config(pool['name']) folders[folder_parts.pop] = "#{datacenter}/vm/#{folder_parts.join('/')}" end folders end |
#purge_unconfigured_resources(allowlist) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/vmpooler/providers/vsphere.rb', line 149 def purge_unconfigured_resources(allowlist) configured_folders = pool_folders(name) base_folders = get_base_folders(configured_folders) @connection_pool.with_metrics do |pool_object| connection = ensured_vsphere_connection(pool_object) base_folders.each do |base_folder| folder_children = get_folder_children(base_folder, connection) next if folder_children.empty? folder_children.each do |folder_hash| folder_hash.each do |folder_title, folder_object| destroy_folder_and_children(folder_object) unless folder_configured?(folder_title, base_folder, configured_folders, allowlist) end end end end end |
#revert_snapshot(pool_name, vm_name, snapshot_name) ⇒ Object
545 546 547 548 549 550 551 552 553 554 555 556 557 |
# File 'lib/vmpooler/providers/vsphere.rb', line 545 def revert_snapshot(pool_name, vm_name, snapshot_name) @connection_pool.with_metrics do |pool_object| connection = ensured_vsphere_connection(pool_object) vm_object = find_vm(pool_name, vm_name, connection) raise("VM #{vm_name} in pool #{pool_name} does not exist for the provider #{name}") if vm_object.nil? snapshot_object = find_snapshot(vm_object, snapshot_name) raise("Snapshot #{snapshot_name} for VM #{vm_name} in pool #{pool_name} does not exist for the provider #{name}") if snapshot_object.nil? snapshot_object.RevertToSnapshot_Task.wait_for_completion end true end |
#run_select_hosts(pool_name, target) ⇒ Object
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/vmpooler/providers/vsphere.rb', line 225 def run_select_hosts(pool_name, target) now = Time.now max_age = @config[:config]['host_selection_max_age'] || 60 loop_delay = 5 datacenter = get_target_datacenter_from_config(pool_name) cluster = get_target_cluster_from_config(pool_name) raise("cluster for pool #{pool_name} cannot be identified") if cluster.nil? raise("datacenter for pool #{pool_name} cannot be identified") if datacenter.nil? dc = "#{datacenter}_#{cluster}" unless target.key?(dc) select_target_hosts(target, cluster, datacenter) return end wait_for_host_selection(dc, target, loop_delay, max_age) if target[dc].key?('checking') select_target_hosts(target, cluster, datacenter) if target[dc].key?('check_time_finished') && now - target[dc]['check_time_finished'] > max_age end |
#select_least_used_hosts(hosts, percentage) ⇒ Object
975 976 977 978 979 980 981 982 983 984 985 986 |
# File 'lib/vmpooler/providers/vsphere.rb', line 975 def select_least_used_hosts(hosts, percentage) raise('Provided hosts list to select_least_used_hosts is empty') if hosts.empty? average_utilization = get_average_cluster_utilization(hosts) least_used_hosts = [] hosts.each do |host| least_used_hosts << host if host[0] <= average_utilization end hosts_to_select = (hosts.count * (percentage / 100.0)).to_int hosts_to_select = hosts.count - 1 if percentage == 100 least_used_hosts.sort[0..hosts_to_select].map { |host| host[1].name } end |
#select_next_host(pool_name, target, architecture = nil) ⇒ Object
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 |
# File 'lib/vmpooler/providers/vsphere.rb', line 266 def select_next_host(pool_name, target, architecture = nil) datacenter = get_target_datacenter_from_config(pool_name) cluster = get_target_cluster_from_config(pool_name) raise("cluster for pool #{pool_name} cannot be identified") if cluster.nil? raise("datacenter for pool #{pool_name} cannot be identified") if datacenter.nil? dc = "#{datacenter}_#{cluster}" @provider_hosts_lock.synchronize do if architecture raise("there is no candidate in vcenter that meets all the required conditions, that the cluster has available hosts in a 'green' status, not in maintenance mode and not overloaded CPU and memory") unless target[dc].key?('architectures') host = target[dc]['architectures'][architecture].shift target[dc]['architectures'][architecture] << host if target[dc]['hosts'].include?(host) target[dc]['hosts'].delete(host) target[dc]['hosts'] << host end else raise("there is no candidate in vcenter that meets all the required conditions, that the cluster has available hosts in a 'green' status, not in maintenance mode and not overloaded CPU and memory") unless target[dc].key?('hosts') host = target[dc]['hosts'].shift target[dc]['hosts'] << host target[dc]['architectures'].each do |arch| target[dc]['architectures'][arch] = arch.partition { |v| v != host }.flatten if arch.include?(host) end end return host end end |
#select_target_hosts(target, cluster, datacenter) ⇒ Object
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/vmpooler/providers/vsphere.rb', line 207 def select_target_hosts(target, cluster, datacenter) percentage = 100 dc = "#{datacenter}_#{cluster}" @provider_hosts_lock.synchronize do begin target[dc] = {} unless target.key?(dc) target[dc]['checking'] = true hosts_hash = find_least_used_hosts(cluster, datacenter, percentage) target[dc] = hosts_hash rescue StandardError target[dc] = {} raise ensure target[dc]['check_time_finished'] = Time.now end end end |
#set_network_device(datacenter_name, template_vm_network_device, network_name, connection) ⇒ Object
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 |
# File 'lib/vmpooler/providers/vsphere.rb', line 465 def set_network_device(datacenter_name, template_vm_network_device, network_name, connection) # Retrieve network object datacenter = connection.serviceInstance.find_datacenter(datacenter_name) new_network = datacenter.network.find { |n| n.name == network_name } raise("Cannot find network #{network_name} in datacenter #{datacenter_name}") unless new_network # Determine network device type # All possible device type options here: https://vdc-download.vmware.com/vmwb-repository/dcr-public/98d63b35-d822-47fe-a87a-ddefd469df06/2e3c7b58-f2bd-486e-8bb1-a75eb0640bee/doc/vim.vm.device.VirtualEthernetCard.html network_device = if template_vm_network_device.instance_of? RbVmomi::VIM::VirtualVmxnet2 RbVmomi::VIM.VirtualVmxnet2 elsif template_vm_network_device.instance_of? RbVmomi::VIM::VirtualVmxnet3 RbVmomi::VIM.VirtualVmxnet3 elsif template_vm_network_device.instance_of? RbVmomi::VIM::VirtualE1000 RbVmomi::VIM.VirtualE1000 elsif template_vm_network_device.instance_of? RbVmomi::VIM::VirtualE1000e RbVmomi::VIM.VirtualE1000e elsif template_vm_network_device.instance_of? RbVmomi::VIM::VirtualSriovEthernetCard RbVmomi::VIM.VirtualSriovEthernetCard else RbVmomi::VIM.VirtualPCNet32 end # Set up new network device attributes network_device.key = template_vm_network_device.key network_device.deviceInfo = RbVmomi::VIM.Description( label: template_vm_network_device.deviceInfo.label, summary: network_name ) network_device.backing = RbVmomi::VIM.VirtualEthernetCardNetworkBackingInfo( deviceName: network_name, network: new_network, useAutoDetect: false ) network_device.addressType = 'assigned' network_device.connectable = RbVmomi::VIM.VirtualDeviceConnectInfo( allowGuestControl: true, startConnected: true, connected: true ) network_device end |
#valid_template_path?(template) ⇒ Boolean
1244 1245 1246 1247 1248 1249 1250 |
# File 'lib/vmpooler/providers/vsphere.rb', line 1244 def valid_template_path?(template) return false unless template.include?('/') return false if template[0] == '/' return false if template[-1] == '/' true end |
#vm_in_target?(pool_name, parent_host, architecture, target) ⇒ Boolean
297 298 299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/vmpooler/providers/vsphere.rb', line 297 def vm_in_target?(pool_name, parent_host, architecture, target) datacenter = get_target_datacenter_from_config(pool_name) cluster = get_target_cluster_from_config(pool_name) raise("cluster for pool #{pool_name} cannot be identified") if cluster.nil? raise("datacenter for pool #{pool_name} cannot be identified") if datacenter.nil? dc = "#{datacenter}_#{cluster}" raise("there is no candidate in vcenter that meets all the required conditions, that the cluster has available hosts in a 'green' status, not in maintenance mode and not overloaded CPU and memory") unless target[dc].key?('hosts') return true if target[dc]['hosts'].include?(parent_host) return true if target[dc]['architectures'][architecture].include?(parent_host) false end |
#vm_ready?(pool_name, vm_name, redis) ⇒ Boolean
577 578 579 580 581 582 583 584 585 586 587 |
# File 'lib/vmpooler/providers/vsphere.rb', line 577 def vm_ready?(pool_name, vm_name, redis) begin domain = domain(pool_name) open_socket(vm_name, domain) rescue StandardError => e redis.hset("vmpooler__vm__#{vm_name}", 'open_socket_error', e.to_s) return false end redis.hdel("vmpooler__vm__#{vm_name}", 'open_socket_error') true end |
#vms_in_pool(pool_name) ⇒ Object
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/vmpooler/providers/vsphere.rb', line 188 def vms_in_pool(pool_name) vms = [] with_circuit_breaker do Timeout.timeout(vsphere_connection_timeout) do @connection_pool.with_metrics do |pool_object| connection = ensured_vsphere_connection(pool_object) folder_object = find_vm_folder(pool_name, connection) next if folder_object.nil? folder_object.childEntity.each do |vm| vms << { 'name' => vm.name } if vm.is_a? RbVmomi::VIM::VirtualMachine end end end end vms end |
#vsphere_connection_ok?(connection) ⇒ Boolean
666 667 668 669 670 671 |
# File 'lib/vmpooler/providers/vsphere.rb', line 666 def vsphere_connection_ok?(connection) _result = connection.serviceInstance.CurrentTime true rescue StandardError false end |
#vsphere_connection_timeout ⇒ Object
696 697 698 699 700 |
# File 'lib/vmpooler/providers/vsphere.rb', line 696 def vsphere_connection_timeout timeout = provider_config['vsphere_timeout'] || global_config&.dig(:config, 'vsphere_timeout') || 60 timeout.to_i end |
#wait_for_host_selection(dc, target, maxloop = 0, loop_delay = 1, max_age = 60) ⇒ Object
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/vmpooler/providers/vsphere.rb', line 243 def wait_for_host_selection(dc, target, maxloop = 0, loop_delay = 1, max_age = 60) loop_count = 1 until target.key?(dc) && target[dc].key?('check_time_finished') sleep(loop_delay) unless maxloop == 0 break if loop_count >= maxloop loop_count += 1 end end return unless target[dc].key?('check_time_finished') loop_count = 1 while Time.now - target[dc]['check_time_finished'] > max_age sleep(loop_delay) unless maxloop == 0 break if loop_count >= maxloop loop_count += 1 end end end |
#with_circuit_breaker(&block) ⇒ Object
653 654 655 656 657 658 659 |
# File 'lib/vmpooler/providers/vsphere.rb', line 653 def with_circuit_breaker(&block) if circuit_breaker circuit_breaker.call(&block) else yield end end |