Log In | Get Help   
Home My Page Projects Code Snippets Project Openings BonFIRE VCOC Demonstration Kit
Summary Activity SCM Files Wiki
[bonfiredemokit] Annotation of /virt-cluster/vc/bonfire.py
[bonfiredemokit] / virt-cluster / vc / bonfire.py Repository:
ViewVC logotype

Annotation of /virt-cluster/vc/bonfire.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 14 - (view) (download) (as text)

1 : agomez 10 #
2 :     # BonFIRE Virtual Clusters on Federated Clouds Demonstration Kit
3 :     #
4 :     # Copyright (c) Fundacion Centro Tecnologico de Supercomputacion de Galicia 2012
5 :     #
6 : agomez 14 # License Apache Software
7 : agomez 10 #
8 :     # The research leading to these results has received funding from
9 :     # the European Community's Seventh Framework Programme (FP7/2007-2013)
10 :     # under agreement number 257386
11 :     #
12 :     # This software is provided with ABSOLUTELY NO WARRANTY
13 :     #
14 :     import urllib2
15 :     import xml.dom.minidom
16 :    
17 :     uri = None
18 :     user = None
19 :     password = None
20 :     experiment_id = None
21 :     hostname = None
22 :     location = None
23 :     wan_ip = None
24 :    
25 :     def read_defaults(file_path):
26 :     global uri, user, password, experiment_id, hostname, wan_ip
27 :     bonfire = {}
28 :     with open(file_path) as file:
29 :     for line in file:
30 :     line = line.strip()
31 :     if line=="" or line.startswith('#'):
32 :     pass
33 :     else:
34 :     k,v=line.split('=',1)
35 :     bonfire[k]=v.strip('"').strip()
36 :    
37 :     uri=bonfire['BONFIRE_URI']
38 :     user,password=bonfire['BONFIRE_CREDENTIALS'].split(':')
39 :     experiment_id=bonfire['BONFIRE_EXPERIMENT_ID']
40 :     hostname=bonfire['HOSTNAME']
41 :     location=bonfire['BONFIRE_PROVIDER']
42 :     wan_ip=bonfire['WAN_IP']
43 :    
44 :     def _init_bonfire():
45 :     passman=urllib2.HTTPPasswordMgrWithDefaultRealm()
46 :     passman.add_password(None, uri=uri, user=user, passwd=password)
47 :     auth_handler = urllib2.HTTPBasicAuthHandler(passman)
48 :     #opener = urllib2.build_opener(auth_handler,urllib2.HTTPHandler(debuglevel=1),urllib2.HTTPSHandler(debuglevel=1))
49 :     opener = urllib2.build_opener(auth_handler)
50 :     urllib2.install_opener(opener)
51 :    
52 :     def make_request(resource, body = None, method = None):
53 :     _init_bonfire()
54 :     headers={'Accept':'*/*'}
55 :     if body != None:
56 :     headers['Content-Type'] = "application/vnd.bonfire+xml"
57 :     request = urllib2.Request(uri+resource,data=body,headers=headers)
58 :     if method != None:
59 :     request.get_method = lambda: method
60 :     response = urllib2.urlopen(request)
61 :     return response.read()
62 :    
63 :     def get_experiment(id=experiment_id):
64 :     element = {}
65 :     dom = xml.dom.minidom.parseString(make_request('/experiments/'+id))
66 :     node = dom.getElementsByTagName("experiment")[0]
67 :     element['resource'] = node.getAttribute("href")
68 :     element['walltime'] = node.getElementsByTagName("walltime")[0].firstChild.nodeValue
69 :     element['name'] = node.getElementsByTagName("name")[0].firstChild.nodeValue
70 :     element['description'] = node.getElementsByTagName("description")[0].firstChild.nodeValue
71 :     element['id'] = node.getElementsByTagName("id")[0].firstChild.nodeValue
72 :     element['status'] = node.getElementsByTagName("status")[0].firstChild.nodeValue
73 :     element['created_at'] = node.getElementsByTagName("created_at")[0].firstChild.nodeValue
74 :     element['updated_at'] = node.getElementsByTagName("updated_at")[0].firstChild.nodeValue
75 :     element['computes'] = [get_compute(node.getAttribute("href")) for node in node.getElementsByTagName("compute")]
76 :     element['networks'] = [get_compute(node.getAttribute("href")) for node in node.getElementsByTagName("network")]
77 :     element['storages'] = [get_compute(node.getAttribute("href")) for node in node.getElementsByTagName("storage")]
78 :     return element
79 :    
80 :     def get_compute(resource):
81 :     element = {}
82 :     dom = xml.dom.minidom.parseString(make_request(resource))
83 :     node = dom.getElementsByTagName("compute")[0]
84 :     element['resource'] = node.getAttribute("href")
85 :     element['name'] = node.getElementsByTagName("name")[0].firstChild.nodeValue
86 :     element['id'] = node.getElementsByTagName("id")[0].firstChild.nodeValue
87 :     element['hostname'] = node.getElementsByTagName("name")[0].firstChild.nodeValue +"-"+ node.getElementsByTagName("id")[0].firstChild.nodeValue
88 :     element['ip'] = node.getElementsByTagName("nic")[0].getElementsByTagName("ip")[0].firstChild.nodeValue
89 :     element['cpu'] = node.getElementsByTagName("cpu")[0].firstChild.nodeValue
90 :     element['memory'] = node.getElementsByTagName("memory")[0].firstChild.nodeValue
91 :     element['state'] = node.getElementsByTagName("state")[0].firstChild.nodeValue
92 :     return element
93 :    
94 :     def get_network(resource):
95 :     element = {}
96 :     dom = xml.dom.minidom.parseString(make_request(resource))
97 :     node = dom.getElementsByTagName("network")[0]
98 :     element['resource'] = node.getAttribute("href")
99 :     element['name'] = node.getElementsByTagName("name")[0].firstChild.nodeValue
100 :     element['id'] = node.getElementsByTagName("id")[0].firstChild.nodeValue
101 :     return element
102 :    
103 :     def get_storage(resource):
104 :     element = {}
105 :     dom = xml.dom.minidom.parseString(make_request(resource))
106 :     node = dom.getElementsByTagName("storage")[0]
107 :     element['resource'] = node.getAttribute("href")
108 :     element['name'] = node.getElementsByTagName("name")[0].firstChild.nodeValue
109 :     element['id'] = node.getElementsByTagName("id")[0].firstChild.nodeValue
110 :     return element
111 :    
112 :     def create_compute(experiment_id,compute):
113 :     doc = xml.dom.minidom.Document()
114 :     c = doc.createElement("compute")
115 :     c.setAttribute("xmlns","http://api.bonfire-project.eu/doc/schemas/occi")
116 :     doc.appendChild(c)
117 :     name = doc.createElement("name")
118 :     name_text = doc.createTextNode(compute['name'])
119 :     name.appendChild(name_text)
120 :     c.appendChild(name)
121 :     inst = doc.createElement("instance_type")
122 :     inst_text = doc.createTextNode("small")
123 :     inst.appendChild(inst_text)
124 :     c.appendChild(inst)
125 :     disk = doc.createElement("disk")
126 :     for s in compute['storages']:
127 :     storage = doc.createElement("storage")
128 :     storage.setAttribute("href", s)
129 :     disk.appendChild(storage)
130 :     c.appendChild(disk)
131 :     nic = doc.createElement("nic")
132 :     for n in compute['networks']:
133 :     net = doc.createElement("network")
134 :     net.setAttribute("href", n)
135 :     nic.appendChild(net)
136 :     c.appendChild(nic)
137 :     loc = doc.createElement("location")
138 :     loc.setAttribute("href", compute['location'])
139 :     loc.setAttribute("rel","location")
140 :     c.appendChild(loc)
141 :    
142 :     response = make_request('/experiments/'+experiment_id+'/computes',body=doc.toxml())
143 :    
144 :     def find_locations():
145 :     dom = xml.dom.minidom.parseString(make_request('/locations'))
146 :     locations = []
147 :     for node in dom.getElementsByTagName("location"):
148 :     element = {}
149 :     element['resource'] = node.getAttribute("href")
150 :     element['name'] = node.getElementsByTagName("name")[0].firstChild.nodeValue
151 :     locations.append(element)
152 :     return locations
153 :    
154 :     def find_networks(location):
155 :     dom = xml.dom.minidom.parseString(make_request('/locations/'+location+'/networks'))
156 :     networks = []
157 :     for node in dom.getElementsByTagName("network"):
158 :     element = {}
159 :     element['resource'] = node.getAttribute("href")
160 :     element['name'] = node.getAttribute("name")
161 :     networks.append(element)
162 :     return networks
163 :    
164 :     def find_storages(location):
165 :     dom = xml.dom.minidom.parseString(make_request('/locations/'+location+'/storages'))
166 :     storages = []
167 :     for node in dom.getElementsByTagName("storage"):
168 :     element = {}
169 :     element['resource'] = node.getAttribute("href")
170 :     element['name'] = node.getAttribute("name")
171 :     storages.append(element)
172 :     return storages
173 :    
174 :     def update_compute(resource,state):
175 :     doc = xml.dom.minidom.Document()
176 :     c = doc.createElement("compute")
177 :     c.setAttribute("xmlns","http://api.bonfire-project.eu/doc/schemas/occi")
178 :     doc.appendChild(c)
179 :     name = doc.createElement("state")
180 :     name_text = doc.createTextNode(state)
181 :     name.appendChild(name_text)
182 :     c.appendChild(name)
183 :     response = make_request(resource,body=doc.toxml(),method='PUT')
184 :    
185 :     if __name__=="__main__":
186 :     test_uri="https://api.bonfire-project.eu:443"
187 :     uri = test_uri
188 :     user = "lmcarril"
189 :     password = "pegaso"
190 :     import sys
191 :     # experiment_id = sys.argv[1]
192 :     #read_defaults('/etc/defaults/bonfire')
193 :     _init_bonfire()
194 :     # print get_experiment(experiment_id)
195 :     print find_locations()
196 :     print find_networks("uk-epcc")
197 :     print find_storages("uk-epcc")
198 :     resource = "/locations/uk-epcc/computes/11701"
199 :     update_compute(resource,'CANCEL')
200 :    

root@forge.cesga.es
ViewVC Help
Powered by ViewVC 1.0.0  

Powered By FusionForge