Skip to content

Commit

Permalink
env: Add detection for environment using systemd-nspawn
Browse files Browse the repository at this point in the history
Signed-off-by: Nathan Chancellor <[email protected]>
  • Loading branch information
nathanchance committed Dec 24, 2024
1 parent 7014edd commit 9a157a6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
7 changes: 7 additions & 0 deletions fish/functions/using_nspawn.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env fish
# SPDX-License-Identifier: MIT
# Copyright (C) 2022-2023 Nathan Chancellor

function using_nspawn -d "Checks if host is using systemd-nspawn for development container"
PYTHONPATH=$PYTHON_FOLDER python3 -c 'import lib.utils, sys; sys.exit(0 if lib.utils.using_nspawn() else 1)'
end
11 changes: 11 additions & 0 deletions python/lib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ def in_container():
'/.dockerenv').is_file()


def in_nspawn():
# An nspawn container has to have systemd-detect-virt but this may not
# always run where systemd-detect-virt exists.
return shutil.which('systemd-detect-virt') and detect_virt('-c') == 'systemd-nspawn'


def path_and_text(*args):
if (path := Path(*args)).exists():
return path, path.read_text(encoding='utf-8')
Expand Down Expand Up @@ -184,6 +190,11 @@ def run_check_rc_zero(*args, **kwargs):
return chronic(*args, **kwargs, check=False).returncode == 0


def using_nspawn():
etc_nspawn = Path('/etc/systemd/nspawn')
return etc_nspawn.exists() and list(etc_nspawn.iterdir())


def print_or_run_cmd(cmd, dryrun, end='\n\n'):
if dryrun:
print_cmd(cmd, end=end)
Expand Down

0 comments on commit 9a157a6

Please sign in to comment.