Beispiel #1
0
def vm_run(prefix):
    engine = prefix.virt_env.engine_vm()
    api = engine.get_api()
    host_names = [h.name() for h in prefix.virt_env.host_vms()]

    start_params = params.Action(
        use_cloud_init=True,
        vm=params.VM(
            placement_policy=params.VmPlacementPolicy(
                host=params.Host(
                    name=sorted(host_names)[0]
                ),
            ),
            initialization=params.Initialization(
                domain=params.Domain(
                    name='lago.example.com'
                ),
                cloud_init=params.CloudInit(
                    host=params.Host(
                        address='VM0'
                    ),
                ),
            ),
        ),
    )
    api.vms.get(VM0_NAME).start(start_params)
    testlib.assert_true_within_long(
        lambda: api.vms.get(VM0_NAME).status.state == 'up',
    )
def vm_start(conn,
             vmname,
             hostname=None,
             ip=None,
             netmask=None,
             gateway=None,
             domain=None,
             dns=None,
             rootpw=None,
             key=None):
    vm = conn.vms.get(name=vmname)
    use_cloud_init = False
    nics = None
    nic = None
    if hostname or ip or netmask or gateway or domain or dns or rootpw or key:
        use_cloud_init = True
    if ip and netmask and gateway:
        ipinfo = params.IP(address=ip, netmask=netmask, gateway=gateway)
        nic = params.GuestNicConfiguration(name='eth0',
                                           boot_protocol='STATIC',
                                           ip=ipinfo,
                                           on_boot=True)
        nics = params.Nics()
    nics = params.GuestNicsConfiguration(nic_configuration=[nic])
    initialization = params.Initialization(regenerate_ssh_keys=True,
                                           host_name=hostname,
                                           domain=domain,
                                           user_name='root',
                                           root_password=rootpw,
                                           nic_configurations=nics,
                                           dns_servers=dns,
                                           authorized_ssh_keys=key)
    action = params.Action(use_cloud_init=use_cloud_init,
                           vm=params.VM(initialization=initialization))
    vm.start(action=action)
Beispiel #3
0
    def deploy_template(self, template, *args, **kwargs):
        self.logger.debug(' Deploying RHEV template %s to VM %s' % (template, kwargs["vm_name"]))
        timeout = kwargs.pop('timeout', 900)
        power_on = kwargs.pop('power_on', True)
        vm_kwargs = {
            'name': kwargs['vm_name'],
            'cluster': self.api.clusters.get(kwargs['cluster']),
            'template': self.api.templates.get(template)
        }

        if 'placement_policy_host' in kwargs and 'placement_policy_affinity' in kwargs:
            host = params.Host(name=kwargs['placement_policy_host'])
            policy = params.VmPlacementPolicy(host=host,
                affinity=kwargs['placement_policy_affinity'])
            vm_kwargs['placement_policy'] = policy
        vm = params.VM(**vm_kwargs)
        self.api.vms.add(vm)
        self.wait_vm_stopped(kwargs['vm_name'], num_sec=timeout)
        if power_on:
            version = self.api.get_product_info().get_full_version()
            cfme_template = any(
                template.startswith(pfx) for pfx in ["cfme-55", "s_tpl", "sprout_template"])
            if cfme_template and version.startswith("3.4"):
                action = params.Action(vm=params.VM(initialization=params.Initialization(
                    cloud_init=params.CloudInit(users=params.Users(
                        user=[params.User(user_name="root", password="******")])))))
                ciargs = {}
                ciargs['initialization'] = action
                self.start_vm(vm_name=kwargs['vm_name'], **ciargs)
            else:
                self.start_vm(vm_name=kwargs['vm_name'])
        return kwargs['vm_name']
