Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions changelogs/fragments/cluster_modules.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
major_changes:
- Added cluster_name and cluster_info module. (https://github.com/ScaleComputing/HyperCoreAnsibleCollection/pull/112)
- Added cluster_shutdown module. (https://github.com/ScaleComputing/HyperCoreAnsibleCollection/pull/117)

minor_changes:
- Updated version check in cluster_name module. (https://github.com/ScaleComputing/HyperCoreAnsibleCollection/pull/123)
- Fixed timeout error in cluster_shutdown module. (https://github.com/ScaleComputing/HyperCoreAnsibleCollection/pull/127)
26 changes: 23 additions & 3 deletions plugins/module_utils/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@

__metaclass__ = type

from time import sleep
from ..module_utils.utils import PayloadMapper
from ..module_utils.rest_client import RestClient
from ..module_utils.errors import ScaleComputingError
from ..module_utils.typed_classes import TypedClusterToAnsible, TypedTaskTag
from typing import Any

Expand Down Expand Up @@ -76,6 +78,24 @@ def update_name(
def shutdown(
rest_client: RestClient, force_shutdown: bool = False, check_mode: bool = False
) -> None:
rest_client.create_record(
"/rest/v1/Cluster/shutdown", dict(forceShutdown=force_shutdown), check_mode
)
try:
rest_client.create_record(
"/rest/v1/Cluster/shutdown",
dict(forceShutdown=force_shutdown),
check_mode,
)
except ScaleComputingError as e:
# To avoid timeout when there are a lot of VMs to shutdown
if "Request timed out" in str(e):
try:
while True:
# get cluster until "[Errno 111] Connection refused"
Cluster.get(rest_client)
sleep(5)
except ScaleComputingError as e2:
if "Connection refused" in str(e2): # cluster is shutdown
pass
else:
raise e2 # Some other unexpected error
else:
raise e # UnexpectedAPIResponse error
6 changes: 3 additions & 3 deletions plugins/modules/cluster_shutdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

author:
- Polona Mihalič (@PolonaM)
short_description: Shutdown cluster.
short_description: Shutdown the cluster.
description:
- Shutdown cluster.
- Shutdown the cluster. All running VirDomains will be restarted on next system boot.
version_added: 1.2.0
extends_documentation_fragment:
- scale_computing.hypercore.cluster_instance
Expand Down Expand Up @@ -72,7 +72,7 @@ def main() -> None:
client = Client.get_client(module.params["cluster_instance"])
rest_client = RestClient(client)
shutdown = run(module, rest_client)
module.exit_json(shutdown=shutdown)
module.exit_json(changed=True, shutdown=shutdown)
except errors.ScaleComputingError as e:
module.fail_json(msg=str(e))

Expand Down
6 changes: 5 additions & 1 deletion tests/unit/plugins/module_utils/test_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@
from ansible_collections.scale_computing.hypercore.plugins.module_utils.cluster import (
Cluster,
)
from ansible_collections.scale_computing.hypercore.plugins.module_utils.utils import (
MIN_PYTHON_VERSION,
)

pytestmark = pytest.mark.skipif(
sys.version_info < (3, 8), reason="requires python3.8 or higher"
sys.version_info < MIN_PYTHON_VERSION,
reason=f"requires python{MIN_PYTHON_VERSION[0]}.{MIN_PYTHON_VERSION[1]} or higher",
)


Expand Down