fist release sfa interaction with bonfire
[sfa.git] / sfa / client / bonfire.py
1 #tested with federica 18
2 #yum -y install python-pip
3 #pip install requests
4 import requests
5 import xml.etree.ElementTree as ET
6
7 # helping functions
8 # ########################################################## #
9 def callcurl(url):
10     r = requests.get(url, verify=False, auth=('nlebreto', 'GDRU_23tc$'))
11     if r.status_code == 200:
12         return r.text
13
14 def buildpagehttp(part1, part2):
15     res = []
16     for page in locations:
17         res.append(part1 + page  + "/" + part2)
18     return res
19
20 def boucle(itemname, xmltree, hashrspec, name):
21     for item in xmltree.findall(itemname):
22         hashrspec[name.text][itemname] = item.text
23
24 def jfedfeat(bonfires, pageurl):
25     pageforstatus = callcurl(pageurl)
26     xmlreduit = ET.fromstring(pageforstatus)
27     hashrspec = {}
28     itemshost = ["DISK_USAGE", "MEM_USAGE", "CPU_USAGE", "MAX_DISK", "MAX_MEM",  "MAX_CPU",
29                  "FREE_DISK",  "FREE_MEM",  "FREE_CPU", "FREE_MEM",  "FREE_CPU", "USED_DISK",
30                  "USED_MEM",   "USED_CPU",  "RUNNING_VMS"
31                 ]
32     # retrieve info for xml tree
33     for host in xmlreduit.findall('HOST'):
34         for name in host.findall('NAME'):
35             hashrspec[name.text] = {"name" : name.text}
36             for hostshare in host.findall('HOST_SHARE'):
37                 for itemshostname in itemshost:
38                     boucle(itemshostname, hostshare, hashrspec, name)
39     # jfed feature
40     for clef in hashrspec:
41         bonfires.append("<node component_manager_id=\"urn:publicid:IDN+topdomain+authority+cm" +
42                         " component_id=\"urn:publicid:IDN+topdomain:" + hashrspec[clef]["name"] +
43                         "\" component_name=" + hashrspec[clef]["name"] + "exclusive=\"false\">" +
44                         "  <location country=\"unknown\" longitude=\"123456\" latitude=\"654321\"/>" +
45                         "  <interface component_id=\"urn:publicid:IDN+ple+interface+node14312:eth0\"/>" +
46                         "  <available now=\"true\"/>" +
47                         "  <sliver_type name=\"" + hashrspec[clef]["name"] + "\">" +
48                         "      <planetlab:initscript name=\"" + hashrspec[clef]["name"]  + "\"/>" +
49                         "  </sliver_type>")
50         for infohost in itemshost:
51             bonfires.append("  <planetlab:attribute name=\"" + infohost + "\"value=\"" + hashrspec[clef][infohost]  + "\"/>")
52         bonfires.append("</node>")
53
54 def remove_needless_txt(txt):
55     txt=txt.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n","\n")
56     txt=txt.replace("<?xml version='1.0' encoding='UTF-8'?>\n","\n")
57     return txt
58
59 # parameters
60 # ########################################################## #
61 locations = ["fr-inria", "be-ibbt", "uk-epcc"]
62 urlnetworks = buildpagehttp("https://api.integration.bonfire.grid5000.fr/locations/", "networks")
63 urlstorages = buildpagehttp("https://api.integration.bonfire.grid5000.fr/locations/", "storages")
64 urlcomputes = buildpagehttp("https://api.integration.bonfire.grid5000.fr/locations/", "computes")
65
66 # main code
67 # ########################################################## #
68
69 # list for all bonfire resources
70 def bonsources():
71     bonfires = []
72     bonfires.append("<managed_experiments>")
73     manag_exp =  remove_needless_txt(callcurl("https://api.bonfire-project.eu/managed_experiments"))
74     bonfires.append(manag_exp)
75     bonfires.append("</managed_experiments><sites><machines>")
76     jfedfeat(bonfires, "http://frontend.bonfire.grid5000.fr/one-status.xml")
77 #   jfedfeat(bonfires, "http://bonfire.epcc.ed.ac.uk/logs/one-status.xml")
78     jfedfeat(bonfires, "http://bonfire.psnc.pl/one-status.xml")
79     jfedfeat(bonfires, "http://nebulosus.rus.uni-stuttgart.de/one-status.xml")
80     bonfires.append("</machines><networks>")
81     for xmlnetworks in urlnetworks:
82         bonfires.append(remove_needless_txt(callcurl(xmlnetworks)))
83     bonfires.append("</networks><storages>")
84     for xmlstorages in urlstorages:
85         bonfires.append(remove_needless_txt(callcurl(xmlstorages)))
86     bonfires.append("</storages><instance_types><computes>")
87     for xmlcomputes in urlcomputes:
88         bonfires.append(remove_needless_txt(callcurl(xmlcomputes)))
89     bonfires.append("</computes></instance_types></sites><experiments>")
90     exp = callcurl("https://api.integration.bonfire.grid5000.fr/experiments")
91     rexp = remove_needless_txt(exp)
92     bonfires.append(rexp)
93     bonfires.append("</experiments><reservations>")
94     reserv = callcurl("https://api.integration.bonfire.grid5000.fr/locations/fr-inria/reservations")
95     rreserv = remove_needless_txt(reserv)
96     bonfires.append(rreserv)
97     bonfires.append("</reservations>")
98     bonfires.append("</RSpec>")
99     bonfires = "\n".join(bonfires)
100     bonfires = bonfires.replace("\n\n","")
101     return bonfires