diaspora: Completed debconf configuration for Diaspora.

This commit is contained in:
Joseph Nuthalpati 2017-02-23 12:31:15 +05:30 committed by James Valleroy
parent 01e97e7369
commit d269a0a645
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
4 changed files with 156 additions and 71 deletions

41
actions/diaspora Normal file → Executable file
View File

@ -18,10 +18,12 @@
# #
""" """
Configuration helper for diaspora* web client. Configuration helper for diaspora* pod.
""" """
import argparse import argparse
import socket
import subprocess
from plinth import action_utils from plinth import action_utils
@ -30,6 +32,15 @@ def parse_arguments():
"""Return parsed command line arguments as dictionary.""" """Return parsed command line arguments as dictionary."""
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest='subcommand', help='Sub command') subparsers = parser.add_subparsers(dest='subcommand', help='Sub command')
pre_install = subparsers.add_parser(
'pre-install',
help='Preseed debconf values before packages are installed.')
# Preseed debconf values before packages are installed.
# pre_install.add_argument(
# '--thirdpartyservices',
# help='Third party services enabled: Facebook, Twitter, Tumblr, Wordpress')
subparsers.add_parser('enable', help='Enable diaspora*') subparsers.add_parser('enable', help='Enable diaspora*')
subparsers.add_parser('disable', help='Disable diaspora*') subparsers.add_parser('disable', help='Disable diaspora*')
@ -37,14 +48,38 @@ def parse_arguments():
return parser.parse_args() return parser.parse_args()
def subcommand_pre_install(arguments):
"""Pre installation configuration for diaspora"""
presets= [
b'diaspora-common diaspora-common/url string http://localhost/diaspora',
b'diaspora-common diaspora-common/dbpass note ',
b'diaspora-common diaspora-common/enablessl boolean false',
b'diaspora-common diaspora-common/useletsencrypt string false',
b'diaspora-common diaspora-common/services multiselect ',
b'diaspora-common diaspora-common/ssl boolean false',
b'diaspora-common diaspora-common/pgsql/authmethod-admin string ident',
b'diaspora-common diaspora-common/letsencrypt boolean false',
b'diaspora-common diaspora-common/remote/host string localhost',
b'diaspora-common diaspora-common/database-type string pgsql',
b'diaspora-common diaspora-common/dbconfig-install boolean true'
]
for preset in presets:
subprocess.check_output(['debconf-set-selections'], input=preset)
# hostname = arguments.hostname or socket.gethostname()
# subprocess.check_output(
# ['debconf-set-selections'],
# input=b'diaspora diaspora/url string ' + hostname.encode())
def subcommand_enable(_): def subcommand_enable(_):
"""Enable web configuration and reload.""" """Enable web configuration and reload."""
action_utils.webserver_enable('diaspora') action_utils.webserver_enable('diaspora-plinth')
def subcommand_disable(_): def subcommand_disable(_):
"""Disable web configuration and reload.""" """Disable web configuration and reload."""
action_utils.webserver_disable('diaspora') action_utils.webserver_disable('diaspora-plinth')
def main(): def main():

View File

@ -0,0 +1,3 @@
Location "/diaspora">
ProxyPass "unix:/var/run/diaspora/diaspora.sock|http://localhost"
</Location>

View File

