configuration: Use augeas to edit Apache files

Move the file editing code to actions/config since it must be executed by a
super user.

Signed-off-by: Joseph Nuthalapati <njoseph@thoughtworks.com>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Joseph Nuthalapati 2018-07-25 15:58:48 +05:30 committed by James Valleroy
parent 9a3af288fa
commit a309b28035
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
4 changed files with 95 additions and 17 deletions

77
actions/config Executable file
View File

@ -0,0 +1,77 @@
#!/usr/bin/python3
# -*- mode: python -*-
#
# This file is part of FreedomBox.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
"""
Configuration helper for FreedomBox general configuration.
"""
import argparse
import augeas
from plinth import action_utils
APACHE_CONFIGURATION = '/etc/apache2/conf-available/freedombox.conf'
def parse_arguments():
"""Return parsed command line arguments as dictionary."""
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest='subcommand', help='Sub command')
set_default_app = subparsers.add_parser(
'set-default-app',
help='Set the default app for this FreedomBox instance.')
set_default_app.add_argument('app', help='name of the default app')
subparsers.required = True
return parser.parse_args()
def subcommand_set_default_app(arguments):
"""Set the default app for this FreedomBox."""
app = arguments.app
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]', APACHE_CONFIGURATION)
aug.load()
aug.defvar('conf', '/files' + APACHE_CONFIGURATION)
for match in aug.match('/files' + APACHE_CONFIGURATION +
'/directive["RedirectMatch"]'):
if aug.get(match + "/arg[1]") == '''"^/$"''':
aug.set(match + "/arg[2]", '"/{}/"'.format(app))
aug.save()
action_utils.service_reload('apache2')
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()

View File

@ -10,5 +10,4 @@ Header set Strict-Transport-Security "max-age=31536000; includeSubDomains" env=H
##
RedirectMatch "^/$" "/plinth"
RedirectMatch "^/freedombox" "/plinth"
RedirectMatch "^/home" "/plinth"

View File

@ -18,9 +18,9 @@
FreedomBox app for basic system configuration.
"""
import re
import socket
import augeas
from django.utils.translation import ugettext_lazy
from plinth import actions
@ -51,11 +51,21 @@ def get_hostname():
def get_default_app():
"""Get the default application for the domain."""
with open('/etc/apache2/conf-available/freedombox.conf') as conf_file:
for line in conf_file:
if re.findall(r'\^\/\$', line):
app_path = line.split()[-1].strip('"')
break
APACHE_CONFIGURATION = '/etc/apache2/conf-available/freedombox.conf'
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]', APACHE_CONFIGURATION)
aug.load()
aug.defvar('conf', '/files' + APACHE_CONFIGURATION)
for match in aug.match('/files' + APACHE_CONFIGURATION +
'/directive["RedirectMatch"]'):
if aug.get(match + "/arg[1]") == '''"^/$"''':
app_path = aug.get(match + "/arg[2]")
return app_path.strip("/")

View File

@ -114,16 +114,8 @@ def change_default_app(app_id):
shortcut['url'] for shortcut in shortcuts
if shortcut['id'] == app_id
][0]
lines = []
freedombox_apache_conf = '/etc/apache2/conf-available/freedombox.conf'
with open(freedombox_apache_conf, 'r') as conf_file:
for line in conf_file:
if re.findall(r'\^\/\$', line):
line = 'RedirectMatch "^/$" ' + '"{}"'.format(url)
lines.append(line)
with open(freedombox_apache_conf, 'w') as conf_file:
conf_file.write("\n".join(lines))
action_utils.service_reload('apache2')
actions.superuser_run('config', ['set-default-app', url.strip("/")])
def set_hostname(hostname):