Beispiel #1
0
    def add_vm_from_template(self,
                             cluster_name,
                             template_name='Blank',
                             new_name='my_new_vm',
                             timeout=300):
        """
        Create a VM from template.

        :param cluster_name: cluster name.
        :param template_name: default template is 'Blank'.
        :param new_name: 'my_new_vm' is a default new VM's name.
        :param timeout: Time out
        """
        end_time = time.time() + timeout
        vm_params = types.VM(
            name=new_name,
            cluster=self.connection.clusters.get(cluster_name),
            template=self.connection.templates.get(template_name))
        try:
            logging.info('Creating a VM %s from template %s' %
                         (new_name, template_name))
            self.connection.vms.add(vm_params)
            logging.info('Waiting for VM to reach <Down> status')
            vm_down = False
            while time.time() < end_time:
                if self.is_dead():
                    vm_down = True
                    break
                time.sleep(1)
            if not vm_down:
                raise WaitVMStateTimeoutError("DOWN", self.state())
            logging.info('VM was created from template successfully')
        except Exception as e:
            logging.error('Failed to create VM from template:\n%s' % str(e))
def add_vm_template(api):
    #TODO: Fix the exported domain generation
    raise SkipTest('Exported domain generation not supported yet')
    vm_params = types.VM(
        name=VM1_NAME,
        memory=512 * MB,
        cluster=types.Cluster(name=TEST_CLUSTER, ),
        template=types.Template(name=TEMPLATE_CENTOS7, ),
        display=types.Display(type_='spice', ),
    )
    api.vms.add(vm_params)
    testlib.assert_true_within_long(
        lambda: api.vms.get(VM1_NAME).status.state == 'down', )
    disk_name = api.vms.get(VM1_NAME).disks.list()[0].name
    testlib.assert_true_within_long(lambda: api.vms.get(VM1_NAME).disks.get(
        disk_name).status.state == 'ok')
Beispiel #3
0
    def add(self,
            memory,
            disk_size,
            cluster_name,
            storage_name,
            nic_name='eth0',
            network_interface='virtio',
            network_name='ovirtmgmt',
            disk_interface='virtio',
            disk_format='raw',
            template_name='Blank',
            timeout=300):
        """
        Create VM with one NIC and one Disk.

        :param memory: VM's memory size such as 1024*1024*1024=1GB.
        :param disk_size: VM's disk size such as 512*1024=512MB.
        :param nic_name: VM's NICs name such as 'eth0'.
        :param network_interface: VM's network interface such as 'virtio'.
        :param network_name: network such as ovirtmgmt for ovirt, rhevm for rhel.
        :param disk_format: VM's disk format such as 'raw' or 'cow'.
        :param disk_interface: VM's disk interface such as 'virtio'.
        :param cluster_name: cluster name.
        :param storage_name: storage domain name.
        :param template_name: VM's template name, default is 'Blank'.
        :param timeout: Time out
        """
        end_time = time.time() + timeout
        # network name is ovirtmgmt for ovirt, rhevm for rhel.
        vm_params = types.VM(
            name=self.name,
            memory=memory,
            cluster=self.connection.clusters.get(cluster_name),
            template=self.connection.templates.get(template_name))

        storage = self.connection.storagedomains.get(storage_name)

        storage_params = types.StorageDomains(storage_domain=[storage])

        nic_params = types.NIC(name=nic_name,
                               network=types.Network(name=network_name),
                               interface=network_interface)

        disk_params = types.Disk(storage_domains=storage_params,
                                 size=disk_size,
                                 type_='system',
                                 status=None,
                                 interface=disk_interface,
                                 format=disk_format,
                                 sparse=True,
                                 bootable=True)

        try:
            logging.info('Creating a VM %s' % self.name)
            self.connection.vms.add(vm_params)

            logging.info('NIC is added to VM %s' % self.name)
            self.instance.nics.add(nic_params)

            logging.info('Disk is added to VM %s' % self.name)
            self.instance.disks.add(disk_params)

            logging.info('Waiting for VM to reach <Down> status')
            vm_down = False
            while time.time() < end_time:
                if self.is_dead():
                    vm_down = True
                    break
                time.sleep(1)
            if not vm_down:
                raise WaitVMStateTimeoutError("DOWN", self.state())
        except Exception as e:
            logging.error('Failed to create VM with disk and NIC\n%s' % str(e))