forked from CMSCompOps/SiteReadiness
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IsSiteInSiteDB.py
executable file
·115 lines (97 loc) · 3.66 KB
/
IsSiteInSiteDB.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env python
""" @author: Josep Flix ([email protected]) """
import sys, xml.dom.minidom, os, datetime, time, pprint
from xml import xpath
from optparse import OptionParser
import httplib
import json
usage = "usage: (example) %prog -p /home/jflix/tmp2"
parser = OptionParser(usage=usage, version="%prog 1.0")
parser.add_option("-p", "--path_out", dest="path_out", help="Sets the PATH to store the produced data", metavar="PATH")
(options, args) = parser.parse_args()
if len(sys.argv) != 3:
parser.error("incorrect number of arguments. Check needed arguments with --help")
today=datetime.datetime.utcnow()
todaystamp=today.strftime("%Y-%m-%d")
todaystampfile=today.strftime("%Y-%m-%d %H:%M:%S")
todaystampfileSSB=today.strftime("%Y-%m-%d 00:00:01")
todaystamptofile=today.strftime("%Y%m%d_%H")
todaystamptotxt=today.strftime("%Y%m%d %H")
reptime="# - Report made on %s (UTC)\n" % todaystampfile
pathSiteDB=options.path_out + "/INPUTxmls"
fileSiteDB=pathSiteDB + "/sitedb.xml"
pathout= options.path_out + "/toSSB"
pathoutHTML= options.path_out + "/HTML"
pathoutPLOTS= options.path_out + "/PLOTS"
pathoutASCII= options.path_out + "/ASCii"
ssbout="%s/IsSiteInSiteDB_SSBfeed.txt" % pathout
if not os.path.exists(options.path_out):
os.makedirs(options.path_out)
if not os.path.exists(pathout):
os.makedirs(pathout)
if not os.path.exists(pathoutHTML):
os.makedirs(pathoutHTML)
if not os.path.exists(pathoutPLOTS):
os.makedirs(pathoutPLOTS)
if not os.path.exists(pathSiteDB):
os.makedirs(pathSiteDB)
if not os.path.exists(pathoutASCII):
os.makedirs(pathoutASCII)
#SiteDB_url="https://cmsweb.cern.ch/sitedb/reports/showXMLReport?reportid=naming_convention.ini"
#print "Getting the url %s" % SiteDB_url
#os.system("curl -ks -H 'Accept: text/xml' '%s' > %s" % (SiteDB_url,fileSiteDB))
#f=file(fileSiteDB,'r')
#t= xml.dom.minidom.parse(f)
#f.close()
def fetch_all_sites(url,api):
headers = {"Accept": "application/json"}
if 'X509_USER_PROXY' in os.environ:
print 'X509_USER_PROXY found'
conn = httplib.HTTPSConnection(url, cert_file = os.getenv('X509_USER_PROXY'), key_file = os.getenv('X509_USER_PROXY'))
elif 'X509_USER_CERT' in os.environ and 'X509_USER_KEY' in os.environ:
print 'X509_USER_CERT and X509_USER_KEY found'
conn = httplib.HTTPSConnection(url, cert_file = os.getenv('X509_USER_CERT'), key_file = os.getenv('X509_USER_KEY'))
elif os.path.isfile('/data/certs/servicecert.pem') and os.path.isfile('/data/certs/servicekey.pem'):
conn = httplib.HTTPSConnection(url, cert_file = '/data/certs/servicecert.pem', key_file = '/data/certs/servicekey.pem')
else:
print 'You need a valid proxy or cert/key files'
sys.exit()
r1=conn.request("GET",api, None, headers)
r2=conn.getresponse()
inputjson=r2.read()
jn = json.loads(inputjson)
conn.close()
return jn
url = "cmsweb.cern.ch"
api = "/sitedb/data/prod/federations-sites"
SiteDB_url = url + api
SiteDB_sites=[]
allSitesList = fetch_all_sites(url, api)
info = {}
for site in allSitesList['result']:
#print site[3]
SiteDB_sites.append(site[3])
#for urls in xpath.Evaluate('/report/result/item', t):
# info={}
# for target in xpath.Evaluate("cms", urls):
# if target.hasChildNodes():
# s=target.firstChild.nodeValue.encode('ascii')
# else:
# s=""
# if s not in SiteDB_sites:
# SiteDB_sites.append(s)
f=file(ssbout,'w')
f.write('# Is Site in SiteDB?\n')
f.write('# Information taken daily from SiteDB: ' + SiteDB_url + '\n')
f.write('#\n')
f.write(reptime)
f.write('#\n')
SiteDB_sites.sort()
for i in SiteDB_sites:
site=i
status="true"
color="green"
link = SiteDB_url
f.write('%s\t%s\t%s\t%s\t%s\n' % (todaystampfileSSB, site, status, color, link))
f.close()
sys.exit(0)