Beispiel #4
0
def vm_run(prefix):
    api = prefix.virt_env.engine_vm().get_api()
    host_names = [h.name() for h in prefix.virt_env.host_vms()]

    start_params = params.Action(
        use_cloud_init=True,
        vm=params.VM(
            placement_policy=params.VmPlacementPolicy(
                host=params.Host(
                    name=sorted(host_names)[0]
                ),
            ),
            initialization=params.Initialization(
                domain=params.Domain(
                    name='lago.example.com'
                ),
                cloud_init=params.CloudInit(
                    host=params.Host(
                        address='VM0'
                    ),
                    users=params.Users(
                        active=True,
                        user=[params.User(
                            user_name='root',
                            password='******'
                        )]
                    ),
                    network_configuration=params.NetworkConfiguration(
                        nics=params.Nics(
                            nic=[params.NIC(
                                name='eth0',
                                boot_protocol='STATIC',
                                on_boot='True',
                                network=params.Network(
                                    ip=params.IP(
                                        address='192.168.1.2.',
                                        netmask='255.255.255.0',
                                        gateway='192.168.1.1',
                                    ),
                                ),
                            )]
                        ),
                    ),
                ),
            ),
        ),
    )
    api.vms.get(VM0_NAME).start(start_params)
    testlib.assert_true_within_short(
        lambda: api.vms.get(VM0_NAME).status.state == 'up',
    )
 def deploy_template(self, template, *args, **kwargs):
     power_on = kwargs.get('power_on', True)
     vm_name = super(RHEVMSystem, self).deploy_template(template, *args, **kwargs)
     if power_on:
         version = self.api.get_product_info().get_full_version()
         cfme_template = any(
             template.startswith(pfx) for pfx in ["cfme-55", "s_tpl", "sprout_template"])
         if cfme_template and version.startswith("3.4"):
             action = params.Action(vm=params.VM(initialization=params.Initialization(
                 cloud_init=params.CloudInit(users=params.Users(
                     user=[params.User(user_name="root", password="******")])))))
             self.start_vm(vm_name=vm_name, initialization=action)
         else:
             self.start_vm(vm_name=vm_name)
     return vm_name
def run_vms(prefix):
    engine = prefix.virt_env.engine_vm()
    api = engine.get_api()
    vm_ip = '.'.join(engine.ip().split('.')[0:3] + ['199'])
    vm_gw = '.'.join(engine.ip().split('.')[0:3] + ['1'])
    host_names = [h.name() for h in prefix.virt_env.host_vms()]

    start_params = params.Action(
        use_cloud_init=True,
        vm=params.VM(
            placement_policy=params.VmPlacementPolicy(
                host=params.Host(name=sorted(host_names)[0]), ),
            initialization=params.Initialization(
                domain=params.Domain(name='lago.example.com'),
                cloud_init=params.CloudInit(
                    host=params.Host(address='VM0'),
                    users=params.Users(active=True,
                                       user=[
                                           params.User(user_name='root',
                                                       password='******')
                                       ]),
                    network_configuration=params.NetworkConfiguration(
                        nics=params.Nics(nic=[
                            params.NIC(
                                name='eth0',
                                boot_protocol='STATIC',
                                on_boot=True,
                                network=params.Network(ip=params.IP(
                                    address=vm_ip,
                                    netmask='255.255.255.0',
                                    gateway=vm_gw,
                                ), ),
                            )
                        ]), ),
                ),
            ),
        ),
    )
    api.vms.get(VM0_NAME).start(start_params)
    api.vms.get(BACKUP_VM_NAME).start(start_params)

    start_params.vm.initialization.cloud_init = params.CloudInit(
        host=params.Host(address='VM2'), )
    api.vms.get(VM2_NAME).start(start_params)

    testlib.assert_true_within_long(
        lambda: api.vms.get(VM0_NAME).status.state == 'up' and api.vms.get(
            BACKUP_VM_NAME).status.state == 'up', )
Beispiel #7
0
def vm_run_once(oe_api, vm_name, vm_password, vm_nic_info):
    """
        vm run once with cloud-init
    """
    try:
        if vm_name not in [vm_online.name for vm_online in oe_api.vms.list()]:
            print("[E] VM not found: {0}".format(vm_name))
            return 1
        elif vm_nic_info is None:
            print('[E] VM nic info is needed: "name_of_nic, ip_address, net_mask, gateway"')
            return 2
        elif oe_api.vms.get(vm_name).status.state == 'down':
            print('[I] Starting VM with cloud-init.')
            p_host = params.Host(address="{0}".format(vm_name))
            p_users = params.Users(user=[params.User(user_name="root", 
                                                     password=vm_password)])
            vm_nic = [nic for nic in vm_nic_info.split(', ')]
            if len(vm_nic) != 4:
                print('[E] VM nic info need 4 args: "name_of_nic, ip_address, net_mask, gateway"')
                return 3
            p_nic = params.Nics(nic=[params.NIC(name=vm_nic[0],
                                                boot_protocol="STATIC",
                                                on_boot=True,
                                                network=params.Network(ip=params.IP(address=vm_nic[1],
                                                                                    netmask=vm_nic[2],
                                                                                    gateway=vm_nic[3])))])
            p_network = params.NetworkConfiguration(nics=p_nic)
            p_cloud_init = params.CloudInit(host=p_host,
                                            users=p_users,
                                            regenerate_ssh_keys=True,
                                            network_configuration=p_network)
            p_initialization = params.Initialization(cloud_init=p_cloud_init)
            vm_params = params.VM(initialization=p_initialization)
            vm_action = params.Action(vm=vm_params, use_cloud_init=True)
            oe_api.vms.get(vm_name).start(vm_action)
            
            print('[I] Waiting for VM to reach Up status... ', end='')
            while oe_api.vms.get(vm_name).status.state != 'up':
                print('.', end='')
                sleep(1)
            print('VM {0} is up!'.format(vm_name))
        else:
            print('[E] VM already up.')
            
    except Exception as err:
        print('[E] Failed to Start VM with cloud-init: {0}'.format(str(err)))
    def cloudinit(self,
                  name,
                  numinterfaces=None,
                  ip1=None,
                  subnet1=None,
                  ip2=None,
                  subnet2=None,
                  ip3=None,
                  subnet3=None,
                  ip4=None,
                  subnet4=None,
                  gateway=None,
                  rootpw=None,
                  dns=None,
                  dns1=None):
        api = self.api
        while api.vms.get(name).status.state == "image_locked":
            time.sleep(5)
        action = params.Action()
        hostname = params.Host(address=name)
        nic1, nic2, nic3, nic4 = None, None, None, None
        if ip1 and subnet1:
            ip = params.IP(address=ip1, netmask=subnet1, gateway=gateway)
            network = params.Network(ip=ip)
            nic1 = params.NIC(name='eth0',
                              boot_protocol='STATIC',
                              network=network,
                              on_boot=True)
        else:
            nic1 = params.NIC(name='eth0', boot_protocol='DHCP', on_boot=True)
        if numinterfaces > 1:
            if ip2 and subnet2:
                ip = params.IP(address=ip2, netmask=subnet2)
                network = params.Network(ip=ip)
                nic2 = params.NIC(name='eth1',
                                  boot_protocol='STATIC',
                                  network=network,
                                  on_boot=True)
            else:
                nic2 = params.NIC(name='eth1',
                                  boot_protocol='DHCP',
                                  on_boot=True)
        if numinterfaces > 2:
            if ip3 and subnet3:
                ip = params.IP(address=ip3, netmask=subnet3)
                network = params.Network(ip=ip)
                nic3 = params.NIC(name='eth2',
                                  boot_protocol='STATIC',
                                  network=network,
                                  on_boot=True)
            else:
                nic3 = params.NIC(name='eth2',
                                  boot_protocol='DHCP',
                                  on_boot=True)
        if numinterfaces > 3:
            if ip4 and subnet4:
                ip = params.IP(address=ip4, netmask=subnet4)
                network = params.Network(ip=ip)
                nic4 = params.NIC(name='eth3',
                                  boot_protocol='STATIC',
                                  network=network,
                                  on_boot=True)
            else:
                nic4 = params.NIC(name='eth3',
                                  boot_protocol='DHCP',
                                  on_boot=True)
        nics = params.Nics()
        nics.add_nic(nic1)
        if numinterfaces > 1:
            nics.add_nic(nic2)
        if numinterfaces > 2:
            nics.add_nic(nic3)
        if numinterfaces > 3:
            nics.add_nic(nic4)
        networkconfiguration = params.NetworkConfiguration(nics=nics)
        users = None
        if rootpw:
            user = params.User(user_name='root', password=rootpw)
            users = params.Users()
            users.add_user(user)
#buggy at the moment  in ovirt
#        if dns:
#            domainhost = params.Host(address=dns)
#            domainhosts = params.Hosts()
#            domainhosts.add_host(domainhost)
#            dns = params.DNS(search_domains=domainhosts)
#            if dns1:
#                resolvhost = params.Host(address=dns1)
#                resolvhosts = params.Hosts()
#                resolvhosts.add_host(resolvhost)
#                dns.set_servers(resolvhosts)
#            networkconfiguration.set_dns(dns)
        files = None
        if dns1 and dns1:
            filepath, filecontent = '/etc/resolv.conf', "search %s\nnameserver %s" % (
                dns, dns1)
            files = params.Files()
            cifile = params.File(name=filepath,
                                 content=filecontent,
                                 type_='PLAINTEXT')
            files = params.Files(file=[cifile])
        cloudinit = params.CloudInit(
            host=hostname,
            network_configuration=networkconfiguration,
            regenerate_ssh_keys=True,
            users=users,
            files=files)
        initialization = params.Initialization(cloud_init=cloudinit)
        action.vm = params.VM(initialization=initialization)
        api.vms.get(name).start(action=action)
        return "%s started with cloudinit" % name
