Skip to content

Commit

Permalink
Minor bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
micafer committed Dec 10, 2015
1 parent ccb1a1e commit 8c4e2dc
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 24 deletions.
11 changes: 7 additions & 4 deletions IM/ConfManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ def launch_ctxt_agent(self, vm, tasks):
Launch the ctxt agent to configure the specified tasks in the specified VM
"""
pid = None

tmp_dir = None
try:
ip = vm.getPublicIP()
if not ip:
Expand All @@ -263,8 +263,6 @@ def launch_ctxt_agent(self, vm, tasks):
ssh = self.inf.vm_master.get_ssh(retry = True)
ssh.sftp_mkdir(remote_dir)
ssh.sftp_put(conf_file, remote_dir + "/" + os.path.basename(conf_file))

shutil.rmtree(tmp_dir, ignore_errors=True)

if vm.configured is None:
(pid, _, _) = ssh.execute("nohup python_ansible " + Config.REMOTE_CONF_DIR + "/" + str(self.inf.id) + "/" + "/ctxt_agent.py "
Expand All @@ -281,6 +279,9 @@ def launch_ctxt_agent(self, vm, tasks):
except:
pid = None
ConfManager.logger.exception("Inf ID: " + str(self.inf.id) + ": Error launching the ansible process to configure VM with ID %s" % str(vm.im_id))
finally:
if tmp_dir:
shutil.rmtree(tmp_dir, ignore_errors=True)

# If the process is not correctly launched the configuration of this VM fails
if pid is None:
Expand Down Expand Up @@ -588,6 +589,7 @@ def configure_master(self):
* Copy the contextualization agent files
"""
success = True
tmp_dir = None
if not self.inf.ansible_configured:
success = False
cont = 0
Expand Down Expand Up @@ -630,7 +632,8 @@ def configure_master(self):
if not self.inf.ansible_configured: self.inf.ansible_configured = False
success = False
finally:
shutil.rmtree(tmp_dir, ignore_errors=True)
if tmp_dir:
shutil.rmtree(tmp_dir, ignore_errors=True)

if success:
self.inf.ansible_configured = True
Expand Down
4 changes: 3 additions & 1 deletion IM/InfrastructureInfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,5 +453,7 @@ def Contextualize(self, auth, vm_list = None):
if self.cm is None or not self.cm.isAlive():
self.cm = ConfManager.ConfManager(self,auth,max_ctxt_time)
self.cm.start()
else:
else:
# update the ConfManager auth
self.cm.auth = auth
self.cm.init_time = time.time()
57 changes: 38 additions & 19 deletions connectors/OCCI.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ def setIPs(self, vm, occi_res, auth_data):

def add_public_ip(self, vm, auth_data):
auth_header = self.get_auth_header(auth_data)
conn = None
try:
conn = self.get_http_connection(auth_data)
conn.putrequest('POST', "/link/networkinterface/")
Expand All @@ -241,7 +242,6 @@ def add_public_ip(self, vm, auth_data):
conn.endheaders(body)

resp = conn.getresponse()
self.delete_proxy(conn)
output = str(resp.read())
if resp.status != 201:
return (False, "Error adding public IP the VM: " + resp.reason + "\n" + output)
Expand All @@ -250,6 +250,8 @@ def add_public_ip(self, vm, auth_data):
except Exception:
self.logger.exception("Error connecting with OCCI server")
return (False, "Error connecting with OCCI server")
finally:
self.delete_proxy(conn)

