lib/vmpooler/providers/vsphere.rb
# The connection_pool method is normally used only for testing
The connection_pool method is normally used only for testing
The connection_pool method is normally used only for testing
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
          @connection_pool.with_metrics do |pool_object|
            connection = ensured_vsphere_connection(pool_object)
            template_path = pool['template']
            target_folder_path = pool['folder']
            target_datastore = pool['datastore']
            target_datacenter_name = get_target_datacenter_from_config(pool_name)
            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
            config_spec = create_config_spec(new_vmname, template_path, extra_config)
            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
            relocate_spec = create_relocate_spec(target_datastore, target_datacenter_name, pool_name, connection)
            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
            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
          vm_hash
        end
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 @connection_pool . with_metrics do | pool_object | connection = ensured_vsphere_connection ( pool_object ) template_path = pool [ 'template' ] target_folder_path = pool [ 'folder' ] target_datastore = pool [ 'datastore' ] target_datacenter_name = get_target_datacenter_from_config ( pool_name ) 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 config_spec = create_config_spec ( new_vmname , template_path , extra_config ) 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 relocate_spec = create_relocate_spec ( target_datastore , target_datacenter_name , pool_name , connection ) 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 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 vm_hash end
The connection_pool method is normally used only for testing
The connection_pool method is normally used only for testing
# Assume all pool config is valid i.e. not missing # Get the template VM object # Annotate with creation time, origin template, etc. # Add extraconfig options that can be queried by vmtools # Check if alternate network configuration is specified and add configuration # Put the VM in the specified folder and resource pool # Create a clone spec # Create the new VM
pool_name
new_vmname