@ -14,19 +14,19 @@
# You should have received a copy of the GNU Affero General Public License # 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/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
# #
""" """
Python action utility functions. Python action utility functions.
""" """
from django.utils.translation import ugettext as _
import os import os
import logging import logging
import psutil
import shutil import shutil
import socket import socket
import subprocess import subprocess
import tempfile import tempfile
import psutil
from django.utils.translation import ugettext as _
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -43,10 +43,14 @@ def service_is_running(servicename):
""" """
try: try:
if is_systemd_running(): if is_systemd_running():
subprocess.run(['systemctl', 'status', servicename], check=True, subprocess.run(
['systemctl', 'status', servicename],
check=True,
stdout=subprocess.DEVNULL) stdout=subprocess.DEVNULL)
else: else:
subprocess.run(['service', servicename, 'status'], check=True, subprocess.run(
['service', servicename, 'status'],
check=True,
stdout=subprocess.DEVNULL) stdout=subprocess.DEVNULL)
return True return True
@ -59,8 +63,11 @@ def service_is_running(servicename):
def service_is_enabled(service_name): def service_is_enabled(service_name):
"""Check if service is enabled in systemd.""" """Check if service is enabled in systemd."""
try: try:
subprocess.run(['systemctl', 'is-enabled', service_name], check=True, subprocess.run(
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) ['systemctl', 'is-enabled', service_name],
check=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL)
return True return True
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
return False return False
@ -84,41 +91,41 @@ def service_disable(service_name):
def service_start(service_name): def service_start(service_name):
"""Start a service with systemd or sysvinit.""" """Start a service with systemd or sysvinit."""
if is_systemd_running(): if is_systemd_running():
subprocess.run(['systemctl', 'start', service_name], subprocess.run(
stdout=subprocess.DEVNULL) ['systemctl', 'start', service_name], stdout=subprocess.DEVNULL)
else: else:
subprocess.run(['service', service_name, 'start'], subprocess.run(
stdout=subprocess.DEVNULL) ['service', service_name, 'start'], stdout=subprocess.DEVNULL)
def service_stop(service_name): def service_stop(service_name):
"""Stop a service with systemd or sysvinit.""" """Stop a service with systemd or sysvinit."""
if is_systemd_running(): if is_systemd_running():
subprocess.run(['systemctl', 'stop', service_name], subprocess.run(
stdout=subprocess.DEVNULL) ['systemctl', 'stop', service_name], stdout=subprocess.DEVNULL)
else: else:
subprocess.run(['service', service_name, 'stop'], subprocess.run(
stdout=subprocess.DEVNULL) ['service', service_name, 'stop'], stdout=subprocess.DEVNULL)
def service_restart(service_name): def service_restart(service_name):
"""Restart a service with systemd or sysvinit.""" """Restart a service with systemd or sysvinit."""
if is_systemd_running(): if is_systemd_running():
subprocess.run(['systemctl', 'restart', service_name], subprocess.run(
stdout=subprocess.DEVNULL) ['systemctl', 'restart', service_name], stdout=subprocess.DEVNULL)
else: else:
subprocess.run(['service', service_name, 'restart'], subprocess.run(
stdout=subprocess.DEVNULL) ['service', service_name, 'restart'], stdout=subprocess.DEVNULL)
def service_reload(service_name): def service_reload(service_name):
"""Reload a service with systemd or sysvinit.""" """Reload a service with systemd or sysvinit."""
if is_systemd_running(): if is_systemd_running():
subprocess.run(['systemctl', 'reload', service_name], subprocess.run(
stdout=subprocess.DEVNULL) ['systemctl', 'reload', service_name], stdout=subprocess.DEVNULL)
else: else:
subprocess.run(['service', service_name, 'reload'], subprocess.run(
stdout=subprocess.DEVNULL) ['service', service_name, 'reload'], stdout=subprocess.DEVNULL)
def webserver_is_enabled(name, kind='config'): def webserver_is_enabled(name, kind='config'):
@ -129,8 +136,8 @@ def webserver_is_enabled(name, kind='config'):
option_map = {'config': '-c', 'site': '-s', 'module': '-m'} option_map = {'config': '-c', 'site': '-s', 'module': '-m'}
try: try:
# Don't print anything on the terminal # Don't print anything on the terminal
subprocess.check_output(['a2query', option_map[kind], name], subprocess.check_output(
stderr=subprocess.STDOUT) ['a2query', option_map[kind], name], stderr=subprocess.STDOUT)
return True return True
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
return False return False
@ -147,9 +154,11 @@ def webserver_enable(name, kind='config', apply_changes=True):
if webserver_is_enabled(name, kind): if webserver_is_enabled(name, kind):
return return
command_map = {'config': 'a2enconf', command_map = {
'config': 'a2enconf',
'site': 'a2ensite', 'site': 'a2ensite',
'module': 'a2enmod'} 'module': 'a2enmod'
}
subprocess.check_output([command_map[kind], name]) subprocess.check_output([command_map[kind], name])
action_required = 'restart' if kind == 'module' else 'reload' action_required = 'restart' if kind == 'module' else 'reload'
@ -174,9 +183,11 @@ def webserver_disable(name, kind='config', apply_changes=True):
if not webserver_is_enabled(name, kind): if not webserver_is_enabled(name, kind):
return return
command_map = {'config': 'a2disconf', command_map = {
'config': 'a2disconf',
'site': 'a2dissite', 'site': 'a2dissite',
'module': 'a2dismod'} 'module': 'a2dismod'
}
subprocess.check_output([command_map[kind], name]) subprocess.check_output([command_map[kind], name])
action_required = 'restart' if kind == 'module' else 'reload' action_required = 'restart' if kind == 'module' else 'reload'
@ -293,8 +304,13 @@ def _check_port(port, kind='tcp', listen_address=None):
return False return False
def diagnose_url(url, kind=None, env=None, check_certificate=True, def diagnose_url(url,
extra_options=None, wrapper=None, expected_output=None): kind=None,
env=None,
check_certificate=True,
extra_options=None,
wrapper=None,
expected_output=None):
"""Run a diagnostic on whether a URL is accessible. """Run a diagnostic on whether a URL is accessible.
Kind can be '4' for IPv4 or '6' for IPv6. Kind can be '4' for IPv4 or '6' for IPv6.
@ -315,7 +331,10 @@ def diagnose_url(url, kind=None, env=None, check_certificate=True,
try: try:
process = subprocess.run( process = subprocess.run(
command, env=env, check=True, stdout=subprocess.PIPE, command,
env=env,
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE) stderr=subprocess.PIPE)
result = 'passed' result = 'passed'
if expected_output and expected_output not in process.stdout.decode(): if expected_output and expected_output not in process.stdout.decode():
@ -329,8 +348,10 @@ def diagnose_url(url, kind=None, env=None, check_certificate=True,
result = 'error' result = 'error'
if kind: if kind:
return [_('Access URL {url} on tcp{kind}') return [
.format(url=url, kind=kind), result] _('Access URL {url} on tcp{kind}').format(url=url, kind=kind),
result
]
else: else:
return [_('Access URL {url}').format(url=url), result] return [_('Access URL {url}').format(url=url), result]
@ -340,8 +361,8 @@ def diagnose_url_on_all(url, **kwargs):
results = [] results = []
for address in get_addresses(): for address in get_addresses():
current_url = url.format(host=address['url_address']) current_url = url.format(host=address['url_address'])
results.append(diagnose_url(current_url, kind=address['kind'], results.append(
**kwargs)) diagnose_url(current_url, kind=address['kind'], **kwargs))
return results return results
@ -350,8 +371,10 @@ def diagnose_netcat(host, port, input='', negate=False):
"""Run a diagnostic using netcat.""" """Run a diagnostic using netcat."""
try: try:
process = subprocess.Popen( process = subprocess.Popen(
['nc', host, str(port)], stdin=subprocess.PIPE, ['nc', host, str(port)],
stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
process.communicate(input=input.encode()) process.communicate(input=input.encode())
if process.returncode != 0: if process.returncode != 0:
result = 'failed' result = 'failed'
@ -373,12 +396,24 @@ def get_addresses():
addresses = get_ip_addresses() addresses = get_ip_addresses()
hostname = get_hostname() hostname = get_hostname()
addresses.append({'kind': '4', 'address': 'localhost', 'numeric': False, addresses.append({
'url_address': 'localhost'}) 'kind': '4',
addresses.append({'kind': '6', 'address': 'localhost', 'numeric': False, 'address': 'localhost',
'url_address': 'localhost'}) 'numeric': False,
addresses.append({'kind': '4', 'address': hostname, 'numeric': False, 'url_address': 'localhost'
'url_address': hostname}) })
addresses.append({
'kind': '6',
'address': 'localhost',
'numeric': False,
'url_address': 'localhost'
})
addresses.append({
'kind': '4',
'address': hostname,
'numeric': False,
'url_address': hostname
})
# XXX: When a hostname is resolved to IPv6 address, it may likely # XXX: When a hostname is resolved to IPv6 address, it may likely
# be link-local address. Link local IPv6 addresses are valid only # be link-local address. Link local IPv6 addresses are valid only
@ -397,12 +432,14 @@ def get_ip_addresses():
output = subprocess.check_output(['ip', '-o', 'addr']) output = subprocess.check_output(['ip', '-o', 'addr'])
for line in output.decode().splitlines(): for line in output.decode().splitlines():
parts = line.split() parts = line.split()
address = {'kind': '4' if parts[2] == 'inet' else '6', address = {
'kind': '4' if parts[2] == 'inet' else '6',
'address': parts[3].split('/')[0], 'address': parts[3].split('/')[0],
'url_address': parts[3].split('/')[0], 'url_address': parts[3].split('/')[0],
'numeric': True, 'numeric': True,
'scope': parts[5], 'scope': parts[5],
'interface': parts[1]} 'interface': parts[1]
}
if address['kind'] == '6' and address['numeric']: if address['kind'] == '6' and address['numeric']:
if address['scope'] != 'link': if address['scope'] != 'link':

View File

@ -34,9 +34,8 @@ managed_services = ['diaspora']
managed_packages = ['diaspora'] managed_packages = ['diaspora']
description = [ description = [
_('diaspora* is a decentralized social network where you can store and control your own data.'), _('diaspora* is a decentralized social network where you can store and control your own data.'
), _('When enabled, the diaspora* pod will be available from '
_('When enabled, the diaspora* pod will be available from '
'<a href="/diaspora">/diaspora</a> path on the web server.') '<a href="/diaspora">/diaspora</a> path on the web server.')
] ]
@ -50,8 +49,12 @@ def init():
setup_helper = globals()['setup_helper'] setup_helper = globals()['setup_helper']
if setup_helper.get_state() != 'needs-setup': if setup_helper.get_state() != 'needs-setup':
service = service_module.Service( service = service_module.Service(
managed_services[0], title, ports=['http', 'https'], managed_services[0],
is_external=True, is_enabled=is_enabled, enable=enable, title,
ports=['http', 'https'],
is_external=True,
is_enabled=is_enabled,
enable=enable,
disable=disable) disable=disable)
if is_enabled(): if is_enabled():
@ -60,21 +63,26 @@ def init():
def setup(helper, old_version=None): def setup(helper, old_version=None):
"""Install and configure the module.""" """Install and configure the module."""
helper.call('pre', actions.superuser_run, 'diaspora', ['pre-install'])
helper.install(managed_packages) helper.install(managed_packages)
helper.call('post', actions.superuser_run, 'diaspora', ['enable']) helper.call('post', actions.superuser_run, 'diaspora', ['enable'])
global service global service
if service is None: if service is None:
service = service_module.Service( service = service_module.Service(
managed_services[0], title, ports=['http', 'https'], managed_services[0],
is_external=True, is_enabled=is_enabled, enable=enable, title,
ports=['http', 'https'],
is_external=True,
is_enabled=is_enabled,
enable=enable,
disable=disable) disable=disable)
helper.call('post', service.notify_enabled, None, True) helper.call('post', service.notify_enabled, None, True)
helper.call('post', add_shortcut) helper.call('post', add_shortcut)
def add_shortcut(): def add_shortcut():
frontpage.add_shortcut('diaspora', title, url='/diaspora', frontpage.add_shortcut(
login_required=True) 'diaspora', title, url='/diaspora', login_required=True)
def is_enabled(): def is_enabled():
@ -98,9 +106,11 @@ def diagnose():
"""Run diagnostics and return the results.""" """Run diagnostics and return the results."""
results = [] results = []
results.append(action_utils.diagnose_port_listening(8112, 'tcp4')) # results.append(action_utils.service_is_enabled('diaspora'))
results.append(action_utils.diagnose_port_listening(8112, 'tcp6')) # results.append(action_utils.service_is_running('diaspora'))
results.extend(action_utils.diagnose_url_on_all( # results.append(is_enabled())
results.extend(
action_utils.diagnose_url_on_all(
'https://{host}/diaspora', check_certificate=False)) 'https://{host}/diaspora', check_certificate=False))
return results return results