From ba7892e91caba8bff5fa59f6dd9f443829cc5736 Mon Sep 17 00:00:00 2001 From: Michael Hudson-Doyle Date: Thu, 4 Nov 2021 16:21:43 +1300 Subject: [PATCH] move CommandRunner to its own file --- subiquity/server/controllers/install.py | 58 +++------------------- subiquity/server/runner.py | 66 +++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 51 deletions(-) create mode 100644 subiquity/server/runner.py diff --git a/subiquity/server/controllers/install.py b/subiquity/server/controllers/install.py index b156d2c8..d9efee46 100644 --- a/subiquity/server/controllers/install.py +++ b/subiquity/server/controllers/install.py @@ -20,7 +20,6 @@ import logging import os import re import shutil -import subprocess import sys from curtin.commands.install import ( @@ -35,20 +34,20 @@ from subiquitycore.async_helpers import ( run_in_thread, ) from subiquitycore.context import Status, with_context -from subiquitycore.utils import ( - astart_command, - ) from subiquity.common.errorreport import ErrorReportKind -from subiquity.server.controller import ( - SubiquityController, - ) from subiquity.common.types import ( ApplicationState, ) from subiquity.journald import ( journald_listen, ) +from subiquity.server.controller import ( + SubiquityController, + ) +from subiquity.server.runner import ( + get_command_runner, + ) log = logging.getLogger("subiquity.server.controllers.install") @@ -72,45 +71,6 @@ class TracebackExtractor: self.traceback.append(line) -class LoggedCommandRunner: - - def __init__(self, ident): - self.ident = ident - - async def start(self, cmd): - return await astart_command([ - 'systemd-cat', '--level-prefix=false', '--identifier='+self.ident, - ] + cmd) - - async def run(self, cmd): - proc = await self.start(cmd) - await proc.communicate() - if proc.returncode != 0: - raise subprocess.CalledProcessError(proc.returncode, cmd) - else: - return subprocess.CompletedProcess(cmd, proc.returncode) - - -class DryRunCommandRunner(LoggedCommandRunner): - - def __init__(self, ident, delay): - super().__init__(ident) - self.delay = delay - - async def start(self, cmd): - if 'scripts/replay-curtin-log.py' in cmd: - delay = 0 - else: - cmd = ['echo', 'not running:'] + cmd - if 'unattended-upgrades' in cmd: - delay = 3*self.delay - else: - delay = self.delay - proc = await super().start(cmd) - await asyncio.sleep(delay) - return proc - - class CurtinCommandRunner: def __init__(self, runner, event_syslog_id, config_location): @@ -206,11 +166,7 @@ class InstallController(SubiquityController): self.unattended_upgrades_ctx = None self._event_syslog_id = 'curtin_event.%s' % (os.getpid(),) self.tb_extractor = TracebackExtractor() - if self.app.opts.dry_run: - self.command_runner = DryRunCommandRunner( - self.app.log_syslog_id, 2/self.app.scale_factor) - else: - self.command_runner = LoggedCommandRunner(self.app.log_syslog_id) + self.command_runner = get_command_runner(app) self.curtin_runner = None def stop_uu(self): diff --git a/subiquity/server/runner.py b/subiquity/server/runner.py new file mode 100644 index 00000000..c98fc066 --- /dev/null +++ b/subiquity/server/runner.py @@ -0,0 +1,66 @@ +# Copyright 2021 Canonical, Ltd. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +import asyncio +import subprocess + +from subiquitycore.utils import astart_command + + +class LoggedCommandRunner: + + def __init__(self, ident): + self.ident = ident + + async def start(self, cmd): + return await astart_command([ + 'systemd-cat', '--level-prefix=false', '--identifier='+self.ident, + ] + cmd) + + async def run(self, cmd): + proc = await self.start(cmd) + await proc.communicate() + if proc.returncode != 0: + raise subprocess.CalledProcessError(proc.returncode, cmd) + else: + return subprocess.CompletedProcess(cmd, proc.returncode) + + +class DryRunCommandRunner(LoggedCommandRunner): + + def __init__(self, ident, delay): + super().__init__(ident) + self.delay = delay + + async def start(self, cmd): + if 'scripts/replay-curtin-log.py' in cmd: + delay = 0 + else: + cmd = ['echo', 'not running:'] + cmd + if 'unattended-upgrades' in cmd: + delay = 3*self.delay + else: + delay = self.delay + proc = await super().start(cmd) + await asyncio.sleep(delay) + return proc + + +def get_command_runner(app): + if app.opts.dry_run: + return DryRunCommandRunner( + app.log_syslog_id, 2/app.scale_factor) + else: + return LoggedCommandRunner(app.log_syslog_id)