def compress(data, compresslevel=9): ''' Returns the data compressed at gzip level compression. ''' buf = StringIO() with open_fileobj(buf, 'wb', compresslevel) as ogz: ogz.write(data) compressed = buf.getvalue() return compressed
def get_disks(vm_): ''' Return the disks of a named vm CLI Example:: salt '*' virt.get_disks <vm name> ''' disks = {} doc = minidom.parse(StringIO(get_xml(vm_))) for elem in doc.getElementsByTagName('disk'): sources = elem.getElementsByTagName('source') targets = elem.getElementsByTagName('target') if len(sources) > 0: source = sources[0] else: continue if len(targets) > 0: target = targets[0] else: continue if 'dev' in list(target.attributes) and 'file' in list(source.attributes): disks[target.getAttribute('dev')] = { 'file': source.getAttribute('file')} for dev in disks: try: disks[dev].update(yaml.safe_load(subprocess.Popen('qemu-img info ' + disks[dev]['file'], shell=True, stdout=subprocess.PIPE).communicate()[0])) except TypeError: disks[dev].update(yaml.safe_load('image: Does not exist')) return disks
def get_macs(vm_): ''' Return a list off MAC addresses from the named vm CLI Example:: salt '*' virt.get_macs <vm name> ''' macs = [] doc = minidom.parse(StringIO(get_xml(vm_))) for node in doc.getElementsByTagName("devices"): i_nodes = node.getElementsByTagName("interface") for i_node in i_nodes: for v_node in i_node.getElementsByTagName('mac'): macs.append(v_node.getAttribute('address')) return macs
def get_graphics(vm_): ''' Returns the information on vnc for a given vm CLI Example:: salt '*' virt.get_graphics <vm name> ''' out = {'autoport': 'None', 'keymap': 'None', 'listen': 'None', 'port': 'None', 'type': 'vnc'} xml = get_xml(vm_) ssock = StringIO(xml) doc = minidom.parse(ssock) for node in doc.getElementsByTagName("domain"): g_nodes = node.getElementsByTagName("graphics") for g_node in g_nodes: for key in g_node.attributes: out[key] = g_node.getAttribute(key) return out
def get_nics(vm_): ''' Return info about the network interfaces of a named vm CLI Example:: salt '*' virt.get_nics <vm name> ''' nics = {} doc = minidom.parse(StringIO(get_xml(vm_))) for node in doc.getElementsByTagName("devices"): i_nodes = node.getElementsByTagName("interface") for i_node in i_nodes: nic = {} nic['type'] = i_node.getAttribute('type') for v_node in i_node.getElementsByTagName('*'): if v_node.tagName == "mac": nic['mac'] = v_node.getAttribute('address') if v_node.tagName == "model": nic['model'] = v_node.getAttribute('type') # driver, source, and match can all have optional attributes if re.match('(driver|source|address)', v_node.tagName): temp = {} for key in v_node.attributes: temp[key] = v_node.getAttribute(key) nic[str(v_node.tagName)] = temp # virtualport needs to be handled separately, to pick up the # type attribute of the virtualport itself if v_node.tagName == "virtualport": temp = {} temp['type'] = v_node.getAttribute('type') for key in v_node.attributes: temp[key] = v_node.getAttribute(key) nic['virtualport'] = temp if 'mac' not in nic: continue nics[nic['mac']] = nic return nics
def uncompress(data): buf = StringIO(data) with open_fileobj(buf, 'rb') as igz: unc = igz.read() return unc