def add_serial_console_vm2(api): engine = api.system_service() # Find the virtual machine. Note the use of the `all_content` parameter, it is # required in order to obtain additional information that isn't retrieved by # default, like the configuration of the serial console. vm = engine.vms_service().list(search='name={}'.format(VM2_NAME), all_content=True)[0] if not vm.console.enabled: vm_service = test_utils.get_vm_service(engine, VM2_NAME) vm_service.update(types.Vm(console=types.Console(enabled=True)))
def create(self, vm_name, cluster, template, stateless=False): """ :type vm_name: string :type cluster: clusterlib.Cluster :type template: string :type stateless: boolean """ MB256 = 256 * 2**20 sdk_type = types.Vm( name=vm_name, cluster=cluster.get_sdk_type(), template=types.Template(name=template), stateless=stateless, memory=MB256, memory_policy=types.MemoryPolicy(guaranteed=MB256 // 2), console=types.Console(enabled=True), ) self._create_sdk_entity(sdk_type)
def build_entity(self): return otypes.InstanceType( id=self.param('id'), name=self.param('name'), console=( otypes.Console(enabled=self.param('serial_console')) ) if self.param('serial_console') is not None else None, usb=( otypes.Usb(enabled=self.param('usb_support')) ) if self.param('usb_support') is not None else None, high_availability=otypes.HighAvailability( enabled=self.param('high_availability'), priority=self.param('high_availability_priority'), ) if self.param('high_availability') is not None or self.param('high_availability_priority') else None, cpu=otypes.Cpu( topology=otypes.CpuTopology( cores=self.param('cpu_cores'), sockets=self.param('cpu_sockets'), threads=self.param('cpu_threads'), ) if any(( self.param('cpu_cores'), self.param('cpu_sockets'), self.param('cpu_threads') )) else None, cpu_tune=otypes.CpuTune( vcpu_pins=[ otypes.VcpuPin(vcpu=int(pin['vcpu']), cpu_set=str(pin['cpu'])) for pin in self.param('cpu_pinning') ], ) if self.param('cpu_pinning') else None, mode=otypes.CpuMode(self.param('cpu_mode')) if self.param( 'cpu_mode') else None, ) if any(( self.param('cpu_cores'), self.param('cpu_sockets'), self.param('cpu_threads'), self.param('cpu_mode'), self.param('cpu_pinning') )) else None, os=otypes.OperatingSystem( type=self.param('operating_system'), boot=otypes.Boot( devices=[ otypes.BootDevice(dev) for dev in self.param('boot_devices') ], ) if self.param('boot_devices') else None ), rng_device=otypes.RngDevice( source=otypes.RngSource(self.param('rng_device')), rate=otypes.Rate( bytes=self.param('rng_bytes'), period=self.param('rng_period') ) ) if self.param('rng_device') else None, memory=convert_to_bytes( self.param('memory') ) if self.param('memory') else None, virtio_scsi=otypes.VirtioScsi( enabled=self.param('virtio_scsi') ) if self.param('virtio_scsi') else None, memory_policy=otypes.MemoryPolicy( guaranteed=convert_to_bytes(self.param('memory_guaranteed')), ballooning=self.param('ballooning_enabled'), max=convert_to_bytes(self.param('memory_max')), ) if any(( self.param('memory_guaranteed'), self.param('ballooning_enabled') is not None, self.param('memory_max') )) else None, description=self.param('description'), placement_policy=otypes.VmPlacementPolicy( affinity=otypes.VmAffinity(self.param('placement_policy')), hosts=[ otypes.Host(name=self.param('host')), ] if self.param('host') else None, ) if self.param('placement_policy') else None, soundcard_enabled=self.param('soundcard_enabled'), display=otypes.Display( smartcard_enabled=self.param('smartcard_enabled') ) if self.param('smartcard_enabled') is not None else None, io=otypes.Io( threads=self.param('io_threads'), ) if self.param('io_threads') is not None else None, )
def create(self, name, virttype='kvm', profile='', flavor=None, plan='kvirt', cpumodel='Westmere', cpuflags=[], numcpus=2, memory=512, guestid='guestrhel764', pool='default', template=None, disks=[{ 'size': 10 }], disksize=10, diskthin=True, diskinterface='virtio', nets=['default'], iso=None, vnc=False, cloudinit=True, reserveip=False, reservedns=False, reservehost=False, start=True, keys=None, cmds=[], ips=None, netmasks=None, gateway=None, nested=True, dns=None, domain=None, tunnel=False, files=[], enableroot=True, alias=[], overrides={}, tags=None): """ :param name: :param virttype: :param profile: :param flavor: :param plan: :param cpumodel: :param cpuflags: :param numcpus: :param memory: :param guestid: :param pool: :param template: :param disks: :param disksize: :param diskthin: :param diskinterface: :param nets: :param iso: :param vnc: :param cloudinit: :param reserveip: :param reservedns: :param reservehost: :param start: :param keys: :param cmds: :param ips: :param netmasks: :param gateway: :param nested: :param dns: :param domain: :param tunnel: :param files: :param enableroot: :param alias: :param overrides: :param tags: :return: """ clone = not diskthin templateobject = types.Template(name=template) if template else None console = types.Console(enabled=True) try: vm = self.vms_service.add(types.Vm( name=name, cluster=types.Cluster(name=self.cluster), template=templateobject, console=console), clone=clone) vm_service = self.vms_service.vm_service(vm.id) except Exception as e: if self.debug: print(e) return {'result': 'failure', 'reason': e} timeout = 0 while True: vm = vm_service.get() if vm.status == types.VmStatus.DOWN: break else: timeout += 5 sleep(5) common.pprint("Waiting for vm to be ready", color='green') if timeout > 60: return { 'result': 'failure', 'reason': 'timeout waiting for vm to be ready' } profiles_service = self.conn.system_service().vnic_profiles_service() netprofiles = {} for prof in profiles_service.list(): netprofiles[prof.name] = prof.id if 'default' not in netprofiles and 'ovirtmgmt' in netprofiles: netprofiles['default'] = netprofiles['ovirtmgmt'] nics_service = self.vms_service.vm_service(vm.id).nics_service() nic_configurations = [] for index, net in enumerate(nets): netname = None netmask = None mac = None if isinstance(net, str): netname = net elif isinstance(net, dict) and 'name' in net: netname = net['name'] ip = None mac = net.get('mac') netmask = next( (e for e in [net.get('mask'), net.get('netmask')] if e is not None), None) gateway = net.get('gateway') noconf = net.get('noconf') if noconf is not None: continue if 'ip' in net: ip = net['ip'] # if 'alias' in net: # alias = net['alias'] if ips and len(ips) > index and ips[index] is not None: ip = ips[index] if ip is not None and netmask is not None and gateway is not None: nic_configuration = types.NicConfiguration( name='eth%s' % index, on_boot=True, boot_protocol=types.BootProtocol.STATIC, ip=types.Ip(version=types.IpVersion.V4, address=ip, netmask=netmask, gateway=gateway)) nic_configurations.append(nic_configuration) if netname is not None and netname in netprofiles: profile_id = netprofiles[netname] nics_service.add( types.Nic(name='eth%s' % index, mac=mac, vnic_profile=types.VnicProfile(id=profile_id))) for index, disk in enumerate(disks): diskpool = pool diskthin = True disksize = '10' if index == 0 and template is not None: continue if isinstance(disk, int): disksize = disk elif isinstance(disk, str) and disk.isdigit(): disksize = int(disk) elif isinstance(disk, dict): disksize = disk.get('size', disksize) diskpool = disk.get('pool', pool) diskthin = disk.get('thin', diskthin) self.add_disk(name, disksize, pool=diskpool, thin=diskthin) initialization = None if cloudinit: custom_script = '' if files: data = common.process_files(files=files, overrides=overrides) if data != '': custom_script += "write_files:\n" custom_script += data cmds.append('sleep 60') if template.lower().startswith('centos'): cmds.append('yum -y install centos-release-ovirt42') if template.lower().startswith('centos') or template.lower().startswith('fedora')\ or template.lower().startswith('rhel'): cmds.append('yum -y install ovirt-guest-agent-common') cmds.append('systemctl enable ovirt-guest-agent') cmds.append('systemctl start ovirt-guest-agent') if template.lower().startswith('debian'): cmds.append( 'echo "deb http://download.opensuse.org/repositories/home:/evilissimo:/deb/Debian_7.0/ ./" ' '>> /etc/apt/sources.list') cmds.append( 'gpg -v -a --keyserver http://download.opensuse.org/repositories/home:/evilissimo:/deb/' 'Debian_7.0/Release.key --recv-keys D5C7F7C373A1A299') cmds.append('gpg --export --armor 73A1A299 | apt-key add -') cmds.append('apt-get update') cmds.append('apt-get -Y install ovirt-guest-agent') cmds.append('service ovirt-guest-agent enable') cmds.append('service ovirt-guest-agent start') if [x for x in common.ubuntus if x in template.lower()]: cmds.append( 'echo deb http://download.opensuse.org/repositories/home:/evilissimo:/ubuntu:/16.04/' 'xUbuntu_16.04/ /') cmds.append( 'wget http://download.opensuse.org/repositories/home:/evilissimo:/ubuntu:/16.04/' 'xUbuntu_16.04//Release.key') cmds.append('apt-key add - < Release.key') cmds.append('apt-get update') cmds.append('apt-get -Y install ovirt-guest-agent') data = common.process_cmds(cmds=cmds, overrides=overrides) custom_script += "runcmd:\n" custom_script += data custom_script = None if custom_script == '' else custom_script user_name = common.get_user(template) root_password = None dns_servers = '8.8.8.8 1.1.1.1' key = get_home_ssh_key() initialization = types.Initialization( user_name=user_name, root_password=root_password, regenerate_ssh_keys=True, authorized_ssh_keys=key, host_name=name, nic_configurations=nic_configurations, dns_servers=dns_servers, dns_search=domain, custom_script=custom_script) tags_service = self.conn.system_service().tags_service() existing_tags = [tag.name for tag in tags_service.list()] if "profile_%s" % profile not in existing_tags: tags_service.add(types.Tag(name="profile_%s" % profile)) if "plan_%s" % plan not in existing_tags: tags_service.add(types.Tag(name="plan_%s" % plan)) tags_service = vm_service.tags_service() tags_service.add(tag=types.Tag(name="profile_%s" % profile)) tags_service.add(tag=types.Tag(name="plan_%s" % plan)) vm_service.start(use_cloud_init=cloudinit, vm=types.Vm(initialization=initialization)) return {'result': 'success'}
logging.basicConfig(level=logging.DEBUG, filename='example.log') # This example will connect to the server, find a virtual machine and enable the # serial console if it isn't enabled yet: # Create the connection to the server: connection = sdk.Connection( url='https://engine41.example.com/ovirt-engine/api', username='******', password='******', ca_file='ca.pem', debug=True, log=logging.getLogger(), ) # Find the virtual machine. Note the use of the `all_content` parameter, it is # required in order to obtain additional information that isn't retrieved by # default, like the configuration of the serial console. vms_service = connection.system_service().vms_service() vm = vms_service.list(search='name=myvm', all_content=True)[0] # Check if the serial console is enabled, and if it isn't then update the # virtual machine to enable it: if not vm.console.enabled: vm_service = vms_service.vm_service(vm.id) vm_service.update(types.Vm(console=types.Console(enabled=True))) # Close the connection to the server: connection.close()