def get_graphics(vm_): ''' Returns the information on vnc for a given vm CLI Example: .. code-block:: bash 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.keys(): out[key] = g_node.getAttribute(key) return out
def get_disk_devs(vm_): doc = minidom.parse(_StringIO(get_xml(vm_))) disks = [] for elem in doc.getElementsByTagName('disk'): targets = elem.getElementsByTagName('target') target = targets[0] disks.append(target.getAttribute('dev')) 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_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.keys(): out[key] = g_node.getAttribute(key) return out
def get_nics(vm_): ''' Return info about the network interfaces of a named vm CLI Example: .. code-block:: bash 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') if v_node.tagName == 'target': nic['target'] = v_node.getAttribute('dev') # 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.keys(): 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.keys(): temp[key] = v_node.getAttribute(key) nic['virtualport'] = temp if 'mac' not in nic: continue nics[nic['mac']] = nic return nics
def get_nics(vm_): """ Return info about the network interfaces of a named vm CLI Example: .. code-block:: bash 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") if v_node.tagName == "target": nic["target"] = v_node.getAttribute("dev") # 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.keys(): 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.keys(): temp[key] = v_node.getAttribute(key) nic["virtualport"] = temp if "mac" not in nic: continue nics[nic["mac"]] = nic return nics
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.keys(): out[key] = g_node.getAttribute(key) return out
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 target.hasAttribute('dev'): qemu_target = '' if source.hasAttribute('file'): qemu_target = source.getAttribute('file') elif source.hasAttribute('dev'): qemu_target = source.getAttribute('dev') elif source.hasAttribute('protocol') and \ source.hasAttribute('name'): # For rbd network qemu_target = '%s:%s' % ( source.getAttribute('protocol'), source.getAttribute('name')) if qemu_target: disks[target.getAttribute('dev')] = {\ 'file': qemu_target} for dev in disks: try: output = [] qemu_output = subprocess.Popen(['qemu-img', 'info', disks[dev]['file']], shell=False, stdout=subprocess.PIPE).communicate()[0] snapshots = False columns = None lines = qemu_output.strip().split('\n') for line in lines: if line.startswith('Snapshot list:'): snapshots = True continue elif snapshots: if line.startswith('ID'): # Do not parse table headers line = line.replace('VM SIZE', 'VMSIZE') line = line.replace('VM CLOCK', 'TIME VMCLOCK') columns = re.split(r'\s+', line) columns = [c.lower() for c in columns] output.append('snapshots:') continue fields = re.split(r'\s+', line) for i, field in enumerate(fields): sep = ' ' if i == 0: sep = '-' output.append( '{0} {1}: "{2}"'.format( sep, columns[i], field ) ) continue output.append(line) output = '\n'.join(output) disks[dev].update(yaml.safe_load(output)) except TypeError: disks[dev].update(yaml.safe_load('image: Does not exist')) return disks
def get_disks(vm_): ''' Return the disks of a named vm CLI Example: .. code-block:: bash 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 target.hasAttribute('dev'): qemu_target = '' if source.hasAttribute('file'): qemu_target = source.getAttribute('file') elif source.hasAttribute('dev'): qemu_target = source.getAttribute('dev') elif source.hasAttribute('protocol') and \ source.hasAttribute('name'): # For rbd network qemu_target = '%s:%s' % ( source.getAttribute('protocol'), source.getAttribute('name')) if qemu_target: disks[target.getAttribute('dev')] = { 'file': qemu_target} for dev in disks: try: hypervisor = __salt__['config.get']('libvirt:hypervisor', 'kvm') if hypervisor not in ['qemu', 'kvm']: break output = [] qemu_output = subprocess.Popen(['qemu-img', 'info', disks[dev]['file']], shell=False, stdout=subprocess.PIPE).communicate()[0] snapshots = False columns = None lines = qemu_output.strip().split('\n') for line in lines: if line.startswith('Snapshot list:'): snapshots = True continue # If this is a copy-on-write image, then the backing file # represents the base image # # backing file: base.qcow2 (actual path: /var/shared/base.qcow2) elif line.startswith('backing file'): matches = re.match(r'.*\(actual path: (.*?)\)', line) if matches: output.append('backing file: {0}'.format(matches.group(1))) continue elif snapshots: if line.startswith('ID'): # Do not parse table headers line = line.replace('VM SIZE', 'VMSIZE') line = line.replace('VM CLOCK', 'TIME VMCLOCK') columns = re.split(r'\s+', line) columns = [c.lower() for c in columns] output.append('snapshots:') continue fields = re.split(r'\s+', line) for i, field in enumerate(fields): sep = ' ' if i == 0: sep = '-' output.append( '{0} {1}: "{2}"'.format( sep, columns[i], field ) ) continue output.append(line) output = '\n'.join(output) disks[dev].update(yaml.safe_load(output)) except TypeError: disks[dev].update(yaml.safe_load('image: Does not exist')) return disks
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 target.hasAttribute('dev'): qemu_target = '' if source.hasAttribute('file'): qemu_target = source.getAttribute('file') elif source.hasAttribute('dev'): qemu_target = source.getAttribute('dev') elif source.hasAttribute('protocol') and \ source.hasAttribute('name'): # For rbd network qemu_target = '%s:%s' % (source.getAttribute('protocol'), source.getAttribute('name')) if qemu_target: disks[target.getAttribute('dev')] = {\ 'file': qemu_target} for dev in disks: try: output = [] qemu_output = subprocess.Popen( ['qemu-img', 'info', disks[dev]['file']], shell=False, stdout=subprocess.PIPE).communicate()[0] snapshots = False columns = None lines = qemu_output.strip().split('\n') for line in lines: if line.startswith('Snapshot list:'): snapshots = True continue elif snapshots: if line.startswith('ID'): # Do not parse table headers line = line.replace('VM SIZE', 'VMSIZE') line = line.replace('VM CLOCK', 'TIME VMCLOCK') columns = re.split('\s+', line) columns = [c.lower() for c in columns] output.append('snapshots:') continue fields = re.split('\s+', line) for i, field in enumerate(fields): sep = ' ' if i == 0: sep = '-' output.append('{0} {1}: "{2}"'.format( sep, columns[i], field)) continue output.append(line) output = '\n'.join(output) disks[dev].update(yaml.safe_load(output)) except TypeError: disks[dev].update(yaml.safe_load('image: Does not exist')) return disks
def get_disks(vm_): ''' Return the disks of a named vm CLI Example: .. code-block:: bash 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 target.hasAttribute('dev'): qemu_target = '' if source.hasAttribute('file'): qemu_target = source.getAttribute('file') elif source.hasAttribute('dev'): qemu_target = source.getAttribute('dev') elif source.hasAttribute('protocol') and \ source.hasAttribute('name'): # For rbd network qemu_target = '%s:%s' % (source.getAttribute('protocol'), source.getAttribute('name')) if qemu_target: disks[target.getAttribute('dev')] = {'file': qemu_target} for dev in disks: try: hypervisor = __salt__['config.get']('libvirt:hypervisor', 'kvm') if hypervisor not in ['qemu', 'kvm']: break output = [] qemu_output = subprocess.Popen( ['qemu-img', 'info', disks[dev]['file']], shell=False, stdout=subprocess.PIPE).communicate()[0] snapshots = False columns = None lines = qemu_output.strip().split('\n') for line in lines: if line.startswith('Snapshot list:'): snapshots = True continue # If this is a copy-on-write image, then the backing file # represents the base image # # backing file: base.qcow2 (actual path: /var/shared/base.qcow2) elif line.startswith('backing file'): matches = re.match(r'.*\(actual path: (.*?)\)', line) if matches: output.append('backing file: {0}'.format( matches.group(1))) continue elif snapshots: if line.startswith('ID'): # Do not parse table headers line = line.replace('VM SIZE', 'VMSIZE') line = line.replace('VM CLOCK', 'TIME VMCLOCK') columns = re.split(r'\s+', line) columns = [c.lower() for c in columns] output.append('snapshots:') continue fields = re.split(r'\s+', line) for i, field in enumerate(fields): sep = ' ' if i == 0: sep = '-' output.append('{0} {1}: "{2}"'.format( sep, columns[i], field)) continue output.append(line) output = '\n'.join(output) disks[dev].update(yaml.safe_load(output)) except TypeError: disks[dev].update(yaml.safe_load('image: Does not exist')) return disks
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 target.hasAttribute("dev"): qemu_target = "" if source.hasAttribute("file"): qemu_target = source.getAttribute("file") elif source.hasAttribute("dev"): qemu_target = source.getAttribute("dev") elif source.hasAttribute("protocol") and source.hasAttribute("name"): # For rbd network qemu_target = "%s:%s" % (source.getAttribute("protocol"), source.getAttribute("name")) if qemu_target: disks[target.getAttribute("dev")] = {"file": qemu_target} for dev in disks: try: hypervisor = __salt__["config.get"]("libvirt:hypervisor", "qemu") if hypervisor not in ["qemu", "kvm"]: break output = [] qemu_output = subprocess.Popen( ["qemu-img", "info", disks[dev]["file"]], shell=False, stdout=subprocess.PIPE ).communicate()[0] snapshots = False columns = None lines = qemu_output.strip().split("\n") for line in lines: if line.startswith("Snapshot list:"): snapshots = True continue elif snapshots: if line.startswith("ID"): # Do not parse table headers line = line.replace("VM SIZE", "VMSIZE") line = line.replace("VM CLOCK", "TIME VMCLOCK") columns = re.split(r"\s+", line) columns = [c.lower() for c in columns] output.append("snapshots:") continue fields = re.split(r"\s+", line) for i, field in enumerate(fields): sep = " " if i == 0: sep = "-" output.append('{0} {1}: "{2}"'.format(sep, columns[i], field)) continue output.append(line) output = "\n".join(output) disks[dev].update(yaml.safe_load(output)) except TypeError: disks[dev].update(yaml.safe_load("image: Does not exist")) return disks