move CommandRunner to its own file

This commit is contained in:
Michael Hudson-Doyle 2021-11-04 16:21:43 +13:00
parent 386e19b0ea
commit ba7892e91c
2 changed files with 73 additions and 51 deletions

View File

@ -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):

View File

@ -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 <http://www.gnu.org/licenses/>.
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)