Beispiel #9
0
        vmstat = vm.get_status().state
        if vmstat == 'down':

            try:
                osVersion = vm.get_os().get_type()
                if (osVersion == "rhel_6x64" or osVersion == "rhel_6"
                        or osVersion == "rhel_7x64") and CLOUDINIT == "yes":
                    print "Starting VM: " + vm.name + " with cloud-init options"
                    scontent = buildYamlFile()
                    action = params.Action(vm=params.VM(
                        initialization=params.Initialization(
                            cloud_init=params.CloudInit(
                                #host=params.Host(address="rheltest029"),
                                users=params.Users(user=[
                                    params.User(user_name="root",
                                                password="******")
                                ]),
                                files=params.Files(file=[
                                    params.File(name="/etc/resolv.conf",
                                                content=scontent,
                                                type_="PLAINTEXT")
                                ])))))
                    vm.start(action)
                else:
                    print "Starting VM " + vm.name
                    vm.start()
                while vmstat != 'down':
                    sleep(1)
                    vmstat = vm.get_status().state
            except Exception, err:
                print "Error on starting VM"
                print err
Beispiel #10
0
 initialization=params.Initialization(cloud_init=params.CloudInit(
     host=params.Host(address=vm_name),
     network_configuration=params.NetworkConfiguration(
         nics=params.Nics(nic=[
             params.NIC(name="eth0",
                        boot_protocol="static",
                        on_boot=True,
                        network=params.Network(
                            ip=params.IP(address="192.168.1.1",
                                         netmask="255.255.255.0",
                                         gateway="192.168.0.1"))),
             params.NIC(name="eth1",
                        boot_protocol="static",
                        on_boot=True,
                        network=params.Network(
                            ip=params.IP(address="192.168.2.1",
                                         netmask="255.255.255.0",
                                         gateway="192.168.0.1"))),
             params.NIC(name="eth2",
                        boot_protocol="static",
                        on_boot=True,
                        network=params.Network(
                            ip=params.IP(address="192.168.3.1",
                                         netmask="255.255.255.0",
                                         gateway="192.168.0.1"))),
             params.NIC(name="eth3",
                        boot_protocol="static",
                        on_boot=True,
                        network=params.Network(
                            ip=params.IP(address="192.168.4.1",
                                         netmask="255.255.255.0",
                                         gateway="192.168.0.1"))),
             params.NIC(name="eth4",
                        boot_protocol="static",
                        on_boot=True,
                        network=params.Network(
                            ip=params.IP(address="192.168.5.1",
                                         netmask="255.255.255.0",
                                         gateway="192.168.0.1"))),
             params.NIC(name="eth5",
                        boot_protocol="static",
                        on_boot=True,
                        network=params.Network(
                            ip=params.IP(address="192.168.6.1",
                                         netmask="255.255.255.0",
                                         gateway="192.168.0.1"))),
             params.NIC(name="eth6",
                        boot_protocol="static",
                        on_boot=True,
                        network=params.Network(
                            ip=params.IP(address="192.168.7.1",
                                         netmask="255.255.255.0",
                                         gateway="192.168.0.1"))),
             params.NIC(name="eth7",
                        boot_protocol="static",
                        on_boot=True,
                        network=params.Network(
                            ip=params.IP(address="192.168.8.1",
                                         netmask="255.255.255.0",
                                         gateway="192.168.0.1"))),
             params.NIC(name="eth8",
                        boot_protocol="static",
                        on_boot=True,
                        network=params.Network(
                            ip=params.IP(address="192.168.9.1",
                                         netmask="255.255.255.0",
                                         gateway="192.168.0.1"))),
             params.NIC(name="eth9",
                        boot_protocol="static",
                        on_boot=True,
                        network=params.Network(
                            ip=params.IP(address="192.168.10.1",
                                         netmask="255.255.255.0",
                                         gateway="192.168.0.1"))),
             params.NIC(name="eth10",
                        boot_protocol="static",
                        on_boot=True,
                        network=params.Network(
                            ip=params.IP(address="172.25.1.1",
                                         netmask="255.255.255.0"))),
             params.NIC(name="eth11",
                        boot_protocol="static",
                        on_boot=True,
                        network=params.Network(
                            ip=params.IP(address="172.25.2.1",
                                         netmask="255.255.255.0"))),
             params.NIC(name="eth12",
                        boot_protocol="static",
                        on_boot=True,
                        network=params.Network(
                            ip=params.IP(address="172.25.3.1",
                                         netmask="255.255.255.0"))),
             params.NIC(name="eth13",
                        boot_protocol="static",
                        on_boot=True,
                        network=params.Network(
                            ip=params.IP(address="172.25.4.1",
                                         netmask="255.255.255.0"))),
             params.NIC(name="eth14",
                        boot_protocol="static",
                        on_boot=True,
                        network=params.Network(
                            ip=params.IP(address="172.25.5.1",
                                         netmask="255.255.255.0"))),
             params.NIC(name="eth15",
                        boot_protocol="static",
                        on_boot=True,
                        network=params.Network(
                            ip=params.IP(address="172.25.6.1",
                                         netmask="255.255.255.0"))),
             params.NIC(name="eth16",
                        boot_protocol="static",
                        on_boot=True,
                        network=params.Network(
                            ip=params.IP(address="172.25.7.1",
                                         netmask="255.255.255.0"))),
             params.NIC(name="eth17",
                        boot_protocol="static",
                        on_boot=True,
                        network=params.Network(
                            ip=params.IP(address="172.25.8.1",
                                         netmask="255.255.255.0"))),
             params.NIC(name="eth18",
                        boot_protocol="static",
                        on_boot=True,
                        network=params.Network(
                            ip=params.IP(address="172.25.9.1",
                                         netmask="255.255.255.0"))),
             params.NIC(name="eth19",
                        boot_protocol="static",
                        on_boot=True,
                        network=params.Network(
                            ip=params.IP(address="172.25.10.1",
                                         netmask="255.255.255.0"))),
         ])))))))
