Skip to content

Commit

Permalink
Delete error'd server
Browse files Browse the repository at this point in the history
ENH: The VMs which are created but are in error state will be deleted.
  • Loading branch information
azahmd committed Feb 20, 2025
1 parent 9b08113 commit ad3763a
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 8 deletions.
5 changes: 5 additions & 0 deletions actions/hv.create.test.server.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,9 @@ parameters:
description: Test all avaliable flavors for the hypervisor, otherwise test random flavor
default: false
required: true
delete_on_failure:
type: boolean
description: Delete servers that errored during creation
default: true
required: true
runner_type: python-script
2 changes: 1 addition & 1 deletion actions/hv.post.reboot.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ name: hv.post.reboot
parameters:
hypervisor_hostname:
type: string
description: Hostname of hypervisor to run action against
description: Hostname of hypervisor to run action against
required: true
lib_entry_point:
type: string
Expand Down
15 changes: 11 additions & 4 deletions lib/openstack_api/openstack_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from openstack.connection import Connection
from openstack.compute.v2.image import Image
from openstack.compute.v2.server import Server
from openstack.exceptions import ResourceTimeout
from openstack.exceptions import ResourceFailure


def snapshot_and_migrate_server(
Expand Down Expand Up @@ -67,6 +69,7 @@ def build_server(
image_name: str,
network_name: str,
hypervisor_hostname: Optional[str] = None,
delete_on_failure: Optional[bool] = False,
) -> Server:
"""
Builds a server, with option to specify a hypervisor
Expand Down Expand Up @@ -100,10 +103,14 @@ def build_server(
"openstack_api_version": "2.74",
}
)

conn.compute.wait_for_server(
server, status="ACTIVE", failures=None, interval=5, wait=300
)
try:
conn.compute.wait_for_server(
server, status="ACTIVE", failures=None, interval=5, wait=300
)
except (ResourceTimeout, ResourceFailure) as e:
if delete_on_failure:
conn.compute.delete_server(server, force=True)
raise e
return server


Expand Down
6 changes: 5 additions & 1 deletion lib/workflows/hv_create_test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@


def create_test_server(
conn: Connection, hypervisor_name: str, test_all_flavors: bool
conn: Connection,
hypervisor_name: str,
test_all_flavors: bool,
delete_on_failure: bool,
) -> None:
"""
Create a test server on a hypervisor, option to test all possible flavors avaliable to the hypervisor
Expand All @@ -32,5 +35,6 @@ def create_test_server(
"ubuntu-focal-20.04-nogui",
"Internal",
hypervisor_name,
delete_on_failure,
)
delete_server(conn, server.id)
1 change: 1 addition & 0 deletions tests/lib/openstack_api/test_openstack_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ def test_build_server():
"test-image",
"test-network",
"hvxyz.nubes.rl.ac.uk",
False,
)

mock_conn.compute.find_flavor.assert_called_once_with("test-flavor")
Expand Down
6 changes: 4 additions & 2 deletions tests/lib/workflows/test_hv_create_test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def test_create_single_test_server(
mock_server = MagicMock()
mock_build_server.return_value = mock_server

create_test_server(mock_conn, "hvxyz.nubes.rl.ac.uk", False)
create_test_server(mock_conn, "hvxyz.nubes.rl.ac.uk", False, False)

mock_get_available_flavors.assert_called_once_with(
mock_conn, "hvxyz.nubes.rl.ac.uk"
Expand All @@ -35,6 +35,7 @@ def test_create_single_test_server(
"ubuntu-focal-20.04-nogui",
"Internal",
"hvxyz.nubes.rl.ac.uk",
False,
)
mock_delete_server.assert_called_once_with(mock_conn, mock_server.id)

Expand All @@ -55,7 +56,7 @@ def test_create_test_server_all_flavors(
mock_server = MagicMock()
mock_build_server.return_value = mock_server

create_test_server(mock_conn, "hvxyz.nubes.rl.ac.uk", True)
create_test_server(mock_conn, "hvxyz.nubes.rl.ac.uk", True, True)

mock_get_available_flavors.assert_called_once_with(
mock_conn, "hvxyz.nubes.rl.ac.uk"
Expand All @@ -69,5 +70,6 @@ def test_create_test_server_all_flavors(
"ubuntu-focal-20.04-nogui",
"Internal",
"hvxyz.nubes.rl.ac.uk",
True,
)
mock_delete_server.assert_any_call(mock_conn, mock_server.id)

0 comments on commit ad3763a

Please sign in to comment.