Skip to content
This repository has been archived by the owner on Aug 9, 2024. It is now read-only.

Commit

Permalink
Include constants.py and update dispatch
Browse files Browse the repository at this point in the history
  • Loading branch information
jaimesouza committed Oct 12, 2023
1 parent 090f095 commit b9b8287
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 27 deletions.
5 changes: 2 additions & 3 deletions dispatch
Original file line number Diff line number Diff line change
Expand Up @@ -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' ]]
Expand Down
11 changes: 3 additions & 8 deletions lib/charms/operator_libs_linux/v0/juju_systemd_notices.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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"""
Expand All @@ -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
Expand Down
11 changes: 2 additions & 9 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand Down
20 changes: 20 additions & 0 deletions src/constants.py
Original file line number Diff line number Diff line change
@@ -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()
14 changes: 9 additions & 5 deletions src/utils/slurmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/utils/test_slurmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit b9b8287

Please sign in to comment.