config: Move default-app configuration to a dedicated file

Signed-off-by: Joseph Nuthalapati <njoseph@thoughtworks.com>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Joseph Nuthalapati 2019-02-15 14:52:20 +05:30 committed by James Valleroy
parent 70cb3d46b4
commit 5ad22114ed
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
2 changed files with 22 additions and 25 deletions

View File

@ -21,12 +21,10 @@ Configuration helper for FreedomBox general configuration.
""" """
import argparse import argparse
import os
import augeas
from plinth import action_utils from plinth import action_utils
from plinth.modules.config import DEFAULT_APP_CONF_FILE_NAME
APACHE_CONFIGURATION = '/etc/apache2/conf-available/freedombox.conf'
def parse_arguments(): def parse_arguments():
@ -44,24 +42,15 @@ def parse_arguments():
def subcommand_set_default_app(arguments): def subcommand_set_default_app(arguments):
"""Set the default app for this FreedomBox.""" """Set the default app for this FreedomBox."""
app = arguments.app conf_file_path = os.path.join('/etc/apache2/conf-available',
DEFAULT_APP_CONF_FILE_NAME)
aug = augeas.Augeas(flags=augeas.Augeas.NO_LOAD + redirect_rule = 'RedirectMatch "^/$" "/{}"\n'.format(arguments.app)
augeas.Augeas.NO_MODL_AUTOLOAD)
aug.set('/augeas/load/Httpd/lens', 'Httpd.lns')
aug.set('/augeas/load/Httpd/incl[last() + 1]', APACHE_CONFIGURATION)
aug.load()
aug.defvar('conf', '/files' + APACHE_CONFIGURATION) with open(conf_file_path, 'w') as conf_file:
conf_file.write(redirect_rule)
for match in aug.match('/files' + APACHE_CONFIGURATION + action_utils.webserver_enable('default-app')
'/directive["RedirectMatch"]'):
if aug.get(match + "/arg[1]") == '''"^/$"''':
aug.set(match + "/arg[2]", '"/{}/"'.format(app))
aug.save()
action_utils.service_reload('apache2')
def main(): def main():

View File

@ -18,6 +18,7 @@
FreedomBox app for basic system configuration. FreedomBox app for basic system configuration.
""" """
import os
import socket import socket
import augeas import augeas
@ -37,6 +38,13 @@ depends = ['firewall', 'names']
manual_page = 'Configure' manual_page = 'Configure'
APACHE_CONF_ENABLED_DIR = '/etc/apache2/conf-enabled'
DEFAULT_APP_CONF_FILE_NAME = 'default-app.conf'
DEFAULT_APP_APACHE_CONFIG = os.path.join(APACHE_CONF_ENABLED_DIR,
DEFAULT_APP_CONF_FILE_NAME)
FREEDOMBOX_APACHE_CONFIG = os.path.join(APACHE_CONF_ENABLED_DIR,
'freedombox.conf')
def get_domainname(): def get_domainname():
"""Return the domainname""" """Return the domainname"""
@ -51,22 +59,22 @@ def get_hostname():
def get_default_app(): def get_default_app():
"""Get the default application for the domain.""" """Get the default application for the domain."""
APACHE_CONFIGURATION = '/etc/apache2/conf-available/freedombox.conf'
aug = augeas.Augeas(flags=augeas.Augeas.NO_LOAD + aug = augeas.Augeas(flags=augeas.Augeas.NO_LOAD +
augeas.Augeas.NO_MODL_AUTOLOAD) augeas.Augeas.NO_MODL_AUTOLOAD)
aug.set('/augeas/load/Httpd/lens', 'Httpd.lns') aug.set('/augeas/load/Httpd/lens', 'Httpd.lns')
aug.set('/augeas/load/Httpd/incl[last() + 1]', APACHE_CONFIGURATION) conf_file = DEFAULT_APP_APACHE_CONFIG if os.path.exists(
DEFAULT_APP_APACHE_CONFIG) else FREEDOMBOX_APACHE_CONFIG
aug.set('/augeas/load/Httpd/incl[last() + 1]', conf_file)
aug.load() aug.load()
aug.defvar('conf', '/files' + APACHE_CONFIGURATION) aug.defvar('conf', '/files' + conf_file)
for match in aug.match('/files' + APACHE_CONFIGURATION + for match in aug.match('/files' + conf_file +
'/directive["RedirectMatch"]'): '/directive["RedirectMatch"]'):
if aug.get(match + "/arg[1]") == '''"^/$"''': if aug.get(match + "/arg[1]") == '''"^/$"''':
app_path = aug.get(match + "/arg[2]") app_path = aug.get(match + "/arg[2]")
return app_path.strip('"').strip('/') return app_path.strip('/"')
def init(): def init():