#!/usr/bin/python ############################################################ # # Python client to download the VO XML file, # parse it and store the output into the HLRmon DB. # Giuseppe Misurelli # giuseppe.misurelli@cnaf.infn.it # Enrico Fattibene # enrico.fattibene@cnaf.infn.it # Marco Bencivenni # marco.bencivenni@cnaf.infn.it # Diego Michelotto # dmichelotto@fe.infn.it # Last modification: July 10th, 2011 # ############################################################ import sys, pycurl, StringIO, MySQLdb from xml.dom import minidom import time now = time.strftime("%Y-%m-%d %H:%M:%S") # Main function that execute all the functions defined def main(): # CIC portal endpoint to query for the xml file global cic_ep global result result = StringIO.StringIO() cic_ep = "http://operations-portal.in2p3.fr/xml/voIDCard/public/all/true" # Functions that will be invoked and executed curlDownload() #xmlParsing() mysqlSaving() # Performs curl download and store result into the global IO string variable def curlDownload(): c = pycurl.Curl() c.setopt(c.URL, cic_ep) #c.setopt(c.VERBOSE, 1) # Need verbosity? c.setopt(c.SSLCERT, "/etc/grid-security/hostcert.pem") c.setopt(c.SSLKEY, "/etc/grid-security/hostkey.pem") c.setopt(c.CAPATH, "/etc/grid-security/certificates/") c.setopt(c.WRITEFUNCTION, result.write) c.perform() c.close() return result # Performs xml parsing from the xml_doc string and save result into a dictionary handler def xmlParsing(): xml_doc = result.getvalue() try: doc = minidom.parseString(xml_doc) except minidom.DOMException, e: print e idcard = doc.getElementsByTagName("IDCard") # Initializing the handler dictionary handler_dict = {} # Iterating over the discipline tags and creating the mysql handler dictionary for ds in idcard: #print "NOME VO = " + ds.getAttributeNode("Name").value if ds.getAttributeNode("Name"): attrs_name = ds.attributes["Name"] # All the DISCIPLINE's tag children vos = ds.childNodes # Initializing the VO list vo_list = [] voms_list = [] for v in vos: if v.nodeName == "Discipline": attrs_voname = v.childNodes[0].nodeValue vo_list.append(str(attrs_voname)) if v.nodeName == "EnrollmentUrl": if v.childNodes: attrs_enroll = v.childNodes[0].nodeValue vo_list.append(str(attrs_enroll)) else: vo_list.append("") if v.nodeName == "AUP": if v.childNodes: attrs_AUP = v.childNodes[0].nodeValue stringa = str(attrs_AUP) stringa_result = stringa.replace("\'","\\\'") vo_list.append(stringa_result) else: vo_list.append("") if v.nodeName == "Description": if v.childNodes: attrs_description = v.childNodes[0].nodeValue stringa = str(attrs_description) stringa_result = stringa.replace("\'","\\\'") vo_list.append(stringa_result) else: vo_list.append("") if v.nodeName == "Middlewares": print "sono dentro a middelware" if v.getAttributeNode("ARC").value=="1": attrs_middleware = "ARC" if v.getAttributeNode("gLite").value=="1": attrs_middleware = "gLite" if v.getAttributeNode("UNICORE")=="1": attrs_middleware = "UNICORE" if v.getAttributeNode("GLOBUS")=="1": attrs_middleware = "GLOBUS" vo_list.append(str(attrs_middleware)) if v.nodeName == "gLiteConf": for voms0 in v.childNodes: if voms0.nodeName == "VOMSServers": x = 0 for voms in voms0.childNodes: if voms.nodeName == "VOMS_Server": for n in [1]: for vomsElement in voms.childNodes: if vomsElement.nodeName == "hostname": x = 1 attrs_enroll = vomsElement.childNodes[0].nodeValue voms_list.append(str(attrs_enroll)) hostport = [attrs_enroll , '8443'] vomshostport = ':'.join(hostport) if ds.getAttributeNode("Name").value != "grid-it": list = [vomshostport , 'voms' , ds.getAttributeNode("Name").value] else: list = [vomshostport , 'voms' , 'gridit'] vomsserver = '/'.join(list) vo_list.append(str(vomsserver)) if x == 0: vo_list.append("") handler_dict[str(attrs_name.value)] = vo_list #print handler_dict return handler_dict # Insert and updates handler entries into the HLRmon database def mysqlSaving(): callback = xmlParsing() # Open connection to the rocrep_hlr_dev db (JUST FOR TEST - THEN WILL BE PARAMETRZIZED) try: conn = MySQLdb.connect(host = 'fullyqualified.hostname', user = 'PortalUser', passwd = 'secret', db = 'PortalUser') # Required cursor to deal with the slq statements to insert values into the fullscale table cursor = conn.cursor() # Generating all the SQL query for the INSERT operations for disciplines in callback.keys(): select = "SELECT * FROM VO WHERE VO = '" + disciplines + "'" cursor.execute(select) if cursor.fetchone() == None: print "INSERISCO" sql = "INSERT into VO (VO, Host, Discipline, Description, VoManagerMail, EnrollementURL, AUP, Middleware, Insert_Time) VALUES" sql += "('" + disciplines +"', '" sql += callback[disciplines][5] + "', '" sql += callback[disciplines][0] + "', '" sql += callback[disciplines][3] + "', '" sql += "', '" sql += callback[disciplines][1] +"', '" sql += callback[disciplines][2] + "', '" sql += callback[disciplines][4] + "', '" sql += now + "');" cursor.execute(sql) else: print "AGGIORNO" sql = "UPDATE VO set " sql += "VO = '" + disciplines +"', " sql += "Host = '" + callback[disciplines][5] + "', " sql += "Discipline = '" + callback[disciplines][0] + "', " sql += "Description = '" + callback[disciplines][3] + "', " sql += "EnrollementURL = '" + callback[disciplines][1] + "', " sql += "AUP = '" + callback[disciplines][2] + "', " sql += "Middleware = '" + callback[disciplines][4] + "', " sql += "Insert_Time = '" + now sql += "' WHERE VO = '" + disciplines + "';" cursor.execute(sql) cursor.execute("DELETE FROM VO WHERE Host='';") cursor.close() conn.commit() conn.close() except MySQLdb.Error, e: print "Error %d: %s" % (e.args[0], e.args[1]) sys.exit(1) if __name__=="__main__": main()