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 1 - (view) (download) (as text)

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

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

Powered By FusionForge