1 : |
agomez |
18 |
#
|
2 : |
|
|
# BonFIRE Virtual Clusters on Federated Clouds Demonstration Kit
|
3 : |
|
|
#
|
4 : |
|
|
# Copyright (c) Fundacion Centro Tecnologico de Supercomputacion de Galicia 2012
|
5 : |
|
|
#
|
6 : |
|
|
# License Apache Software
|
7 : |
|
|
#
|
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 httplib2
|
15 : |
|
|
import simplejson
|
16 : |
|
|
|
17 : |
|
|
|
18 : |
|
|
|
19 : |
|
|
class sub:
|
20 : |
|
|
"""Submit request. Input: usr, passwd, json, theurl
|
21 : |
|
|
Output: List of request"""
|
22 : |
|
|
|
23 : |
|
|
def _init_(self, user, passwd):
|
24 : |
|
|
self.user = user
|
25 : |
|
|
self.passwd = passwd
|
26 : |
|
|
|
27 : |
|
|
|
28 : |
|
|
def submit(self, theurl, exp):
|
29 : |
|
|
"""Submit JSON. Input: exp, user, passwd"""
|
30 : |
|
|
|
31 : |
|
|
h = httplib2.Http(disable_ssl_certificate_validation=True)
|
32 : |
|
|
h.add_credentials(self.user, self.passwd)
|
33 : |
|
|
jsondata = simplejson.dumps(exp)
|
34 : |
|
|
resp, content = h.request(theurl,
|
35 : |
|
|
'POST',
|
36 : |
|
|
jsondata,
|
37 : |
|
|
headers={'Content-Type': 'application/json'})
|
38 : |
|
|
return content
|
39 : |
|
|
|
40 : |
|
|
def submit_xml(self, theurl, name, instance, disk, net, loc):
|
41 : |
|
|
"""Add one compute defined in xml. Input: compute_xml, user, passwd"""
|
42 : |
|
|
|
43 : |
|
|
h = httplib2.Http(disable_ssl_certificate_validation=True)
|
44 : |
|
|
h.add_credentials(self.user, self.passwd)
|
45 : |
|
|
xmldata ='<?xml version="1.0" encoding="UTF-8"?>\
|
46 : |
|
|
<compute xmlns="http://api.bonfire-project.eu/doc/schemas/occi">\
|
47 : |
|
|
<name>'+name+'</name>\
|
48 : |
|
|
<instance_type>'+instance+'</instance_type>\
|
49 : |
|
|
<disk>\
|
50 : |
|
|
<storage href='+'"'+disk+'"'+' />\
|
51 : |
|
|
</disk>\
|
52 : |
|
|
<nic>\
|
53 : |
|
|
<network href='+'"'+net+'"'+' />\
|
54 : |
|
|
</nic>\
|
55 : |
|
|
<location href='+'"'+loc+'"'+' />\
|
56 : |
|
|
</compute>'
|
57 : |
|
|
resp, content = h.request(theurl,
|
58 : |
|
|
'POST',
|
59 : |
|
|
xmldata,
|
60 : |
|
|
headers={'Content-Type': 'application/vnd.bonfire+xml','Accept':'*/*'})
|
61 : |
|
|
#print resp, content, jsondata
|
62 : |
|
|
return content
|
63 : |
|
|
|
64 : |
|
|
|
65 : |
|
|
def log(self, url_log):
|
66 : |
|
|
"""GET the URL log. Input: url_log, user, passwd"""
|
67 : |
|
|
|
68 : |
|
|
h = httplib2.Http(disable_ssl_certificate_validation=True)
|
69 : |
|
|
h.add_credentials(self.user, self.passwd)
|
70 : |
|
|
resp, content = h.request(url_log,
|
71 : |
|
|
'GET',
|
72 : |
|
|
headers={'Content-Type': 'application/xml'})
|
73 : |
|
|
out='bad'
|
74 : |
|
|
while out == 'bad':
|
75 : |
|
|
try:
|
76 : |
|
|
content = simplejson.loads(content)
|
77 : |
|
|
except (simplejson.decoder.JSONDecodeError, ValueError):
|
78 : |
|
|
print 'No valid Json response from '+url_log, content
|
79 : |
|
|
resp, content = h.request(url_log,
|
80 : |
|
|
'GET',
|
81 : |
|
|
headers={'Content-Type': 'application/xml'})
|
82 : |
|
|
else:
|
83 : |
|
|
out='good'
|
84 : |
|
|
|
85 : |
|
|
return content
|
86 : |
|
|
|
87 : |
|
|
|
88 : |
|
|
|
89 : |
|
|
|
90 : |
|
|
def computes(self, url_log):
|
91 : |
|
|
"""Get info of experiment compute nodes. Input: url_log"""
|
92 : |
|
|
|
93 : |
|
|
h = httplib2.Http(disable_ssl_certificate_validation=True)
|
94 : |
|
|
h.add_credentials(self.user, self.passwd)
|
95 : |
|
|
resp, content = h.request(url_log,
|
96 : |
|
|
'GET',
|
97 : |
|
|
headers={'Accept':'*/*'})
|
98 : |
|
|
return content
|
99 : |
|
|
|
100 : |
|
|
|
101 : |
|
|
def delete(self, url_del):
|
102 : |
|
|
"""Delete experiment. Input: url_del, user, passwd"""
|
103 : |
|
|
|
104 : |
|
|
h = httplib2.Http(disable_ssl_certificate_validation=True)
|
105 : |
|
|
h.add_credentials(self.user, self.passwd)
|
106 : |
|
|
resp, content = h.request(url_del,
|
107 : |
|
|
'DELETE',
|
108 : |
|
|
headers={'Content-Type': 'application/xml'})
|
109 : |
|
|
return content
|
110 : |
|
|
|