Introduce uwsgi component to manage uWSGI configuration

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa authored and James Valleroy committed 2019-06-13 20:17:53 -04:00
1 parent 4100d36381
commit 862d87920a
7 files changed
+173 -28

No files matched your search

+38
View File
@@ -58,3 +58,41 @@ class Webserver(app.LeaderComponent):
actions.superuser_run(
'apache',
['disable', '--name', self.web_name, '--kind', self.kind])
class Uwsgi(app.LeaderComponent):
"""Component to enable/disable uWSGI configuration."""
def __init__(self, component_id, uwsgi_name):
"""Initialize the uWSGI component.
component_id should be a unique ID across all components of an app and
across all components.
uwsgi_name is the primary part of the configuration file path which must
be enabled/disabled by this component.
"""
super().__init__(component_id)
self.uwsgi_name = uwsgi_name
def is_enabled(self):
"""Return whether the uWSGI configuration is enabled."""
return action_utils.uwsgi_is_enabled(self.uwsgi_name) \
and action_utils.service_is_enabled('uwsgi')
def enable(self):
"""Enable the uWSGI configuration."""
actions.superuser_run('apache',
['uwsgi-enable', '--name', self.uwsgi_name])
def disable(self):
"""Disable the uWSGI configuration."""
actions.superuser_run('apache',
['uwsgi-disable', '--name', self.uwsgi_name])
def is_running(self):
"""Return whether the uWSGI daemon is running with configuration."""
return action_utils.uwsgi_is_enabled(self.uwsgi_name) \
and action_utils.service_is_running('uwsgi')
+78 -1
View File
@@ -22,7 +22,7 @@ from unittest.mock import call, patch
import pytest
from plinth.modules.apache.components import Webserver
from plinth.modules.apache.components import Uwsgi, Webserver
def test_webserver_init():
@@ -75,3 +75,80 @@ def test_webserver_disable(superuser_run):
call('apache',
['disable', '--name', 'test-config', '--kind', 'module'])
])
def test_uwsgi_init():
"""Test that uWSGI component can be initialized."""
with pytest.raises(ValueError):
Uwsgi(None, None)
uwsgi = Uwsgi('test-uwsgi', 'test-config')
assert uwsgi.component_id == 'test-uwsgi'
assert uwsgi.uwsgi_name == 'test-config'
@patch('plinth.action_utils.service_is_enabled')
@patch('plinth.action_utils.uwsgi_is_enabled')
def test_uwsgi_is_enabled(uwsgi_is_enabled, service_is_enabled):
"""Test that checking uwsgi configuration enabled works."""
uwsgi = Uwsgi('test-uwsgi', 'test-config')
uwsgi_is_enabled.return_value = True
service_is_enabled.return_value = True
assert uwsgi.is_enabled()
uwsgi_is_enabled.assert_has_calls([call('test-config')])
service_is_enabled.assert_has_calls([call('uwsgi')])
service_is_enabled.return_value = False
assert not uwsgi.is_enabled()
uwsgi_is_enabled.return_value = False
assert not uwsgi.is_enabled()
service_is_enabled.return_value = False
assert not uwsgi.is_enabled()
@patch('plinth.actions.superuser_run')
def test_uwsgi_enable(superuser_run):
"""Test that enabling uwsgi configuration works."""
uwsgi = Uwsgi('test-uwsgi', 'test-config')
uwsgi.enable()
superuser_run.assert_has_calls(
[call('apache', ['uwsgi-enable', '--name', 'test-config'])])
@patch('plinth.actions.superuser_run')
def test_uwsgi_disable(superuser_run):
"""Test that disabling uwsgi configuration works."""
uwsgi = Uwsgi('test-uwsgi', 'test-config')
uwsgi.disable()
superuser_run.assert_has_calls(
[call('apache', ['uwsgi-disable', '--name', 'test-config'])])
@patch('plinth.action_utils.service_is_running')
@patch('plinth.action_utils.uwsgi_is_enabled')
def test_uwsgi_is_running(uwsgi_is_enabled, service_is_running):
"""Test checking whether uwsgi is running with a configuration."""
uwsgi = Uwsgi('test-uwsgi', 'test-config')
uwsgi_is_enabled.return_value = True
service_is_running.return_value = True
assert uwsgi.is_running()
uwsgi_is_enabled.assert_has_calls([call('test-config')])
service_is_running.assert_has_calls([call('uwsgi')])
uwsgi_is_enabled.return_value = False
service_is_running.return_value = True
assert not uwsgi.is_running()
uwsgi_is_enabled.return_value = True
service_is_running.return_value = False
assert not uwsgi.is_running()
uwsgi_is_enabled.return_value = False
service_is_running.return_value = False
assert not uwsgi.is_running()
+32 -7
View File
@@ -30,7 +30,7 @@ from plinth import action_utils, actions
from plinth import app as app_module
from plinth import cfg, frontpage, menu
from plinth import service as service_module
from plinth.modules.apache.components import Webserver
from plinth.modules.apache.components import Uwsgi, Webserver
from plinth.modules.firewall.components import Firewall
from plinth.utils import format_lazy
@@ -99,6 +99,9 @@ class RadicaleApp(app_module.App):
webserver = RadicaleWebserver('webserver-radicale', None)
self.add(webserver)
uwsgi = RadicaleUwsgi('uwsgi-radicale', 'radicale')
self.add(uwsgi)
class RadicaleWebserver(Webserver):
"""Webserver enable/disable behavior specific for radicale."""
@@ -117,6 +120,30 @@ class RadicaleWebserver(Webserver):
"""Set the web name"""
class RadicaleUwsgi(Uwsgi):
"""uWSGI enable/disable behavior specific for radicale."""
def is_enabled(self):
"""Return whether the uWSGI configuration is enabled if version>=2."""
package_version = get_package_version()
if package_version and package_version >= VERSION_2:
return super().is_enabled()
return True
def enable(self):
"""Enable the uWSGI configuration if version >=2."""
package_version = get_package_version()
if package_version and package_version >= VERSION_2:
super().enable()
def disable(self):
"""Disable the uWSGI configuration if version >=2."""
package_version = get_package_version()
if package_version and package_version >= VERSION_2:
super().disable()
def init():
"""Initialize the radicale module."""
global app
@@ -184,19 +211,17 @@ def is_running():
if get_package_version() < VERSION_2:
return action_utils.service_is_running('radicale')
return action_utils.service_is_running('uwsgi') \
and action_utils.uwsgi_is_enabled('radicale')
return app.is_enabled()
def is_enabled():
"""Return whether the module is enabled."""
package_version = get_package_version()
if package_version >= VERSION_2:
daemon_enabled = action_utils.uwsgi_is_enabled('radicale')
else:
daemon_enabled = True
if package_version and package_version < VERSION_2:
daemon_enabled = action_utils.service_is_enabled('radicale')
return (app.is_enabled() and daemon_enabled)
return app.is_enabled() and daemon_enabled
def enable():
+5 -4
View File
@@ -26,7 +26,7 @@ from plinth import action_utils, actions
from plinth import app as app_module
from plinth import frontpage, menu
from plinth import service as service_module
from plinth.modules.apache.components import Webserver
from plinth.modules.apache.components import Uwsgi, Webserver
from plinth.modules.firewall.components import Firewall
from plinth.modules.users import register_group
@@ -88,6 +88,9 @@ class SearxApp(app_module.App):
'searx-freedombox-auth')
self.add(webserver)
uwsgi = Uwsgi('uwsgi-searx', 'searx')
self.add(uwsgi)
def set_shortcut_login_required(self, login_required):
"""Change the login_required property of shortcut."""
shortcut = self.remove('shortcut-searx')
@@ -157,18 +160,16 @@ def is_public_access_enabled():
def is_enabled():
"""Return whether the module is enabled."""
return (app.is_enabled() and action_utils.uwsgi_is_enabled('searx'))
return app.is_enabled()
def enable():
"""Enable the module."""
actions.superuser_run('searx', ['enable'])
app.enable()
def disable():
"""Disable the module."""
actions.superuser_run('searx', ['disable'])
app.disable()