def instance(name): try: conn = libvrt.wvmInstance(name) media = conn.get_media_device() conn.close() except libvirtError as err: error_msg(err) return {'media': media}
def instance(name): try: conn = libvrt.wvmInstance(name) status = conn.get_state() conn.close() except libvirtError as err: error_msg(err) return {'status': status}
def instance(name, media: InstanceMedia): try: conn = libvrt.wvmInstance(name) conn.umount_iso(media.device, media.image) conn.close() except libvirtError as err: error_msg(err) return media
def instance(name): try: conn = libvrt.wvmInstance(name) drivers = conn.get_disk_device() for drive in drivers: if drive.get('dev') == 'vda' or drive.get('dev') == 'sda': sconn = libvrt.wvmStorage(drive.get('pool')) vol = sconn.get_volume(drive.get('name')) vol.delete() sconn.refresh() sconn.close() conn.delete() conn.close() except libvirtError as err: error_msg(err)
def instance(name, resize: InstanceResize): try: conn = libvrt.wvmInstance(name) if conn.get_state() != 'shutoff': error_msg('Please shutoff the virtual machine.') conn.resize_resources(resize.vcpu, resize.memory) if resize.disk_size: drivers = conn.get_disk_device() for drive in drivers: if drive.get('dev') == 'vda' or drive.get('dev') == 'sda': sconn = libvrt.wvmStorage(drive.get('pool')) sconn.resize_volume(drive.get('name'), resize.disk_size) sconn.close() conn.close() except libvirtError as err: error_msg(err) return resize
def instance(name): try: conn = libvrt.wvmInstance(name) instance = { 'name': name, 'status': conn.get_state(), 'uuid': conn.get_uuid(), 'vcpu': conn.get_vcpu(), 'memory': conn.get_memory(), 'disks': conn.get_disk_device(), 'media': conn.get_media_device(), 'ifaces': conn.get_net_ifaces() } conn.close() except libvirtError as err: error_msg(err) return {'instance': instance}
def instance(instance: InstanceCreate): # Download and deploy images template for img in instance.images: if img.get('primary') is True: template = images.Template(img.get('name'), img.get('md5sum')) err_msg, template_path = template.download(img.get('url')) if err_msg is None: image = images.Image(img.get('name'), img.get('pool')) err_msg = image.deploy_template( template=template, disk_size=img.get('size'), networks=instance.network, public_key=instance.public_keys, hostname=instance.name, root_password=instance.root_password) else: try: conn = libvrt.wvmStorage(img.get('pool')) conn.create_volume(img.get('name'), img.get('size'), fmt='raw') conn.close() except libvirtError as err: error_msg(err) if err_msg is not None: error_msg(err_msg) # Create XML try: conn = libvrt.wvmCreate() conn.create_xml(instance.name, instance.vcpu, instance.memory, instance.images, instance.network) conn.close() except libvirtError as err: error_msg(err) # Run Instance try: conn = libvrt.wvmInstance(instance.name) conn.start() conn.close() except libvirtError as err: error_msg(err) return instance
def instances(): instances = [] conn = libvrt.wvmConnect() instance_names = conn.get_instances() conn.close() for name in instance_names: dconn = libvrt.wvmInstance(name) instances.append({ 'name': name, 'status': dconn.get_state(), 'uuid': dconn.get_uuid(), 'vcpu': dconn.get_vcpu(), 'memory': dconn.get_memory(), 'disks': dconn.get_disk_device(), 'media': dconn.get_media_device(), 'ifaces': dconn.get_net_ifaces() }) dconn.close() return {'instances': instances}
def instance(name, status: InstanceStatus): if status.action not in ['start', 'stop', 'suspend', 'resume']: error_msg('Status does not exist.') try: conn = libvrt.wvmInstance(name) if status.action == 'start': conn.start() if status.action == 'stop': conn.shutdown() if status.action == 'suspend': conn.suspend() if status.action == 'resume': conn.resume() conn.close() except libvirtError as err: error_msg(err) return status
def instance(name, reset_pass: ResetPassword): error_msg = None try: conn = libvrt.wvmInstance(name) drives = conn.get_disk_device() conn.close() except libvirtError as err: error_msg(err) try: image = images.Image(drives[0].get('name'), drives[0].get('pool')) err_msg = image.reset_password(reset_pass.get('distro'), reset_pass.get('password')) except Exception as e: error_msg(err) if err_msg: error_msg(err_msg) return reset_pass