def add_vm_blank(api): vm_memory = 256 * MB vm_params = params.VM( memory=vm_memory, os=params.OperatingSystem( type_='other_linux', boot=[ params.Boot(dev=types.BootDevice.HD), params.Boot(dev=types.BootDevice.NETWORK) ], ), type_='server', high_availability=params.HighAvailability(enabled=False, ), cluster=params.Cluster(name=TEST_CLUSTER, ), template=params.Template(name=TEMPLATE_BLANK, ), display=params.Display( smartcard_enabled=True, keyboard_layout='en-us', file_transfer_enabled=True, copy_paste_enabled=True, ), memory_policy=params.MemoryPolicy(guaranteed=vm_memory / 2, ), name=VM0_NAME) api.vms.add(vm_params) testlib.assert_true_within_short( lambda: api.vms.get(VM0_NAME).status.state == 'down', ) vm_params.name = VM1_NAME api.vms.add(vm_params) testlib.assert_true_within_short( lambda: api.vms.get(VM1_NAME).status.state == 'down', )
def setPXEBootSecond(api, guest_name): vm = api.vms.get(name=guest_name) hd_boot_dev = params.Boot(dev='hd') net_boot_dev = params.Boot(dev='network') vm.os.set_boot([hd_boot_dev]) vm.update() sleep(1) vm.os.set_boot([hd_boot_dev, net_boot_dev]) vm.update()
def CreateVm(vm_name, vm_type, vm_mem, vm_cluster, vm_template): vm_params = params.VM( name=vm_name, memory=vm_mem * MB, cluster=api.clusters.get(name=vm_cluster), template=api.templates.get(name=vm_template), os=params.OperatingSystem(boot=[params.Boot(dev="hd")])) vm_params.set_type(vm_type) try: api.vms.add(vm=vm_params) print "Virtual machine '%s' added." % vm_name except Exception as ex: print "Adding virtual machine '%s' failed: %s" % (vm_name, ex)
def set_BootOrder(self, vmname, boot_order): VM = self.get_VM(vmname) bootorder = [] for device in boot_order: bootorder.append(params.Boot(dev=device)) VM.os.boot = bootorder try: VM.update() setChanged() except Exception as e: setMsg("Failed to update the boot order.") setMsg(str(e)) setFailed() return False return True
def __new_vm_base(self, name, template, ram): memory = 1024**3 * ram # convert from bytes to GB cluster = self.__entrypoint().clusters.get(name='Default') template = self.__entrypoint().templates.get(name=template) os = params.OperatingSystem(boot=[params.Boot(dev='hd')]) vm_params = params.VM(name=name, memory=memory, cluster=cluster, template=template, os=os, type_='Server') try: self.__entrypoint().vms.add(vm=vm_params) print('added vm %s' % name) except Exception as ex: print('Unexpected error: %s' % ex)
from ovirtsdk.api import API from ovirtsdk.xml import params try: api = API (url="https://_HOST_", username="******", password="******", ca_file="_ca.crt_") vm_name = "vm1" vm_memory = 512 * 1024 * 1024 vm_cluster = api.clusters.get(name="Default") vm_template = api.templates.get(name="Blank") vm_os = params.OperatingSystem(boot=[params.Boot(dev="hd")]) vm_params = params.VM(name=vm_name, memory=vm_memory, cluster=vm_cluster, template=vm_template, os=vm_os) try: api.vms.add(vm=vm_params) print "Virtual machine '%s' added." % vm_name except Exception as ex: print "Adding virtual machine '%s' failed: %s" % (vm_name, ex) api.disconnect() except Exception as ex: print "Unexpected error: %s" % ex
def trigger_add_vm(**kwargs): vm_params = params.VM(name=kwargs['vm_name'], template=kwargs['template_object'], disks=kwargs['template_disks'], cluster=kwargs['cluster_object'], host=kwargs['host_object'], cpu=kwargs['cpu_object'], memory=kwargs['appliance_memory'] * GB, placement_policy=kwargs['placement_object'], type_=kwargs['appliance_type']) try: cfme_appliance = api.vms.add(vm_params) except RequestError as E: print("Error while creating vm(s)") sys.exit(E) while cfme_appliance.status.state == 'image_locked': time.sleep(10) cfme_appliance = api.vms.get(name=kwargs['vm_name']) for disk in kwargs['disks']: disk_size = kwargs['disks'][disk]['size'] * GB interface_type = kwargs['disks'][disk]['interface'] disk_format = kwargs['disks'][disk]['format'] allocation = kwargs['disks'][disk]['allocation'] location = kwargs['disks'][disk]['location'] store = api.storagedomains.get(name=location) domain = params.StorageDomains(storage_domain=[store]) disk_param = params.Disk(description=disk, storage_domains=domain, size=disk_size, interface=interface_type, format=disk_format, type_=allocation) new_disk = cfme_appliance.disks.add(disk=disk_param) if len(kwargs['appliance_nics']) > 0: current_nics = cfme_appliance.get_nics().list() current_networks = [] for nic in current_nics: network_id = nic.get_network().id current_networks.append(api.networks.get(id=network_id).name) new_set = set(kwargs['appliance_nics']) current_set = set(current_networks) appliance_nics = list(new_set - current_set) for i in range(len(appliance_nics)): network_name = params.Network(name=appliance_nics[i]) nic_name = params.NIC(name='nic{}'.format(i + 1), network=network_name) cfme_appliance.nics.add(nic=nic_name) while locked_disks(cfme_appliance): time.sleep(10) cfme_appliance = api.vms.get(name=kwargs['vm_name']) dev = params.Boot(dev='network') cfme_appliance.os.boot.append(dev) # boot_params = { # # "ks": "http://<satellite-server>/ks", # # "ksdevice": boot_if, # # "dns": "1.2.3.4,1.2.3.5", # # "ip": "10.9.8.7", # # "netmask": "255.255.255.0", # # "gateway": "10.9.8.1", # "hostname": "{0}.my.domain".format(VM_NAME) # } # cmdline = " ".join(map("{0[0]}={0[1]}".format, boot_params.iteritems())) # cfme_appliance.set_os(params.OperatingSystem( # # kernel="iso://vmlinuz", # # initrd="iso://initrd.img", # cmdline=cmdline) # ) cfme_appliance.update() for disk in cfme_appliance.disks.list(): if disk.description in appliance['disks'] \ or already_moved(kwargs['domain_object'], disk): continue disk.move(action=kwargs['actions']) cfme_appliance = api.vms.get(name=kwargs['vm_name']) while locked_disks(cfme_appliance): time.sleep(10) cfme_appliance = api.vms.get(name=kwargs['vm_name']) cfme_appliance.start()
def create(self, name, clu, numcpu, numinterfaces, netinterface, diskthin1, disksize1, diskinterface, memory, storagedomain, guestid, net1, net2=None, net3=None, net4=None, mac1=None, mac2=None, launched=True, iso=None, diskthin2=None, disksize2=None, vnc=False): boot1, boot2 = 'hd', 'network' if iso in ["", "xx", "yy"]: iso = None if iso: boot2 = 'cdrom' api = self.api memory = memory * MB disksize1 = disksize1 * GB if disksize2: disksize2 = disksize2 * GB #VM CREATION IN OVIRT #TODO check that clu and storagedomain exist and that there is space there diskformat1, diskformat2 = 'raw', 'raw' sparse1, sparse2 = False, False if diskthin1: diskformat1 = 'cow' sparse1 = True if disksize2 and diskthin2: diskformat2 = 'cow' sparse2 = True vm = api.vms.get(name=name) if vm: return "VM %s allready existing.Leaving...\n" % name clu = api.clusters.get(name=clu) storagedomain = api.storagedomains.get(name=storagedomain) try: disk1 = params.Disk(storage_domains=params.StorageDomains( storage_domain=[storagedomain]), name="%s_Disk1" % (name), size=disksize1, type_='system', status=None, interface=diskinterface, format=diskformat1, sparse=sparse1, bootable=True) disk1 = api.disks.add(disk1) disk1id = disk1.get_id() except: return "Insufficient space in storage domain for disk1.Leaving...\n" if disksize2: try: disk2 = params.Disk(storage_domains=params.StorageDomains( storage_domain=[storagedomain]), name="%s_Disk2" % (name), size=disksize2, type_='system', status=None, interface=diskinterface, format=diskformat2, sparse=sparse2, bootable=False) disk2 = api.disks.add(disk2) disk2id = disk2.get_id() except: return "Insufficient space in storage domain for disk2.Leaving...\n" #boot order boot = [params.Boot(dev=boot1), params.Boot(dev=boot2)] #vm creation kernel, initrd, cmdline = None, None, None if vnc: display = params.Display(type_='vnc') else: display = params.Display(type_='spice') api.vms.add( params.VM( name=name, memory=memory, cluster=clu, display=display, template=api.templates.get('Blank'), os=params.OperatingSystem(type_=guestid, boot=boot, kernel=kernel, initrd=initrd, cmdline=cmdline), cpu=params.CPU(topology=params.CpuTopology(cores=numcpu)), type_="server")) #add nics api.vms.get(name).nics.add( params.NIC(name='eth0', network=params.Network(name=net1), interface=netinterface)) if numinterfaces >= 2: api.vms.get(name).nics.add( params.NIC(name='eth1', network=params.Network(name=net2), interface=netinterface)) #compare eth0 and eth1 to get sure eth0 has a lower mac eth0ok = True maceth0 = api.vms.get(name).nics.get(name="eth0").mac.address maceth1 = api.vms.get(name).nics.get(name="eth1").mac.address eth0 = maceth0.split(":") eth1 = maceth1.split(":") for i in range(len(eth0)): el0 = int(eth0[i], 16) el1 = int(eth1[i], 16) if el0 == el1: pass elif el0 > el1: eth0ok = False if not eth0ok: tempnic = "00:11:11:11:11:11" nic = api.vms.get(name).nics.get(name="eth0") nic.mac.address = tempnic nic.update() nic = api.vms.get(name).nics.get(name="eth1") nic.mac.address = maceth0 nic.update() nic = api.vms.get(name).nics.get(name="eth0") nic.mac.address = maceth1 nic.update() if mac1: nic = api.vms.get(name).nics.get(name="eth0") if not ":" in mac1: mac1 = "%s%s" % (nic.mac.address[:-2], mac1) nic.mac.address = mac1 nic.update() if mac2: nic = api.vms.get(name).nics.get(name="eth1") if not ":" in mac2: mac2 = "%s%s" % (nic.mac.address[:-2], mac2) nic.mac.address = mac2 nic.update() if numinterfaces >= 3: api.vms.get(name).nics.add( params.NIC(name='eth2', network=params.Network(name=net3), interface=netinterface)) if numinterfaces >= 4: api.vms.get(name).nics.add( params.NIC(name='eth3', network=params.Network(name=net4), interface=netinterface)) api.vms.get(name).update() if iso: iso = checkiso(api, iso) cdrom = params.CdRom(file=iso) api.vms.get(name).cdroms.add(cdrom) while api.disks.get(id=disk1id).get_status().get_state() != "ok": time.sleep(5) api.vms.get(name).disks.add(disk1) while not api.vms.get(name).disks.get(id=disk1id): time.sleep(2) api.vms.get(name).disks.get(id=disk1id).activate() if disksize2: while api.disks.get(id=disk2id).get_status().get_state() != "ok": time.sleep(5) api.vms.get(name).disks.add(disk2) while not api.vms.get(name).disks.get(id=disk2id): time.sleep(2) api.vms.get(name).disks.get(id=disk2id).activate() #retrieve MACS for cobbler vm = api.vms.get(name=name) for nic in vm.nics.list(): self.macaddr.append(nic.mac.address)
def create_vm(vmprefix,disksize, storagedomain,network, vmcores,vmsockets,addstorage): print ("------------------------------------------------------") print ("Creating", num, "RHEV based virtual machines") print ("-------------------------------------------------------") for machine in range(0,int(num)): try: vm_name = str(vmprefix) + "_" + str(machine) + "_sockets_" + str(vmsockets) vm_memory = int(memory)*1024*1024*1024 vm_cluster = api.clusters.get(name=cluster) vm_template = api.templates.get(name=vmtemplate) vm_os = params.OperatingSystem(boot=[params.Boot(dev="hd")]) cpu_params = params.CPU(topology=params.CpuTopology(sockets=vmsockets,cores=vmcores)) # set proper VM parameters - based on will VM be on "thin" disk or "preallocated" disk if vmdiskpreallocated == "yes": vm_params = params.VM(name=vm_name,memory=vm_memory,cluster=vm_cluster,template=vm_template,os=vm_os,cpu=cpu_params, disks=params.Disks(clone=True)) elif vmdiskpreallocated == "no": vm_params = params.VM(name=vm_name,memory=vm_memory,cluster=vm_cluster, template=vm_template, os=vm_os,cpu=cpu_params) print ("creating virtual machine", vm_name) api.vms.add(vm=vm_params) api.vms.get(vm_name).nics.add(params.NIC(name=nicname, network=params.Network(name=network), interface='virtio')) # update vm and add disk to it wait_vm_state(vm_name,"down") print ("Virtual machine created: ", vm_name, "and it has parameters"," memory:", memory,"[GB]", " cores:", vmcores, " sockets", vmsockets, " waiting on machine to unlock so we proceed with configuration") wait_vm_state(vm_name, "down") diskname = "disk_" + str(vmprefix) + str(machine) # if there is necessary to add additional disk to VM - can be preallocated or thin if addstorage == "yes" and diskpreallocated == "no": for disk in range(0,int(numdisks)): # add one disk at time - one will be added by default - only add thin disks api.vms.get(vm_name).disks.add(params.Disk(name=diskname + "_" + str(disk), storage_domains=params.StorageDomains(storage_domain=[api.storagedomains.get(name=storagedomain)]), size=int(disksize)*1024*1024*1024, status=None, interface='virtio', format='cow', sparse=True, bootable=False)) print ("Disk of size:",disksize,"GB originating from", storagedomain, "storage domain is attached to VM - but we cannot start machine before disk is in OK state" " starting machine with disk attached to VM and same time having disk in Locked state will result in machine start failure") wait_disk_state(diskname + "_" + str(disk) ,"ok") print ("Machine", vm_name, "is ready to be started") api.vms.get(vm_name).start() print ("Machine", vm_name, "started successfully, machine parameters are memory:",memory,"[GB]", "cores:", vmcores, " sockets", vmsockets, " storage disk", disksize, "[GB]") elif addstorage == "yes" and diskpreallocated == "yes": for disk in range(0, int(numdisks)): api.vms.get(vm_name).disks.add(params.Disk(name=diskname + "_" + str(disk) , storage_domains=params.StorageDomains(storage_domain=[api.storagedomains.get(name=storagedomain)]), size=int(disksize)*1024*1024*1024, status=None, interface='virtio', format='raw', sparse=False, bootable=False )) # if disk is not in "OK" state ... wait here - we cannot start machine if this is not the case print ("Disk of size:",disksize,"GB originating from", storagedomain, "storage domain is attached to VM - but we cannot start machine before disk is in OK state" " starting machine with disk attached to VM and same time having disk in Locked state will result in machine start failure") wait_disk_state(diskname + "_" + str(disk) ,"ok") print ("Machine", vm_name, "is ready to be started") api.vms.get(vm_name).start() print ("Machine", vm_name, "started successfully, machine parameters are memory:",memory,"[GB]" " cores:", vmcores, " sockets", vmsockets, " storage disk", disksize, "[GB]" ) elif addstorage == "no": print ("addstorage=no was specified for", vm_name,"no additional disk will be added, starting VM:", vm_name) api.vms.get(vm_name).start() print ("Machine", vm_name, "started successfully, machine parameters are memory:",memory,"[GB]" "cores:", vmcores, "sockets:", vmsockets, "storage_disk", disksize, "[GB]" ) except Exception as e: print ("Adding virtual machine '%s' failed: %s", vm_name, e)
def set_boot_order(vm): vm.set_os(params.OperatingSystem(boot=[params.Boot(dev='hd')])) vm.update()
def trigger_add_vm(**kwargs): vm_params = params.VM(name=kwargs['vm_name'], template=kwargs['template_object'], disks=kwargs['template_disks'], cluster=kwargs['cluster_object'], host=kwargs['host_object'], cpu=kwargs['cpu_object'], memory=kwargs['appliance_memory'] * GB, placement_policy=kwargs['placement_object'], type_=kwargs['appliance_type']) try: cfme_appliance = api.vms.add(vm_params) except RequestError as E: print("Error while creating vm(s)") sys.exit(E) while cfme_appliance.status.state == 'image_locked': time.sleep(10) cfme_appliance = api.vms.get(name=kwargs['vm_name']) for disk in kwargs['disks']: disk_size = kwargs['disks'][disk]['size'] * GB interface_type = kwargs['disks'][disk]['interface'] disk_format = kwargs['disks'][disk]['format'] allocation = kwargs['disks'][disk]['allocation'] location = kwargs['disks'][disk]['location'] store = api.storagedomains.get(name=location) domain = params.StorageDomains(storage_domain=[store]) disk_param = params.Disk(description=disk, storage_domains=domain, size=disk_size, interface=interface_type, format=disk_format, type_=allocation) new_disk = cfme_appliance.disks.add(disk=disk_param) if len(kwargs['appliance_nics']) > 0: current_nics = cfme_appliance.get_nics().list() current_networks = [] for nic in current_nics: network_id = nic.get_network().id current_networks.append(api.networks.get(id=network_id).name) new_set = set(kwargs['appliance_nics']) current_set = set(current_networks) appliance_nics = list(new_set - current_set) for i in range(len(appliance_nics)): network_name = params.Network(name=appliance_nics[i]) nic_name = params.NIC(name='card{}'.format(i), network=network_name) cfme_appliance.nics.add(nic=nic_name) while locked_disks(cfme_appliance): time.sleep(10) cfme_appliance = api.vms.get(name=kwargs['vm_name']) dev = params.Boot(dev='network') cfme_appliance.os.boot.append(dev) cfme_appliance.update() for disk in cfme_appliance.disks.list(): if disk.description in appliance['disks'] \ or already_moved(kwargs['domain_object'], disk): continue disk.move(action=kwargs['actions']) cfme_appliance = api.vms.get(name=kwargs['vm_name']) while locked_disks(cfme_appliance): time.sleep(10) cfme_appliance = api.vms.get(name=kwargs['vm_name']) cfme_appliance.start()
api.vms.add(vmparams) except: print "Error creating VM with specified parameters, recheck" sys.exit(1) if options.verbosity > 1: print "VM created successfuly" if options.verbosity > 1: print "Attaching networks and boot order..." vm = api.vms.get(name=options.name) vm.nics.add(nic_gest) vm.nics.add(nic_serv) # Setting VM to boot always from network, then from HD boot1 = params.Boot(dev="network") boot2 = params.Boot(dev="hd") vm.os.boot = [boot1, boot2] try: vm.update() except: print "Error attaching networks, please recheck and remove configurations left behind" sys.exit(1) if options.verbosity > 1: print "Adding HDD" try: vm.disks.add(vmdisk) except: print "Error attaching disk, please recheck and remove any leftover configuration"
url = "https://%s:%s/api" % (ohost, oport) else: url = "http://%s:%s/api" % (ohost, oport) api = API(url=url, username=ouser, password=opassword, insecure=True) # searchs vms with matching tag vms = api.vms.list() launched = 0 for vm in vms: if vm.status.state == "up": continue for tg in vm.get_tags().list(): if tg.name == tag: # delete tag tg.delete() # remove kernel options vm.os.kernel, vm.os.initrd, vm.os.cmdline = "", "", "" # ensure first boot is hd( and set second as cdrom vm.os.boot = [params.Boot(dev="hd"), params.Boot(dev="cdrom")] # launch vm vm.start() print "vm %s started" % vm.name launched = launched + 1 if launched == 0: print "No matching vms found" else: print "%d vms were launched" % (launched) sys.exit(0)