*: Use action_utils.run instead of subprocess.call

- 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 2025-08-14 21:47:47 -07:00 committed by Veiko Aasa
parent 80e6d940a4
commit bf9005ac48
No known key found for this signature in database
GPG Key ID: 478539CAE680674E
5 changed files with 15 additions and 17 deletions

View File

@ -28,9 +28,9 @@ def create_library(name: str):
calibre.validate_library_name(name)
library = LIBRARIES_PATH / name
library.mkdir(mode=0o755) # Raise exception if already exists
subprocess.call(
action_utils.run(
['calibredb', '--with-library', library, 'list_categories'],
stdout=subprocess.DEVNULL)
stdout=subprocess.DEVNULL, check=False)
# Force systemd StateDirectory= logic to assign proper ownership to the
# DynamicUser=

View File

@ -29,9 +29,8 @@ def fixture_patch():
path = pathlib.Path(args[0][2]) / 'metadata.db'
path.touch()
with patch('subprocess.call') as subprocess_call, \
patch('subprocess.run'), patch('shutil.chown'):
subprocess_call.side_effect = side_effect
with patch('subprocess.run') as subprocess_run, patch('shutil.chown'):
subprocess_run.side_effect = side_effect
yield

View File

@ -145,7 +145,7 @@ def pre_change_hostname(old_hostname: str, new_hostname: str):
logger.info('ejabberdctl not found')
return
subprocess.call(['ejabberdctl', 'backup', EJABBERD_BACKUP])
action_utils.run(['ejabberdctl', 'backup', EJABBERD_BACKUP], check=False)
subprocess.check_output([
'ejabberdctl', 'mnesia-change-nodename', 'ejabberd@' + old_hostname,
'ejabberd@' + new_hostname, EJABBERD_BACKUP, EJABBERD_BACKUP_NEW
@ -160,12 +160,12 @@ def change_hostname():
return
action_utils.service_stop('ejabberd')
subprocess.call(['pkill', '-u', 'ejabberd'])
action_utils.run(['pkill', '-u', 'ejabberd'], check=False)
# Make sure there aren't files in the Mnesia spool dir
os.makedirs('/var/lib/ejabberd/oldfiles', exist_ok=True)
subprocess.call('mv /var/lib/ejabberd/*.* /var/lib/ejabberd/oldfiles/',
shell=True)
action_utils.run('mv /var/lib/ejabberd/*.* /var/lib/ejabberd/oldfiles/',
shell=True, check=False)
action_utils.service_start('ejabberd')
@ -278,7 +278,7 @@ def mam(command: str) -> bool | None:
yaml.dump(conf, file_handle)
if action_utils.service_is_running('ejabberd'):
subprocess.call(['ejabberdctl', 'reload_config'])
action_utils.run(['ejabberdctl', 'reload_config'], check=False)
return None
@ -359,7 +359,7 @@ def configure_turn(turn_server_config: dict[str, Any], managed: bool):
Path(EJABBERD_MANAGED_COTURN).unlink(missing_ok=True)
if action_utils.service_is_running('ejabberd'):
subprocess.call(['ejabberdctl', 'reload_config'])
action_utils.run(['ejabberdctl', 'reload_config'], check=False)
def _get_version():

View File

@ -104,8 +104,8 @@ def _setup_firewall():
'firewall-cmd', '--zone', 'internal',
'--{}-interface'.format(operation), interface
]
subprocess.call(command)
subprocess.call(command + ['--permanent'])
action_utils.run(command, check=False)
action_utils.run(command + ['--permanent'], check=False)
def _is_tunplus_enabled():
"""Return whether tun+ interface is already added."""

View File

@ -1,18 +1,17 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Shutdown/restart the system."""
import subprocess
from plinth import action_utils
from plinth.actions import privileged
@privileged
def restart():
"""Restart the system."""
subprocess.call('reboot')
action_utils.run('reboot', check=False)
@privileged
def shutdown():
"""Shut down the system."""
subprocess.call(['shutdown', 'now'])
action_utils.run(['shutdown', 'now'], check=False)