From 1c6cf31a0d653376e03d718131db9a842a254ddc Mon Sep 17 00:00:00 2001 From: rhysrevans3 Date: Thu, 1 Apr 2021 14:14:30 +0100 Subject: [PATCH 1/4] Adding previsioned by caas metadata to machines. --- jasmin_cloud/provider/dto.py | 3 ++- jasmin_cloud/provider/openstack/provider.py | 7 +++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/jasmin_cloud/provider/dto.py b/jasmin_cloud/provider/dto.py index 74c057b..e9ebb3e 100644 --- a/jasmin_cloud/provider/dto.py +++ b/jasmin_cloud/provider/dto.py @@ -75,7 +75,7 @@ class Size(namedtuple('Size', ['id', 'name', 'cpus', 'ram', 'disk'])): class Machine(namedtuple('Machine', ['id', 'name', 'image', 'size', 'status', 'power_state', 'task', 'internal_ip', 'external_ip', 'nat_allowed', - 'attached_volume_ids', 'owner', 'created'])): + 'attached_volume_ids', 'owner', 'created', 'previsioned_by_caas'])): """ Represents a machine in a tenancy. @@ -93,6 +93,7 @@ class Machine(namedtuple('Machine', ['id', 'name', 'image', 'size', attached_volume_ids: A tuple of ids of attached volumes for the machine. owner: The username of the user who deployed the machine. created: The `datetime` at which the machine was deployed. + previsioned_by_caas: 1 if the machine was previsioned by caas, 0 otherwise. """ class Status(namedtuple('Status', ['type', 'name', 'details'])): """ diff --git a/jasmin_cloud/provider/openstack/provider.py b/jasmin_cloud/provider/openstack/provider.py index 4c030fc..b236f99 100644 --- a/jasmin_cloud/provider/openstack/provider.py +++ b/jasmin_cloud/provider/openstack/provider.py @@ -646,11 +646,14 @@ def delete_machine(self, machine): """ See :py:meth:`.base.ScopedSession.delete_machine`. """ + caas_machine = True if machine.previsioned_by_caas and machine.previsioned_by_caas == 1 else False + machine = machine.id if isinstance(machine, dto.Machine) else machine self._log("Deleting machine '%s'", machine) # First, delete any associated ports - for port in self._connection.network.ports.all(device_id = machine): - port._delete() + if not caas_machine: + for port in self._connection.network.ports.all(device_id = machine): + port._delete() self._connection.compute.servers.delete(machine) try: return self.find_machine(machine) From 05a8daf48ebfc9627d0d2004d6f93a89038504d7 Mon Sep 17 00:00:00 2001 From: rhysrevans3 Date: Wed, 7 Apr 2021 11:28:20 +0100 Subject: [PATCH 2/4] Correcting spelling error. --- jasmin_cloud/provider/dto.py | 4 ++-- jasmin_cloud/provider/openstack/provider.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/jasmin_cloud/provider/dto.py b/jasmin_cloud/provider/dto.py index e9ebb3e..187b84e 100644 --- a/jasmin_cloud/provider/dto.py +++ b/jasmin_cloud/provider/dto.py @@ -75,7 +75,7 @@ class Size(namedtuple('Size', ['id', 'name', 'cpus', 'ram', 'disk'])): class Machine(namedtuple('Machine', ['id', 'name', 'image', 'size', 'status', 'power_state', 'task', 'internal_ip', 'external_ip', 'nat_allowed', - 'attached_volume_ids', 'owner', 'created', 'previsioned_by_caas'])): + 'attached_volume_ids', 'owner', 'created', 'provisioned_by_caas'])): """ Represents a machine in a tenancy. @@ -93,7 +93,7 @@ class Machine(namedtuple('Machine', ['id', 'name', 'image', 'size', attached_volume_ids: A tuple of ids of attached volumes for the machine. owner: The username of the user who deployed the machine. created: The `datetime` at which the machine was deployed. - previsioned_by_caas: 1 if the machine was previsioned by caas, 0 otherwise. + provisioned_by_caas: 1 if the machine was provisioned by caas, 0 otherwise. """ class Status(namedtuple('Status', ['type', 'name', 'details'])): """ diff --git a/jasmin_cloud/provider/openstack/provider.py b/jasmin_cloud/provider/openstack/provider.py index b236f99..611ab0e 100644 --- a/jasmin_cloud/provider/openstack/provider.py +++ b/jasmin_cloud/provider/openstack/provider.py @@ -646,7 +646,7 @@ def delete_machine(self, machine): """ See :py:meth:`.base.ScopedSession.delete_machine`. """ - caas_machine = True if machine.previsioned_by_caas and machine.previsioned_by_caas == 1 else False + caas_machine = True if machine.provisioned_by_caas and machine.provisioned_by_caas == 1 else False machine = machine.id if isinstance(machine, dto.Machine) else machine self._log("Deleting machine '%s'", machine) From 39350bf95f06fc18552fb5f9e31b01d12779086a Mon Sep 17 00:00:00 2001 From: rhysrevans3 Date: Wed, 7 Apr 2021 13:43:44 +0100 Subject: [PATCH 3/4] Populating new field and ensuring population. --- jasmin_cloud/provider/openstack/provider.py | 25 +++++++++++++-------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/jasmin_cloud/provider/openstack/provider.py b/jasmin_cloud/provider/openstack/provider.py index 611ab0e..d8f4054 100644 --- a/jasmin_cloud/provider/openstack/provider.py +++ b/jasmin_cloud/provider/openstack/provider.py @@ -468,6 +468,11 @@ def _from_api_server(self, api_server): size = self.find_size(api_server.flavor.id) except (AttributeError, errors.ObjectNotFoundError): size = None + # Try to get provisioned_by_caas from the machine metadata + try: + provisioned_by_caas = bool(int(api_server.metadata['provisioned_by_caas'])) + except (KeyError, TypeError): + provisioned_by_caas = False # Try to get nat_allowed from the machine metadata # If the nat_allowed metadata is not present, use the image # If the image does not exist anymore, assume it is allowed @@ -508,7 +513,8 @@ def ip_of_type(ip_type): nat_allowed, tuple(v['id'] for v in api_server.attached_volumes), api_server.user_id, - dateutil.parser.parse(api_server.created) + dateutil.parser.parse(api_server.created), + provisioned_by_caas ) @convert_exceptions @@ -646,17 +652,18 @@ def delete_machine(self, machine): """ See :py:meth:`.base.ScopedSession.delete_machine`. """ - caas_machine = True if machine.provisioned_by_caas and machine.provisioned_by_caas == 1 else False - - machine = machine.id if isinstance(machine, dto.Machine) else machine - self._log("Deleting machine '%s'", machine) + machine = machine if isinstance(machine, dto.Machine) else self.find_machine(machine) + self._log("Deleting machine '%s'", machine.id) # First, delete any associated ports - if not caas_machine: - for port in self._connection.network.ports.all(device_id = machine): + try: + if not machine.provisioned_by_caas: + raise AttributeError(f'{machine.id} not provisioned by CaaS.') + except (AttributeError, TypeError): + for port in self._connection.network.ports.all(device_id = machine.id): port._delete() - self._connection.compute.servers.delete(machine) + self._connection.compute.servers.delete(machine.id) try: - return self.find_machine(machine) + return self.find_machine(machine.id) except errors.ObjectNotFoundError: return None From 03f9bf2989145b50a5b71d8a61ad18a33aa38df7 Mon Sep 17 00:00:00 2001 From: rhysrevans3 Date: Thu, 8 Apr 2021 10:50:43 +0100 Subject: [PATCH 4/4] Adding jasmin to meta data and removing redundant try/except. --- jasmin_cloud/provider/openstack/provider.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/jasmin_cloud/provider/openstack/provider.py b/jasmin_cloud/provider/openstack/provider.py index d8f4054..85098dc 100644 --- a/jasmin_cloud/provider/openstack/provider.py +++ b/jasmin_cloud/provider/openstack/provider.py @@ -470,7 +470,7 @@ def _from_api_server(self, api_server): size = None # Try to get provisioned_by_caas from the machine metadata try: - provisioned_by_caas = bool(int(api_server.metadata['provisioned_by_caas'])) + provisioned_by_caas = bool(int(api_server.metadata['jasmin_provisioned_by_caas'])) except (KeyError, TypeError): provisioned_by_caas = False # Try to get nat_allowed from the machine metadata @@ -655,10 +655,7 @@ def delete_machine(self, machine): machine = machine if isinstance(machine, dto.Machine) else self.find_machine(machine) self._log("Deleting machine '%s'", machine.id) # First, delete any associated ports - try: - if not machine.provisioned_by_caas: - raise AttributeError(f'{machine.id} not provisioned by CaaS.') - except (AttributeError, TypeError): + if not machine.provisioned_by_caas: for port in self._connection.network.ports.all(device_id = machine.id): port._delete() self._connection.compute.servers.delete(machine.id)