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)
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(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', )
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.200.200.',
                                    netmask='255.255.255.0',
                                    gateway='192.168.200.1',
                                ), ),
                            )
                        ]), ),
                ),
            ),
        ),
    )
    api.vms.get(VM0_NAME).start(start_params)
    testlib.assert_true_within_short(
        lambda: api.vms.get(VM0_NAME).status.state == 'up', )
Esempio n. 4
0
    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
Esempio n. 5
0
    def set_Host(self, host_name, cluster, ifaces):
        HOST = self.get_Host(host_name)
        CLUSTER = self.get_cluster(cluster)

        if HOST is None:
            setMsg("Host does not exist.")
            ifacelist = dict()
            networklist = []
            manageip = ''

            try:
                for iface in ifaces:
                    try:
                        setMsg('creating host interface ' + iface['name'])
                        if 'management' in iface:
                            manageip = iface['ip']
                        if 'boot_protocol' not in iface:
                            if 'ip' in iface:
                                iface['boot_protocol'] = 'static'
                            else:
                                iface['boot_protocol'] = 'none'
                        if 'ip' not in iface:
                            iface['ip'] = ''
                        if 'netmask' not in iface:
                            iface['netmask'] = ''
                        if 'gateway' not in iface:
                            iface['gateway'] = ''

                        if 'network' in iface:
                            if 'bond' in iface:
                                bond = []
                                for slave in iface['bond']:
                                    bond.append(ifacelist[slave])
                                try:
                                    tmpiface = params.Bonding(
                                        slaves=params.Slaves(host_nic=bond),
                                        options=params.Options(
                                            option=[
                                                params.Option(name='miimon', value='100'),
                                                params.Option(name='mode', value='4')
                                            ]
                                        )
                                    )
                                except Exception as e:
                                    setMsg('Failed to create the bond for  ' + iface['name'])
                                    setFailed()
                                    setMsg(str(e))
                                    return False
                                try:
                                    tmpnetwork = params.HostNIC(
                                        network=params.Network(name=iface['network']),
                                        name=iface['name'],
                                        boot_protocol=iface['boot_protocol'],
                                        ip=params.IP(
                                            address=iface['ip'],
                                            netmask=iface['netmask'],
                                            gateway=iface['gateway']
                                        ),
                                        override_configuration=True,
                                        bonding=tmpiface)
                                    networklist.append(tmpnetwork)
                                    setMsg('Applying network ' + iface['name'])
                                except Exception as e:
                                    setMsg('Failed to set' + iface['name'] + ' as network interface')
                                    setFailed()
                                    setMsg(str(e))
                                    return False
                            else:
                                tmpnetwork = params.HostNIC(
                                    network=params.Network(name=iface['network']),
                                    name=iface['name'],
                                    boot_protocol=iface['boot_protocol'],
                                    ip=params.IP(
                                        address=iface['ip'],
                                        netmask=iface['netmask'],
                                        gateway=iface['gateway']
                                    ))
                                networklist.append(tmpnetwork)
                                setMsg('Applying network ' + iface['name'])
                        else:
                            tmpiface = params.HostNIC(
                                name=iface['name'],
                                network=params.Network(),
                                boot_protocol=iface['boot_protocol'],
                                ip=params.IP(
                                    address=iface['ip'],
                                    netmask=iface['netmask'],
                                    gateway=iface['gateway']
                                ))
                        ifacelist[iface['name']] = tmpiface
                    except Exception as e:
                        setMsg('Failed to set ' + iface['name'])
                        setFailed()
                        setMsg(str(e))
                        return False
            except Exception as e:
                setMsg('Failed to set networks')
                setMsg(str(e))
                setFailed()
                return False

            if manageip == '':
                setMsg('No management network is defined')
                setFailed()
                return False

            try:
                HOST = params.Host(name=host_name, address=manageip, cluster=CLUSTER, ssh=params.SSH(authentication_method='publickey'))
                if self.conn.hosts.add(HOST):
                    setChanged()
                    HOST = self.get_Host(host_name)
                    state = HOST.status.state
                    while (state != 'non_operational' and state != 'up'):
                        HOST = self.get_Host(host_name)
                        state = HOST.status.state
                        time.sleep(1)
                        if state == 'non_responsive':
                            setMsg('Failed to add host to RHEVM')
                            setFailed()
                            return False

                    setMsg('status host: up')
                    time.sleep(5)

                    HOST = self.get_Host(host_name)
                    state = HOST.status.state
                    setMsg('State before setting to maintenance: ' + str(state))
                    HOST.deactivate()
                    while state != 'maintenance':
                        HOST = self.get_Host(host_name)
                        state = HOST.status.state
                        time.sleep(1)
                    setMsg('status host: maintenance')

                    try:
                        HOST.nics.setupnetworks(params.Action(
                            force=True,
                            check_connectivity=False,
                            host_nics=params.HostNics(host_nic=networklist)
                        ))
                        setMsg('nics are set')
                    except Exception as e:
                        setMsg('Failed to apply networkconfig')
                        setFailed()
                        setMsg(str(e))
                        return False

                    try:
                        HOST.commitnetconfig()
                        setMsg('Network config is saved')
                    except Exception as e:
                        setMsg('Failed to save networkconfig')
                        setFailed()
                        setMsg(str(e))
                        return False
            except Exception as e:
                if 'The Host name is already in use' in str(e):
                    setMsg("Host already exists")
                else:
                    setMsg("Failed to add host")
                    setFailed()
                    setMsg(str(e))
                return False

            HOST.activate()
            while state != 'up':
                HOST = self.get_Host(host_name)
                state = HOST.status.state
                time.sleep(1)
                if state == 'non_responsive':
                    setMsg('Failed to apply networkconfig.')
                    setFailed()
                    return False
            setMsg('status host: up')
        else:
            setMsg("Host exists.")

        return True