Beispiel #11
0
 initialization=params.Initialization(
     cloud_init=params.CloudInit(
         host=params.Host(address=name+str(num)+".onapp.labs"),
 network_configuration=params.NetworkConfiguration(
     nics=params.Nics(
         nic=[params.NIC(
             name="eth0",
             boot_protocol="static",
             on_boot=True,
             network=params.Network(
                 ip=params.IP(
                     address="192.168."+str(nic1)+".12",
                     netmask="255.255.255.0",
                     gateway="192.168."+str(nic1)+".1"
                 )
             )
         ),
             params.NIC(
             name="eth1",
             boot_protocol="static",
             on_boot=True,
             network=params.Network(
                 ip=params.IP(
                     address="172.25."+str(nic2)+".12",
                     netmask="255.255.255.0"
                 )
             )
             )
         ]
         )
 )
     )
 )
Beispiel #12
0
vm_backup.disks.add(disk)

# You can get the device logical name within the VM if needed
# (you should have the ovirt-guest-agent installed within the guest os).
# note it take few minutes for the logical name to be reported from the guest.
diskwithinfo = api.vms.get(VM_THAT_PERFORM_BACKUP).disks.get(id=disk.id)
print diskwithinfo.get_logical_name()

# Detach the backed up disk
detach = params.Action(detach=True)
diskwithinfo.delete(action=detach)

########################### RESTORE #################################
#Create a new VM using the backed up configuration
newVm = params.VM(name="newVm", cluster=api.clusters.get(name='Default'))
newVm.initialization = params.Initialization()
newVm.initialization.set_regenerate_ids(True)
newVm.initialization.configuration = params.Configuration()
newVm.initialization.configuration.set_type("ovf")
newVm.initialization.configuration.set_data(configuration_data)
my_vm = api.vms.add(newVm)

DOMAIN_NAME = 'sdffds'
MB = 1024 * 1024
INTERFACE = 'virtio'
FORMAT = 'qcow'

#Create a disk to restore the data to and attach it to that VM
storage_domain = api.storagedomains.get(DOMAIN_NAME)

#Find the VM that has access to the backup:
Beispiel #13
0
try:
    vm = api.vms.get(name=VMNAME)
    action = params.Action(
                    vm=params.VM(
                        initialization=params.Initialization(
                            cloud_init=params.CloudInit(
                                host=params.Host(address=FQDN),
                                authorized_keys=params.AuthorizedKeys(
                                    authorized_key=[params.AuthorizedKey(user=params.User(user_name="root"), key=SSHKEY)]
                                    ),
                                regenerate_ssh_keys=True,
                                users=params.Users(
                                    user=[params.User(user_name="root", password=SPASSWORD)]
                                    ),
                                network_configuration=params.NetworkConfiguration(
                                    nics=params.Nics(nic=[params.NIC(name="eth0",
                                                        boot_protocol="STATIC",
                                                        on_boot=True,
                                                        network=params.Network(ip=params.IP(
                                                                                address=IPADDR,
                                                                                netmask=NETMASK,
                                                                                gateway=GATEWAY)))])
                                    ),
                                files=params.Files(
                                    file=[params.File(name="/etc/motd", content=scontent, type_="PLAINTEXT")]
                                    )
                                )
                            )
                        )
                    )
    logDebug( "Starting VM %s with cloud-init options" %(VMNAME) )
    vm.start( action )