Esempio n. 1
0
 def __init__(self, url, username, password):
     self.log = logging.getLogger('%s.%s' % (__name__, self.__class__.__name__))
     self.vim = Vim(url)
     self.vim.login(username, password)
Esempio n. 2
0
 def __init__(self, url, username, password):
     self.log = logging.getLogger('%s.%s' %
                                  (__name__, self.__class__.__name__))
     self.vim = Vim(url)
     self.vim.login(username, password)
Esempio n. 3
0
class VMImport:
    def __init__(self, url, username, password):
        self.log = logging.getLogger('%s.%s' % (__name__, self.__class__.__name__))
        self.vim = Vim(url)
        self.vim.login(username, password)

    def curl_progress(self, download_t, download_d, upload_t, upload_d):
        curtime=time()
        # TODO: Make poke frequency variable
        # 5 seconds isn't too much and it makes the status bar in the vSphere GUI look nice :-)
        if  (curtime - self.time_at_last_poke) >= 5:
            self.vim.invoke('HttpNfcLeaseProgress', _this=self.lease_mo_ref, percent = int(upload_d*100/upload_t))
            self.time_at_last_poke = time()

    def import_vm(self, datastore, network_name, name, disksize_kb,
                  memory, num_cpus, guest_id, host=None, imagefilename=None):

        nic = {'network_name': network_name, 'type': 'VirtualE1000'}
        nics = [nic]
        # If the host is not set, use the ComputeResource as the target
        if not host:
            target = self.vim.find_entity_view(view_type='ComputeResource')
            target.update_view_data(['name', 'datastore', 'network', 'parent',
                                     'resourcePool'])
            resource_pool = target.resourcePool
        else:
            target = self.vim.find_entity_view(view_type='HostSystem',
                                                filter={'name': host})
            # Retrieve the properties we're going to use
            target.update_view_data(['name', 'datastore', 'network', 'parent'])
            host_cr = self.vim.get_view(mo_ref=target.parent, vim=self.vim)
            host_cr.update_view_data(properties=['resourcePool'])
            resource_pool = host_cr.resourcePool

        # A list of devices to be assigned to the VM
        vm_devices = []

        # Create a disk controller
        controller = self.create_controller('VirtualLsiLogicController')
        vm_devices.append(controller)

        # Find the given datastore and ensure it is suitable
        if host:
            ds_target = host_cr
        else:
            ds_target = target

        try:
            ds = ds_target.find_datastore(name=datastore)
        except ObjectNotFoundError, e:
            raise ImageFactoryException('Could not find datastore with name %s: %s' % (datastore,
                                                                 e.error))


        ds.update_view_data(properties=['summary'])
        # Ensure the datastore is accessible and has enough space
        if (not ds.summary.accessible or
            ds.summary.freeSpace < disksize_kb * 1024):
            raise ImageFactoryException('Datastore (%s) exists, but is not accessible or'
                  'does not have sufficient free space.' % ds.summary.name)

        disk = self.create_disk(datastore=ds, disksize_kb=disksize_kb)
        vm_devices.append(disk)

        cdrom = self.create_cdrom(datastore=ds)
        vm_devices.append(cdrom)

        for nic in nics:
            nic_spec = self.create_nic(target, nic)
            if not nic_spec:
                raise ImageFactoryException('Could not create spec for NIC')

            # Append the nic spec to the vm_devices list
            vm_devices.append(nic_spec)

        vmfi = self.vim.create_object('VirtualMachineFileInfo')
        vmfi.vmPathName = '[%s]' % ds.summary.name
        vm_config_spec = self.vim.create_object('VirtualMachineConfigSpec')
        vm_config_spec.name = name
        vm_config_spec.memoryMB = memory
        vm_config_spec.files = vmfi
        vm_config_spec.annotation = 'Auto-provisioned by pSphere'
        vm_config_spec.numCPUs = num_cpus
        vm_config_spec.guestId = guest_id
        vm_config_spec.deviceChange = vm_devices

        # Find the datacenter of the target
        try:
            dc = target.find_datacenter()
        except ObjectNotFoundError, e:
            raise ImageFactoryException('Error while trying to find datacenter for %s: %s' %
                  (target.name, e.error))
