| 1 |
# Copyright: (c) 2007, 2012 Red Hat, Inc |
| 2 |
# Michael DeHaan <[email protected]> |
| 3 |
# Seth Vidal <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
from __future__ import (absolute_import, division, print_function) |
| 7 |
__metaclass__ = type |
| 8 |
|
| 9 |
|
| 10 |
try: |
| 11 |
import libvirt |
| 12 |
except ImportError: |
| 13 |
HAS_VIRT = False |
| 14 |
else: |
| 15 |
HAS_VIRT = True |
| 16 |
|
| 17 |
try: |
| 18 |
from lxml import etree |
| 19 |
except ImportError: |
| 20 |
HAS_XML = False |
| 21 |
else: |
| 22 |
HAS_XML = True |
| 23 |
|
| 24 |
|
| 25 |
VIRT_FAILED = 1 |
| 26 |
VIRT_SUCCESS = 0 |
| 27 |
VIRT_UNAVAILABLE = 2 |
| 28 |
|
| 29 |
VIRT_STATE_NAME_MAP = { |
| 30 |
0: 'running', |
| 31 |
1: 'running', |
| 32 |
2: 'running', |
| 33 |
3: 'paused', |
| 34 |
4: 'shutdown', |
| 35 |
5: 'shutdown', |
| 36 |
6: 'crashed', |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
class VMNotFound(Exception): |
| 41 |
pass |
| 42 |
|
| 43 |
|
| 44 |
class LibvirtConnection(object): |
| 45 |
|
| 46 |
def __init__(self, uri, module): |
| 47 |
|
| 48 |
self.module = module |
| 49 |
|
| 50 |
cmd = "uname -r" |
| 51 |
rc, stdout, stderr = self.module.run_command(cmd) |
| 52 |
|
| 53 |
if "xen" in stdout: |
| 54 |
conn = libvirt.open(None) |
| 55 |
elif "esx" in uri: |
| 56 |
auth = [[libvirt.VIR_CRED_AUTHNAME, |
| 57 |
libvirt.VIR_CRED_NOECHOPROMPT], [], None] |
| 58 |
conn = libvirt.openAuth(uri, auth) |
| 59 |
else: |
| 60 |
conn = libvirt.open(uri) |
| 61 |
|
| 62 |
if not conn: |
| 63 |
raise Exception("hypervisor connection failure") |
| 64 |
|
| 65 |
self.conn = conn |
| 66 |
|
| 67 |
def find_vm(self, vmid): |
| 68 |
""" |
| 69 |
Extra bonus feature: vmid = -1 returns a list of everything |
| 70 |
""" |
| 71 |
|
| 72 |
vms = self.conn.listAllDomains() |
| 73 |
|
| 74 |
if vmid == -1: |
| 75 |
return vms |
| 76 |
|
| 77 |
for vm in vms: |
| 78 |
if vm.name() == vmid: |
| 79 |
return vm |
| 80 |
|
| 81 |
raise VMNotFound("virtual machine %s not found" % vmid) |
| 82 |
|
| 83 |
def shutdown(self, vmid): |
| 84 |
return self.find_vm(vmid).shutdown() |
| 85 |
|
| 86 |
def pause(self, vmid): |
| 87 |
return self.suspend(vmid) |
| 88 |
|
| 89 |
def unpause(self, vmid): |
| 90 |
return self.resume(vmid) |
| 91 |
|
| 92 |
def suspend(self, vmid): |
| 93 |
return self.find_vm(vmid).suspend() |
| 94 |
|
| 95 |
def resume(self, vmid): |
| 96 |
return self.find_vm(vmid).resume() |
| 97 |
|
| 98 |
def create(self, vmid): |
| 99 |
return self.find_vm(vmid).create() |
| 100 |
|
| 101 |
def destroy(self, vmid): |
| 102 |
return self.find_vm(vmid).destroy() |
| 103 |
|
| 104 |
def undefine(self, vmid, flag): |
| 105 |
vm = self.find_vm(vmid) |
| 106 |
if flag & 32: |
| 107 |
self.delete_domain_volumes(vmid) |
| 108 |
return vm.undefineFlags(flag) |
| 109 |
|
| 110 |
def get_status2(self, vm): |
| 111 |
state = vm.info()[0] |
| 112 |
return VIRT_STATE_NAME_MAP.get(state, "unknown") |
| 113 |
|
| 114 |
def get_status(self, vmid): |
| 115 |
state = self.find_vm(vmid).info()[0] |
| 116 |
return VIRT_STATE_NAME_MAP.get(state, "unknown") |
| 117 |
|
| 118 |
def nodeinfo(self): |
| 119 |
return self.conn.getInfo() |
| 120 |
|
| 121 |
def get_type(self): |
| 122 |
return self.conn.getType() |
| 123 |
|
| 124 |
def get_xml(self, vmid): |
| 125 |
vm = self.conn.lookupByName(vmid) |
| 126 |
return vm.XMLDesc(0) |
| 127 |
|
| 128 |
def get_maxVcpus(self, vmid): |
| 129 |
vm = self.conn.lookupByName(vmid) |
| 130 |
return vm.maxVcpus() |
| 131 |
|
| 132 |
def get_maxMemory(self, vmid): |
| 133 |
vm = self.conn.lookupByName(vmid) |
| 134 |
return vm.maxMemory() |
| 135 |
|
| 136 |
def getFreeMemory(self): |
| 137 |
return self.conn.getFreeMemory() |
| 138 |
|
| 139 |
def get_autostart(self, vmid): |
| 140 |
vm = self.conn.lookupByName(vmid) |
| 141 |
return vm.autostart() |
| 142 |
|
| 143 |
def set_autostart(self, vmid, val): |
| 144 |
vm = self.conn.lookupByName(vmid) |
| 145 |
return vm.setAutostart(val) |
| 146 |
|
| 147 |
def define_from_xml(self, xml): |
| 148 |
return self.conn.defineXML(xml) |
| 149 |
|
| 150 |
def get_uuid(self, vmid): |
| 151 |
vm = self.conn.lookupByName(vmid) |
| 152 |
return vm.UUIDString() |
| 153 |
|
| 154 |
def get_interfaces(self, vmid): |
| 155 |
dom_xml = self.get_xml(vmid) |
| 156 |
root = etree.fromstring(dom_xml) |
| 157 |
interfaces = root.findall("./devices/interface") |
| 158 |
interface_type_map = { |
| 159 |
'network': 'NAT', |
| 160 |
'direct': 'macvtap', |
| 161 |
'bridge': 'bridge' |
| 162 |
} |
| 163 |
interface_counter = 0 |
| 164 |
interfaces_dict = {} |
| 165 |
interfaces_dict['network_interfaces'] = {} |
| 166 |
for interface in interfaces: |
| 167 |
interface_counter += 1 |
| 168 |
interface_type = interface.get('type') |
| 169 |
source = interface.find("source").get({ |
| 170 |
'bridge': 'bridge', |
| 171 |
'direct': 'dev', |
| 172 |
'network': 'network' |
| 173 |
}.get(interface_type)) |
| 174 |
mac_address = interface.find("mac").get("address") |
| 175 |
pci_bus = interface.find("address").get("bus") |
| 176 |
interface_info = { |
| 177 |
"type": interface_type_map.get(interface_type, interface_type), |
| 178 |
"mac": mac_address, |
| 179 |
"pci_bus": pci_bus, |
| 180 |
"source": source |
| 181 |
} |
| 182 |
interfaces_dict['network_interfaces'].update( |
| 183 |
{"interface_{0}".format(interface_counter): interface_info}) |
| 184 |
return interfaces_dict |
| 185 |
|
| 186 |
def delete_domain_volumes(self, vmid): |
| 187 |
dom_xml = self.get_xml(vmid) |
| 188 |
root = etree.fromstring(dom_xml) |
| 189 |
disk_objects = root.findall(".//disk[@type='file']/source") |
| 190 |
for disk in disk_objects: |
| 191 |
disk_path = disk.get('file') |
| 192 |
disk_volumes = self.conn.storageVolLookupByPath(disk_path) |
| 193 |
if disk_volumes: |
| 194 |
disk_volumes.delete() |