From 4247a0bd5d949687dc25e02968e3d025a0cc0d42 Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Wed, 11 Mar 2020 15:42:35 -0700 Subject: [PATCH] pagekite: Merge all the configuration retrieval actions - Merge actions is-disabled, get-frontend, get-kite and get-services into get-config. This improves the initial startup time for FreedomBox service and also the page load time for pagekite app. This also significantly simplifies the code. - Only use the pagekite service enabled status determine if pagekite is enabled. Don't use the configuration setting. - For custom services, provide additional data such as display URL from get-config action. This removes the need for additional processing prepare_service_for_display() and template tag create_pagekite_service_url. - Also reduce the number of times configuration is retrieved to 1 when loading the app view page and during startup of FreedomBox service. - Ensure that all keys of the configuration always present and use that to simplify some code. - Remove ContextMixin from view DeleteServiceView that does not need it. Use AppView and drop ContextMixin. Signed-off-by: Sunil Mohan Adapa Reviewed-by: Veiko Aasa --- actions/pagekite | 91 +++++++++------ .../templates/pagekite_configure.html | 12 +- .../pagekite/templatetags/pagekite_extras.py | 29 ----- plinth/modules/pagekite/utils.py | 106 ++---------------- plinth/modules/pagekite/views.py | 49 +++----- 5 files changed, 86 insertions(+), 201 deletions(-) delete mode 100644 plinth/modules/pagekite/templatetags/pagekite_extras.py diff --git a/actions/pagekite b/actions/pagekite index 310fb94d4..b560062e2 100755 --- a/actions/pagekite +++ b/actions/pagekite @@ -5,11 +5,12 @@ Configuration helper for PageKite interface. """ import argparse -import augeas import json import os import sys +import augeas + from plinth import action_utils from plinth.modules.pagekite import utils @@ -40,19 +41,16 @@ def parse_arguments(): subparsers.add_parser('start-and-enable', help='Enable PageKite service') subparsers.add_parser('stop-and-disable', help='Disable PageKite service') subparsers.add_parser('restart', help='Restart PageKite service') - subparsers.add_parser( - 'is-disabled', help=('Whether PageKite is disabled in the file ' - '/etc/pagekite.d/10_accounts.rc')) + + # Configuration + subparsers.add_parser('get-config', help='Return current configuration') # Frontend - subparsers.add_parser('get-frontend', help='Get pagekite frontend') set_frontend = subparsers.add_parser('set-frontend', help='Set pagekite frontend') set_frontend.add_argument('url', help='frontend url') # Kite details (name + secret) - subparsers.add_parser('get-kite', - help='Get configured kite name and secret') set_kite = subparsers.add_parser( 'set-kite', help='Configure kite name and its secret. Secret is read from stdin.') @@ -60,7 +58,6 @@ def parse_arguments(): help='Name of the kite (eg: mybox.pagekite.me)') # Add/remove pagekite services (service_on entries) - subparsers.add_parser('get-services', help='Get list of enabled services') add_service = subparsers.add_parser('add-service', help='Add a pagekite service') add_service.add_argument('--service', help='json service dictionary') @@ -78,13 +75,6 @@ def subcommand_restart(_): print('restarted') -def subcommand_is_disabled(_): - if aug.match(PATHS['abort_not_configured']): - print('true') - else: - print('false') - - def subcommand_start_and_enable(_): aug.remove(PATHS['abort_not_configured']) aug.save() @@ -100,13 +90,58 @@ def subcommand_stop_and_disable(_): print('disabled') -def subcommand_get_frontend(_): - """Get pagekite frontend url""" +def subcommand_get_config(_): + """Print the current configuration as JSON dictionary.""" if aug.match(PATHS['defaults']): - print("pagekite.net") + frontend = 'pagekite.net' else: - url = aug.get(PATHS['frontend']) - print(url or '') + frontend = aug.get(PATHS['frontend']) or '' + + frontend = frontend.split(':') + server_domain = frontend[0] + server_port = frontend[1] if len(frontend) >= 2 else '80' + + status = { + 'is_enabled': action_utils.service_is_enabled('pagekite'), + 'kite_name': aug.get(PATHS['kitename']), + 'kite_secret': aug.get(PATHS['kitesecret']), + 'server_domain': server_domain, + 'server_port': server_port, + 'predefined_services': { + proto: False + for proto in utils.PREDEFINED_SERVICES + }, + 'custom_services': [], + } + + # 1. predefined_services: {'http': False, 'ssh': True, 'https': True} + # 2. custom_services: [{'protocol': 'http', 'secret' 'nono', ..}, [..]} + for match in aug.match(PATHS['service_on']): + service = dict([(param, aug.get(os.path.join(match, param))) + for param in utils.SERVICE_PARAMS]) + for name, predefined_service in utils.PREDEFINED_SERVICES.items(): + if service == predefined_service['params']: + status['predefined_services'][name] = True + break + else: + status['custom_services'].append(service) + if '/' in service['protocol']: + service['protocol'], service['frontend_port'] = service[ + 'protocol'].split('/') + + service['subdomains'] = service['kitename'].startswith('*.') + kite_name = status['kite_name'] + protocol = service['protocol'] + if service['subdomains']: + kite_name = f'*.{kite_name}' + + url = f'{protocol}://{kite_name}' + if 'frontend_port' in service and service['frontend_port']: + url = "%s:%s" % (url, service['frontend_port']) + + service['url'] = url + + print(json.dumps(status)) def subcommand_set_frontend(arguments): @@ -131,14 +166,6 @@ def enable_pagekitenet_frontend(): print("enabled") -def subcommand_get_services(arguments): - """ lists all available (enabled) services """ - for match in aug.match(PATHS['service_on']): - service = dict([(param, aug.get(os.path.join(match, param))) - for param in utils.SERVICE_PARAMS]) - print(json.dumps(service)) - - def subcommand_remove_service(arguments): """Searches and removes the service(s) that match all given parameters""" service = utils.load_service(arguments.service) @@ -213,14 +240,6 @@ def get_new_service_path(protocol): return os.path.join(root, str(new_index)) -def subcommand_get_kite(_): - """Print details of the currently configured kite""" - kitename = aug.get(PATHS['kitename']) - kitesecret = aug.get(PATHS['kitesecret']) - print(kitename or '') - print(kitesecret or '') - - def subcommand_set_kite(arguments): """Set details of the kite""" aug.set(PATHS['kitename'], arguments.kite_name) diff --git a/plinth/modules/pagekite/templates/pagekite_configure.html b/plinth/modules/pagekite/templates/pagekite_configure.html index 4ab42f477..706348a1c 100644 --- a/plinth/modules/pagekite/templates/pagekite_configure.html +++ b/plinth/modules/pagekite/templates/pagekite_configure.html @@ -6,7 +6,6 @@ {% load bootstrap %} {% load i18n %} {% load static %} -{% load pagekite_extras %} {% block page_head %}