Diaspora: Generate apache configuration at setup

- Deleted static configuration file
- Generating configuration with the domain name set.
This commit is contained in:
Joseph Nuthalpati 2017-07-25 11:53:35 +05:30 committed by James Valleroy
parent d1b8d8e811
commit f3860b197a
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
4 changed files with 89 additions and 29 deletions

View File

@ -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')

View File

@ -1,17 +0,0 @@
<VirtualHost diaspora.localhost>
ServerName diaspora.localhost
DocumentRoot "/var/lib/diaspora/public/"
<Location "/">
ProxyPass "unix:/var/run/diaspora/diaspora.sock|http://localhost/"
</Location>
<Location "/assets">
ProxyPass !
</Location>
<Directory /var/lib/diaspora/public/>
Require all granted
</Directory>
</VirtualHost>

View File

@ -15,6 +15,7 @@
#
import os
import augeas
from django.utils.translation import ugettext_lazy as _
@ -65,7 +66,7 @@ description = [
'<a href="https://diaspora.{host}">diaspora.{host}</a> 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()

View File

@ -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 <http://www.gnu.org/licenses/>.
#
"""
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'])