Sunil Mohan Adapa 222563a482
*: Use privileged decorator for service actions
Tests:

- DONE: Unit tests work
- DONE: Transmission
  - DONE: Enabling/disabling an app with a daemon works: transmission
  - DONE: Showing the status of whether the app is enabled with daemon
    is-enabled works.
  - DONE: A message is shown if app is enabled and service is not running
  - DONE: Service is stopped and re-started during backup
  - DONE: Adding user to share group during initial setup restarts the service
- Not tested: Enabling/disabling a service with alias works (no such apps)
- DONE: Restarting/try-restarting a service works
- DONE: Masking/unmasking works
  - DONE: rsyslog is masked after initial setup
  - DONE: systemd-journald is try-restarted during initial setup
- DONE: Avahi, email, security initial setup works
  - DONE: Fail2ban is unmasked and enabled
- DONE: Enabling/disabling fail2ban is security app works
- DONE: Enabling/disabling password authentication in SSH works
- ?? Let's encrypt
  - Services are try-restarted during certificate setup, obtain, renew
- Not tested: upgrade pagekite from version 1

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
2022-10-08 18:53:55 -04:00

119 lines
3.2 KiB
Python

# SPDX-License-Identifier: AGPL-3.0-or-later
"""List and handle system services."""
import os
from plinth import action_utils
from plinth import app as app_module
from plinth import cfg, module_loader
from plinth.actions import privileged
from plinth.daemon import Daemon, RelatedDaemon
cfg.read()
module_config_path = os.path.join(cfg.config_dir, 'modules-enabled')
@privileged
def start(service: str):
"""Start a service."""
_assert_service_is_managed_by_plinth(service)
action_utils.service_start(service)
@privileged
def stop(service: str):
"""Stop a running service."""
_assert_service_is_managed_by_plinth(service)
action_utils.service_stop(service)
@privileged
def enable(service: str):
"""Enable a service so that it start on system boot."""
_assert_service_is_managed_by_plinth(service)
action_utils.service_enable(service)
@privileged
def disable(service: str):
"""Disable a service so that it does not start on system boot."""
_assert_service_is_managed_by_plinth(service)
action_utils.service_disable(service)
@privileged
def restart(service: str):
"""Restart a service."""
_assert_service_is_managed_by_plinth(service)
action_utils.service_restart(service)
@privileged
def try_restart(service: str):
"""Restart a service if it is running."""
_assert_service_is_managed_by_plinth(service)
action_utils.service_try_restart(service)
@privileged
def reload(service: str):
"""Reload a service."""
_assert_service_is_managed_by_plinth(service)
action_utils.service_reload(service)
@privileged
def mask(service: str):
"""Mask a service."""
_assert_service_is_managed_by_plinth(service)
action_utils.service_mask(service)
@privileged
def unmask(service: str):
"""Unmask a service."""
_assert_service_is_managed_by_plinth(service)
action_utils.service_unmask(service)
@privileged
def is_enabled(service: str) -> bool:
"""Return whether a service is enabled."""
_assert_service_is_managed_by_plinth(service)
return action_utils.service_is_enabled(service)
@privileged
def is_running(service: str) -> bool:
"""Return whether a service is running."""
_assert_service_is_managed_by_plinth(service)
return action_utils.service_is_running(service)
def _get_managed_services():
"""Get a set of all services managed by FreedomBox."""
services = set()
module_loader.load_modules()
app_module.apps_init()
for app in app_module.App.list():
components = app.get_components_of_type(Daemon)
for component in components:
services.add(component.unit)
if component.alias:
services.add(component.alias)
components = app.get_components_of_type(RelatedDaemon)
for component in components:
services.add(component.unit)
return services
def _assert_service_is_managed_by_plinth(service_name):
managed_services = _get_managed_services()
if service_name not in managed_services:
msg = ("The service '%s' is not managed by FreedomBox. Access is only "
"permitted for services listed in the 'managed_services' "
"variable of any FreedomBox app.") % service_name
raise ValueError(msg)