adding provisioning feature
[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
10 def create_fed4fire_exp(name, groups, description, walltime, slice_id, exp_owner):
11     # create experiement with tag fed4fire
12     xmldescription='<experiment xmlns="http://api.bonfire-project.eu/doc/schemas/occi"><name>' + name + '</name><groups>' + groups + '</groups><description>' + description + '</description><walltime>' + walltime + '</walltime><status>ready</status><fed4fire><slice_id>' + slice_id + '</slice_id><exp_owner>' + exp_owner + '<exp_owner></fed4fire></experiment>'
13     postexp("https://api.integration.bonfire.grid5000.fr/experiments", xmldescription)
14
15 def postexp(url, xmldescription):
16     headers = {'content-type': 'application/vnd.bonfire+xml'}
17     r = requests.post(url, data=xmldescription, headers=headers, verify=False, auth=('nlebreto', 'GDRU_23tc$'))
18
19 def stop_vm(testbed, num_compute):
20     # compute bonfire to stopped state
21     url = "https://api.integration.bonfire.grid5000.fr/" + "locations/" + testbed + "/computes/" + num_compute
22     xmldescription = '<compute xmlns="http://api.bonfire-project.eu/doc/schemas/occi"><state>stopped</state></compute>'
23     headers = {'content-type': 'application/vnd.bonfire+xml'}
24     requests.put(url, data=xmldescription, headers=headers, verify=False, auth=('nlebreto', 'GDRU_23tc$'))
25
26 def provisioning(num_experiment):
27     # experiment bonfire to running status
28     url = "https://api.integration.bonfire.grid5000.fr/experiments/" + num_experiment
29     xmldescription = '<experiment xmlns="http://api.bonfire-project.eu/doc/schemas/occi"><status>running</status></experiment>'
30     headers = {'content-type': 'application/vnd.bonfire+xml'}
31     requests.put(url, data=xmldescription, headers=headers, verify=False, auth=('nlebreto', 'GDRU_23tc$'))
32
33 def callcurl(url):
34     r = requests.get(url, verify=False, auth=('nlebreto', 'GDRU_23tc$'))
35     if r.status_code == 200:
36         return r.text
37
38 def buildpagehttp(part1, part2):
39     res = []
40     for page in locations:
41         res.append(part1 + page  + "/" + part2)
42     return res
43
44 def boucle(itemname, xmltree, hashrspec, name):
45     for item in xmltree.findall(itemname):
46         hashrspec[name.text][itemname] = item.text
47
48 def jfedfeat(bonfires, pageurl):
49     pageforstatus = callcurl(pageurl)
50     xmlreduit = ET.fromstring(pageforstatus)
51     hashrspec = {}
52     itemshost = ["DISK_USAGE", "MEM_USAGE", "CPU_USAGE", "MAX_DISK", "MAX_MEM",  "MAX_CPU",
53                  "FREE_DISK",  "FREE_MEM",  "FREE_CPU", "FREE_MEM",  "FREE_CPU", "USED_DISK",
54                  "USED_MEM",   "USED_CPU",  "RUNNING_VMS"
55                 ]
56     # retrieve info for xml tree
57     for host in xmlreduit.findall('HOST'):
58         for name in host.findall('NAME'):
59             hashrspec[name.text] = {"name" : name.text}
60             for hostshare in host.findall('HOST_SHARE'):
61                 for itemshostname in itemshost:
62                     boucle(itemshostname, hostshare, hashrspec, name)
63     # jfed feature
64     for clef in hashrspec:
65         bonfires.append("<node component_manager_id=\"urn:publicid:IDN+topdomain+authority+cm" +
66                         " component_id=\"urn:publicid:IDN+topdomain:" + hashrspec[clef]["name"] +
67                         "\" component_name=" + hashrspec[clef]["name"] + "exclusive=\"false\">" +
68                         "  <location country=\"unknown\" longitude=\"123456\" latitude=\"654321\"/>" +
69                         "  <interface component_id=\"urn:publicid:IDN+ple+interface+node14312:eth0\"/>" +
70                         "  <available now=\"true\"/>" +
71                         "  <sliver_type name=\"" + hashrspec[clef]["name"] + "\">" +
72                         "      <planetlab:initscript name=\"" + hashrspec[clef]["name"]  + "\"/>" +
73                         "  </sliver_type>")
74         for infohost in itemshost:
75             bonfires.append("  <planetlab:attribute name=\"" + infohost + "\"value=\"" + hashrspec[clef][infohost]  + "\"/>")
76         bonfires.append("</node>")
77
78 def remove_needless_txt(txt):
79     txt=str(txt)
80     txt=txt.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n","\n")
81     txt=txt.replace("<?xml version='1.0' encoding='UTF-8'?>\n","\n")
82     return txt
83
84 def rsa_user_bonfire(testbed, num_compute):
85     url = "https://api.integration.bonfire.grid5000.fr/" + "locations/" + testbed + "/computes/" + num_compute
86     pagebonfirecompute = callcurl(url)
87     xmlreduit = ET.fromstring(pagebonfirecompute)
88     hash = {}
89     hash["url"] = url
90     for name in xmlreduit:
91         if name.tag == "{http://api.bonfire-project.eu/doc/schemas/occi}groups":
92            hash["name"] = name.text
93         for context in name:
94             if context.tag == "{http://api.bonfire-project.eu/doc/schemas/occi}authorized_keys":
95                hash["keys"] = context.text
96     return hash
97
98 # parameters
99 # ########################################################## #
100 locations = ["fr-inria", "be-ibbt", "uk-epcc"]
101 urlnetworks = buildpagehttp("https://api.integration.bonfire.grid5000.fr/locations/", "networks")
102 urlstorages = buildpagehttp("https://api.integration.bonfire.grid5000.fr/locations/", "storages")
103 urlcomputes = buildpagehttp("https://api.integration.bonfire.grid5000.fr/locations/", "computes")
104
105 # main code
106 # ########################################################## #
107
108 # list for all bonfire resources
109 def bonsources():
110     bonfires = []
111     bonfires.append("<RSpec>")
112     bonfires.append("<managed_experiments>")
113     manag_exp =  remove_needless_txt(callcurl("https://api.bonfire-project.eu/managed_experiments"))
114     bonfires.append(manag_exp)
115     bonfires.append("</managed_experiments><sites><machines>")
116     jfedfeat(bonfires, "http://frontend.bonfire.grid5000.fr/one-status.xml")
117     jfedfeat(bonfires, "http://bonfire.epcc.ed.ac.uk/one-status.xml")
118     jfedfeat(bonfires, "http://bonfire.psnc.pl/one-status.xml")
119     jfedfeat(bonfires, "http://nebulosus.rus.uni-stuttgart.de/one-status.xml")
120     bonfires.append("</machines><networks>")
121     for xmlnetworks in urlnetworks:
122         bonfires.append(remove_needless_txt(callcurl(xmlnetworks)))
123     bonfires.append("</networks><storages>")
124     for xmlstorages in urlstorages:
125         bonfires.append(remove_needless_txt(callcurl(xmlstorages)))
126     bonfires.append("</storages><instance_types><computes>")
127     for xmlcomputes in urlcomputes:
128         bonfires.append(remove_needless_txt(callcurl(xmlcomputes)))
129     bonfires.append("</computes></instance_types></sites><experiments>")
130     exp = callcurl("https://api.integration.bonfire.grid5000.fr/experiments")
131     rexp = remove_needless_txt(exp)
132     bonfires.append(rexp)
133     bonfires.append("</experiments><reservations>")
134     reserv = callcurl("https://api.integration.bonfire.grid5000.fr/locations/fr-inria/reservations")
135     rreserv = remove_needless_txt(reserv)
136     bonfires.append(rreserv)
137     bonfires.append("</reservations>")
138     bonfires.append("</RSpec>")
139     bonfires = "\n".join(bonfires)
140     bonfires = bonfires.replace("\n\n","")
141     return bonfires