curtin-replay: improve CLI of replay-curtin-log

We now use argparse on replay-curtin-log, which provides the --help
option along with named parameters for what used to be positional
parameters only.

Signed-off-by: Olivier Gayot <olivier.gayot@canonical.com>
This commit is contained in:
Olivier Gayot 2022-07-25 13:12:32 +02:00
parent d1dc7d4844
commit 3efed0e211
2 changed files with 29 additions and 13 deletions

View File

@ -1,16 +1,18 @@
#!/usr/bin/python3
""" Script that replays curtin events from a journald export.
curtin events are injected back in journald and log lines are written to a log
file. """
import argparse
import json
import os
import sys
import time
from typing import TextIO
from systemd import journal
json_file = sys.argv[1]
event_identifier = sys.argv[2]
log_location = sys.argv[3]
scale_factor = float(os.environ.get('SUBIQUITY_REPLAY_TIMESCALE', "4"))
def time_for_entry(e):
@ -18,7 +20,7 @@ def time_for_entry(e):
rc = 0
def report(e, log_file):
def report(e, log_file: TextIO, event_identifier: str):
global rc
if e['SYSLOG_IDENTIFIER'].startswith("curtin_event"):
e['SYSLOG_IDENTIFIER'] = event_identifier
@ -30,18 +32,31 @@ def report(e, log_file):
elif r == "FAIL":
rc = 1
elif e['SYSLOG_IDENTIFIER'].startswith("curtin_log") and scale_factor < 10:
print(e['MESSAGE'], flush=True)
log_file.write(e['MESSAGE'] + '\n')
with open(log_location, 'w') as fp:
def main() -> int:
""" Entry point. """
parser = argparse.ArgumentParser()
parser.add_argument("replay-file")
parser.add_argument("--event-identifier", required=True)
parser.add_argument("--output", type=argparse.FileType("w"), default="-")
args = vars(parser.parse_args())
prev_ev = None
for line in open(json_file):
for line in open(args["replay-file"]):
ev = json.loads(line.strip())
if prev_ev is not None:
report(prev_ev, fp)
report(prev_ev, args["output"],
event_identifier=args["event_identifier"])
delay = time_for_entry(ev) - time_for_entry(prev_ev)
time.sleep(min(delay, 8)/scale_factor)
prev_ev = ev
report(ev, fp)
report(ev, args["output"], event_identifier=args["event_identifier"])
return rc
sys.exit(rc)
if __name__ == "__main__":
sys.exit(main())

View File

@ -139,9 +139,10 @@ class _DryRunCurtinCommand(_CurtinCommand):
return [
sys.executable,
"scripts/replay-curtin-log.py",
"--event-identifier", self._event_syslog_id,
"--output", self.opts.output_base + INSTALL_LOG,
"--",
self.event_file,
self._event_syslog_id,
self.opts.output_base + INSTALL_LOG,
]
else:
return super().make_command(command, *args, config=config)