diff --git a/src/charm.py b/src/charm.py index 8bbc562..4f20b35 100755 --- a/src/charm.py +++ b/src/charm.py @@ -25,7 +25,6 @@ from interface_slurmrestd import ( Slurmrestd, SlurmrestdAvailableEvent, - SlurmrestdUnavailableEvent, ) from ops import ( ActionEvent, @@ -61,9 +60,7 @@ def __init__(self, *args): new_nodes=[], nhc_params=str(), slurm_installed=False, - slurmdbd_available=False, slurmdbd_host=str(), - slurmrestd_available=False, user_supplied_slurm_conf_params=str(), ) @@ -91,9 +88,8 @@ def __init__(self, *args): self._slurmd.on.partition_unavailable: self._on_write_slurm_conf, self._slurmd.on.slurmd_available: self._on_write_slurm_conf, self._slurmd.on.slurmd_departed: self._on_write_slurm_conf, - # slurmrestd lifecycle hook events + # slurmrestd available self._slurmrestd.on.slurmrestd_available: self._on_slurmrestd_available, - self._slurmrestd.on.slurmrestd_unavailable: self._on_slurmrestd_unavailable, # NOTE: a second slurmctld should get the jwt/munge keys and configure them # fluentbit self.on["fluentbit"].relation_created: self._on_fluentbit_relation_created, @@ -177,25 +173,17 @@ def _on_slurmrestd_available(self, event: SlurmrestdAvailableEvent) -> None: """Check that we have slurm_config when slurmrestd available otherwise defer the event.""" if self.model.unit.is_leader(): if self._check_status(): - slurm_config = self._assemble_slurm_conf() - self._stored.slurmrestd_available = True - self._slurmrestd.set_slurm_config_on_app_relation_data(slurm_config) + self._slurmrestd.set_slurm_config_on_app_relation_data(self._assemble_slurm_conf()) return logger.debug("Cluster not ready yet, deferring event.") event.defer() - def _on_slurmrestd_unavailable(self, event: SlurmrestdUnavailableEvent) -> None: - """Set slurmrestd_available to False.""" - self._stored.slurmrestd_available = False - def _on_slurmdbd_available(self, event: SlurmdbdAvailableEvent) -> None: self._stored.slurmdbd_host = event.slurmdbd_host - self._stored.slurmdbd_available = True self._on_write_slurm_conf(event) def _on_slurmdbd_unavailable(self, event: SlurmdbdUnavailableEvent) -> None: self._stored.slurmdbd_host = "" - self._stored.slurmdbd_available = False self._check_status() def _on_drain_nodes_action(self, event: ActionEvent) -> None: @@ -284,7 +272,7 @@ def _on_write_slurm_conf( self.new_nodes = new_nodes_from_slurm_config.copy() # slurmrestd needs the slurm.conf file, so send it every time it changes. - if self._stored.slurmrestd_available is True: + if self._slurmrestd.is_joined is not False: self._slurmrestd.set_slurm_config_on_app_relation_data(slurm_config) else: logger.debug("## Should write slurm.conf, but we don't have it. " "Deferring.") @@ -315,9 +303,9 @@ def _assemble_slurmctld_parameters() -> str: return ",".join(slurmctld_param_config) accounting_params = {} - if self._slurmdbd.is_joined: + if (slurmdbd_host := self._stored.slurmdbd_host) != "": accounting_params = { - "AccountingStorageHost": self._stored.slurmdbd_host, + "AccountingStorageHost": slurmdbd_host, "AccountingStorageType": "accounting_storage/slurmdbd", "AccountingStoragePass": "/var/run/munge/munge.socket.2", "AccountingStoragePort": "6819", @@ -364,12 +352,9 @@ def _check_status(self) -> bool: # noqa C901 """Check for all relations and set appropriate status. This charm needs these conditions to be satisfied in order to be ready: - - Slurm components installed. - - Munge running. + - Slurmctld component installed + - Munge running """ - # NOTE: slurmd and slurmrestd are not needed for slurmctld to work, - # only for the cluster to operate. But we need slurmd inventory - # to assemble slurm.conf if self.slurm_installed is not True: self.unit.status = BlockedStatus("Error installing slurmctld") return False @@ -439,11 +424,6 @@ def slurm_installed(self, slurm_installed: bool) -> None: """Set slurm_installed in stored state.""" self._stored.slurm_installed = slurm_installed - @property - def slurmdbd_available(self) -> bool: - """Return slurmdbd_available from stored state.""" - return True if self._stored.slurmdbd_available is True else False - if __name__ == "__main__": main.main(SlurmctldCharm) diff --git a/src/interface_slurmd.py b/src/interface_slurmd.py index 6a56064..4e4a44c 100644 --- a/src/interface_slurmd.py +++ b/src/interface_slurmd.py @@ -2,7 +2,7 @@ """Interface slurmd.""" import json import logging -from typing import Any, Dict, List, Union +from typing import Any, Dict from ops import ( EventBase, @@ -76,7 +76,7 @@ def __init__(self, charm, relation_name): def _on_relation_created(self, event: RelationCreatedEvent) -> None: """Set our data on the relation.""" # Need to wait until the charm has installed slurm before we can proceed. - if not (self._charm.slurm_installed and self._charm.slurmdbd_available): + if not self._charm.slurm_installed: event.defer() return @@ -149,13 +149,12 @@ def set_nhc_params(self, params: str = "#") -> None: # so we set it to something that behaves like empty logger.debug(f"## set_nhc_params: {params}") - if self.is_joined: - if relations := self._relations: - for relation in relations: - app = self.model.app - cluster_info = json.loads(relation.data[app]["cluster_info"]) - cluster_info["nhc_params"] = params - relation.data[app]["cluster_info"] = json.dumps(cluster_info) + if relations := self.framework.model.relations.get(self._relation_name): + for relation in relations: + app = self.model.app + cluster_info = json.loads(relation.data[app]["cluster_info"]) + cluster_info["nhc_params"] = params + relation.data[app]["cluster_info"] = json.dumps(cluster_info) else: logger.debug("## slurmd not joined") @@ -194,7 +193,7 @@ def get_new_nodes_and_nodes_and_partitions(self) -> Dict[str, Any]: nodes = {} new_nodes = [] - if relations := self._relations: + if relations := self.framework.model.relations.get(self._relation_name): for relation in relations: partition_as_dict = self._get_partition_from_relation(relation) @@ -246,12 +245,3 @@ def get_new_nodes_and_nodes_and_partitions(self) -> Dict[str, Any]: else [] ) return {"down_nodes": new_node_down_nodes, "nodes": nodes, "partitions": partitions} - - @property - def _relations(self) -> Union[List[Relation], None]: - return self.framework.model.relations.get(self._relation_name) - - @property - def is_joined(self) -> bool: - """Return True if self._relation is not None.""" - return True if self._relations else False diff --git a/src/interface_slurmdbd.py b/src/interface_slurmdbd.py index 1a88ce1..3e06947 100644 --- a/src/interface_slurmdbd.py +++ b/src/interface_slurmdbd.py @@ -2,14 +2,12 @@ """Slurmdbd.""" import json import logging -from typing import Union from ops import ( EventBase, EventSource, Object, ObjectEvents, - Relation, RelationBrokenEvent, RelationChangedEvent, RelationCreatedEvent, @@ -114,13 +112,3 @@ def _on_relation_broken(self, event: RelationBrokenEvent) -> None: if self.framework.model.unit.is_leader(): event.relation.data[self.model.app]["cluster_info"] = "" self.on.slurmdbd_unavailable.emit() - - @property - def _relation(self) -> Union[Relation, None]: - """Return the relation or None.""" - return self.framework.model.get_relation(self._relation_name) - - @property - def is_joined(self) -> bool: - """Return True if the relation to slurmdbd exists.""" - return True if self._charm.framework.model.relations.get(self._relation_name) else False diff --git a/src/interface_slurmrestd.py b/src/interface_slurmrestd.py index 93b3295..04f1268 100644 --- a/src/interface_slurmrestd.py +++ b/src/interface_slurmrestd.py @@ -52,10 +52,7 @@ def __init__(self, charm, relation_name): @property def is_joined(self) -> bool: """Return True if relation is joined.""" - if self._charm.framework.model.relations.get(self._relation_name): - return True - else: - return False + return True if self.model.relations.get(self._relation_name) else False def _on_relation_created(self, event: RelationCreatedEvent) -> None: # Check that slurm has been installed so that we know the munge key is diff --git a/tests/unit/test_charm.py b/tests/unit/test_charm.py index ab3429c..393e412 100644 --- a/tests/unit/test_charm.py +++ b/tests/unit/test_charm.py @@ -164,9 +164,9 @@ def test_on_slurmrestd_available_if_available(self, *_) -> None: def test_on_slurmdbd_available(self) -> None: """Test that the on_slurmdbd_method works.""" self.harness.charm._slurmdbd.on.slurmdbd_available.emit("slurmdbdhost") - self.assertEqual(self.harness.charm._stored.slurmdbd_available, True) + self.assertEqual(self.harness.charm._stored.slurmdbd_host, "slurmdbdhost") def test_on_slurmdbd_unavailable(self) -> None: """Test that the on_slurmdbd_unavailable method works.""" self.harness.charm._slurmdbd.on.slurmdbd_unavailable.emit() - self.assertEqual(self.harness.charm._stored.slurmdbd_available, False) + self.assertEqual(self.harness.charm._stored.slurmdbd_host, "")