diff --git a/actions/diaspora b/actions/diaspora index 28bddbcec..082be3b96 100755 --- a/actions/diaspora +++ b/actions/diaspora @@ -25,6 +25,7 @@ import augeas import subprocess from plinth import action_utils +from plinth.modules import diaspora def parse_arguments(): @@ -68,7 +69,7 @@ def set_domain_name(domain_name): aug = augeas.Augeas(flags=augeas.Augeas.NO_LOAD + augeas.Augeas.NO_MODL_AUTOLOAD) - # shell-script config file lens + # lens for shell-script config file aug.set('/augeas/load/Shellvars/lens', 'Shellvars.lns') aug.set('/augeas/load/Shellvars/incl[last() + 1]', conf_file) aug.load() @@ -78,15 +79,8 @@ def set_domain_name(domain_name): 'http://{}:3000'.format(domain_name)) aug.save() - # lens to edit apache2 config files - aug.set('/augeas/load/Httpd/lens', 'Httpd.lns') - aug.set('/augeas/load/Httpd/incl[last() + 1]', - '/etc/apache2/conf-available/*') - aug.load() - - aug.set('/files/etc/apache2/conf-available/diaspora-plinth.conf/VirtualHost/arg', domain_name) - aug.set("/files/etc/apache2/conf-available/diaspora-plinth.conf/VirtualHost/directive[1]/arg", domain_name) - aug.save() + diaspora.generate_apache_configuration('/etc/apache2/conf-available/diaspora-plinth.conf', + domain_name) action_utils.service_start('diaspora') diff --git a/data/etc/apache2/conf-available/diaspora-plinth.conf b/data/etc/apache2/conf-available/diaspora-plinth.conf deleted file mode 100644 index 3a22aaf26..000000000 --- a/data/etc/apache2/conf-available/diaspora-plinth.conf +++ /dev/null @@ -1,17 +0,0 @@ - - ServerName diaspora.localhost - - DocumentRoot "/var/lib/diaspora/public/" - - - ProxyPass "unix:/var/run/diaspora/diaspora.sock|http://localhost/" - - - - ProxyPass ! - - - - Require all granted - - \ No newline at end of file diff --git a/plinth/modules/diaspora/__init__.py b/plinth/modules/diaspora/__init__.py index cdd6626df..676bf7a51 100644 --- a/plinth/modules/diaspora/__init__.py +++ b/plinth/modules/diaspora/__init__.py @@ -15,6 +15,7 @@ # import os +import augeas from django.utils.translation import ugettext_lazy as _ @@ -65,7 +66,7 @@ description = [ 'diaspora.{host} path on the ' 'web server.'.format(host=get_configured_domain_name()) if is_setup() else 'Please register a domain name for your FreedomBox to be able to' - ' federate with other diaspora* pods.') + ' federate with other diaspora* pods.') ] @@ -115,7 +116,8 @@ def add_shortcut(): """Add shortcut to diaspora on the Plinth homepage""" if is_setup(): frontpage.add_shortcut( - 'diaspora', title, + 'diaspora', + title, url='https://diaspora.{}'.format(get_configured_domain_name()), login_required=True) @@ -154,3 +156,47 @@ def diagnose(): check_certificate=False)) return results + + +def generate_apache_configuration(conf_file, domain_name): + """Generate Diaspora's apache configuration with the given domain name""" + open(conf_file, 'w').close() + + diaspora_domain_name = ".".join(["diaspora", domain_name]) + + 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]', conf_file) + aug.load() + + aug.defvar('conf', + '/files' + conf_file) + + aug.set('$conf/VirtualHost', None) + aug.defvar('vh', '$conf/VirtualHost') + aug.set('$vh/arg', diaspora_domain_name) + aug.set('$vh/directive[1]', 'ServerName') + aug.set('$vh/directive[1]/arg', diaspora_domain_name) + aug.set('$vh/directive[2]', 'DocumentRoot') + aug.set('$vh/directive[2]/arg', '"/var/lib/diaspora/public/"') + + aug.set('$vh/Location', None) + aug.set('$vh/Location/arg', '"/"') + aug.set('$vh/Location/directive[1]', 'ProxyPass') + aug.set('$vh/Location/directive[1]/arg', + '"unix:/var/run/diaspora/diaspora.sock|http://localhost/"') + + aug.set('$vh/Location[last() + 1]', None) + aug.set('$vh/Location[last()]/arg', '"/assets"') + aug.set('$vh/Location[last()]/directive[1]', 'ProxyPass') + aug.set('$vh/Location[last()]/directive[1]/arg', '!') + + aug.set('$vh/Directory', None) + aug.set('$vh/Directory/arg', '/var/lib/diaspora/public/') + aug.set('$vh/Directory/directive[1]', 'Require') + aug.set('$vh/Directory/directive[1]/arg[1]', 'all') + aug.set('$vh/Directory/directive[1]/arg[2]', 'granted') + + aug.save() diff --git a/plinth/modules/diaspora/tests/test_apache_config.py b/plinth/modules/diaspora/tests/test_apache_config.py new file mode 100644 index 000000000..5e094a82e --- /dev/null +++ b/plinth/modules/diaspora/tests/test_apache_config.py @@ -0,0 +1,37 @@ +#!/usr/bin/pytho part of Plinth. +# +# 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 . +# +""" +Test Apache configuration generation for diaspora* +""" + +import os +import unittest +from plinth.modules import diaspora + + +class TestDiaspora(unittest.TestCase): + def test_generate_apache_configuration(self): + test_root = "/tmp/apache2/conf-available/" + conf_file = test_root + "diaspora-plinth.conf" + os.path.exists(test_root) or os.makedirs(test_root) + diaspora.generate_apache_configuration(conf_file, "freedombox.rocks") + + assert os.stat(conf_file).st_size != 0 + + with open(conf_file) as f: + contents = f.read() + assert all(w in contents + for w in ['VirtualHost', 'Location', 'Directory', 'assets'])