Esempio n. 4
0
class VMImport:
    def __init__(self, url, username, password):
        self.log = logging.getLogger('%s.%s' %
                                     (__name__, self.__class__.__name__))
        self.vim = Vim(url)
        self.vim.login(username, password)

    def curl_progress(self, download_t, download_d, upload_t, upload_d):
        #print "Total to download", download_t
        #print "Total downloaded", download_d
        #print "Total to upload", upload_t
        #print "Total uploaded", upload_d
        curtime = time()
        # TODO: Make poke frequency variable
        # 5 seconds isn't too much and it makes the status bar in the vSphere GUI look nice :-)
        if (curtime - self.time_at_last_poke) >= 5:
            #print "Current time (%s) - time at last poke (%s)" % (curtime, self.time_at_last_poke)
            #print "Ten or more seconds since last poke"
            #print "Trying to do a poke with progress of %d" % (int(upload_d*100/upload_t))
            self.vim.invoke('HttpNfcLeaseProgress',
                            _this=self.lease_mo_ref,
                            percent=int(upload_d * 100 / upload_t))
            self.time_at_last_poke = time()

    def import_vm(self,
                  datastore,
                  network_name,
                  name,
                  disksize_kb,
                  memory,
                  num_cpus,
                  guest_id,
                  host=None,
                  imagefilename=None):

        nic = {'network_name': network_name, 'type': 'VirtualE1000'}
        nics = [nic]
        # If the host is not set, use the ComputeResource as the target
        if not host:
            target = self.vim.find_entity_view(view_type='ComputeResource')
            #                                            filter={'name': compute_resource})
            target.update_view_data(
                ['name', 'datastore', 'network', 'parent', 'resourcePool'])
            resource_pool = target.resourcePool
        else:
            target = self.vim.find_entity_view(view_type='HostSystem',
                                               filter={'name': host})
            # Retrieve the properties we're going to use
            target.update_view_data(['name', 'datastore', 'network', 'parent'])
            host_cr = self.vim.get_view(mo_ref=target.parent, vim=self.vim)
            host_cr.update_view_data(properties=['resourcePool'])
            resource_pool = host_cr.resourcePool

        # Compute image size in KB rounding up
        # disksize_kb = int(math.ceil((1.0*os.path.getsize(imagefilename))/(1024.0)))

        # A list of devices to be assigned to the VM
        vm_devices = []

        # Create a disk controller
        controller = self.create_controller('VirtualLsiLogicController')
        vm_devices.append(controller)

        # Find the given datastore and ensure it is suitable
        if host:
            ds_target = host_cr
        else:
            ds_target = target

        try:
            ds = ds_target.find_datastore(name=datastore)
        except ObjectNotFoundError, e:
            self.log.error('Could not find datastore with name %s: %s' %
                           (datastore, e.error))
            sys.exit()

        ds.update_view_data(properties=['summary'])
        # Ensure the datastore is accessible and has enough space
        if (not ds.summary.accessible
                or ds.summary.freeSpace < disksize_kb * 1024):
            self.log.error('Datastore (%s) exists, but is not accessible or'
                           'does not have sufficient free space.' %
                           ds.summary.name)
            sys.exit()

        disk = self.create_disk(datastore=ds, disksize_kb=disksize_kb)
        vm_devices.append(disk)

        for nic in nics:
            nic_spec = self.create_nic(target, nic)
            if not nic_spec:
                self.log.error('Could not create spec for NIC')
                sys.exit()

            # Append the nic spec to the vm_devices list
            vm_devices.append(nic_spec)

        vmfi = self.vim.create_object('VirtualMachineFileInfo')
        vmfi.vmPathName = '[%s]' % ds.summary.name
        vm_config_spec = self.vim.create_object('VirtualMachineConfigSpec')
        vm_config_spec.name = name
        vm_config_spec.memoryMB = memory
        vm_config_spec.files = vmfi
        vm_config_spec.annotation = 'Auto-provisioned by pSphere'
        vm_config_spec.numCPUs = num_cpus
        vm_config_spec.guestId = guest_id
        vm_config_spec.deviceChange = vm_devices

        # Find the datacenter of the target
        try:
            dc = target.find_datacenter()
        except ObjectNotFoundError, e:
            self.log.error('Error while trying to find datacenter for %s: %s' %
                           (target.name, e.error))
            sys.exit()