From 7c8f1018681cc96b16955e5c7336747b938c2697 Mon Sep 17 00:00:00 2001 From: Disassembler Date: Tue, 7 Aug 2018 16:22:44 +0200 Subject: [PATCH] Instantiate AppMgr on every request --- basic/srv/spotter/appmgr/wsgiapp.py | 49 ++++++++++++++++++----------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/basic/srv/spotter/appmgr/wsgiapp.py b/basic/srv/spotter/appmgr/wsgiapp.py index 2bdaa54..7645757 100644 --- a/basic/srv/spotter/appmgr/wsgiapp.py +++ b/basic/srv/spotter/appmgr/wsgiapp.py @@ -44,7 +44,6 @@ class Lang: class WSGIApp(object): def __init__(self): - self.mgr = AppMgr() self.lang = Lang() self.jinja_env = Environment(loader=FileSystemLoader('/srv/spotter/templates'), autoescape=True, lstrip_blocks=True, trim_blocks=True) self.jinja_env.globals.update(is_service_autostarted=tools.is_service_autostarted) @@ -94,9 +93,10 @@ class WSGIApp(object): def portal_view(self, request): # Default view. If domain is set to the default dummy domain, redirects to first-run setup instead. - if self.mgr.domain == 'spotter.vm': + mgr = AppMgr() + if mgr.domain == 'spotter.vm': return redirect('/setup-host') - return self.render_template('portal.html', conf=self.mgr.conf) + return self.render_template('portal.html', conf=mgr.conf) def setup_host_view(self, request): # First-run setup view. @@ -106,18 +106,21 @@ class WSGIApp(object): in_ipv6 = tools.get_local_ipv6() is_letsencrypt = os.path.exists('/etc/periodic/daily/acme-sh') cert_info = tools.get_cert_info() - return self.render_template('setup-host.html', conf=self.mgr.conf, ex_ipv4=ex_ipv4, ex_ipv6=ex_ipv6, in_ipv4=in_ipv4, in_ipv6=in_ipv6, is_letsencrypt=is_letsencrypt, cert_info=cert_info) + mgr = AppMgr() + return self.render_template('setup-host.html', conf=mgr.conf, ex_ipv4=ex_ipv4, ex_ipv6=ex_ipv6, in_ipv4=in_ipv4, in_ipv6=in_ipv6, is_letsencrypt=is_letsencrypt, cert_info=cert_info) def setup_apps_view(self, request): # Application manager view. - return self.render_template('setup-apps.html', conf=self.mgr.conf) + mgr = AppMgr() + return self.render_template('setup-apps.html', conf=mgr.conf) def update_host_action(self, request): # Update domain and port, then restart nginx (done via ClosingIterator in self.wsgi_app()) try: domain = request.form['domain'] port = request.form['port'] - self.mgr.update_host(domain, port, False) + mgr = AppMgr() + mgr.update_host(domain, port, False) server_name = request.environ['HTTP_X_FORWARDED_SERVER_NAME'] url = 'https://{}/setup-host'.format('{}:{}'.format(server_name, port) if port != '443' else server_name) return self.render_json({'ok': self.lang.host_updated(url, url)}) @@ -131,7 +134,8 @@ class WSGIApp(object): def verify_dns_action(self, request): # Check if all FQDNs for all applications are resolvable and point to current external IP - domains = [self.mgr.domain]+['{}.{}'.format(self.mgr.conf['apps'][app]['host'], self.mgr.domain) for app in self.mgr.conf['apps']] + mgr = AppMgr() + domains = [mgr.domain]+['{}.{}'.format(mgr.conf['apps'][app]['host'], mgr.domain) for app in mgr.conf['apps']] ipv4 = tools.get_external_ipv4() ipv6 = tools.get_external_ipv6() for domain in domains: @@ -151,20 +155,22 @@ class WSGIApp(object): def verify_http_action(self, request, **kwargs): # Check if all applications are accessible from the internet using 3rd party ping service proto = kwargs['proto'] - domains = [self.mgr.domain]+['{}.{}'.format(self.mgr.conf['apps'][app]['host'], self.mgr.domain) for app in self.mgr.conf['apps']] + mgr = AppMgr() + domains = [mgr.domain]+['{}.{}'.format(mgr.conf['apps'][app]['host'], mgr.domain) for app in mgr.conf['apps']] for domain in domains: - host = '{}:{}'.format(domain, self.mgr.port) if proto == 'https' and self.mgr.port != '443' else domain + host = '{}:{}'.format(domain, mgr.port) if proto == 'https' and mgr.port != '443' else domain url = '{}://{}/'.format(proto, host) try: if not tools.ping_url(url): return self.render_json({'error': self.lang.http_host_not_reachable(url)}) except: return self.render_json({'error': self.lang.http_timeout()}) - return self.render_json({'ok': self.lang.http_hosts_ok(self.mgr.port if proto == 'https' else '80')}) + return self.render_json({'ok': self.lang.http_hosts_ok(mgr.port if proto == 'https' else '80')}) def update_cert_action(self, request): # Update certificate - either request via Let's Encrypt or manually upload files try: + mgr = AppMgr() if request.form['method'] not in ['auto', 'manual']: raise BadRequest() if request.form['method'] == 'manual': @@ -174,11 +180,11 @@ class WSGIApp(object): return self.render_json({'error': self.lang.key_file_missing()}) request.files['public'].save('/tmp/public.pem') request.files['private'].save('/tmp/private.pem') - self.mgr.install_cert('/tmp/public.pem', '/tmp/private.pem') + mgr.install_cert('/tmp/public.pem', '/tmp/private.pem') os.unlink('/tmp/public.pem') os.unlink('/tmp/private.pem') else: - self.mgr.request_cert() + mgr.request_cert() except BadRequest: return self.render_json({'error': self.lang.malformed_request()}) except: @@ -187,34 +193,38 @@ class WSGIApp(object): def update_common_action(self, request): try: - self.mgr.update_common(request.form['email'], request.form['gmaps-api-key']) + mgr = AppMgr() + mgr.update_common(request.form['email'], request.form['gmaps-api-key']) except BadRequest: return self.render_json({'error': self.lang.malformed_request()}) return self.render_json({'ok': self.lang.common_updated()}) def update_app_visibility_action(self, request): try: + mgr = AppMgr() if request.form['value'] == 'true': - self.mgr.show_tiles(request.form['app']) + mgr.show_tiles(request.form['app']) else: - self.mgr.hide_tiles(request.form['app']) + mgr.hide_tiles(request.form['app']) except (BadRequest, InvalidValueException): return self.render_json({'error': self.lang.malformed_request()}) return self.render_json({'ok': 'ok'}) def update_app_autostart_action(self, request): try: + mgr = AppMgr() if request.form['value'] == 'true': - self.mgr.enable_autostart(request.form['app']) + mgr.enable_autostart(request.form['app']) else: - self.mgr.disable_autostart(request.form['app']) + mgr.disable_autostart(request.form['app']) except (BadRequest, InvalidValueException): return self.render_json({'error': self.lang.malformed_request()}) return self.render_json({'ok': 'ok'}) def start_app_action(self, request): try: - self.mgr.start_app(request.form['app']) + mgr = AppMgr() + mgr.start_app(request.form['app']) except (BadRequest, InvalidValueException): return self.render_json({'error': self.lang.malformed_request()}) except: @@ -223,7 +233,8 @@ class WSGIApp(object): def stop_app_action(self, request): try: - self.mgr.stop_app(request.form['app']) + mgr = AppMgr() + mgr.stop_app(request.form['app']) except (BadRequest, InvalidValueException): return self.render_json({'error': self.lang.malformed_request()}) except: