#
# BonFIRE Virtual Clusters on Federated Clouds Demonstration Kit
#
# Copyright (c) Fundacion Centro Tecnologico de Supercomputacion de Galicia 2012
#
# License GPL Version 3
#
# The research leading to these results has received funding from
# the European Community's Seventh Frameqork Programme (FP7/2007-2013)
# under agreement number 257386
#
# This software is provided with ABSOLUTELY NO WARRANTY
#
import urllib2
import xml.dom.minidom
uri = None
user = None
password = None
experiment_id = None
hostname = None
location = None
wan_ip = None
def read_defaults(file_path):
global uri, user, password, experiment_id, hostname, wan_ip
bonfire = {}
with open(file_path) as file:
for line in file:
line = line.strip()
if line=="" or line.startswith('#'):
pass
else:
k,v=line.split('=',1)
bonfire[k]=v.strip('"').strip()
uri=bonfire['BONFIRE_URI']
user,password=bonfire['BONFIRE_CREDENTIALS'].split(':')
experiment_id=bonfire['BONFIRE_EXPERIMENT_ID']
hostname=bonfire['HOSTNAME']
location=bonfire['BONFIRE_PROVIDER']
wan_ip=bonfire['WAN_IP']
def _init_bonfire():
passman=urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, uri=uri, user=user, passwd=password)
auth_handler = urllib2.HTTPBasicAuthHandler(passman)
#opener = urllib2.build_opener(auth_handler,urllib2.HTTPHandler(debuglevel=1),urllib2.HTTPSHandler(debuglevel=1))
opener = urllib2.build_opener(auth_handler)
urllib2.install_opener(opener)
def make_request(resource, body = None, method = None):
_init_bonfire()
headers={'Accept':'*/*'}
if body != None:
headers['Content-Type'] = "application/vnd.bonfire+xml"
request = urllib2.Request(uri+resource,data=body,headers=headers)
if method != None:
request.get_method = lambda: method
response = urllib2.urlopen(request)
return response.read()
def get_experiment(id=experiment_id):
element = {}
dom = xml.dom.minidom.parseString(make_request('/experiments/'+id))
node = dom.getElementsByTagName("experiment")[0]
element['resource'] = node.getAttribute("href")
element['walltime'] = node.getElementsByTagName("walltime")[0].firstChild.nodeValue
element['name'] = node.getElementsByTagName("name")[0].firstChild.nodeValue
element['description'] = node.getElementsByTagName("description")[0].firstChild.nodeValue
element['id'] = node.getElementsByTagName("id")[0].firstChild.nodeValue
element['status'] = node.getElementsByTagName("status")[0].firstChild.nodeValue
element['created_at'] = node.getElementsByTagName("created_at")[0].firstChild.nodeValue
element['updated_at'] = node.getElementsByTagName("updated_at")[0].firstChild.nodeValue
element['computes'] = [get_compute(node.getAttribute("href")) for node in node.getElementsByTagName("compute")]
element['networks'] = [get_compute(node.getAttribute("href")) for node in node.getElementsByTagName("network")]
element['storages'] = [get_compute(node.getAttribute("href")) for node in node.getElementsByTagName("storage")]
return element
def get_compute(resource):
element = {}
dom = xml.dom.minidom.parseString(make_request(resource))
node = dom.getElementsByTagName("compute")[0]
element['resource'] = node.getAttribute("href")
element['name'] = node.getElementsByTagName("name")[0].firstChild.nodeValue
element['id'] = node.getElementsByTagName("id")[0].firstChild.nodeValue
element['hostname'] = node.getElementsByTagName("name")[0].firstChild.nodeValue +"-"+ node.getElementsByTagName("id")[0].firstChild.nodeValue
element['ip'] = node.getElementsByTagName("nic")[0].getElementsByTagName("ip")[0].firstChild.nodeValue
element['cpu'] = node.getElementsByTagName("cpu")[0].firstChild.nodeValue
element['memory'] = node.getElementsByTagName("memory")[0].firstChild.nodeValue
element['state'] = node.getElementsByTagName("state")[0].firstChild.nodeValue
return element
def get_network(resource):
element = {}
dom = xml.dom.minidom.parseString(make_request(resource))
node = dom.getElementsByTagName("network")[0]
element['resource'] = node.getAttribute("href")
element['name'] = node.getElementsByTagName("name")[0].firstChild.nodeValue
element['id'] = node.getElementsByTagName("id")[0].firstChild.nodeValue
return element
def get_storage(resource):
element = {}
dom = xml.dom.minidom.parseString(make_request(resource))
node = dom.getElementsByTagName("storage")[0]
element['resource'] = node.getAttribute("href")
element['name'] = node.getElementsByTagName("name")[0].firstChild.nodeValue
element['id'] = node.getElementsByTagName("id")[0].firstChild.nodeValue
return element
def create_compute(experiment_id,compute):
doc = xml.dom.minidom.Document()
c = doc.createElement("compute")
c.setAttribute("xmlns","http://api.bonfire-project.eu/doc/schemas/occi")
doc.appendChild(c)
name = doc.createElement("name")
name_text = doc.createTextNode(compute['name'])
name.appendChild(name_text)
c.appendChild(name)
inst = doc.createElement("instance_type")
inst_text = doc.createTextNode("small")
inst.appendChild(inst_text)
c.appendChild(inst)
disk = doc.createElement("disk")
for s in compute['storages']:
storage = doc.createElement("storage")
storage.setAttribute("href", s)
disk.appendChild(storage)
c.appendChild(disk)
nic = doc.createElement("nic")
for n in compute['networks']:
net = doc.createElement("network")
net.setAttribute("href", n)
nic.appendChild(net)
c.appendChild(nic)
loc = doc.createElement("location")
loc.setAttribute("href", compute['location'])
loc.setAttribute("rel","location")
c.appendChild(loc)
response = make_request('/experiments/'+experiment_id+'/computes',body=doc.toxml())
def find_locations():
dom = xml.dom.minidom.parseString(make_request('/locations'))
locations = []
for node in dom.getElementsByTagName("location"):
element = {}
element['resource'] = node.getAttribute("href")
element['name'] = node.getElementsByTagName("name")[0].firstChild.nodeValue
locations.append(element)
return locations
def find_networks(location):
dom = xml.dom.minidom.parseString(make_request('/locations/'+location+'/networks'))
networks = []
for node in dom.getElementsByTagName("network"):
element = {}
element['resource'] = node.getAttribute("href")
element['name'] = node.getAttribute("name")
networks.append(element)
return networks
def find_storages(location):
dom = xml.dom.minidom.parseString(make_request('/locations/'+location+'/storages'))
storages = []
for node in dom.getElementsByTagName("storage"):
element = {}
element['resource'] = node.getAttribute("href")
element['name'] = node.getAttribute("name")
storages.append(element)
return storages
def update_compute(resource,state):
doc = xml.dom.minidom.Document()
c = doc.createElement("compute")
c.setAttribute("xmlns","http://api.bonfire-project.eu/doc/schemas/occi")
doc.appendChild(c)
name = doc.createElement("state")
name_text = doc.createTextNode(state)
name.appendChild(name_text)
c.appendChild(name)
response = make_request(resource,body=doc.toxml(),method='PUT')
if __name__=="__main__":
test_uri="https://api.bonfire-project.eu:443"
uri = test_uri
user = "lmcarril"
password = "pegaso"
import sys
# experiment_id = sys.argv[1]
#read_defaults('/etc/defaults/bonfire')
_init_bonfire()
# print get_experiment(experiment_id)
print find_locations()
print find_networks("uk-epcc")
print find_storages("uk-epcc")
resource = "/locations/uk-epcc/computes/11701"
update_compute(resource,'CANCEL')