diff --git a/lib/charms/hpc_libs/v0/slurm_ops.py b/lib/charms/hpc_libs/v0/slurm_ops.py index 774a0fe..fa07d21 100644 --- a/lib/charms/hpc_libs/v0/slurm_ops.py +++ b/lib/charms/hpc_libs/v0/slurm_ops.py @@ -68,6 +68,7 @@ def _on_install(self, _) -> None: import json import logging import re +import socket import subprocess from collections.abc import Mapping from enum import Enum @@ -83,7 +84,7 @@ def _on_install(self, _) -> None: # Increment this PATCH version before using `charmcraft publish-lib` or reset # to 0 if you are raising the major API version -LIBPATCH = 3 +LIBPATCH = 4 # Charm library dependencies to fetch during `charmcraft pack`. PYDEPS = ["pyyaml>=6.0.1"] @@ -291,3 +292,8 @@ def __init__(self, service: ServiceType) -> None: self._service = service self.config = ConfigurationManager(service.config_name) self.munge = MungeManager() + + @property + def hostname(self) -> str: + """The hostname where this manager is running.""" + return socket.gethostname().split(".")[0] diff --git a/tests/unit/test_slurm_ops.py b/tests/unit/test_slurm_ops.py index 935f06b..6d12d77 100644 --- a/tests/unit/test_slurm_ops.py +++ b/tests/unit/test_slurm_ops.py @@ -219,6 +219,14 @@ def test_configure_munge(self, subcmd) -> None: args = subcmd.call_args[0][0] self.assertEqual(args, ["snap", "set", "slurm", "munge.max-thread-count=24"]) + @patch("charms.hpc_libs.v0.slurm_ops.socket.gethostname") + def test_hostname(self, gethostname, *_) -> None: + """Test that manager is able to correctly get the host name.""" + gethostname.return_value = "machine" + self.assertEqual(self.manager.hostname, "machine") + gethostname.return_value = "machine.domain.com" + self.assertEqual(self.manager.hostname, "machine") + parameters = [ (SlurmManagerBase(ServiceType.SLURMCTLD), "slurm"),