From b5ea556517367f36234c221bd1392fa45c3a4304 Mon Sep 17 00:00:00 2001 From: "Jason C. Nucciarone" Date: Tue, 16 Jul 2024 17:51:04 -0400 Subject: [PATCH] refactor: use context manager to create config files on demand - No longer create empty slurm.conf file at installation. Instead the context manager will create an empty `SlurmConfig` object and make the new file the first time the admin sets a config value. Signed-off-by: Jason C. Nucciarone --- src/slurmhelpers/hooks.py | 8 --- src/slurmhelpers/models.py | 110 +++++++++++++++++++------------------ 2 files changed, 56 insertions(+), 62 deletions(-) diff --git a/src/slurmhelpers/hooks.py b/src/slurmhelpers/hooks.py index 2ee8f86..a47c83a 100644 --- a/src/slurmhelpers/hooks.py +++ b/src/slurmhelpers/hooks.py @@ -89,9 +89,7 @@ def install(snap: Snap) -> None: """ setup_logging(snap.paths.common / "hooks.log") munge = Munge(snap) - slurm = Slurm(snap) slurmd = Slurmd(snap) - slurmdbd = Slurmdbd(snap) slurmrestd = Slurmrestd(snap) logging.info("Executing snap `install` hook.") @@ -107,12 +105,6 @@ def install(snap: Snap) -> None: logging.info("Generating default munge.key secret.") munge.generate_key() - logging.info("Creating empty `slurm.conf` file.") - slurm.config_file.touch(0o644) - - logging.info("Creating empty `slurmdbd.conf` file.") - slurmdbd.config_file.touch(0o644) - def configure(snap: Snap) -> None: """Configure hook for the Slurm snap.""" diff --git a/src/slurmhelpers/models.py b/src/slurmhelpers/models.py index 99b7254..791e435 100644 --- a/src/slurmhelpers/models.py +++ b/src/slurmhelpers/models.py @@ -228,26 +228,27 @@ def __init__(self, *args) -> None: def update_config(self, config: Dict[str, str]) -> None: """Update configuration for the `slurmdbd` service.""" - # Load current `slurmdbd.conf` configuration file. - # Preserve old configuration so that we can determine - # if substantial changes were made to the `slurmdbd.conf` file. - sconf = slurmdbdconfig.load(self.config_file) - old = sconf.dict() + with slurmdbdconfig.edit(self.config_file) as sconf: + # Preserve old configuration so that we can determine + # if substantial changes were made to the `slurmdbd.conf` file. + old = sconf.dict() - # Assemble new `slurmdbd.conf` file. - for k, v in config.items(): - key = k.replace("-", "_") - if not hasattr(sconf, key): - raise AttributeError(f"`slurmdbd` config file does not support option {key}.") + # Assemble new `slurmdbd.conf` file. + for k, v in config.items(): + key = k.replace("-", "_") + if not hasattr(sconf, key): + raise AttributeError(f"`slurmdbd` config file does not support option {key}.") - setattr(sconf, key, _apply_callback(key, v, model=sconf)) + setattr(sconf, key, _apply_callback(key, v, model=sconf)) - if sconf.dict() == old: - logging.debug("No change in `slurmdbd` service configuration. Not updating.") - return + if sconf.dict() == old: + logging.debug("No change in `slurmdbd` service configuration. Not updating.") + return + + logging.info("Updating `slurmdbd` configuration file %s.", self.config_file) + self._needs_restart(["slurmdbd"]) - slurmdbdconfig.dump(sconf, self.config_file) - self._needs_restart(["slurmdbd"]) + self.config_file.chmod(0o600) class Slurmrestd(_BaseModel): @@ -402,41 +403,42 @@ def __init__(self, *args) -> None: def update_config(self, config: Dict[str, Any]) -> None: """Update configuration of the Slurm workload manager.""" - # Load current `slurm.conf` configuration file. - # Preserve old configuration so that we can determine - # if substantial changes were made to the `slurm.conf` file. - sconf = slurmconfig.load(self.config_file) - old = sconf.dict() - - # Assemble new `slurm.conf` file. - for k, v in config.items(): - match key := k.replace("-", "_"): - case "include": - # Multiline configuration options. Requires special handling. - sconf.include = v.split(",") - case "slurmctld_host": - # Multiline configuration options. Requires special handling. - sconf.slurmctld_host = v.split(",") - case "nodes": - sconf.nodes = _process_nodes(v) - case "frontend_nodes": - sconf.frontend_nodes = _process_frontend_nodes(v) - case "down_nodes": - sconf.down_nodes = _process_down_nodes(v) - case "node_sets": - sconf.node_sets = _process_node_sets(v) - case "partitions": - sconf.partitions = _process_partitions(v) - case _: - if not hasattr(sconf, key): - raise AttributeError(f"Slurm config file does not support option {key}.") - - setattr(sconf, key, _apply_callback(key, v, model=sconf)) - - if sconf.dict() == old: - logging.debug("No change in Slurm workload manager configuration. Not updating.") - return - - logging.info("Updating Slurm workload manager configuration file %s.", self.config_file) - slurmconfig.dump(sconf, self.config_file) - self._needs_restart(["slurmctld", "slurmd", "slurmdbd", "slurmrestd"]) + with slurmconfig.edit(self.config_file) as sconf: + # Preserve old configuration so that we can determine + # if substantial changes were made to the `slurm.conf` file. + old = sconf.dict() + # Assemble new `slurm.conf` file. + for k, v in config.items(): + match key := k.replace("-", "_"): + case "include": + # Multiline configuration options. Requires special handling. + sconf.include = v.split(",") + case "slurmctld_host": + # Multiline configuration options. Requires special handling. + sconf.slurmctld_host = v.split(",") + case "nodes": + sconf.nodes = _process_nodes(v) + case "frontend_nodes": + sconf.frontend_nodes = _process_frontend_nodes(v) + case "down_nodes": + sconf.down_nodes = _process_down_nodes(v) + case "node_sets": + sconf.node_sets = _process_node_sets(v) + case "partitions": + sconf.partitions = _process_partitions(v) + case _: + if not hasattr(sconf, key): + raise AttributeError( + f"Slurm config file does not support option {key}." + ) + + setattr(sconf, key, _apply_callback(key, v, model=sconf)) + + if sconf.dict() == old: + logging.debug("No change in Slurm workload manager configuration. Not updating.") + return + + logging.info("Updating Slurm configuration file %s.", self.config_file) + self._needs_restart(["slurmctld", "slurmd", "slurmdbd", "slurmrestd"]) + + self.config_file.chmod(0o600)