class SimpleStack(object): """ This module is the base implementation for all hypervisors available. It uses libvirt as the fallback implementation and throw an error (FeatureNotImplemented) if the hypervisor doesn't have an implementation for the given method and doesn't support libvirt also. """ def __init__(self): self.connection = False self.format_for = Formatter() def libvirt_connect(self): return libvirt.open(self.libvirt_connection_path()) def libvirt_connection_path(self): proto = config.get("libvirt", "transport") if proto == "ssh": keyfile = config.get("libvirt", "ssh_keyfile") if os.path.exists(keyfile): params = "keyfile=%s" % keyfile else: raise SSHKeyNotFound else: params = "no_verify=1" conn_str = "qemu+%(proto)s://%(username)s@%(server)s/system?%(params)s" return (conn_str % { "params": params, "proto": proto, "server": self.poolinfo.get("api_server"), "username": self.poolinfo.get("username"), }) def connect(self): """ Each hypervisor should implement this method to return its own connection object or just use the libvirt_connect() here. """ raise FeatureNotImplemented() @require_libvirt(True) def pool_info(self): free = self.libvirt_connection.getFreeMemory() / 1024 / 1024 total = self.libvirt_connection.getInfo()[1] address = self.poolinfo.get("api_server").split(":")[0] return self.format_for.pool(total - free, free, address) @require_libvirt(True) def host_list(self): hosts = [] hosts.append( {"id": "libvirt:" + self.poolinfo.get("api_server").split(":")[0]}) return hosts @require_libvirt(True) def host_info(self, host_id): return (self.format_for.host( "libvirt:" + self.poolinfo.get("api_server").split(":")[0], self.poolinfo.get("api_server").split(":")[0], self.poolinfo.get("api_server").split(":")[0], )) @require_libvirt(True) def storage_list(self): storages = [] for storage in self.libvirt_connection.listAllStoragePools(0): storages.append({"id": storage.UUIDString()}) return storages @require_libvirt(True) def storage_info(self, storage_id): s = self.libvirt_connection.storagePoolLookupByUUIDString(storage_id) info = s.info() return (self.format_for.storage( storage_id, s.name(), "Not defined for Libvirt", int(info[2]), int(info[2]), int(info[1]), )) @require_libvirt(True) def guest_list(self): not_running = [ self.libvirt_vm_info(self.libvirt_connection.lookupByName(vm_name)) for vm_name in self.libvirt_connection.listDefinedDomains() ] running = [ self.libvirt_vm_info(self.libvirt_connection.lookupByID(vm_id)) for vm_id in self.libvirt_connection.listDomainsID() ] return not_running + running def guest_create(self, guestdata): raise FeatureNotImplemented() def guest_clone(self, guest_id, guestdata): raise FeatureNotImplemented() def guest_import(self, gueststream, guestsize, storage_id=None): raise FeatureNotImplemented() def guest_export(self, guest_id): raise FeatureNotImplemented() @require_libvirt(True) def guest_info(self, guest_id): dom = self.libvirt_connection.lookupByUUIDString(guest_id) return self.libvirt_vm_info(dom) @require_libvirt(True) def guest_shutdown(self, guest_id, force=False): dom = self.libvirt_connection.lookupByUUIDString(guest_id) if force: return dom.destroy() else: return dom.shutdown() @require_libvirt(True) def guest_start(self, guest_id): dom = self.libvirt_connection.lookupByUUIDString(guest_id) dom.create() @require_libvirt(True) def guest_suspend(self, guest_id): dom = self.libvirt_connection.lookupByUUIDString(guest_id) return dom.suspend() @require_libvirt(True) def guest_resume(self, guest_id): dom = self.libvirt_connection.lookupByUUIDString(guest_id) return dom.resume() @require_libvirt(True) def guest_reboot(self, guest_id, force=False): dom = self.libvirt_connection.lookupByUUIDString(guest_id) if force: return vm.reset(0) else: return vm.reboot(0) def guest_update(self, guest_id, guestdata): raise FeatureNotImplemented() @require_libvirt(True) def guest_delete(self, guest_id): dom = self.libvirt_connection.lookupByUUIDString(guest_id) dom.undefine() dom.destroy() def disk_list(self, guest_id): raise FeatureNotImplemented() def disk_create(self, guest_id, data): raise FeatureNotImplemented() def disk_info(self, guest_id, disk_id): raise FeatureNotImplemented() def disk_update(self, guest_id, disk_id, data): raise FeatureNotImplemented() def disk_delete(self, guest_id, disk_id): raise FeatureNotImplemented() def media_mount(self, guest_id, media_data): raise FeatureNotImplemented() def media_info(self, guest_id): raise FeatureNotImplemented() def media_unmount(self, guest_id): raise FeatureNotImplemented() @require_libvirt(True) def network_interface_list(self, guest_id): dom = self.libvirt_connection.lookupByUUIDString(guest_id) root = et.fromstring(dom.XMLDesc(0)) interfaces = [] for iface in root.iter("interface"): iface_param = {} for i in iface.getchildren(): key = i.attrib.keys()[0] iface_param[key] = i.attrib[key] # Keeping the contract for id values iface_param["id"] = iface_param["address"] interfaces.append(iface_param) return interfaces @require_libvirt(True) def network_interface_create(self, guest_id, data): dom = self.libvirt_connection.lookupByUUIDString(guest_id) root = et.fromstring(dom.XMLDesc(0)) interface = et.Element('interface') interface.set("type", "network") model = et.SubElement(interface, 'model') model.set("type", "virtio") if data.get("mac"): mac = et.SubElement(interface, 'mac') mac.set("address", str(data["mac"])) if data.get("network"): mac = et.SubElement(interface, 'source') mac.set("network", str(data["network"])) if data.get("name"): mac = et.SubElement(interface, 'alias') mac.set("name", str(data["name"])) dom.attachDeviceFlags(et.tostring(interface), 2) nw_interfaces = self.network_interface_list(guest_id) return {"id": nw_interfaces[-1]["id"]} @require_libvirt(True) def network_interface_info(self, guest_id, network_interface_id): dom = self.libvirt_connection.lookupByUUIDString(guest_id) root = et.fromstring(dom.XMLDesc(0)) nw_interfaces = self.network_interface_list(guest_id) for iface in nw_interfaces: if iface["id"] == network_interface_id: return iface raise EntityNotFound("Network interface", network_interface_id) def network_interface_update(self, guest_id, network_interface_id, data): dom = self.libvirt_connection.lookupByUUIDString(guest_id) root = et.fromstring(dom.XMLDesc(0)) # For each children of each interface, set the attributes to data # if the mac is the same of network_interface_id for iface in root.iter("interface"): for i in iface.getchildren(): if i.attrib.get("mac") == network_interface_id: for k, v in data: data.set(k, v) dom.updateDeviceFlags(et.tostring(iface), 2) def network_interface_delete(self, guest_id, network_interface_id): dom = self.libvirt_connection.lookupByUUIDString(guest_id) root = et.fromstring(dom.XMLDesc(0)) iface_id = "address" # defining the simplestack's libvirt id for iface in root.iter("interface"): for i in iface.getchildren(): if i.attrib.get(iface_id) == network_interface_id: dom.detachDeviceFlags(et.tostring(iface), 2) return {"id": network_interface_id} raise EntityNotFound("Network interface", network_interface_id) @require_libvirt(True) def snapshot_list(self, guest_id): dom = self.libvirt_connection.lookupByID(guest_id) snaps = [ self.libvirt_snapshot_info(s) for s in dom.snapshotListNames() ] return snaps def snapshot_create(self, guestname, name=None): raise FeatureNotImplemented() @require_libvirt(True) def snapshot_info(self, guestname, snapshot_name): dom = self.libvirt_connection.lookupByUUIDString(guest_id) snap = self.libvirt_get_snapshot(dom, snapshot_id) if snap: return self.libvirt_snapshot_info(snap) else: raise EntityNotFound("Snapshot", snapshot_id) @require_libvirt(True) def snapshot_delete(self, guest_id, snapshot_id): dom = self.libvirt_connection.lookupByUUIDString(guest_id) snap = self.libvirt_get_snapshot(dom, snapshot_id) snap.delete(0) @require_libvirt(True) def snapshot_revert(self, guest_id, snapshot_id): dom = self.libvirt_connection.lookupByUUIDString(guest_id) snap = self.libvirt_get_snapshot(dom, snapshot_id) dom.revertToSnapshot(snap) def tag_list(self, guest_id): raise FeatureNotImplemented() def tag_create(self, guest_id, tag_name): raise FeatureNotImplemented() def tag_delete(self, guest_id, tag_name): raise FeatureNotImplemented() def libvirt_vm_info(self, dom): infos = dom.info() return (self.format_for.guest(dom.UUIDString(), dom.name(), infos[3], infos[1] / 1024, None, None, None, None, self.state_translation[infos[0]], None)) def libvirt_snapshot_info(self, snapshot): return (self.format_for.snapshot(snapshot.get_description(), snapshot.name(), snapshot.get_state(), snapshot.get_path(), snapshot.get_create_time())) def libvirt_get_snapshot(self, dom, snapshot_id): pass
class SimpleStack(object): """ This module is the base implementation for all hypervisors available. It uses libvirt as the fallback implementation and throw an error (FeatureNotImplemented) if the hypervisor doesn't have an implementation for the given method and doesn't support libvirt also. """ def __init__(self): self.connection = False self.format_for = Formatter() def libvirt_connect(self): return libvirt.open(self.libvirt_connection_path()) def libvirt_connection_path(self): proto = config.get("libvirt", "transport") if proto == "ssh": keyfile = config.get("libvirt", "ssh_keyfile") if os.path.exists(keyfile): params = "keyfile=%s" % keyfile else: raise SSHKeyNotFound else: params = "no_verify=1" conn_str = "qemu+%(proto)s://%(username)s@%(server)s/system?%(params)s" return(conn_str % { "params": params, "proto" : proto, "server": self.poolinfo.get("api_server"), "username": self.poolinfo.get("username"), }) def connect(self): """ Each hypervisor should implement this method to return its own connection object or just use the libvirt_connect() here. """ raise FeatureNotImplemented() @require_libvirt(True) def pool_info(self): free = self.libvirt_connection.getFreeMemory()/1024/1024 total = self.libvirt_connection.getInfo()[1] address = self.poolinfo.get("api_server").split(":")[0] return self.format_for.pool(total - free, free, address) @require_libvirt(True) def host_list(self): hosts = [] hosts.append( {"id": "libvirt:" + self.poolinfo.get("api_server").split(":")[0]} ) return hosts @require_libvirt(True) def host_info(self, host_id): return( self.format_for.host( "libvirt:" + self.poolinfo.get("api_server").split(":")[0], self.poolinfo.get("api_server").split(":")[0], self.poolinfo.get("api_server").split(":")[0], ) ) @require_libvirt(True) def storage_list(self): storages = [] for storage in self.libvirt_connection.listAllStoragePools(0): storages.append({"id": storage.UUIDString()}) return storages @require_libvirt(True) def storage_info(self, storage_id): s = self.libvirt_connection.storagePoolLookupByUUIDString(storage_id) info = s.info() return( self.format_for.storage( storage_id, s.name(), "Not defined for Libvirt", int(info[2]), int(info[2]), int(info[1]), ) ) @require_libvirt(True) def guest_list(self): not_running = [ self.libvirt_vm_info(self.libvirt_connection.lookupByName(vm_name)) for vm_name in self.libvirt_connection.listDefinedDomains() ] running = [ self.libvirt_vm_info(self.libvirt_connection.lookupByID(vm_id)) for vm_id in self.libvirt_connection.listDomainsID() ] return not_running + running def guest_create(self, guestdata): raise FeatureNotImplemented() def guest_clone(self, guest_id, guestdata): raise FeatureNotImplemented() def guest_import(self, gueststream, guestsize, storage_id=None): raise FeatureNotImplemented() def guest_export(self, guest_id): raise FeatureNotImplemented() @require_libvirt(True) def guest_info(self, guest_id): dom = self.libvirt_connection.lookupByUUIDString(guest_id) return self.libvirt_vm_info(dom) @require_libvirt(True) def guest_shutdown(self, guest_id, force=False): dom = self.libvirt_connection.lookupByUUIDString(guest_id) if force: return dom.destroy() else: return dom.shutdown() @require_libvirt(True) def guest_start(self, guest_id): dom = self.libvirt_connection.lookupByUUIDString(guest_id) dom.create() @require_libvirt(True) def guest_suspend(self, guest_id): dom = self.libvirt_connection.lookupByUUIDString(guest_id) return dom.suspend() @require_libvirt(True) def guest_resume(self, guest_id): dom = self.libvirt_connection.lookupByUUIDString(guest_id) return dom.resume() @require_libvirt(True) def guest_reboot(self, guest_id, force=False): dom = self.libvirt_connection.lookupByUUIDString(guest_id) if force: return vm.reset(0) else: return vm.reboot(0) def guest_update(self, guest_id, guestdata): raise FeatureNotImplemented() @require_libvirt(True) def guest_delete(self, guest_id): dom = self.libvirt_connection.lookupByUUIDString(guest_id) dom.undefine() dom.destroy() def disk_list(self, guest_id): raise FeatureNotImplemented() def disk_create(self, guest_id, data): raise FeatureNotImplemented() def disk_info(self, guest_id, disk_id): raise FeatureNotImplemented() def disk_update(self, guest_id, disk_id, data): raise FeatureNotImplemented() def disk_delete(self, guest_id, disk_id): raise FeatureNotImplemented() def media_mount(self, guest_id, media_data): raise FeatureNotImplemented() def media_info(self, guest_id): raise FeatureNotImplemented() def media_unmount(self, guest_id): raise FeatureNotImplemented() @require_libvirt(True) def network_interface_list(self, guest_id): dom = self.libvirt_connection.lookupByUUIDString(guest_id) root = et.fromstring(dom.XMLDesc(0)) interfaces = [] for iface in root.iter("interface"): iface_param = {} for i in iface.getchildren(): key = i.attrib.keys()[0] iface_param[key] = i.attrib[key] # Keeping the contract for id values iface_param["id"] = iface_param["address"] interfaces.append(iface_param) return interfaces @require_libvirt(True) def network_interface_create(self, guest_id, data): dom = self.libvirt_connection.lookupByUUIDString(guest_id) root = et.fromstring(dom.XMLDesc(0)) interface = et.Element('interface') interface.set("type", "network") model = et.SubElement(interface, 'model') model.set("type", "virtio") if data.get("mac"): mac = et.SubElement(interface, 'mac') mac.set("address", str(data["mac"])) if data.get("network"): mac = et.SubElement(interface, 'source') mac.set("network", str(data["network"])) if data.get("name"): mac = et.SubElement(interface, 'alias') mac.set("name", str(data["name"])) dom.attachDeviceFlags(et.tostring(interface), 2) nw_interfaces = self.network_interface_list(guest_id) return {"id": nw_interfaces[-1]["id"]} @require_libvirt(True) def network_interface_info(self, guest_id, network_interface_id): dom = self.libvirt_connection.lookupByUUIDString(guest_id) root = et.fromstring(dom.XMLDesc(0)) nw_interfaces = self.network_interface_list(guest_id) for iface in nw_interfaces: if iface["id"] == network_interface_id: return iface raise EntityNotFound("Network interface", network_interface_id) def network_interface_update(self, guest_id, network_interface_id, data): dom = self.libvirt_connection.lookupByUUIDString(guest_id) root = et.fromstring(dom.XMLDesc(0)) # For each children of each interface, set the attributes to data # if the mac is the same of network_interface_id for iface in root.iter("interface"): for i in iface.getchildren(): if i.attrib.get("mac") == network_interface_id: for k,v in data: data.set(k,v) dom.updateDeviceFlags(et.tostring(iface), 2) def network_interface_delete(self, guest_id, network_interface_id): dom = self.libvirt_connection.lookupByUUIDString(guest_id) root = et.fromstring(dom.XMLDesc(0)) iface_id = "address" # defining the simplestack's libvirt id for iface in root.iter("interface"): for i in iface.getchildren(): if i.attrib.get(iface_id) == network_interface_id: dom.detachDeviceFlags(et.tostring(iface), 2) return {"id": network_interface_id} raise EntityNotFound("Network interface", network_interface_id) @require_libvirt(True) def snapshot_list(self, guest_id): dom = self.libvirt_connection.lookupByID(guest_id) snaps = [ self.libvirt_snapshot_info(s) for s in dom.snapshotListNames() ] return snaps def snapshot_create(self, guestname, name=None): raise FeatureNotImplemented() @require_libvirt(True) def snapshot_info(self, guestname, snapshot_name): dom = self.libvirt_connection.lookupByUUIDString(guest_id) snap = self.libvirt_get_snapshot(dom, snapshot_id) if snap: return self.libvirt_snapshot_info(snap) else: raise EntityNotFound("Snapshot", snapshot_id) @require_libvirt(True) def snapshot_delete(self, guest_id, snapshot_id): dom = self.libvirt_connection.lookupByUUIDString(guest_id) snap = self.libvirt_get_snapshot(dom, snapshot_id) snap.delete(0) @require_libvirt(True) def snapshot_revert(self, guest_id, snapshot_id): dom = self.libvirt_connection.lookupByUUIDString(guest_id) snap = self.libvirt_get_snapshot(dom, snapshot_id) dom.revertToSnapshot(snap) def tag_list(self, guest_id): raise FeatureNotImplemented() def tag_create(self, guest_id, tag_name): raise FeatureNotImplemented() def tag_delete(self, guest_id, tag_name): raise FeatureNotImplemented() def libvirt_vm_info(self, dom): infos = dom.info() return ( self.format_for.guest( dom.UUIDString(), dom.name(), infos[3], infos[1] / 1024, None, None, None, None, self.state_translation[infos[0]], None ) ) def libvirt_snapshot_info(self, snapshot): return ( self.format_for.snapshot( snapshot.get_description(), snapshot.name(), snapshot.get_state(), snapshot.get_path(), snapshot.get_create_time() ) ) def libvirt_get_snapshot(self, dom, snapshot_id): pass
class Stack(SimpleStack): state_translation = {"POWERED ON": "STARTED", "POWERED OFF": "STOPPED", "SUSPENDED": "PAUSED"} def __init__(self, poolinfo): self.connection = False self.poolinfo = poolinfo self.format_for = Formatter() self.connect() def libvirt_connect(self): self.libvirt_connection = libvirt.open( "esx://%s@%s/" % (self.poolinfo.get("username"), self.poolinfo.get("api_server")) ) def connect(self): self.connection = VIServer() self.connection.connect( self.poolinfo.get("api_server"), self.poolinfo.get("username"), self.poolinfo.get("password") ) return def pool_info(self): return self.format_for.pool(0, 0, self.poolinfo.get("api_server")) def guest_list(self): return [ {"id": self.connection.get_vm_by_path(path).properties.name} for path in self.connection.get_registered_vms(cluster=self.poolinfo.get("cluster")) ] def guest_info(self, guest_id): vm = self._vm_ref(guest_id) return self._vm_info(vm) def guest_shutdown(self, guest_id, force=False): vm = self._vm_ref(guest_id) if force: return vm.power_off() else: return vm.shutdown_guest() def guest_start(self, guest_id): vm = self._vm_ref(guest_id) return vm.power_on() def guest_reboot(self, guest_id, force=False): vm = self._vm_ref(guest_id) if force: return vm.reset() else: return vm.reboot_guest() def guest_suspend(self, guest_id): vm = self._vm_ref(guest_id) return vm.suspend() def guest_resume(self, guest_id): return self.guest_start(guest_id) def guest_update(self, guest_id, guestdata): vm = self._vm_ref(guest_id) vmware.update_vm(self.connection, vm, guestdata) return self._vm_info(self._vm_ref(guest_id)) def guest_delete(self, guest_id): vm = self._vm_ref(guest_id) vmware.delete_vm(self.connection, vm) def media_mount(self, guest_id, media_data): vm = self._vm_ref(guest_id) vmware.update_vm(self.connection, vm, {"iso": media_data}) def media_info(self, guest_id): vm = self._vm_ref(guest_id) media = vmware.get_cd(vm) if media.connectable.connected: return {"name": media.backing.fileName} else: return {"name": None} def network_interface_list(self, guest_id): vm = self._vm_ref(guest_id) vifs = vmware.get_network_interfaces(vm) return [self._network_interface_info(n) for n in vifs] def network_interface_info(self, guest_id, network_interface_id): vm = self._vm_ref(guest_id) vifs = vmware.get_network_interfaces(vm) for vif in vifs: if vif.macAddress == network_interface_id: return self._network_interface_info(vif) entity_info = "%s - on Guest %s" % (network_interface_id, guest_id) raise EntityNotFound("NetworkInterface", entity_info) def snapshot_list(self, guest_id): vm = self._vm_ref(guest_id) snaps = [self._snapshot_info(s) for s in vm.get_snapshots()] return snaps def snapshot_create(self, guest_id, snapshot_name=None): if not snapshot_name: snapshot_name = str(datetime.datetime.now()) vm = self._vm_ref(guest_id) snap = vmware.create_snapshot(self.connection, vm, snapshot_name) return self._snapshot_info(snap) def snapshot_info(self, guest_id, snapshot_id): vm = self._vm_ref(guest_id) snap = vmware.get_snapshot(vm, snapshot_id) if snap: return self._snapshot_info(snap) else: raise EntityNotFound("Snapshot", snapshot_id) def snapshot_revert(self, guest_id, snapshot_id): vm = self._vm_ref(guest_id) snap = vmware.get_snapshot(vm, snapshot_id) vmware.revert_to_snapshot(self.connection, vm, snap) def snapshot_delete(self, guest_id, snapshot_id): vm = self._vm_ref(guest_id) snap = vmware.get_snapshot(vm, snapshot_id) vmware.delete_snapshot(self.connection, vm, snap) def tag_list(self, guest_id): vm = self._vm_ref(guest_id) return vmware.get_tags(vm) def tag_create(self, guest_id, tag_name): vm = self._vm_ref(guest_id) tags = vmware.create_tag(tag_name, vm) vmware.update_vm(self.connection, vm, {"tags": tags}) return tags def tag_delete(self, guest_id, tag_name): vm = self._vm_ref(guest_id) tags = vmware.delete_tag(tag_name, vm) vmware.update_vm(self.connection, vm, {"tags": tags}) return tags def _vm_ref(self, vm_id): regex = r"[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}" if re.match(regex, vm_id, re.I): return vmware.get_vm_by_uuid(self.connection, vm_id) else: return self.connection.get_vm_by_name(vm_id) def _vm_info(self, vm): vm_info = vm.get_properties() return self.format_for.guest( vm.properties.config.uuid, vm_info.get("name"), vm_info.get("num_cpu"), vm_info.get("memory_mb"), vmware.get_disk_size(vm) / (1024 * 1024), vmware.get_vmi_driver(vm) is not None, vm.properties.guest.toolsStatus == "toolsOk", None, self.state_translation[vm.get_status()], None, ) def _network_interface_info(self, vif): return self.format_for.network_interface( vif.macAddress, vif.unitNumber, vif.macAddress, "", "", "", vif.backing.network.name ) def _snapshot_info(self, snapshot): return self.format_for.snapshot( snapshot.get_description(), snapshot.get_name(), snapshot.get_state(), snapshot.get_path(), snapshot.get_create_time(), )
class Stack(SimpleStack): state_translation = { "POWERED ON": "STARTED", "POWERED OFF": "STOPPED", "SUSPENDED": "PAUSED" } def __init__(self, poolinfo): self.connection = False self.poolinfo = poolinfo self.format_for = Formatter() self.connect() def libvirt_connect(self): self.libvirt_connection = libvirt.open( "esx://%s@%s/" % (self.poolinfo.get("username"), self.poolinfo.get("api_server"))) def connect(self): self.connection = VIServer() self.connection.connect(self.poolinfo.get("api_server"), self.poolinfo.get("username"), self.poolinfo.get("password")) return def pool_info(self): return (self.format_for.pool(0, 0, self.poolinfo.get("api_server"))) def guest_list(self): return [{ "id": self.connection.get_vm_by_path(path).properties.name } for path in self.connection.get_registered_vms( cluster=self.poolinfo.get('cluster'))] def guest_info(self, guest_id): vm = self._vm_ref(guest_id) return self._vm_info(vm) def guest_shutdown(self, guest_id, force=False): vm = self._vm_ref(guest_id) if force: return vm.power_off() else: return vm.shutdown_guest() def guest_start(self, guest_id): vm = self._vm_ref(guest_id) return vm.power_on() def guest_reboot(self, guest_id, force=False): vm = self._vm_ref(guest_id) if force: return vm.reset() else: return vm.reboot_guest() def guest_suspend(self, guest_id): vm = self._vm_ref(guest_id) return vm.suspend() def guest_resume(self, guest_id): return self.guest_start(guest_id) def guest_update(self, guest_id, guestdata): vm = self._vm_ref(guest_id) vmware.update_vm(self.connection, vm, guestdata) return self._vm_info(self._vm_ref(guest_id)) def guest_delete(self, guest_id): vm = self._vm_ref(guest_id) vmware.delete_vm(self.connection, vm) def media_mount(self, guest_id, media_data): vm = self._vm_ref(guest_id) vmware.update_vm(self.connection, vm, {"iso": media_data}) def media_info(self, guest_id): vm = self._vm_ref(guest_id) media = vmware.get_cd(vm) if media.connectable.connected: return {"name": media.backing.fileName} else: return {"name": None} def network_interface_list(self, guest_id): vm = self._vm_ref(guest_id) vifs = vmware.get_network_interfaces(vm) return [self._network_interface_info(n) for n in vifs] def network_interface_info(self, guest_id, network_interface_id): vm = self._vm_ref(guest_id) vifs = vmware.get_network_interfaces(vm) for vif in vifs: if vif.macAddress == network_interface_id: return self._network_interface_info(vif) entity_info = "%s - on Guest %s" % (network_interface_id, guest_id) raise EntityNotFound("NetworkInterface", entity_info) def snapshot_list(self, guest_id): vm = self._vm_ref(guest_id) snaps = [self._snapshot_info(s) for s in vm.get_snapshots()] return snaps def snapshot_create(self, guest_id, snapshot_name=None): if not snapshot_name: snapshot_name = str(datetime.datetime.now()) vm = self._vm_ref(guest_id) snap = vmware.create_snapshot(self.connection, vm, snapshot_name) return self._snapshot_info(snap) def snapshot_info(self, guest_id, snapshot_id): vm = self._vm_ref(guest_id) snap = vmware.get_snapshot(vm, snapshot_id) if snap: return self._snapshot_info(snap) else: raise EntityNotFound("Snapshot", snapshot_id) def snapshot_revert(self, guest_id, snapshot_id): vm = self._vm_ref(guest_id) snap = vmware.get_snapshot(vm, snapshot_id) vmware.revert_to_snapshot(self.connection, vm, snap) def snapshot_delete(self, guest_id, snapshot_id): vm = self._vm_ref(guest_id) snap = vmware.get_snapshot(vm, snapshot_id) vmware.delete_snapshot(self.connection, vm, snap) def tag_list(self, guest_id): vm = self._vm_ref(guest_id) return vmware.get_tags(vm) def tag_create(self, guest_id, tag_name): vm = self._vm_ref(guest_id) tags = vmware.create_tag(tag_name, vm) vmware.update_vm(self.connection, vm, {"tags": tags}) return tags def tag_delete(self, guest_id, tag_name): vm = self._vm_ref(guest_id) tags = vmware.delete_tag(tag_name, vm) vmware.update_vm(self.connection, vm, {"tags": tags}) return tags def _vm_ref(self, vm_id): regex = r'[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}' if re.match(regex, vm_id, re.I): return vmware.get_vm_by_uuid(self.connection, vm_id) else: return self.connection.get_vm_by_name(vm_id) def _vm_info(self, vm): vm_info = vm.get_properties() return (self.format_for.guest( vm.properties.config.uuid, vm_info.get('name'), vm_info.get('num_cpu'), vm_info.get('memory_mb'), vmware.get_disk_size(vm) / (1024 * 1024), vmware.get_vmi_driver(vm) is not None, vm.properties.guest.toolsStatus == "toolsOk", None, self.state_translation[vm.get_status()], None)) def _network_interface_info(self, vif): return (self.format_for.network_interface(vif.macAddress, vif.unitNumber, vif.macAddress, '', '', '', vif.backing.network.name)) def _snapshot_info(self, snapshot): return (self.format_for.snapshot(snapshot.get_description(), snapshot.get_name(), snapshot.get_state(), snapshot.get_path(), snapshot.get_create_time()))