From 172265da070234f3138c98193be5105f69f4afdf Mon Sep 17 00:00:00 2001 From: Thomas S Hatch Date: Wed, 16 Mar 2011 21:32:52 -0600 Subject: [PATCH] Moved libvirt module to virt, a lot of pylint fixes --- salt/modules/{libvirt.py => virt.py} | 78 +++++++++++++--------------- 1 file changed, 36 insertions(+), 42 deletions(-) rename salt/modules/{libvirt.py => virt.py} (74%) diff --git a/salt/modules/libvirt.py b/salt/modules/virt.py similarity index 74% rename from salt/modules/libvirt.py rename to salt/modules/virt.py index 72287011c3..0e2cb9c75f 100644 --- a/salt/modules/libvirt.py +++ b/salt/modules/virt.py @@ -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' } 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 + salt '*' virt.shutdown ''' - 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 + salt '*' virt.pause ''' - 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 + salt '*' virt.unpause ''' - 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 + salt '*' virt.create ''' - 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 + salt '*' virt.create_xml_str ''' 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 + salt '*' virt.create_xml_path ''' 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 + salt '*' virt.destroy ''' - 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 + salt '*' virt.undefine ''' - dom = _get_dom(vm) + dom = _get_dom(vm_) dom.undefine() return True