*: Use action_utils.run instead of subprocess.run

- This is to capture stdout and stderr and transmit that from privileged daemon
back to the service to be displayed in HTML.

Tests:

- Unit tests and code checks pass.

- Some of the modified actions work as expected.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: Veiko Aasa <veiko17@disroot.org>
This commit is contained in:
Sunil Mohan Adapa authored and Veiko Aasa committed 2025-09-29 16:58:53 +03:00
1 parent 355812c9f2
commit 61ff15a04f
36 files changed
+257 -244

No files matched your search

+7 -8
View File
@@ -5,7 +5,6 @@ import contextlib
import datetime
import logging
import pathlib
import subprocess
from datetime import timezone
from typing import Generator
@@ -218,11 +217,11 @@ def _snapshot_run_and_disable() -> Generator[None, None, None]:
try:
logger.info('Taking a snapshot before dist upgrade...')
command = ['snapper', 'create', '--description', 'before dist-upgrade']
subprocess.run(command, check=True)
action_utils.run(command, check=True)
aug = snapshot_module.load_augeas()
if snapshot_module.is_apt_snapshots_enabled(aug):
logger.info('Disabling apt snapshots during dist upgrade...')
subprocess.run([
action_utils.run([
'/usr/bin/freedombox-cmd',
'snapshot',
'disable_apt_snapshot',
@@ -235,7 +234,7 @@ def _snapshot_run_and_disable() -> Generator[None, None, None]:
finally:
if reenable:
logger.info('Re-enabling apt snapshots...')
subprocess.run([
action_utils.run([
'/usr/bin/freedombox-cmd', 'snapshot', 'disable_apt_snapshot'
], input='{"args": ["no"], "kwargs": {}}'.encode(), check=True)
else:
@@ -303,7 +302,7 @@ def _apt_update():
def _apt_fix():
"""Try to fix any problems with apt/dpkg before the upgrade."""
logger.info('Fixing any broken apt/dpkg states...')
subprocess.run(['dpkg', '--configure', '-a'], check=False)
action_utils.run(['dpkg', '--configure', '-a'], check=False)
_apt_run(['--fix-broken', 'install'])
@@ -341,7 +340,7 @@ def _unattended_upgrades_run():
To handle upgrading the freedombox package.
"""
logger.info('Running unattended-upgrade...')
subprocess.run(['unattended-upgrade', '--verbose'], check=False)
action_utils.run(['unattended-upgrade', '--verbose'], check=False)
def _freedombox_restart():
@@ -360,7 +359,7 @@ def _trigger_on_complete():
# file will not be possible. For that, we need to launch a new process with
# a different systemd service (which does not have the bind mounts).
logger.info('Triggering on-complete to commit sources.lists')
subprocess.run([
action_utils.run([
'systemd-run', '--unit=freedombox-dist-upgrade-on-complete',
'--description=Finish up upgrade to new stable Debian release',
'/usr/bin/freedombox-cmd', 'upgrades', 'dist_upgrade_on_complete',
@@ -417,7 +416,7 @@ def start_service():
'--property=KillMode=process', '--property=TimeoutSec=72hr',
f'--property=BindPaths={temp_sources_list}:{sources_list}'
]
subprocess.run(['systemd-run'] + args + [
action_utils.run(['systemd-run'] + args + [
'systemd-inhibit', '/usr/bin/freedombox-cmd', 'upgrades',
'dist_upgrade', '--no-args'
], check=True)
+4 -3
View File
@@ -7,6 +7,7 @@ import pathlib
import re
import subprocess
from plinth import action_utils
from plinth.action_utils import (apt_hold_flag, apt_unhold_freedombox,
is_package_manager_busy, run_apt_command,
service_is_running)
@@ -130,14 +131,14 @@ def release_held_packages():
output = subprocess.check_output(['apt-mark', 'showhold']).decode().strip()
holds = output.split('\n')
logger.info('Releasing package holds: %s', holds)
subprocess.run(['apt-mark', 'unhold', *holds], stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL, check=True)
action_utils.run(['apt-mark', 'unhold', *holds], stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL, check=True)
@privileged
def run():
"""Run unattended-upgrades."""
subprocess.run(['dpkg', '--configure', '-a'], check=False)
action_utils.run(['dpkg', '--configure', '-a'], check=False)
run_apt_command(['--fix-broken', 'install'])
_release_held_freedombox()
@@ -219,7 +219,7 @@ def test_snapshot_run_and_disable(is_supported, is_apt_snapshots_enabled, run):
with distupgrade._snapshot_run_and_disable():
assert run.call_args_list == [
call(['snapper', 'create', '--description', 'before dist-upgrade'],
check=True)
stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True)
]
run.reset_mock()
@@ -230,16 +230,18 @@ def test_snapshot_run_and_disable(is_supported, is_apt_snapshots_enabled, run):
with distupgrade._snapshot_run_and_disable():
assert run.call_args_list == [
call(['snapper', 'create', '--description', 'before dist-upgrade'],
check=True),
stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True),
call([
'/usr/bin/freedombox-cmd', 'snapshot', 'disable_apt_snapshot'
], input=b'{"args": ["yes"], "kwargs": {}}', check=True)
], input=b'{"args": ["yes"], "kwargs": {}}',
stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True)
]
run.reset_mock()
assert run.call_args_list == [
call(['/usr/bin/freedombox-cmd', 'snapshot', 'disable_apt_snapshot'],
input=b'{"args": ["no"], "kwargs": {}}', check=True)
input=b'{"args": ["no"], "kwargs": {}}', stdout=subprocess.PIPE,
stderr=subprocess.PIPE, check=True)
]
@@ -278,8 +280,10 @@ def test_apt_hold_packages(check_output, check_call, run, tmp_path):
expected_call = [call(['apt-mark', 'hold', 'freedombox'])]
assert check_call.call_args_list == expected_call
expected_calls = [
call(['apt-mark', 'hold', 'package1'], check=False),
call(['apt-mark', 'hold', 'package2'], check=False)
call(['apt-mark', 'hold', 'package1'], stdout=subprocess.PIPE,
stderr=subprocess.PIPE, check=False),
call(['apt-mark', 'hold', 'package2'], stdout=subprocess.PIPE,
stderr=subprocess.PIPE, check=False)
]
assert run.call_args_list == expected_calls
check_call.reset_mock()
@@ -340,7 +344,8 @@ def test_apt_fix(run, apt_run):
"""Test that apt fixes work."""
distupgrade._apt_fix()
assert run.call_args_list == [
call(['dpkg', '--configure', '-a'], check=False)
call(['dpkg', '--configure', '-a'], stdout=subprocess.PIPE,
stderr=subprocess.PIPE, check=False)
]
assert apt_run.call_args_list == [call(['--fix-broken', 'install'])]
@@ -365,7 +370,9 @@ def test_apt_full_upgrade(apt_run):
def test_unatteneded_upgrades_run(run):
"""Test that running unattended upgrades works."""
distupgrade._unattended_upgrades_run()
run.assert_called_with(['unattended-upgrade', '--verbose'], check=False)
run.assert_called_with(['unattended-upgrade', '--verbose'],
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
check=False)
@patch('plinth.action_utils.service_restart')
@@ -384,7 +391,7 @@ def test_trigger_on_complete(run):
'--description=Finish up upgrade to new stable Debian release',
'/usr/bin/freedombox-cmd', 'upgrades', 'dist_upgrade_on_complete',
'--no-args'
], check=True)
], stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True)
def test_on_complete(tmp_path):