daemon: Allow using an alias when enabling a daemon

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2020-05-07 20:15:50 -07:00 committed by James Valleroy
parent 7f7b5b4e67
commit 6d2f4b6cea
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
2 changed files with 26 additions and 2 deletions

View File

@ -15,7 +15,7 @@ from plinth import action_utils, actions, app
class Daemon(app.LeaderComponent):
"""Component to manage a background daemon or any systemd unit."""
def __init__(self, component_id, unit, strict_check=False,
listen_ports=None):
listen_ports=None, alias=None):
"""Initialize a new daemon component.
'component_id' must be a unique string across all apps and components
@ -28,12 +28,24 @@ class Daemon(app.LeaderComponent):
'tcp', 'udp4', 'udp6', 'udp' indicating the protocol that the daemon
listens on. This information is used to run diagnostic tests.
'alias' is an alternate name for the same unit file. When a unit file
is renamed, the new unit file usually contains an Alias= setting in
[Install] section with value of old unit name. When the unit is
enabled, a symlink with the name of the alias is created. All
operations such as is-enabled, is-running and disable work with the
alias along with the primary unit name. However, for the case of
enabling the unit file, the alias does not work. To be able to provide
management for multiple versions of the unit file with different names,
specify an alias. Both the names are taken into consideration when
enabling the unit file.
"""
super().__init__(component_id)
self.unit = unit
self.strict_check = strict_check
self.listen_ports = listen_ports or []
self.alias = alias
def is_enabled(self):
"""Return if the daemon/unit is enabled."""
@ -43,6 +55,8 @@ class Daemon(app.LeaderComponent):
def enable(self):
"""Run operations to enable the daemon/unit."""
actions.superuser_run('service', ['enable', self.unit])
if self.alias:
actions.superuser_run('service', ['enable', self.alias])
def disable(self):
"""Run operations to disable the daemon/unit."""

View File

@ -29,12 +29,14 @@ def test_initialization():
assert daemon.unit == 'test-unit'
assert not daemon.strict_check
assert daemon.listen_ports == []
assert daemon.alias is None
listen_ports = [(345, 'tcp4'), (123, 'udp')]
daemon = Daemon('test-daemon', 'test-unit', strict_check=True,
listen_ports=listen_ports)
listen_ports=listen_ports, alias='test-unit-2')
assert daemon.strict_check
assert daemon.listen_ports == listen_ports
assert daemon.alias == 'test-unit-2'
@patch('plinth.action_utils.service_is_enabled')
@ -60,6 +62,13 @@ def test_enable(superuser_run, daemon):
daemon.enable()
superuser_run.assert_has_calls([call('service', ['enable', 'test-unit'])])
daemon.alias = 'test-unit-2'
daemon.enable()
superuser_run.assert_has_calls([
call('service', ['enable', 'test-unit']),
call('service', ['enable', 'test-unit-2'])
])
@patch('plinth.actions.superuser_run')
def test_disable(superuser_run, daemon):
@ -83,6 +92,7 @@ def test_is_running(service_is_running, daemon):
@patch('plinth.daemon.diagnose_port_listening')
def test_diagnose(port_listening, service_is_running, daemon):
"""Test running diagnostics."""
def side_effect(port, kind):
return [f'test-result-{port}-{kind}', 'passed']