From b9b8287f6a9187845383f148813121966ba4208e Mon Sep 17 00:00:00 2001 From: Jaime Freire Date: Thu, 12 Oct 2023 13:37:03 +0000 Subject: [PATCH] Include constants.py and update dispatch --- dispatch | 5 ++--- .../v0/juju_systemd_notices.py | 11 +++------- src/charm.py | 11 ++-------- src/constants.py | 20 +++++++++++++++++++ src/utils/slurmd.py | 14 ++++++++----- tests/unit/utils/test_slurmd.py | 4 ++-- 6 files changed, 38 insertions(+), 27 deletions(-) create mode 100644 src/constants.py diff --git a/dispatch b/dispatch index b6575b7..7f58019 100755 --- a/dispatch +++ b/dispatch @@ -16,14 +16,13 @@ then yum -y install epel-release yum -y install wget gcc make tar bzip2-devel zlib-devel xz-devel openssl-devel libffi-devel sqlite-devel ncurses-devel - export PYTHON_VERSION=3.8.16 - CHARM_DIR=$PWD + export PYTHON_VERSION=3.8.16 wget https://www.python.org/ftp/python/${PYTHON_VERSION}/Python-${PYTHON_VERSION}.tar.xz -P /tmp tar xvf /tmp/Python-${PYTHON_VERSION}.tar.xz -C /tmp cd /tmp/Python-${PYTHON_VERSION} ./configure --enable-optimizations make -C /tmp/Python-${PYTHON_VERSION} -j $(nproc) altinstall - cd $CHARM_DIR + cd $OLDPWD rm -rf /tmp/Python* elif [[ $ID == 'ubuntu' ]] diff --git a/lib/charms/operator_libs_linux/v0/juju_systemd_notices.py b/lib/charms/operator_libs_linux/v0/juju_systemd_notices.py index 1c1cf42..08157c9 100644 --- a/lib/charms/operator_libs_linux/v0/juju_systemd_notices.py +++ b/lib/charms/operator_libs_linux/v0/juju_systemd_notices.py @@ -173,12 +173,8 @@ def __init__(self, charm: CharmBase, services: List[str]) -> None: self._charm.on.define_event(f"service_{service}_started", ServiceStartedEvent) self._charm.on.define_event(f"service_{service}_stopped", ServiceStoppedEvent) - def subscribe(self, python_bin: str) -> None: - """Subscribe charmed operator to observe status of systemd services. - - Args: - python_bin: Path of Python's binary file. - """ + def subscribe(self) -> None: + """Subscribe charmed operator to observe status of systemd services.""" _logger.debug("Generating systemd notice hooks for %s", self._services) start_hooks = [Path(f"hooks/service-{service}-started") for service in self._services] stop_hooks = [Path(f"hooks/service-{service}-stopped") for service in self._services] @@ -191,7 +187,6 @@ def subscribe(self, python_bin: str) -> None: _logger.debug("Starting %s daemon", self._service_file.name) if self._service_file.exists(): _logger.debug("Overwriting existing service file %s", self._service_file.name) - self._service_file.write_text( textwrap.dedent( f""" @@ -204,7 +199,7 @@ def subscribe(self, python_bin: str) -> None: Restart=always WorkingDirectory={self._charm.framework.charm_dir} Environment="PYTHONPATH={self._charm.framework.charm_dir / "venv"}" - ExecStart={python_bin} {__file__} {self._charm.unit.name} + ExecStart=/usr/bin/python3 {__file__} {self._charm.unit.name} [Install] WantedBy=multi-user.target diff --git a/src/charm.py b/src/charm.py index 7ddbecf..3a84d37 100755 --- a/src/charm.py +++ b/src/charm.py @@ -7,7 +7,6 @@ import logging from pathlib import Path -import distro from charms.fluentbit.v0.fluentbit import FluentbitClient from charms.operator_libs_linux.v0.juju_systemd_notices import ( ServiceStartedEvent, @@ -48,12 +47,6 @@ def __init__(self, *args, **kwargs): self._slurmd = Slurmd(self, "slurmd") self._systemd_notices = SystemdNotices(self, ["slurmd"]) - # use an specific python version for CentOS 7 - if distro.id() == "centos": - self._python_bin = "/usr/bin/env python3.8" - else: - self._python_bin = "/usr/bin/python3" - event_handler_bindings = { self.on.install: self._on_install, self.on.upgrade_charm: self._on_upgrade, @@ -91,12 +84,12 @@ def _on_install(self, event): successful_installation = self._slurm_manager.install( self.config.get("custom-slurm-repo"), nhc_path ) - slurmd.override_service(self._python_bin) + slurmd.override_service() logger.debug(f"### slurmd installed: {successful_installation}") if successful_installation: self._stored.slurm_installed = True - self._systemd_notices.subscribe(self._python_bin) + self._systemd_notices.subscribe() else: self.unit.status = BlockedStatus("Error installing slurmd") event.defer() diff --git a/src/constants.py b/src/constants.py new file mode 100644 index 0000000..2ee71cc --- /dev/null +++ b/src/constants.py @@ -0,0 +1,20 @@ +#!/usr/bin/python3 +# Copyright 2023 Canonical Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""File containing constants to be used in the charm.""" + +import distro + +DISTRO_ID = distro.id() diff --git a/src/utils/slurmd.py b/src/utils/slurmd.py index 1e4204e..b8fca51 100644 --- a/src/utils/slurmd.py +++ b/src/utils/slurmd.py @@ -28,6 +28,7 @@ from pathlib import Path import charms.operator_libs_linux.v1.systemd as systemd +from constants import DISTRO_ID _logger = logging.getLogger(__name__) @@ -59,18 +60,15 @@ def override_default(host: str, port: int) -> None: textwrap.dedent( f""" SLURMD_OPTIONS="--conf-server {host}:{port}" - PYTHONPATH={Path.cwd() / "lib"} + PYTHONPATH={Path.cwd() / "lib"}:{Path.cwd() / "venv"} """ ).strip() ) -def override_service(python_bin: str) -> None: +def override_service() -> None: """Override the default slurmd systemd service file. - Args: - python_bin: Path of Python's binary file. - Notes: This method makes an invokes `systemd daemon-reload` after writing the overrides.conf file for slurmd. This invocation will reload @@ -80,6 +78,12 @@ def override_service(python_bin: str) -> None: if not (override_dir := Path("/etc/systemd/system/slurmd.service.d")).is_dir(): override_dir.mkdir() + # use an specific python version for CentOS 7 + if DISTRO_ID == "centos": + python_bin = "/usr/bin/env python3.8" + else: + python_bin = "/usr/bin/python3" + overrides = override_dir / "99-slurmd-charm.conf" overrides.write_text( textwrap.dedent( diff --git a/tests/unit/utils/test_slurmd.py b/tests/unit/utils/test_slurmd.py index 5919f95..bef4774 100644 --- a/tests/unit/utils/test_slurmd.py +++ b/tests/unit/utils/test_slurmd.py @@ -48,14 +48,14 @@ def test_overwrite_default(self, _) -> None: @patch("charms.operator_libs_linux.v1.systemd._systemctl") def test_override_service_no_service_d(self, *_) -> None: """Test override_service(...) when slurmd.service.d does not exist.""" - slurmd.override_service(python_bin="/usr/bin/python3") + slurmd.override_service() @patch("pathlib.Path.is_dir", return_value=True) @patch("pathlib.Path.write_text") @patch("charms.operator_libs_linux.v1.systemd._systemctl") def test_override_service_yes_service_d(self, *_) -> None: """Test override_service(...) when slurmd.service.d exists.""" - slurmd.override_service(python_bin="/usr/bin/python3") + slurmd.override_service() @patch("datetime.timedelta", return_value=datetime.timedelta(-1)) def test_start_slurmd_service_fail(self, _) -> None: