mirror of
https://github.com/valitydev/salt.git
synced 2024-11-07 00:55:19 +00:00
Moved libvirt module to virt, a lot of pylint fixes
This commit is contained in:
parent
d21aa7a3b5
commit
172265da07
@ -7,11 +7,6 @@ Work with vitual machines managed by libvirt
|
||||
|
||||
# Import Python Libs
|
||||
import os
|
||||
import sub_process
|
||||
import libvirt
|
||||
import subprocess
|
||||
import StringIO
|
||||
from xml.dom import minidom
|
||||
|
||||
# Import libvirt
|
||||
import libvirt
|
||||
@ -35,25 +30,25 @@ def __get_conn():
|
||||
# all vm layers supported by libvirt
|
||||
return libvirt.open("qemu:///system")
|
||||
|
||||
def _get_dom(vm):
|
||||
def _get_dom(vm_):
|
||||
'''
|
||||
Return a domain object for the named vm
|
||||
'''
|
||||
conn = __get_con()
|
||||
return conn.lookupByName(vm)
|
||||
conn = __get_conn()
|
||||
return conn.lookupByName(vm_)
|
||||
|
||||
def list_vms():
|
||||
'''
|
||||
Return a list of virtual machine names on the minion
|
||||
|
||||
CLI Example:
|
||||
salt '*' libvirt.list_vms
|
||||
salt '*' virt.list_vms
|
||||
'''
|
||||
conn = __get_con()
|
||||
conn = __get_conn()
|
||||
vms = []
|
||||
names = conn.listDefinedDomains()
|
||||
for name in names:
|
||||
vms.append(name)
|
||||
for name in names:
|
||||
vms.append(name)
|
||||
return vms
|
||||
|
||||
def vm_info():
|
||||
@ -67,19 +62,18 @@ def vm_info():
|
||||
'cputime' <int>}
|
||||
|
||||
CLI Example:
|
||||
salt '*' libvirt.vm_info
|
||||
salt '*' virt.vm_info
|
||||
'''
|
||||
info = {}
|
||||
conn = __get_conn()
|
||||
for vm in list_vms():
|
||||
dom = dom = conn.lookupByName(vm)
|
||||
for vm_ in list_vms():
|
||||
dom = _get_dom(vm_)
|
||||
raw = dom.info()
|
||||
info[vm] = {
|
||||
info[vm_] = {
|
||||
'state': VIRT_STATE_NAME_MAP.get(raw[0], 'unknown'),
|
||||
'maxMem': int(raw[1]),
|
||||
'mem': int(raw[2]),
|
||||
'cpu': raw[3],
|
||||
'cputime': int(data[4]),
|
||||
'cputime': int(raw[4]),
|
||||
}
|
||||
return info
|
||||
|
||||
@ -108,59 +102,59 @@ def freemem():
|
||||
to virtual machines on this node
|
||||
|
||||
CLI Example:
|
||||
salt '*' libvirt.freemem
|
||||
salt '*' virt.freemem
|
||||
'''
|
||||
conn = __get_conn()
|
||||
mem = conn.getInfo()[1]
|
||||
# Take off just enough to sustain the hypervisor
|
||||
mem -= 256
|
||||
for vm in list_vms():
|
||||
dom = conn.lookupByName(vm)
|
||||
for vm_ in list_vms():
|
||||
dom = conn.lookupByName(vm_)
|
||||
if dom.ID() > 0:
|
||||
mem -= vm.info()[2]/1024
|
||||
mem -= vm_.info()[2]/1024
|
||||
return mem
|
||||
|
||||
def shutdown(vm):
|
||||
def shutdown(vm_):
|
||||
'''
|
||||
Send a soft shutdown signal to the named vm
|
||||
|
||||
CLI Example:
|
||||
salt '*' libvirt.shutdown <vm name>
|
||||
salt '*' virt.shutdown <vm name>
|
||||
'''
|
||||
dom = _get_dom(vm)
|
||||
dom = _get_dom(vm_)
|
||||
dom.shutdown()
|
||||
return True
|
||||
|
||||
def pause(vm):
|
||||
def pause(vm_):
|
||||
'''
|
||||
Pause the named vm
|
||||
|
||||
CLI Example:
|
||||
salt '*' libvirt.pause <vm name>
|
||||
salt '*' virt.pause <vm name>
|
||||
'''
|
||||
dom = _get_dom(vm)
|
||||
dom = _get_dom(vm_)
|
||||
dom.suspend()
|
||||
return True
|
||||
|
||||
def unpause(vm):
|
||||
def unpause(vm_):
|
||||
'''
|
||||
Unpause the named vm
|
||||
|
||||
CLI Example:
|
||||
salt '*' libvirt.unpause <vm name>
|
||||
salt '*' virt.unpause <vm name>
|
||||
'''
|
||||
dom = _get_dom(vm)
|
||||
dom = _get_dom(vm_)
|
||||
dom.unpause()
|
||||
return True
|
||||
|
||||
def create(vm):
|
||||
def create(vm_):
|
||||
'''
|
||||
Start a defined domain
|
||||
|
||||
CLI Example:
|
||||
salt '*' libvirt.create <vm name>
|
||||
salt '*' virt.create <vm name>
|
||||
'''
|
||||
dom = _get_dom(vm)
|
||||
dom = _get_dom(vm_)
|
||||
dom.create()
|
||||
return True
|
||||
|
||||
@ -169,7 +163,7 @@ def create_xml_str(xml):
|
||||
Start a domain based on the xml passed to the function
|
||||
|
||||
CLI Example:
|
||||
salt '*' libvirt.create_xml_str <xml in string format>
|
||||
salt '*' virt.create_xml_str <xml in string format>
|
||||
'''
|
||||
conn = __get_conn()
|
||||
conn.createXML(xml)
|
||||
@ -180,33 +174,33 @@ def create_xml_path(path):
|
||||
Start a defined domain
|
||||
|
||||
CLI Example:
|
||||
salt '*' libvirt.create_xml_path <path to xml file on the node>
|
||||
salt '*' virt.create_xml_path <path to xml file on the node>
|
||||
'''
|
||||
if not os.path.isfile(path):
|
||||
return False
|
||||
return create_xml_str(open(path, 'r').read())
|
||||
|
||||
def destroy(vm):
|
||||
def destroy(vm_):
|
||||
'''
|
||||
Hard power down the virtual machine, this is equivelent to pulling the
|
||||
power
|
||||
|
||||
CLI Example:
|
||||
salt '*' libvirt.destroy <vm name>
|
||||
salt '*' virt.destroy <vm name>
|
||||
'''
|
||||
dom = _get_dom(vm)
|
||||
dom = _get_dom(vm_)
|
||||
dom.destroy()
|
||||
return True
|
||||
|
||||
def undefine(vm):
|
||||
def undefine(vm_):
|
||||
'''
|
||||
Remove a defined vm, this does not purge the virtual machine image, and
|
||||
this only works if the vm is powered down
|
||||
|
||||
CLI Example:
|
||||
salt '*' libvirt.undefine <vm name>
|
||||
salt '*' virt.undefine <vm name>
|
||||
'''
|
||||
dom = _get_dom(vm)
|
||||
dom = _get_dom(vm_)
|
||||
dom.undefine()
|
||||
return True
|
||||
|
Loading…
Reference in New Issue
Block a user