mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-09-19 04:59:01 +00:00
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:
parent
7f7b5b4e67
commit
6d2f4b6cea
@ -15,7 +15,7 @@ from plinth import action_utils, actions, app
|
|||||||
class Daemon(app.LeaderComponent):
|
class Daemon(app.LeaderComponent):
|
||||||
"""Component to manage a background daemon or any systemd unit."""
|
"""Component to manage a background daemon or any systemd unit."""
|
||||||
def __init__(self, component_id, unit, strict_check=False,
|
def __init__(self, component_id, unit, strict_check=False,
|
||||||
listen_ports=None):
|
listen_ports=None, alias=None):
|
||||||
"""Initialize a new daemon component.
|
"""Initialize a new daemon component.
|
||||||
|
|
||||||
'component_id' must be a unique string across all apps and components
|
'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
|
'tcp', 'udp4', 'udp6', 'udp' indicating the protocol that the daemon
|
||||||
listens on. This information is used to run diagnostic tests.
|
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)
|
super().__init__(component_id)
|
||||||
|
|
||||||
self.unit = unit
|
self.unit = unit
|
||||||
self.strict_check = strict_check
|
self.strict_check = strict_check
|
||||||
self.listen_ports = listen_ports or []
|
self.listen_ports = listen_ports or []
|
||||||
|
self.alias = alias
|
||||||
|
|
||||||
def is_enabled(self):
|
def is_enabled(self):
|
||||||
"""Return if the daemon/unit is enabled."""
|
"""Return if the daemon/unit is enabled."""
|
||||||
@ -43,6 +55,8 @@ class Daemon(app.LeaderComponent):
|
|||||||
def enable(self):
|
def enable(self):
|
||||||
"""Run operations to enable the daemon/unit."""
|
"""Run operations to enable the daemon/unit."""
|
||||||
actions.superuser_run('service', ['enable', self.unit])
|
actions.superuser_run('service', ['enable', self.unit])
|
||||||
|
if self.alias:
|
||||||
|
actions.superuser_run('service', ['enable', self.alias])
|
||||||
|
|
||||||
def disable(self):
|
def disable(self):
|
||||||
"""Run operations to disable the daemon/unit."""
|
"""Run operations to disable the daemon/unit."""
|
||||||
|
|||||||
@ -29,12 +29,14 @@ def test_initialization():
|
|||||||
assert daemon.unit == 'test-unit'
|
assert daemon.unit == 'test-unit'
|
||||||
assert not daemon.strict_check
|
assert not daemon.strict_check
|
||||||
assert daemon.listen_ports == []
|
assert daemon.listen_ports == []
|
||||||
|
assert daemon.alias is None
|
||||||
|
|
||||||
listen_ports = [(345, 'tcp4'), (123, 'udp')]
|
listen_ports = [(345, 'tcp4'), (123, 'udp')]
|
||||||
daemon = Daemon('test-daemon', 'test-unit', strict_check=True,
|
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.strict_check
|
||||||
assert daemon.listen_ports == listen_ports
|
assert daemon.listen_ports == listen_ports
|
||||||
|
assert daemon.alias == 'test-unit-2'
|
||||||
|
|
||||||
|
|
||||||
@patch('plinth.action_utils.service_is_enabled')
|
@patch('plinth.action_utils.service_is_enabled')
|
||||||
@ -60,6 +62,13 @@ def test_enable(superuser_run, daemon):
|
|||||||
daemon.enable()
|
daemon.enable()
|
||||||
superuser_run.assert_has_calls([call('service', ['enable', 'test-unit'])])
|
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')
|
@patch('plinth.actions.superuser_run')
|
||||||
def test_disable(superuser_run, daemon):
|
def test_disable(superuser_run, daemon):
|
||||||
@ -83,6 +92,7 @@ def test_is_running(service_is_running, daemon):
|
|||||||
@patch('plinth.daemon.diagnose_port_listening')
|
@patch('plinth.daemon.diagnose_port_listening')
|
||||||
def test_diagnose(port_listening, service_is_running, daemon):
|
def test_diagnose(port_listening, service_is_running, daemon):
|
||||||
"""Test running diagnostics."""
|
"""Test running diagnostics."""
|
||||||
|
|
||||||
def side_effect(port, kind):
|
def side_effect(port, kind):
|
||||||
return [f'test-result-{port}-{kind}', 'passed']
|
return [f'test-result-{port}-{kind}', 'passed']
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user