Skip to content
This repository has been archived by the owner on May 27, 2024. It is now read-only.

Commit

Permalink
Merge pull request #623 from deNBI/delete_backend
Browse files Browse the repository at this point in the history
import and delte everytime
  • Loading branch information
dweinholz authored Feb 25, 2021
2 parents 398a66d + 28bfc5a commit 35b8588
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions VirtualMachineService/VirtualMachineHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
Which can be used for the PortalClient.
"""
from uuid import uuid4

try:
from VirtualMachineService import Iface
from ttypes import serverNotFoundException
Expand Down Expand Up @@ -909,7 +911,7 @@ def volume_ids(
image = self.get_image(image=image)
flavor = self.get_flavor(flavor=flavor)
network = self.get_network()
key_name = metadata.get("elixir_id")[:-18]
key_name = f"{metadata.get('elixir_id')[:-18]}{str(uuid4())[0:5]}"
public_key = urllib.parse.unquote(public_key)
key_pair = self.import_keypair(key_name, public_key)
init_script = self.create_mount_init_script(
Expand All @@ -933,10 +935,12 @@ def volume_ids(
security_groups=self.DEFAULT_SECURITY_GROUPS + custom_security_groups,
)
openstack_id = server["id"]
self.delete_keypair(key_name)

return {"openstack_id": openstack_id}

except Exception as e:
self.delete_keypair(key_name)
for security_group in custom_security_groups:
self.conn.network.delete_security_group(security_group)
LOG.exception("Start Server {1} error:{0}".format(e, servername))
Expand Down Expand Up @@ -1013,12 +1017,13 @@ def start_server_without_playbook(
custom_security_groups = self.prepare_security_groups_new_server(
resenv=resenv, servername=servername, http=http, https=https
)

key_name = None
try:
image = self.get_image(image=image)
flavor = self.get_flavor(flavor=flavor)
network = self.get_network()
key_name = metadata.get("elixir_id")[:-18]
key_name = f"{metadata.get('elixir_id')[:-18]}{str(uuid4())[0:5]}"

public_key = urllib.parse.unquote(public_key)
key_pair = self.import_keypair(key_name, public_key)
volume_ids = []
Expand All @@ -1032,7 +1037,6 @@ def start_server_without_playbook(
)
for id in volume_ids:
volumes.append(self.conn.get_volume_by_id(id=id))
LOG.info(volumes)
init_script = self.create_mount_init_script(
volume_ids_path_new=volume_ids_path_new,
volume_ids_path_attach=volume_ids_path_attach,
Expand All @@ -1049,8 +1053,6 @@ def start_server_without_playbook(
else:
init_script = self.create_add_keys_script(keys=additional_keys)

LOG.info(init_script)

server = self.conn.create_server(
name=servername,
image=image.id,
Expand All @@ -1065,9 +1067,13 @@ def start_server_without_playbook(
)

openstack_id = server["id"]
self.delete_keypair(key_name)

return {"openstack_id": openstack_id}
except Exception as e:
if key_name:
self.delete_keypair(key_name)

for security_group in custom_security_groups:
self.conn.network.delete_security_group(security_group)
LOG.exception("Start Server {1} error:{0}".format(e, servername))
Expand Down Expand Up @@ -1105,12 +1111,12 @@ def start_server(
custom_security_groups = self.prepare_security_groups_new_server(
resenv=resenv, servername=servername, http=http, https=https
)

key_name = None
try:
image = self.get_image(image=image)
flavor = self.get_flavor(flavor=flavor)
network = self.get_network()
key_name = metadata.get("elixir_id")[:-18]
key_name = f"{metadata.get('elixir_id')[:-18]}{str(uuid4())[0:5]}"
public_key = urllib.parse.unquote(public_key)
key_pair = self.import_keypair(key_name, public_key)

Expand All @@ -1126,9 +1132,12 @@ def start_server(
)

openstack_id = server["id"]
self.delete_keypair(key_name)

return {"openstack_id": openstack_id}
except Exception as e:
if key_name:
self.delete_keypair(key_name)
for security_group in custom_security_groups:
self.conn.network.delete_security_group(security_group)
LOG.exception("Start Server {1} error:{0}".format(e, servername))
Expand Down Expand Up @@ -1604,10 +1613,13 @@ def check_template(self, template_name, template_version):

def get_playbook_logs(self, openstack_id):
global active_playbooks
LOG.info(f"Get Playbook logs {openstack_id}")
if self.redis.exists(openstack_id) == 1 and openstack_id in active_playbooks:
key_name = self.redis.hget(openstack_id, "name").decode("utf-8")
playbook = active_playbooks.pop(openstack_id)
status, stdout, stderr = playbook.get_logs()
LOG.info(f" Playbook logs{openstack_id} stattus: {status}")

playbook.cleanup(openstack_id)
self.delete_keypair(key_name=key_name)
return PlaybookResult(status=status, stdout=stdout, stderr=stderr)
Expand Down

0 comments on commit 35b8588

Please sign in to comment.