Skip to content

Commit

Permalink
Code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
micafer committed Jul 28, 2015
1 parent 5e54c65 commit 7f66922
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 42 deletions.
76 changes: 40 additions & 36 deletions IM/ConfManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,43 +549,47 @@ def configure_master(self):
"""
success = True
if not self.inf.ansible_configured:
try:
ConfManager.logger.info("Inf ID: " + str(self.inf.id) + ": Start the contextualization process.")

ssh = self.inf.vm_master.get_ssh(retry=True)
# Activate tty mode to avoid some problems with sudo in REL
ssh.tty = True

# configuration dir os th emaster node to copy all the contextualization files
tmp_dir = tempfile.mkdtemp()
# Now call the ansible installation process on the master node
configured_ok = self.configure_ansible(ssh, tmp_dir)

if not configured_ok:
ConfManager.logger.error("Inf ID: " + str(self.inf.id) + ": Error in the ansible installation process")
success = False
cont = 0
while not success and cont < Config.PLAYBOOK_RETRIES:
cont += 1
try:
ConfManager.logger.info("Inf ID: " + str(self.inf.id) + ": Start the contextualization process.")

ssh = self.inf.vm_master.get_ssh(retry=True)
# Activate tty mode to avoid some problems with sudo in REL
ssh.tty = True

# configuration dir os th emaster node to copy all the contextualization files
tmp_dir = tempfile.mkdtemp()
# Now call the ansible installation process on the master node
configured_ok = self.configure_ansible(ssh, tmp_dir)

if not configured_ok:
ConfManager.logger.error("Inf ID: " + str(self.inf.id) + ": Error in the ansible installation process")
if not self.inf.ansible_configured: self.inf.ansible_configured = False
else:
ConfManager.logger.info("Inf ID: " + str(self.inf.id) + ": Ansible installation finished successfully")

remote_dir = Config.REMOTE_CONF_DIR
ConfManager.logger.debug("Inf ID: " + str(self.inf.id) + ": Copy the contextualization agent files")
ssh.sftp_mkdir(remote_dir)
files = []
files.append((Config.IM_PATH + "/SSH.py",remote_dir + "/SSH.py"))
files.append((Config.IM_PATH + "/ansible/ansible_callbacks.py", remote_dir + "/ansible_callbacks.py"))
files.append((Config.IM_PATH + "/ansible/ansible_launcher.py", remote_dir + "/ansible_launcher.py"))
files.append((Config.CONTEXTUALIZATION_DIR + "/ctxt_agent.py", remote_dir + "/ctxt_agent.py"))
ssh.sftp_put_files(files)

success = configured_ok

except Exception, ex:
ConfManager.logger.exception("Inf ID: " + str(self.inf.id) + ": Error in the ansible installation process")
self.inf.add_cont_msg("Error in the ansible installation process: " + str(ex))
if not self.inf.ansible_configured: self.inf.ansible_configured = False
else:
ConfManager.logger.info("Inf ID: " + str(self.inf.id) + ": Ansible installation finished successfully")

remote_dir = Config.REMOTE_CONF_DIR
ConfManager.logger.debug("Inf ID: " + str(self.inf.id) + ": Copy the contextualization agent files")
ssh.sftp_mkdir(remote_dir)
files = []
files.append((Config.IM_PATH + "/SSH.py",remote_dir + "/SSH.py"))
files.append((Config.IM_PATH + "/ansible/ansible_callbacks.py", remote_dir + "/ansible_callbacks.py"))
files.append((Config.IM_PATH + "/ansible/ansible_launcher.py", remote_dir + "/ansible_launcher.py"))
files.append((Config.CONTEXTUALIZATION_DIR + "/ctxt_agent.py", remote_dir + "/ctxt_agent.py"))
ssh.sftp_put_files(files)

success = configured_ok

except Exception, ex:
ConfManager.logger.exception("Inf ID: " + str(self.inf.id) + ": Error in the ansible installation process")
self.inf.add_cont_msg("Error in the ansible installation process: " + str(ex))
if not self.inf.ansible_configured: self.inf.ansible_configured = False
success = False
finally:
shutil.rmtree(tmp_dir, ignore_errors=True)
success = False
finally:
shutil.rmtree(tmp_dir, ignore_errors=True)

if success:
self.inf.ansible_configured = True
Expand Down
45 changes: 39 additions & 6 deletions connectors/OpenStack.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,6 @@ def launch(self, inf, radl, requested_radl, num_vm, auth_data):
res.append((True, vm))
else:
res.append((False, "Error creating the node"))



i += 1

# if all the VMs have failed, remove the sg and keypair
Expand Down Expand Up @@ -355,8 +352,7 @@ def finalize(self, vm, auth_data):
self.delete_volumes(vm)

# Delete the SG if this is the last VM
for sg in sgs:
node.driver.ex_delete_security_group(sg)
self.delete_security_group(node, sgs, vm.inf, vm.id)

if not success:
return (False, "Error destroying node: " + vm.id)
Expand All @@ -365,4 +361,41 @@ def finalize(self, vm, auth_data):
else:
self.logger.warn("VM " + str(vm.id) + " not found.")

return (True, "")
return (True, "")

def delete_security_group(self, node, sgs, inf, vm_id, timeout = 60):
"""
Delete the SG of this infrastructure if this is the last VM
"""
if sgs:
# There will be only one
sg = sgs[0]

some_vm = False
for vm in inf.get_vm_list():
if vm.id != vm_id:
some_vm = True

if not some_vm:
# wait it to terminate and then remove the SG
cont = 0
deleted = False
while not deleted and cont < timeout:
time.sleep(5)
cont += 5
try:
node.driver.ex_delete_security_group(sg)
deleted = True
except Exception, ex:
# Check if it has been deleted yet
sg = self._get_security_group(node.driver, sg.name)
if not sg:
self.logger.debug("Error deleting the SG. But it does not exist. Ignore. " + str(ex))
deleted = True
else:
self.logger.exception("Error deleting the SG.")
else:
# If there are more than 1, we skip this step
self.logger.debug("There are active instances. Not removing the SG")
else:
self.logger.warn("No Security Group with name: " + sg.name)

0 comments on commit 7f66922

Please sign in to comment.