def get_occi_attribute_value(self, occi_res, attr_name):
"""
Expand Down Expand Up @@ -288,12 +290,11 @@ def updateVMInfo(self, vm, auth_data):
headers = {'Accept': 'text/plain', 'Connection':'close'}
if auth:
headers.update(auth)

conn = None
try:
conn = self.get_http_connection(auth_data)
conn.request('GET', "/compute/" + vm.id, headers = headers)
resp = conn.getresponse()
self.delete_proxy(conn)

output = resp.read()
if resp.status == 404:
Expand Down Expand Up @@ -330,6 +331,8 @@ def updateVMInfo(self, vm, auth_data):
except Exception, ex:
self.logger.exception("Error connecting with OCCI server")
return (False, "Error connecting with OCCI server: " + str(ex))
finally:
self.delete_proxy(conn)


def gen_cloud_config(self, public_key, user = 'cloudadm', cloud_config_str = None):
Expand Down Expand Up @@ -357,12 +360,11 @@ def query_occi(self, auth_data):
headers = {'Accept': 'text/plain', 'Connection':'close'}
if auth:
headers.update(auth)

conn = None
try:
conn = self.get_http_connection(auth_data)
conn.request('GET', "/-/", headers = headers)
resp = conn.getresponse()
self.delete_proxy(conn)

output = resp.read()
#self.logger.debug(output)
Expand All @@ -375,6 +377,8 @@ def query_occi(self, auth_data):
except:
self.logger.exception("Error querying the OCCI server")
return ""
finally:
self.delete_proxy(conn)

def get_scheme(self, occi_info, category, ctype):
"""
Expand Down Expand Up @@ -456,6 +460,7 @@ def create_volume(self, size, name, auth_data):
returns the OCCI ID of the storage object
"""
conn = None
try:
auth_header = self.get_auth_header(auth_data)

Expand All @@ -479,8 +484,6 @@ def create_volume(self, size, name, auth_data):

output = resp.read()

self.delete_proxy(conn)

if resp.status != 201:
return False, resp.reason + "\n" + output
else:
Expand All @@ -492,6 +495,8 @@ def create_volume(self, size, name, auth_data):
except Exception, ex:
self.logger.exception("Error creating volume")
return False, str(ex)
finally:
self.delete_proxy(conn)

def delete_volume(self, storage_id, auth_data):
auth = self.get_auth_header(auth_data)
Expand All @@ -500,12 +505,11 @@ def delete_volume(self, storage_id, auth_data):
headers.update(auth)

self.logger.debug("Delete storage: %s" % storage_id)

conn = None
try:
conn = self.get_http_connection(auth_data)
conn.request('DELETE', "/storage/" + storage_id, headers = headers)
resp = conn.getresponse()
self.delete_proxy(conn)
output = str(resp.read())
if resp.status == 404:
self.logger.debug("It does not exist.")
Expand All @@ -518,6 +522,8 @@ def delete_volume(self, storage_id, auth_data):
except Exception:
self.logger.exception("Error connecting with OCCI server")
return (False, "Error connecting with OCCI server")
finally:
self.delete_proxy(conn)

def launch(self, inf, radl, requested_radl, num_vm, auth_data):
system = radl.systems[0]
Expand All @@ -539,7 +545,6 @@ def launch(self, inf, radl, requested_radl, num_vm, auth_data):

res = []
i = 0
conn = self.get_http_connection(auth_data)

public_key = system.getValue('disk.0.os.credentials.public_key')

Expand Down Expand Up @@ -583,10 +588,12 @@ def launch(self, inf, radl, requested_radl, num_vm, auth_data):

while i < num_vm:
volumes = {}
conn = None
try:
# First create the volumes
volumes = self.create_volumes(system, auth_data)

conn = self.get_http_connection(auth_data)
conn.putrequest('POST', "/compute/")
if auth_header:
conn.putheader(auth_header.keys()[0], auth_header.values()[0])
Expand Down Expand Up @@ -654,11 +661,11 @@ def launch(self, inf, radl, requested_radl, num_vm, auth_data):
res.append((False, "ERROR: " + str(ex)))
for volume_id in volumes.values():
self.delete_volume(volume_id, auth_data)
finally:
self.delete_proxy(conn)

i += 1

self.delete_proxy(conn)

return res

def get_volume_ids_from_radl(self, system):
Expand All @@ -677,7 +684,7 @@ def delete_volumes(self, vm, auth_data):
headers = {'Accept': 'text/plain', 'Connection':'close'}
if auth:
headers.update(auth)

conn = None
try:
conn = self.get_http_connection(auth_data)
conn.request('GET', "/compute/" + vm.id, headers = headers)
Expand All @@ -704,13 +711,15 @@ def delete_volumes(self, vm, auth_data):
except Exception, ex:
self.logger.exception("Error deleting volumes")
return (False, "Error deleting volumes " + str(ex))
finally:
self.delete_proxy(conn)

def finalize(self, vm, auth_data):
auth = self.get_auth_header(auth_data)
headers = {'Accept': 'text/plain', 'Connection':'close'}
if auth:
headers.update(auth)

conn = None
try:
conn = self.get_http_connection(auth_data)
conn.request('DELETE', "/compute/" + vm.id, headers = headers)
Expand All @@ -721,16 +730,18 @@ def finalize(self, vm, auth_data):
except Exception:
self.logger.exception("Error connecting with OCCI server")
return (False, "Error connecting with OCCI server")
finally:
self.delete_proxy(conn)

# now try to delete the volumes
self.delete_volumes(vm, auth_data)

self.delete_proxy(conn)
return (True, vm.id)


def stop(self, vm, auth_data):
auth_header = self.get_auth_header(auth_data)
conn = None
try:
conn = self.get_http_connection(auth_data)
conn.putrequest('POST', "/compute/" + vm.id + "?action=suspend")
Expand All @@ -745,7 +756,6 @@ def stop(self, vm, auth_data):
conn.endheaders(body)

resp = conn.getresponse()
self.delete_proxy(conn)
output = str(resp.read())
if resp.status != 200:
return (False, "Error stopping the VM: " + resp.reason + "\n" + output)
Expand All @@ -754,9 +764,12 @@ def stop(self, vm, auth_data):
except Exception:
self.logger.exception("Error connecting with OCCI server")
return (False, "Error connecting with OCCI server")
finally:
self.delete_proxy(conn)

def start(self, vm, auth_data):
auth_header = self.get_auth_header(auth_data)
conn = None
try:
conn = self.get_http_connection(auth_data)
conn.putrequest('POST', "/compute/" + vm.id + "?action=start")
Expand All @@ -771,7 +784,6 @@ def start(self, vm, auth_data):
conn.endheaders(body)

resp = conn.getresponse()
self.delete_proxy(conn)
output = str(resp.read())
if resp.status != 200:
return (False, "Error starting the VM: " + resp.reason + "\n" + output)
Expand All @@ -780,6 +792,8 @@ def start(self, vm, auth_data):
except Exception:
self.logger.exception("Error connecting with OCCI server")
return (False, "Error connecting with OCCI server")
finally:
self.delete_proxy(conn)

def alterVM(self, vm, radl, auth_data):
return (False, "Not supported")
Expand All @@ -795,12 +809,12 @@ def get_keystone_uri(occi, auth_data):
Contact the OCCI server to check if it needs to contact a keystone server.
It returns the keystone server URI or None.
"""
conn = None
try:
headers = {'Accept': 'text/plain', 'Connection':'close'}
conn = occi.get_http_connection(auth_data)
conn.request('HEAD', "/-/", headers = headers)
resp = conn.getresponse()
occi.delete_proxy(conn)
www_auth_head = resp.getheader('Www-Authenticate')
if www_auth_head and www_auth_head.startswith('Keystone uri'):
return www_auth_head.split('=')[1].replace("'","")
Expand All @@ -812,12 +826,15 @@ def get_keystone_uri(occi, auth_data):
except:
occi.logger.exception("Error contacting with the OCCI server.")
return None
finally:
occi.delete_proxy(conn)

@staticmethod
def get_keystone_token(occi, keystone_uri, auth):
"""
Contact the specified keystone server to return the token
"""
conn = None
try:
uri = uriparse(keystone_uri)
server = uri[1].split(":")[0]
Expand Down Expand Up @@ -874,4 +891,6 @@ def get_keystone_token(occi, keystone_uri, auth):
return tenant_token_id
except:
occi.logger.exception("Error obtaining Keystone Token.")
return None
return None
finally:
occi.delete_proxy(conn)

0 comments on commit 8c4e2dc

Please sign in to comment.