From d269a0a6456d97d4229fe0a9d201109db06b1d75 Mon Sep 17 00:00:00 2001 From: Joseph Nuthalpati Date: Thu, 23 Feb 2017 12:31:15 +0530 Subject: [PATCH] diaspora: Completed debconf configuration for Diaspora. --- actions/diaspora | 41 ++++- .../conf-available/diaspora-plinth.conf | 3 + plinth/action_utils.py | 145 +++++++++++------- plinth/modules/diaspora/__init__.py | 38 +++-- 4 files changed, 156 insertions(+), 71 deletions(-) mode change 100644 => 100755 actions/diaspora create mode 100644 data/etc/apache2/conf-available/diaspora-plinth.conf diff --git a/actions/diaspora b/actions/diaspora old mode 100644 new mode 100755 index 0714a64a3..3a1adff51 --- a/actions/diaspora +++ b/actions/diaspora @@ -18,10 +18,12 @@ # """ -Configuration helper for diaspora* web client. +Configuration helper for diaspora* pod. """ import argparse +import socket +import subprocess from plinth import action_utils @@ -30,6 +32,15 @@ def parse_arguments(): """Return parsed command line arguments as dictionary.""" parser = argparse.ArgumentParser() 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('disable', help='Disable diaspora*') @@ -37,14 +48,38 @@ def parse_arguments(): 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(_): """Enable web configuration and reload.""" - action_utils.webserver_enable('diaspora') + action_utils.webserver_enable('diaspora-plinth') def subcommand_disable(_): """Disable web configuration and reload.""" - action_utils.webserver_disable('diaspora') + action_utils.webserver_disable('diaspora-plinth') def main(): diff --git a/data/etc/apache2/conf-available/diaspora-plinth.conf b/data/etc/apache2/conf-available/diaspora-plinth.conf new file mode 100644 index 000000000..3e75a029d --- /dev/null +++ b/data/etc/apache2/conf-available/diaspora-plinth.conf @@ -0,0 +1,3 @@ +Location "/diaspora"> + ProxyPass "unix:/var/run/diaspora/diaspora.sock|http://localhost" + diff --git a/plinth/action_utils.py b/plinth/action_utils.py index 19c5ff8d2..c43e96f86 100644 --- a/plinth/action_utils.py +++ b/plinth/action_utils.py @@ -14,19 +14,19 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . # - """ Python action utility functions. """ -from django.utils.translation import ugettext as _ import os import logging -import psutil import shutil import socket import subprocess import tempfile +import psutil + +from django.utils.translation import ugettext as _ logger = logging.getLogger(__name__) @@ -43,11 +43,15 @@ def service_is_running(servicename): """ try: if is_systemd_running(): - subprocess.run(['systemctl', 'status', servicename], check=True, - stdout=subprocess.DEVNULL) + subprocess.run( + ['systemctl', 'status', servicename], + check=True, + stdout=subprocess.DEVNULL) else: - subprocess.run(['service', servicename, 'status'], check=True, - stdout=subprocess.DEVNULL) + subprocess.run( + ['service', servicename, 'status'], + check=True, + stdout=subprocess.DEVNULL) return True except subprocess.CalledProcessError: @@ -59,8 +63,11 @@ def service_is_running(servicename): def service_is_enabled(service_name): """Check if service is enabled in systemd.""" try: - subprocess.run(['systemctl', 'is-enabled', service_name], check=True, - stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + subprocess.run( + ['systemctl', 'is-enabled', service_name], + check=True, + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL) return True except subprocess.CalledProcessError: return False @@ -84,41 +91,41 @@ def service_disable(service_name): def service_start(service_name): """Start a service with systemd or sysvinit.""" if is_systemd_running(): - subprocess.run(['systemctl', 'start', service_name], - stdout=subprocess.DEVNULL) + subprocess.run( + ['systemctl', 'start', service_name], stdout=subprocess.DEVNULL) else: - subprocess.run(['service', service_name, 'start'], - stdout=subprocess.DEVNULL) + subprocess.run( + ['service', service_name, 'start'], stdout=subprocess.DEVNULL) def service_stop(service_name): """Stop a service with systemd or sysvinit.""" if is_systemd_running(): - subprocess.run(['systemctl', 'stop', service_name], - stdout=subprocess.DEVNULL) + subprocess.run( + ['systemctl', 'stop', service_name], stdout=subprocess.DEVNULL) else: - subprocess.run(['service', service_name, 'stop'], - stdout=subprocess.DEVNULL) + subprocess.run( + ['service', service_name, 'stop'], stdout=subprocess.DEVNULL) def service_restart(service_name): """Restart a service with systemd or sysvinit.""" if is_systemd_running(): - subprocess.run(['systemctl', 'restart', service_name], - stdout=subprocess.DEVNULL) + subprocess.run( + ['systemctl', 'restart', service_name], stdout=subprocess.DEVNULL) else: - subprocess.run(['service', service_name, 'restart'], - stdout=subprocess.DEVNULL) + subprocess.run( + ['service', service_name, 'restart'], stdout=subprocess.DEVNULL) def service_reload(service_name): """Reload a service with systemd or sysvinit.""" if is_systemd_running(): - subprocess.run(['systemctl', 'reload', service_name], - stdout=subprocess.DEVNULL) + subprocess.run( + ['systemctl', 'reload', service_name], stdout=subprocess.DEVNULL) else: - subprocess.run(['service', service_name, 'reload'], - stdout=subprocess.DEVNULL) + subprocess.run( + ['service', service_name, 'reload'], stdout=subprocess.DEVNULL) 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'} try: # Don't print anything on the terminal - subprocess.check_output(['a2query', option_map[kind], name], - stderr=subprocess.STDOUT) + subprocess.check_output( + ['a2query', option_map[kind], name], stderr=subprocess.STDOUT) return True except subprocess.CalledProcessError: return False @@ -147,9 +154,11 @@ def webserver_enable(name, kind='config', apply_changes=True): if webserver_is_enabled(name, kind): return - command_map = {'config': 'a2enconf', - 'site': 'a2ensite', - 'module': 'a2enmod'} + command_map = { + 'config': 'a2enconf', + 'site': 'a2ensite', + 'module': 'a2enmod' + } subprocess.check_output([command_map[kind], name]) 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): return - command_map = {'config': 'a2disconf', - 'site': 'a2dissite', - 'module': 'a2dismod'} + command_map = { + 'config': 'a2disconf', + 'site': 'a2dissite', + 'module': 'a2dismod' + } subprocess.check_output([command_map[kind], name]) action_required = 'restart' if kind == 'module' else 'reload' @@ -293,8 +304,13 @@ def _check_port(port, kind='tcp', listen_address=None): return False -def diagnose_url(url, kind=None, env=None, check_certificate=True, - extra_options=None, wrapper=None, expected_output=None): +def diagnose_url(url, + kind=None, + env=None, + check_certificate=True, + extra_options=None, + wrapper=None, + expected_output=None): """Run a diagnostic on whether a URL is accessible. 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: process = subprocess.run( - command, env=env, check=True, stdout=subprocess.PIPE, + command, + env=env, + check=True, + stdout=subprocess.PIPE, stderr=subprocess.PIPE) result = 'passed' 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' if kind: - return [_('Access URL {url} on tcp{kind}') - .format(url=url, kind=kind), result] + return [ + _('Access URL {url} on tcp{kind}').format(url=url, kind=kind), + result + ] else: return [_('Access URL {url}').format(url=url), result] @@ -340,8 +361,8 @@ def diagnose_url_on_all(url, **kwargs): results = [] for address in get_addresses(): current_url = url.format(host=address['url_address']) - results.append(diagnose_url(current_url, kind=address['kind'], - **kwargs)) + results.append( + diagnose_url(current_url, kind=address['kind'], **kwargs)) return results @@ -350,8 +371,10 @@ def diagnose_netcat(host, port, input='', negate=False): """Run a diagnostic using netcat.""" try: process = subprocess.Popen( - ['nc', host, str(port)], stdin=subprocess.PIPE, - stdout=subprocess.PIPE, stderr=subprocess.PIPE) + ['nc', host, str(port)], + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) process.communicate(input=input.encode()) if process.returncode != 0: result = 'failed' @@ -373,12 +396,24 @@ def get_addresses(): addresses = get_ip_addresses() hostname = get_hostname() - addresses.append({'kind': '4', 'address': 'localhost', 'numeric': False, - 'url_address': 'localhost'}) - addresses.append({'kind': '6', 'address': 'localhost', 'numeric': False, - 'url_address': 'localhost'}) - addresses.append({'kind': '4', 'address': hostname, 'numeric': False, - 'url_address': hostname}) + addresses.append({ + 'kind': '4', + 'address': 'localhost', + 'numeric': False, + 'url_address': 'localhost' + }) + 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 # 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']) for line in output.decode().splitlines(): parts = line.split() - address = {'kind': '4' if parts[2] == 'inet' else '6', - 'address': parts[3].split('/')[0], - 'url_address': parts[3].split('/')[0], - 'numeric': True, - 'scope': parts[5], - 'interface': parts[1]} + address = { + 'kind': '4' if parts[2] == 'inet' else '6', + 'address': parts[3].split('/')[0], + 'url_address': parts[3].split('/')[0], + 'numeric': True, + 'scope': parts[5], + 'interface': parts[1] + } if address['kind'] == '6' and address['numeric']: if address['scope'] != 'link': diff --git a/plinth/modules/diaspora/__init__.py b/plinth/modules/diaspora/__init__.py index ea101be03..1abf3eca3 100644 --- a/plinth/modules/diaspora/__init__.py +++ b/plinth/modules/diaspora/__init__.py @@ -34,10 +34,9 @@ managed_services = ['diaspora'] managed_packages = ['diaspora'] description = [ - _('diaspora* is a decentralized social network where you can store and control your own data.'), - - _('When enabled, the diaspora* pod will be available from ' - '/diaspora path on the web server.') + _('diaspora* is a decentralized social network where you can store and control your own data.' + ), _('When enabled, the diaspora* pod will be available from ' + '/diaspora path on the web server.') ] @@ -50,8 +49,12 @@ def init(): setup_helper = globals()['setup_helper'] if setup_helper.get_state() != 'needs-setup': service = service_module.Service( - managed_services[0], title, ports=['http', 'https'], - is_external=True, is_enabled=is_enabled, enable=enable, + managed_services[0], + title, + ports=['http', 'https'], + is_external=True, + is_enabled=is_enabled, + enable=enable, disable=disable) if is_enabled(): @@ -60,21 +63,26 @@ def init(): def setup(helper, old_version=None): """Install and configure the module.""" + helper.call('pre', actions.superuser_run, 'diaspora', ['pre-install']) helper.install(managed_packages) helper.call('post', actions.superuser_run, 'diaspora', ['enable']) global service if service is None: service = service_module.Service( - managed_services[0], title, ports=['http', 'https'], - is_external=True, is_enabled=is_enabled, enable=enable, + managed_services[0], + title, + ports=['http', 'https'], + is_external=True, + is_enabled=is_enabled, + enable=enable, disable=disable) helper.call('post', service.notify_enabled, None, True) helper.call('post', add_shortcut) def add_shortcut(): - frontpage.add_shortcut('diaspora', title, url='/diaspora', - login_required=True) + frontpage.add_shortcut( + 'diaspora', title, url='/diaspora', login_required=True) def is_enabled(): @@ -98,9 +106,11 @@ def diagnose(): """Run diagnostics and return the results.""" results = [] - results.append(action_utils.diagnose_port_listening(8112, 'tcp4')) - results.append(action_utils.diagnose_port_listening(8112, 'tcp6')) - results.extend(action_utils.diagnose_url_on_all( - 'https://{host}/diaspora', check_certificate=False)) + # results.append(action_utils.service_is_enabled('diaspora')) + # results.append(action_utils.service_is_running('diaspora')) + # results.append(is_enabled()) + results.extend( + action_utils.diagnose_url_on_all( + 'https://{host}/diaspora', check_certificate=False)) return results