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