Esempio n. 6
0
                             network=nic_network)
     vm.nics.add(nic_params)
 except Exception as ex:
     print "Adding network machine '%s' failed %s" % (vm_name, ex)
 try:
     vm.start(action=params.Action(vm=params.VM(
         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"))),
Esempio n. 7
0
try:
    #data_center=api.datacenters.get(DC_NAME),
    #host = api.hosts.get(HOST_NAME),
    #ovirtmgmt = data_center.networks.get(name='ovirtmgmt')
    #eth = host.nics.get(name=ETH)
    print 'Adding ovirtmgmt to host'
    hostNics = api.hosts.get(HOST_NAME).nics
    # sync
    hostNicsParam = hostNics.list()
    for nic in hostNicsParam:
        nic.set_override_configuration(True)
    attachnic = params.HostNIC(network=params.Network(name='ovirtmgmt'),
                               name='eth0',
                               boot_protocol='static',
                               ip=params.IP(address=IP_ADDRESS,
                                            netmask=IP_NETMASK,
                                            gateway=IP_GATEWAY),
                               override_configuration=1)
    hostNics.setupnetworks(
        params.Action(force=0,
                      check_connectivity=1,
                      host_nics=params.HostNics(host_nic=[attachnic])))
    # Active host
    host = api.hosts.get(HOST_NAME)
    host.activate()
    print 'Waiting for host to reach the Up status'
    while api.hosts.get(HOST_NAME).status.state != 'up':
        sleep(10)
    print "Host is up"
except Exception as e:
    print 'Failed to setup network:\n%s' % str(e)
Esempio n. 8
0
 vmachine.start(
     action=params.Action(
         vm=params.VM(
             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"
                             )
                         )
                         )
Esempio n. 9
0
MOVE_IP = '192.168.101.11'
NETMASK = '255.255.255.0'
GATEWAY = '192.168.100.1'

try:
    api = API(url=URL, username=USERNAME, password=PASSWORD, ca_file=CA)
    print "Connected to %s successfully!" % api.get_product_info().name

except Exception as err:
    print "Connection failed: %s" % err

nic0 = params.HostNIC(network=params.Network(name='rhevm'),
                      name='eth0',
                      boot_protocol='static',
                      ip=params.IP(address=IP_ADDRESS,
                                   netmask=NETMASK,
                                   gateway=GATEWAY),
                      override_configuration=False)
nic1 = params.HostNIC(name='eth1',
                      network=params.Network(),
                      boot_protocol='none',
                      ip=params.IP(address=None, netmask=None, gateway=None))
nic2 = params.HostNIC(name='eth2',
                      network=params.Network(),
                      boot_protocol='none',
                      ip=params.IP(address=None, netmask=None, gateway=None))

# bond
bond0 = params.Bonding(slaves=params.Slaves(host_nic=[nic1, nic2]),
                       options=params.Options(option=[
                           params.Option(name='miimon', value='100'),
Esempio n. 10
0
                            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 )

        #vm started add sleeptime
        vm = api.vms.get(name=VMNAME)
        while ( vm.get_status().state != 'up' ):