Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use warnings.warn instead of self.log.warning to help avoid duplications #133

Merged
merged 2 commits into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion systemdspawner/systemd.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,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(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warnings almost always want a stacklevel (usually but not always 2), but I think this is actually a case where it wouldn't be helpful, so 👍 here.

f"systemd version {SYSTEMD_LOWEST_RECOMMENDED_VERSION} or higher is recommended, version {systemd_version} is used"
)

Expand Down