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

integration: add background runner script #1594

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
35 changes: 35 additions & 0 deletions scripts/background-runner
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/python3

# Use screen to run a terminal program in the background until it exists.
Copy link
Member

Choose a reason for hiding this comment

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

s/exists/exits/

# This process stays around in the forground.
Copy link
Member

Choose a reason for hiding this comment

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

s/forground/foreground/

# Exit code from the program being run is returned here.

import argparse
import subprocess
import sys
import tempfile


def main():
parser = argparse.ArgumentParser()
parser.add_argument('-l', '--logfile', default=None,
help='append output to logfile')
args, program = parser.parse_known_args()
Copy link
Member

Choose a reason for hiding this comment

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

sadly, there seems to be no way to tell argparse to parse options only at the beginning of the CLI. This means that doing:

scripts/background-runner ls -l /root

will interpret /root as the logfile for background-runner, and not an option of ls.

I suggest to disable prefix matching (i.e., setting allow_abbrev=False when instantiating ArgumentParser) to at least avoid stealing options that look like valid background-runner options.

Another alternative is to insert -- between the background-runner options and the rest of the CLI, e.g.:

scripts/background-runner -- ls -l

but with parse_known_args, argparse considers -- to be an extra argument, so it ends up first in program. We would need to remove it manually before calling subprocess.run:

parser.parse_known_args(["-l", "/tmp/log", "--", "ls", "/root"])
(Namespace(logfile='/tmp/log'), ['--', 'ls', '/root'])


cmd = [
'screen',
'-c', '/dev/null', # ignore any user configuration
'-D', '-m', # run detached from a terminal perspective,
# but the screen process doesn't background
]
if args.logfile:
Copy link
Member

Choose a reason for hiding this comment

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

is not None?

cmd.extend(('-L', '-Logfile', args.logfile))

with tempfile.NamedTemporaryFile() as exitfile:
cmd.extend(('./scripts/capture-exit-code', exitfile.name, *program))
subprocess.run(cmd, check=True)
sys.exit(int(exitfile.read()))


if __name__ == '__main__':
main()
11 changes: 11 additions & 0 deletions scripts/capture-exit-code
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh -u

# Take a filename argument. The rest of the arguments are a program to run.
# Save the exit code of that program to the filename arg.

exitfile="$1"
shift
"$@"
ec=$?
echo $ec > "$exitfile"
exit $ec
24 changes: 17 additions & 7 deletions scripts/runtests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,10 @@ for answers in examples/answers*.yaml; do
opts+=(--dry-run-config "$dr_config")
fi
# The --foreground is important to avoid subiquity getting SIGTTOU-ed.
LANG=C.UTF-8 timeout --foreground 60 \
python3 -m subiquity.cmd.tui < "$tty" \
./scripts/background-runner -l $tmpdir/tui.log \
env LANG=C.UTF-8 \
timeout --foreground 60 \
python3 -m subiquity.cmd.tui \
--dry-run \
--output-base "$tmpdir" \
--answers "$answers" \
Expand All @@ -196,8 +198,10 @@ for answers in examples/answers*.yaml; do
reconf_settings="true"
validate_subtype="answers-reconf"
fi
DRYRUN_RECONFIG="$reconf_settings" LANG=C.UTF-8 timeout --foreground 60 \
python3 -m system_setup.cmd.tui < "$tty" \
./scripts/background-runner -l $tmpdir/tui.log \
env DRYRUN_RECONFIG="$reconf_settings" LANG=C.UTF-8 \
timeout --foreground 60 \
python3 -m system_setup.cmd.tui \
--dry-run \
--answers "$answers" \
--output-base "$tmpdir"
Expand All @@ -207,7 +211,9 @@ for answers in examples/answers*.yaml; do
clean
done

LANG=C.UTF-8 timeout --foreground 60 \
./scripts/background-runner -l $tmpdir/tui.log \
env LANG=C.UTF-8 \
timeout --foreground 60 \
python3 -m subiquity.cmd.tui \
--dry-run \
--output-base "$tmpdir" \
Expand Down Expand Up @@ -241,7 +247,9 @@ grep -q 'finish: subiquity/Install/install/postinstall/run_unattended_upgrades:
$tmpdir/subiquity-server-debug.log

clean
LANG=C.UTF-8 timeout --foreground 60 \
./scripts/background-runner -l $tmpdir/tui.log \
env LANG=C.UTF-8 \
timeout --foreground 60 \
python3 -m subiquity.cmd.tui \
--dry-run \
--output-base "$tmpdir" \
Expand Down Expand Up @@ -275,7 +283,9 @@ if [ "${RELEASE%.*}" -ge 20 ]; then
# Test system_setup autoinstall.
for mode in "" "-full" "-no-shutdown"; do
clean
LANG=C.UTF-8 timeout --foreground 60 \
./scripts/background-runner -l $tmpdir/tui.log \
env LANG=C.UTF-8 \
timeout --foreground 60 \
python3 -m system_setup.cmd.tui \
--dry-run \
--output-base "$tmpdir" \
Expand Down