def doWork(self): # checking marketplace endpoint URL url_is_ok = Util.checkUrlExists(ENDPOINT_MKP, 30) if url_is_ok is True: req = urllib2.Request(ENDPOINT_MKP) response = urllib2.urlopen(req) content = response.read() xml = Util.etree_from_text(content) desc_nodes = xml.iter("{" + RDF + "}Description") all_desc = [] desc = {} for desc_node in desc_nodes: desc["description"] = desc_node.find('{' + DCTERMS + '}description').text desc["identifier"] = desc_node.find('{' + DCTERMS + '}identifier').text desc["creator"] = desc_node.find('{' + DCTERMS + '}creator').text desc["created"] = desc_node.find('{' + DCTERMS + '}created').text desc["os"] = desc_node.find('{' + SLTERMS + '}os').text desc["os-version"] = desc_node.find('{' + SLTERMS + '}os-version').text desc["os-arch"] = desc_node.find('{' + SLTERMS + '}os-arch').text # cast in str for NoneType object (otherwise, we should use try/Except) print "Description: " + str(desc["description"]) print "ID: " + str(desc["identifier"]) print "OS: " + str(desc["os"]), str(desc["os-version"]), "| Arch: " + str(desc["os-arch"]) print "Creator: " + str(desc["creator"]) print "Created at: " + str(desc["created"].replace("Z", "").split('T')) print "####\n" all_desc.append(desc)
def parseManifest(self, manifest): try: xml_tree = Util.etree_from_text(manifest) except SyntaxError, ex: raise ExecutionException('Unable to parse manifest: %s\nMANIFEST:\n%s' % (str(ex), manifest))
def listVms(self, showVmsFromAllUsers=False): fromAllUsers = -2 currentUserOnly = -3 if showVmsFromAllUsers: visibilitySwitch = fromAllUsers else: visibilitySwitch = currentUserOnly # Hack to retry on SSL errors. maxRetries = 3 retries = 0 while True: try: ret, info, _ = self._rpc.one.vmpool.info(self._sessionString, visibilitySwitch, -1, -1, -1) break except ssl.SSLError as e: retries += 1 t = strftime("%Y-%m-%d %H:%M:%S", gmtime()) Util.printDetail('SSL ERROR ENCOUNTERED (%s): %s' % (t, str(e))) if retries >= maxRetries: raise e if not ret: raise OneException(info) vmlist = Util.etree_from_text(info) for xml in vmlist.findall('VM'): self._addStateSummary(xml) return etree.tostring(vmlist)
def _getVmInfoAsXml(self, vmId): info = self._vmInfo(vmId) xml = Util.etree_from_text(info) self._addStateSummary(xml) return xml
def getCreateImageInfo(self, vmId): info = self._vmInfo(vmId) dom = Util.etree_from_text(info) createImage_dom = dom.find('TEMPLATE/CREATE_IMAGE') infos = {} for elem in createImage_dom: value = elem.text or '' infos[elem.tag] = value.strip().strip('"') return infos
def _vmDetail(self, vmId): res = self.cloud.getVmInfo(int(vmId)) vm = Util.etree_from_text(res) if self.portTranslationClient: self.portTranslationClient.addPortTranslationToSingleVmInfo(vm) info = CloudInfo() info.populate(vm) self._addHostnameElement(info) return info
def listNodes(self): nodes = self.cloud.listHosts() correct_nodes = [] for node in self._iterate(Util.etree_from_text(nodes)): # FIXME: remove this later. try: node.attribs['template_usedcpu'] = round(float(node.attribs['template_usedcpu']), 2) except: pass correct_nodes.append(node) return correct_nodes
def listVms(self, showVmsFromAllUsers=False): res = self.cloud.listVms(showVmsFromAllUsers) vms = Util.etree_from_text(res) self._addHostnameToVmsInfo(vms) if self.portTranslationClient: self.portTranslationClient.addPortTranslationToVmsInfo(vms) correct_vms = [] for vm in self._iterate(vms): self._addHostnameElement(vm) if vm.attribs['uname'].startswith('CN'): vm.attribs['uname'] = vm.attribs['uname'].replace("CN%3D", "") \ .replace("%2COU%3D", " OrgUnit:").replace("%2CO%3D", " Org:").replace("%2CC%3D", " Country:") \ .replace("+", " ") correct_vms.append(vm) return correct_vms
def _getVnetIdFromVnetName(self, vnet_name): vnet_info = self._getVnetInfoXml(vnet_name) try: vnet_tree = Util.etree_from_text(vnet_info) except SyntaxError, ex: raise ExecutionException("Unable to parse vnet %s info: %s" % (vnet_name, str(ex)))
def _getVmStateSummary(self, vmId): info = self._vmInfo(vmId) xml = Util.etree_from_text(info) return self._getOneVmStateFromXml(xml)
def getVmSource(self, vmId): info = self._vmInfo(vmId) xml = Util.etree_from_text(info) return xml.find('TEMPLATE/DISK/DISK_ID=0/../SOURCE').text
def getVmDiskSource(self, vmId, diskId): info = self._vmInfo(vmId) xml = Util.etree_from_text(info) sources = [x.find('SOURCE').text for x in xml.findall('TEMPLATE/DISK') if x.find('DISK_ID').text == str(diskId)] return sources[0]
def getVmInfo(self, vmId): info = self._vmInfo(vmId) xml = Util.etree_from_text(info) self._addStateSummary(xml) return etree.tostring(xml)
def _parseXml(xmlAsString): return Util.etree_from_text(xmlAsString)
def doWork(self): id = self.options.image # checking marketplace endpoint URL url_is_ok = Util.checkUrlExists(ENDPOINT_MKP, 30) if url_is_ok is True and id is not False: req = urllib2.Request(ENDPOINT_MKP) response = urllib2.urlopen(req) content = response.read() xml = Util.etree_from_text(content) desc_nodes = xml.iter("{" + RDF + "}Description") desc = {} for desc_node in desc_nodes: desc["identifier"] = desc_node.find('{' + DCTERMS + '}identifier').text if id == desc["identifier"]: desc["checksum"] = {} desc["checksum"]["val"] = [] desc["checksum"]["algo"] = [] desc["description"] = desc_node.find('{' + DCTERMS + '}description').text desc["creator"] = desc_node.find('{' + DCTERMS + '}creator').text desc["created"] = desc_node.find('{' + DCTERMS + '}created').text desc["valid"] = desc_node.find('{' + DCTERMS + '}valid').text desc["os"] = desc_node.find('{' + SLTERMS + '}os').text desc["os-version"] = desc_node.find('{' + SLTERMS + '}os-version').text desc["os-arch"] = desc_node.find('{' + SLTERMS + '}os-arch').text desc["version"] = desc_node.find('{' + SLTERMS + '}version').text desc["compression"] = desc_node.find('{' + DCTERMS + '}compression').text desc["location"] = desc_node.find('{' + SLTERMS + '}location').text desc["location"] = desc_node.find('{' + SLTERMS + '}location').text desc["format"] = desc_node.find('{' + DCTERMS + '}format').text desc["publisher"] = desc_node.find('{' + DCTERMS + '}publisher').text desc["hypervisor"] = desc_node.find('{' + SLTERMS + '}hypervisor').text for check in desc_node.findall('{' + SLREQ + '}checksum'): desc["checksum"]["algo"].append(check.find('{' + SLREQ + '}algorithm').text) desc["checksum"]["val"].append(check.find('{' + SLREQ + '}value').text) for endorsement in desc_node.findall('{' + SLREQ + '}endorsement'): for endorser in endorsement.findall('{' + SLREQ + '}endorser'): desc["email"] = endorser.find('{' + SLREQ + '}email').text # cast in str for None object (otherwise, I should use try/Except) print "Description: " + str(desc["description"]) print "ID: " + str(desc["identifier"]) print "Creator: " + str(desc["creator"]) print "Created at: " + str(desc["created"].replace("Z", "").split('T')) print "Validity: " + str(desc["valid"].replace("Z", "").split('T')) print "OS: " + str(desc["os"]), str(desc["os-version"]), "| Arch: " + str(desc["os-arch"]) print "Version: " + str(desc["version"]) print "Compression: " + str(desc["compression"]) print "Location: " + str(desc["location"]) print "Format: " + str(desc["format"]) print "Publisher: " + str(desc["publisher"]) print "Hypervisor: " + str(desc["hypervisor"]) print "Endorser: " + str(desc["email"]) for i in range(len(desc["checksum"]["algo"])): print str(desc["checksum"]["algo"][i]), str(desc["checksum"]["val"][i]) print "####\n"
def testXmlUnicode(self): utf = unicode('éñôøü', encoding='utf8') xml = "<root>%s</root>" % utf root = Util.etree_from_text(xml) result = root.text self.failUnlessEqual(utf, result)
def getVmNode(self, vmId): info = self._vmInfo(vmId) xml = Util.etree_from_text(info) return xml.find('HISTORY_RECORDS/HISTORY/HOSTNAME').text
def getVmOwner(self, vmId): info = self._vmInfo(vmId) xml = Util.etree_from_text(info) return xml.find('UNAME').text
def _nodeDetail(self, nodeId): res = self.cloud.getHostInfo(int(nodeId)) host = Util.etree_from_text(res) info = CloudInfo() info.populate(host) return info
def doWork(self): key_criteria = {"creator": "", "email": "", "os": "", "description": "", "regexp": "", "arch": ""} use_regexp = False if self.options.creator is not None: creator = self.options.creator key_criteria["creator"] = creator if self.options.email is not None: email = self.options.email key_criteria["email"] = email if self.options.os is not None: os = self.options.os key_criteria["os"] = os if self.options.description is not None: description = self.options.description key_criteria["description"] = description description_compile = None if self.options.regexp is not None: regexp = self.options.regexp use_regexp = True key_criteria["description"] = regexp description_compile = re.compile(regexp) if self.options.arch is not None: arch = self.options.arch key_criteria["arch"] = arch # checking marketplace endpoint URL url_is_ok = Util.checkUrlExists(ENDPOINT_MKP, 30) if url_is_ok is True: req = urllib2.Request(ENDPOINT_MKP) response = urllib2.urlopen(req) content = response.read() xml = Util.etree_from_text(content) desc_nodes = xml.iter("{" + RDF + "}Description") desc = {} found = 0 for desc_node in desc_nodes: if key_criteria["creator"] != "": desc["creator"] = desc_node.find('{' + DCTERMS + '}creator').text if key_criteria["creator"] == desc["creator"]: if found < 1: print "####" found += 1 print "CREATOR FOUND" if key_criteria["os"] != "": desc["os"] = desc_node.find('{' + SLTERMS + '}os').text if key_criteria["os"] == desc["os"]: if found < 1: print "####" found += 1 print "OS FOUND" if key_criteria["arch"] != "": desc["arch"] = desc_node.find('{' + SLTERMS + '}os-arch').text if key_criteria["arch"] == desc["arch"]: if found < 1: print "####" found += 1 print "ARCH FOUND" if key_criteria["description"] != "": desc["description"] = desc_node.find('{' + DCTERMS + '}description').text if use_regexp is False: if key_criteria["description"] == desc["description"]: if found < 1: print "####" found += 1 print "DESCRIPTION FOUND" else: if desc["description"] is not None and description_compile is not None: if description_compile.search(desc["description"]): if found < 1: print "####" found += 1 print "DESCRIPTION[REGEXP] FOUND" if key_criteria["email"] != "": for endorsement in desc_node.findall('{' + SLREQ + '}endorsement'): for endorser in endorsement.findall('{' + SLREQ + '}endorser'): desc["email"] = endorser.find('{' + SLREQ + '}email').text if key_criteria["email"] == desc["email"]: if found < 1: print "####" found += 1 print "EMAIL FOUND" if found >= 1: print "%d critera match your search for this image" % found print "####" self._print_image(desc_node) found = 0