Skip to content

Commit

Permalink
OST detach from vols and SGs before node deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
micafer committed Jun 26, 2020
1 parent d74268d commit 333a15c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
27 changes: 25 additions & 2 deletions IM/connectors/OpenStack.py
Original file line number Diff line number Diff line change
Expand Up @@ -1304,6 +1304,21 @@ def finalize(self, vm, last, auth_data):
success = []
msgs = []
if node:
# First try to detach the volumes and the SGs
for vol_id in vm.volumes:
try:
volume = node.driver.ex_get_volume(vol_id)
node.driver.detach_volume(volume)
except Exception as ex:
self.log_exception("Error dettaching volume %s." % vol_id)

try:
for sg_name in self._get_security_names(vm.inf):
security_group = OpenStackSecurityGroup(None, None, sg_name, "", node.driver)
node.driver.ex_remove_security_group_from_node(security_group, node)
except Exception as ex:
self.log_exception("Error dettaching volume %s." % vol_id)

res = node.destroy()
success.append(res)

Expand Down Expand Up @@ -1361,9 +1376,9 @@ def finalize(self, vm, last, auth_data):

return (all(success), "\n ".join(msgs))

def delete_security_groups(self, driver, inf, timeout=180, delay=10):
def _get_security_names(self, inf):
"""
Delete the SG of this inf
Get the list of SGs for this infra
"""
sg_names = ["im-%s" % inf.id]
for net in inf.radl.networks:
Expand All @@ -1372,6 +1387,14 @@ def delete_security_groups(self, driver, inf, timeout=180, delay=10):
sg_name = "im-%s-%s" % (inf.id, net.id)
sg_names.append(sg_name)

return sg_names

def delete_security_groups(self, driver, inf, timeout=180, delay=10):
"""
Delete the SG of this inf
"""
sg_names = self._get_security_names(inf)

msg = ""
deleted = True
for sg_name in sg_names:
Expand Down
9 changes: 9 additions & 0 deletions test/unit/connectors/OpenStack.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -736,13 +736,22 @@ def test_60_finalize(self, sleep, get_driver):
driver.ex_list_routers.return_value = [router]
driver.ex_add_router_subnet.return_value = True

volume = MagicMock()
driver.ex_get_volume.return_value = volume
driver.detach_volume.return_value = True
driver.ex_remove_security_group_from_node.return_value = True

vm.volumes = ['volid']
success, _ = ost_cloud.finalize(vm, True, auth)

self.assertTrue(success, msg="ERROR: finalizing VM info.")
self.assertNotIn("ERROR", self.log.getvalue(), msg="ERROR found in log: %s" % self.log.getvalue())

self.assertEqual(node.destroy.call_args_list, [call()])
self.assertEqual(driver.detach_volume.call_args_list[0][0][0], volume)
self.assertEqual(driver.ex_remove_security_group_from_node.call_args_list[0][0][0].name, "im-infid")
self.assertEqual(driver.ex_remove_security_group_from_node.call_args_list[1][0][0].name, "im-infid-public")
self.assertEqual(driver.ex_remove_security_group_from_node.call_args_list[2][0][0].name, "im-infid-private")
self.assertEqual(driver.ex_del_router_subnet.call_args_list[0][0][0], router)
self.assertEqual(driver.ex_del_router_subnet.call_args_list[0][0][1].id, "subnet1")
self.assertEqual(driver.ex_delete_network.call_args_list[0][0][0], net1)
Expand Down

0 comments on commit 333a15c

Please sign in to comment.