r1.4 adding more information for sfa plugin
[sfa.git] / sfa / bonfire / bonfire.py
1 #!/usr/bin/python
2 # -*- coding:utf-8 -*-
3 #yum -y install python-pip
4 #pip install requests
5 import requests
6 import xml.etree.ElementTree as ET
7 import subprocess
8 import time
9
10 # module for bonfire to connect with sfa (following the Rspec)
11 # inspired by the following documenation :
12 # https://svn.planet-lab.org/wiki/SfaDeveloperDummyTutorial#RunningSFAinDummyflavour
13
14 # 1) list all the resources  of bonfire from sfa's point of view
15 # python -c 'import bonfire; print bonfire.bonsources()'
16
17 # 2) retrieve the url, the name and the key that will currently use by sfa for a compute N°3656 located at fr-inria
18 # python -c 'import bonfire; print bonfire.rsa_user_bonfire("fr-inria", "3656")'
19
20 # 3) create a new user and slice for sfa wrap
21 # python -c 'import bonfire; print bonfire.new_user_slice()'
22
23 # 4) changing the status to running status for the experiment 2911
24 # python -c 'import bonfire; print bonfire.provisioning("2911")'
25
26 # 5) stop virtual machine n°3756  at fr-inira testbed
27 # python -c 'import bonfire; print bonfire.stop_vm("fr-inria", "3756")'
28
29 # 6) allocation : create an experiment bonfire with slice information
30 # python -c 'import bonfire; print bonfire.allocate("nlebreto", "nlebreto", "tdes", "125", "topdomain.dummy.nicolasi", "https://api.integration.bonfire.grid5000.fr/experiments")'
31
32 # 7) remove slice or key 
33 # python -c 'import bonfire; print bonfire.remove_slice("topdomain.dummy.alice_slice")'
34
35 # 8) attach slice to a user
36 # python -c 'import bonfire; print bonfire.create_slice_attach_user("topdomain.dummy.alice")'
37   
38 # ########################################################## #
39 # ########################################################## #
40  
41 # create a slice and attach a specific user to it
42 def create_slice_attach_user(user_slice):
43     call = "sfa.py add -x {0}_slice -t slice -r {0}@dummy.net".format(user_slice)
44     callcreateslice =  subprocess.Popen(call, shell=True)
45
46 # remove slice or key
47 def remove_slice(name):
48     cmdremove    = "sfaadmin.py reg remove {0}".format(name)
49     removeaction = subprocess.Popen(cmdremove, shell=True)
50
51 # show specific credential of a slice    
52 def show_slice_credential(slice_name):
53     path = "/root/.sfi/{0}.slice.cred".format(slice_name)
54     tree = ET.parse(path)
55     root = tree.getroot()
56     hash = {}
57 # hash["slice_native"] = ET.tostring(root)
58     for target in root.findall('credential'):
59         hash["slice_user_urn"] = target.find('owner_urn').text
60         hash["slice_urn"] = target.find('target_urn').text
61         hash["slice_native"] = target.find('serial').text
62     return hash
63
64 # create a bonfire experiment from a sfa point of view
65 def allocate(user_name, groups, description, walltime, slice_name):
66     hash ={}
67     hash = show_slice_credential(slice_name)
68     create_fed4fire_exp(user_name, groups, description, walltime, hash["slice_urn"], hash["slice_user_urn"], hash["slice_native"])
69     
70
71
72 # retrieve the url, the name and the key that will currently use by sfa
73 def rsa_user_bonfire(testbed, num_compute):
74     url = "https://api.integration.bonfire.grid5000.fr/" + "locations/" + testbed + "/computes/" + num_compute
75     pagebonfirecompute = callcurl(url)
76     xmlreduit = ET.fromstring(pagebonfirecompute)
77     hash = {}
78     hash["url"] = url
79     for name in xmlreduit:
80         if name.tag == "{http://api.bonfire-project.eu/doc/schemas/occi}groups":
81            hash["name"] = name.text
82         for context in name:
83             if context.tag == "{http://api.bonfire-project.eu/doc/schemas/occi}authorized_keys":
84                hash["keys"] = context.text
85     return hash 
86
87 # create a new user and slice for sfa wrap
88 def new_user_slice():
89     n = rsa_user_bonfire("fr-inria", "3656")
90     #url = n["url"] + "." + n["name"]
91     # fix to do add -k id_rsa.pub (pb key convert)
92     url = "topdomain.dummy." + n["name"]
93     txtcreateuser = "sfaadmin.py reg register -x {0} -t user -e {1}@dummy.net".format(url, n["name"])
94     createusersfa = subprocess.Popen(txtcreateuser, shell=True)
95     #slice = n["url"] + "." + n["name"] + "_" + n["name"]
96     slice = "topdomain.dummy." + n["name"] + "_slice"
97     txtslice = "sfaadmin.py reg register -x {0} -t slice -r {1}".format(slice, url)
98     createslice = subprocess.Popen(txtslice, shell=True)
99
100 # create a experiment bonfire with the slice urn and the experiment owner 
101 def create_fed4fire_exp(name, groups, description, walltime, slice_urn, slice_user_urn, slice_native, url_experiment_bonfire):
102     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><slice_urn>' + slice_urn + '</slice_urn><slice_usr_urn>' + slice_user_urn + '<slice_usr_urn><slice_native>' + slice_native + '</slice_native></experiment>'
103     postexp(url_experiment_bonfire, xmldescription)
104
105 # simple post method for request
106 def postexp(url, xmldescription):
107     headers = {'content-type': 'application/vnd.bonfire+xml'}
108     r = requests.post(url, data=xmldescription, headers=headers, verify=False, auth=('nlebreto', 'GDRU_23tc$'))
109
110 # stop a virtual machine for bonfire 
111 # changing the state to stopped state
112 def stop_vm(testbed, num_compute):
113     url = "https://api.integration.bonfire.grid5000.fr/" + "locations/" + testbed + "/computes/" + num_compute
114     xmldescription = '<compute xmlns="http://api.bonfire-project.eu/doc/schemas/occi"><state>stopped</state></compute>'
115     headers = {'content-type': 'application/vnd.bonfire+xml'}
116     requests.put(url, data=xmldescription, headers=headers, verify=False, auth=('nlebreto', 'GDRU_23tc$'))
117
118 # provisioning : set a bonfire's experiment to running  
119 # changing the status to running status
120 def provisioning(num_experiment):
121     url = "https://api.integration.bonfire.grid5000.fr/experiments/" + num_experiment
122     xmldescription = '<experiment xmlns="http://api.bonfire-project.eu/doc/schemas/occi"><status>running</status></experiment>'
123     headers = {'content-type': 'application/vnd.bonfire+xml'}
124     requests.put(url, data=xmldescription, headers=headers, verify=False, auth=('nlebreto', 'GDRU_23tc$'))
125
126 # retrieving the url, the name and the keys for a specific compute 
127 def rsa_user_bonfire(testbed, num_compute):
128     url = "https://api.integration.bonfire.grid5000.fr/" + "locations/" + testbed + "/computes/" + num_compute
129     pagebonfirecompute = callcurl(url)
130     xmlreduit = ET.fromstring(pagebonfirecompute)
131     hash = {}
132     hash["url"] = url
133     for name in xmlreduit:
134         if name.tag == "{http://api.bonfire-project.eu/doc/schemas/occi}groups":
135            hash["name"] = name.text
136         for context in name:
137             if context.tag == "{http://api.bonfire-project.eu/doc/schemas/occi}authorized_keys":
138                hash["keys"] = context.text
139     return hash 
140
141 # do a curl request  
142 def callcurl(url):
143     r = requests.get(url, verify=False, auth=('nlebreto', 'GDRU_23tc$'))
144     if r.status_code == 200:
145         return r.text
146         
147 # create the url page 
148 def buildpagehttp(part1, part2, locations):
149     res = []
150     for page in locations:
151         res.append(part1 + page  + "/" + part2)
152     return res
153
154 def boucle(itemname, xmltree, hashrspec, name):
155     for item in xmltree.findall(itemname):
156         hashrspec[name.text][itemname] = item.text
157         
158 # method to list all information from testbeds
159 def jfedfeat(bonfires, pageurl):
160     pageforstatus = callcurl(pageurl)
161     xmlreduit = ET.fromstring(pageforstatus)
162     hashrspec = {}
163     itemshost = ["DISK_USAGE", "MEM_USAGE", "CPU_USAGE", "MAX_DISK", "MAX_MEM",  "MAX_CPU",
164                  "FREE_DISK",  "FREE_MEM",  "FREE_CPU", "FREE_MEM",  "FREE_CPU", "USED_DISK",
165                  "USED_MEM",   "USED_CPU",  "RUNNING_VMS"
166                 ]
167     # retrieve info for xml tree
168     for host in xmlreduit.findall('HOST'):
169         for name in host.findall('NAME'):
170             hashrspec[name.text] = {"name" : name.text}
171             for hostshare in host.findall('HOST_SHARE'):
172                 for itemshostname in itemshost:
173                     boucle(itemshostname, hostshare, hashrspec, name)
174
175  # jfed feature
176     for clef in hashrspec:
177         bonfires.append("<node component_manager_id=\"urn:publicid:IDN+topdomain+authority+cm" +
178                         " component_id=\"urn:publicid:IDN+topdomain:" + hashrspec[clef]["name"] +
179                         "\" component_name=" + hashrspec[clef]["name"] + "exclusive=\"false\">" +
180                         "  <location country=\"unknown\" longitude=\"123456\" latitude=\"654321\"/>" +
181                         "  <interface component_id=\"urn:publicid:IDN+ple+interface+node14312:eth0\"/>" +
182                         "  <available now=\"true\"/>" +
183                         "  <sliver_type name=\"" + hashrspec[clef]["name"] + "\">" +
184                         "      <bonfire:initscript name=\"" + hashrspec[clef]["name"]  + "\"/>" +
185                         "  </sliver_type>")
186         for infohost in itemshost:
187             bonfires.append("  <bonfire:attribute name=\"" + infohost + "\"value=\"" + hashrspec[clef][infohost]  + "\"/>")
188         bonfires.append("</node>")
189
190 # remove the useless xml tag version 
191 def remove_needless_txt(txt):
192     txt=str(txt)
193     txt=txt.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n","\n")
194     txt=txt.replace("<?xml version='1.0' encoding='UTF-8'?>\n","\n")
195     return txt
196
197 # list all bonfire resources following the sfa specification
198 def bonsources():
199         # parameters
200     locations = ["fr-inria", "be-ibbt", "uk-epcc"]
201     urlnetworks = buildpagehttp("https://api.integration.bonfire.grid5000.fr/locations/", "networks", locations)
202     urlstorages = buildpagehttp("https://api.integration.bonfire.grid5000.fr/locations/", "storages", locations)
203     urlcomputes = buildpagehttp("https://api.integration.bonfire.grid5000.fr/locations/", "computes", locations)
204     # main code
205     bonfires = []
206     generatedtime =  time.strftime("%FT%T%Z")
207     sfabegin = "<RSpec type=\"SFA\" generated=" + generatedtime + "\">"
208     bonfires.append("<?xml version=\"1.0\"?>")
209     bonfires.append(sfabegin)
210     bonfires.append("<managed_experiments>")
211     manag_exp =  remove_needless_txt(callcurl("https://api.bonfire-project.eu/managed_experiments"))
212     bonfires.append(manag_exp)
213     bonfires.append("</managed_experiments><sites><machines>")
214     jfedfeat(bonfires, "http://frontend.bonfire.grid5000.fr/one-status.xml")
215     jfedfeat(bonfires, "http://bonfire.epcc.ed.ac.uk/one-status.xml")
216     jfedfeat(bonfires, "http://bonfire.psnc.pl/one-status.xml")
217     jfedfeat(bonfires, "http://nebulosus.rus.uni-stuttgart.de/one-status.xml")
218     bonfires.append("</machines><networks>")
219     for xmlnetworks in urlnetworks:
220         bonfires.append(remove_needless_txt(callcurl(xmlnetworks)))
221     bonfires.append("</networks><storages>")
222     for xmlstorages in urlstorages:
223         bonfires.append(remove_needless_txt(callcurl(xmlstorages)))
224     bonfires.append("</storages><instance_types><computes>")
225     for xmlcomputes in urlcomputes:
226         bonfires.append(remove_needless_txt(callcurl(xmlcomputes)))
227     bonfires.append("</computes></instance_types></sites><experiments>")
228     exp = callcurl("https://api.integration.bonfire.grid5000.fr/experiments")
229     rexp = remove_needless_txt(exp)
230     bonfires.append(rexp)
231     bonfires.append("</experiments><reservations>")
232     reserv = callcurl("https://api.integration.bonfire.grid5000.fr/locations/fr-inria/reservations")
233     rreserv = remove_needless_txt(reserv)
234     bonfires.append(rreserv)
235     bonfires.append("</reservations>")
236     bonfires.append("</RSpec>")
237     bonfires = "\n".join(bonfires)
238     bonfires = bonfires.replace("\n\n","")
239     return bonfires