Skip to content

Commit

Permalink
Merge pull request #133 from consideRatio/pr/use-warning.warn
Browse files Browse the repository at this point in the history
Use warnings.warn instead of self.log.warning to help avoid duplications
  • Loading branch information
consideRatio authored Jun 8, 2023
2 parents ba5d382 + 2149f4a commit 6940b36
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
15 changes: 14 additions & 1 deletion systemdspawner/systemd.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,25 @@ def get_systemd_version():
"""
try:
version_response = subprocess.check_output(["systemctl", "--version"])
except Exception as e:
warnings.warn(
f"Failed to run `systemctl --version` to get systemd version: {e}",
RuntimeWarning,
stacklevel=2,
)

try:
# Example response from Ubuntu 22.04:
#
# systemd 249 (249.11-0ubuntu3.9)
# +PAM +AUDIT +SELINUX +APPARMOR +IMA +SMACK +SECCOMP +GCRYPT +GNUTLS +OPENSSL +ACL +BLKID +CURL +ELFUTILS +FIDO2 +IDN2 -IDN +IPTC +KMOD +LIBCRYPTSETUP +LIBFDISK +PCRE2 -PWQUALITY -P11KIT -QRENCODE +BZIP2 +LZ4 +XZ +ZLIB +ZSTD -XKBCOMMON +UTMP +SYSVINIT default-hierarchy=unified
#
version = int(float(version_response.split()[1]))
return version
except:
except Exception as e:
warnings.warn(
f"Failed to parse systemd version from `systemctl --version`: {e}. output={version_response}",
RuntimeWarning,
stacklevel=2,
)
return None
9 changes: 5 additions & 4 deletions systemdspawner/systemdspawner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import pwd
import sys
import warnings

from jupyterhub.spawner import Spawner
from jupyterhub.utils import random_port
Expand Down Expand Up @@ -157,16 +158,16 @@ def __init__(self, *args, **kwargs):

systemd_version = systemd.get_systemd_version()
if systemd_version is None:
self.log.warning(
"Failed to parse systemd version from 'systemctl --version'"
)
# not found, nothing to check
# already warned about this in get_systemd_version
pass
elif systemd_version < SYSTEMD_REQUIRED_VERSION:
self.log.critical(
f"systemd version {SYSTEMD_REQUIRED_VERSION} or higher is required, version {systemd_version} is used"
)
sys.exit(1)
elif systemd_version < SYSTEMD_LOWEST_RECOMMENDED_VERSION:
self.log.warning(
warnings.warn(
f"systemd version {SYSTEMD_LOWEST_RECOMMENDED_VERSION} or higher is recommended, version {systemd_version} is used"
)

Expand Down

0 comments on commit 6940b36

Please sign in to comment.