Skip to content

Commit

Permalink
refactor: use context manager to create config files on demand
Browse files Browse the repository at this point in the history
- 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 <[email protected]>
  • Loading branch information
NucciTheBoss committed Jul 16, 2024
1 parent 57446aa commit b5ea556
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 62 deletions.
8 changes: 0 additions & 8 deletions src/slurmhelpers/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand All @@ -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."""
Expand Down
110 changes: 56 additions & 54 deletions src/slurmhelpers/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)

0 comments on commit b5ea556

Please sign in to comment.