diff --git a/actions/config b/actions/config deleted file mode 100755 index 31da406ee..000000000 --- a/actions/config +++ /dev/null @@ -1,80 +0,0 @@ -#!/usr/bin/python3 -# -*- mode: python -*- -# SPDX-License-Identifier: AGPL-3.0-or-later -""" -Configuration helper for FreedomBox general configuration. -""" - -import argparse -import os - -import augeas - -from plinth import action_utils -from plinth.modules.config import (APACHE_HOMEPAGE_CONF_FILE_NAME, - FREEDOMBOX_APACHE_CONFIG) - - -def parse_arguments(): - """Return parsed command line arguments as dictionary.""" - parser = argparse.ArgumentParser() - subparsers = parser.add_subparsers(dest='subcommand', help='Sub command') - - set_home_page = subparsers.add_parser( - 'set-home-page', - help='Set the home page for this FreedomBox instance.') - set_home_page.add_argument('homepage', - help='path to the webserver home page') - - subparsers.add_parser('reset-home-page', - help='Reset the homepage of the Apache server.') - - subparsers.required = True - return parser.parse_args() - - -def subcommand_set_home_page(arguments): - """Set the default app for this FreedomBox.""" - conf_file_path = os.path.join('/etc/apache2/conf-available', - APACHE_HOMEPAGE_CONF_FILE_NAME) - - redirect_rule = 'RedirectMatch "^/$" "{}"\n'.format(arguments.homepage) - - with open(conf_file_path, 'w', encoding='utf-8') as conf_file: - conf_file.write(redirect_rule) - - action_utils.webserver_enable('freedombox-apache-homepage') - - -def subcommand_reset_home_page(_): - """Sets the Apache web server's home page to the default - /plinth.""" - config_file = FREEDOMBOX_APACHE_CONFIG - default_path = 'plinth' - - aug = augeas.Augeas(flags=augeas.Augeas.NO_LOAD + - augeas.Augeas.NO_MODL_AUTOLOAD) - aug.set('/augeas/load/Httpd/lens', 'Httpd.lns') - aug.set('/augeas/load/Httpd/incl[last() + 1]', config_file) - aug.load() - - aug.defvar('conf', '/files' + config_file) - - for match in aug.match('/files' + config_file + - '/directive["RedirectMatch"]'): - if aug.get(match + "/arg[1]") == '''"^/$"''': - aug.set(match + "/arg[2]", '"/{}"'.format(default_path)) - - aug.save() - - -def main(): - """Parse arguments and perform all duties.""" - arguments = parse_arguments() - - subcommand = arguments.subcommand.replace('-', '_') - subcommand_method = globals()['subcommand_' + subcommand] - subcommand_method(arguments) - - -if __name__ == '__main__': - main() diff --git a/plinth/modules/config/__init__.py b/plinth/modules/config/__init__.py index 80d2adb9e..93cedeb84 100644 --- a/plinth/modules/config/__init__.py +++ b/plinth/modules/config/__init__.py @@ -1,15 +1,11 @@ # SPDX-License-Identifier: AGPL-3.0-or-later -""" -FreedomBox app for basic system configuration. -""" +"""FreedomBox app for basic system configuration.""" -import os import socket import augeas from django.utils.translation import gettext_lazy as _ -from plinth import actions from plinth import app as app_module from plinth import frontpage, menu from plinth.daemon import RelatedDaemon @@ -26,12 +22,6 @@ _description = [ 'like hostname, domain name, webserver home page etc.') ] -APACHE_CONF_ENABLED_DIR = '/etc/apache2/conf-enabled' -APACHE_HOMEPAGE_CONF_FILE_NAME = 'freedombox-apache-homepage.conf' -APACHE_HOMEPAGE_CONFIG = os.path.join(APACHE_CONF_ENABLED_DIR, - APACHE_HOMEPAGE_CONF_FILE_NAME) -FREEDOMBOX_APACHE_CONFIG = os.path.join(APACHE_CONF_ENABLED_DIR, - 'freedombox.conf') ADVANCED_MODE_KEY = 'advanced_mode' @@ -103,19 +93,18 @@ class ConfigApp(app_module.App): def get_domainname(): - """Return the domainname""" + """Return the domainname.""" fqdn = socket.getfqdn() return '.'.join(fqdn.split('.')[1:]) def get_hostname(): - """Return the hostname""" + """Return the hostname.""" return socket.gethostname() def home_page_url2scid(url): - """Returns the shortcut ID of the given home page url.""" - + """Return the shortcut ID of the given home page url.""" if url in ('/plinth/', '/plinth', 'plinth'): return 'plinth' @@ -134,7 +123,7 @@ def home_page_url2scid(url): def _home_page_scid2url(shortcut_id): - """Returns the url for the given home page shortcut ID.""" + """Return the url for the given home page shortcut ID.""" if shortcut_id is None: url = None elif shortcut_id == 'plinth': @@ -178,23 +167,22 @@ def _get_home_page_url(conf_file): def get_home_page(): """Return the shortcut ID that is set as current home page.""" - CONF_FILE = APACHE_HOMEPAGE_CONFIG if os.path.exists( - APACHE_HOMEPAGE_CONFIG) else FREEDOMBOX_APACHE_CONFIG + CONF_FILE = privileged.APACHE_HOMEPAGE_CONFIG if os.path.exists( + privileged.APACHE_HOMEPAGE_CONFIG + ) else privileged.FREEDOMBOX_APACHE_CONFIG url = _get_home_page_url(CONF_FILE) return home_page_url2scid(url) def change_home_page(shortcut_id): - """Change the FreedomBox's default redirect to URL of the shortcut - specified. - """ + """Change the FreedomBox's default redirect to URL of a shortcut.""" url = _home_page_scid2url(shortcut_id) if url is None: url = '/plinth/' # fall back to default url if scid is unknown. # URL may be a reverse_lazy() proxy - actions.superuser_run('config', ['set-home-page', str(url)]) + privileged.set_home_page(str(url)) def get_advanced_mode(): @@ -211,12 +199,11 @@ def set_advanced_mode(advanced_mode): def _migrate_home_page_config(): """Move the home page configuration to an external file.""" - # Hold the current home page in a variable home_page = get_home_page() # Reset the home page to plinth in freedombox.conf - actions.superuser_run('config', ['reset-home-page']) + privileged.reset_home_page() # Write the home page setting into the new conf file # This step is run at the end because it reloads the Apache server diff --git a/plinth/modules/config/privileged.py b/plinth/modules/config/privileged.py index 6316e75ac..e2bf76869 100644 --- a/plinth/modules/config/privileged.py +++ b/plinth/modules/config/privileged.py @@ -1,6 +1,7 @@ # SPDX-License-Identifier: AGPL-3.0-or-later """Configure miscellaneous system settings.""" +import os import pathlib import augeas @@ -8,6 +9,13 @@ import augeas from plinth import action_utils from plinth.actions import privileged +APACHE_CONF_ENABLED_DIR = '/etc/apache2/conf-enabled' +APACHE_HOMEPAGE_CONF_FILE_NAME = 'freedombox-apache-homepage.conf' +APACHE_HOMEPAGE_CONFIG = os.path.join(APACHE_CONF_ENABLED_DIR, + APACHE_HOMEPAGE_CONF_FILE_NAME) +FREEDOMBOX_APACHE_CONFIG = os.path.join(APACHE_CONF_ENABLED_DIR, + 'freedombox.conf') + JOURNALD_FILE = pathlib.Path('/etc/systemd/journald.conf.d/50-freedombox.conf') @@ -56,3 +64,39 @@ def set_logging_mode(mode: str): # systemd-journald is socket activated, it may not be running and it does # not support reload. action_utils.service_try_restart('systemd-journald') + + +@privileged +def set_home_page(homepage: str): + """Set the default app for this FreedomBox.""" + conf_file_path = os.path.join('/etc/apache2/conf-available', + APACHE_HOMEPAGE_CONF_FILE_NAME) + + redirect_rule = 'RedirectMatch "^/$" "{}"\n'.format(homepage) + + with open(conf_file_path, 'w', encoding='utf-8') as conf_file: + conf_file.write(redirect_rule) + + action_utils.webserver_enable('freedombox-apache-homepage') + + +@privileged +def reset_home_page(): + """Set the Apache web server's home page to the default - /plinth.""" + config_file = FREEDOMBOX_APACHE_CONFIG + default_path = 'plinth' + + aug = augeas.Augeas(flags=augeas.Augeas.NO_LOAD + + augeas.Augeas.NO_MODL_AUTOLOAD) + aug.set('/augeas/load/Httpd/lens', 'Httpd.lns') + aug.set('/augeas/load/Httpd/incl[last() + 1]', config_file) + aug.load() + + aug.defvar('conf', '/files' + config_file) + + for match in aug.match('/files' + config_file + + '/directive["RedirectMatch"]'): + if aug.get(match + "/arg[1]") == '''"^/$"''': + aug.set(match + "/arg[2]", '"/{}"'.format(default_path)) + + aug.save()