Instantiate AppMgr on every request

This commit is contained in:
Disassembler 2018-08-07 16:22:44 +02:00
parent 325f818494
commit 7c8f101868
Signed by: Disassembler
GPG Key ID: 524BD33A0EE29499

View File

@ -44,7 +44,6 @@ class Lang:
class WSGIApp(object): class WSGIApp(object):
def __init__(self): def __init__(self):
self.mgr = AppMgr()
self.lang = Lang() self.lang = Lang()
self.jinja_env = Environment(loader=FileSystemLoader('/srv/spotter/templates'), autoescape=True, lstrip_blocks=True, trim_blocks=True) 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) self.jinja_env.globals.update(is_service_autostarted=tools.is_service_autostarted)
@ -94,9 +93,10 @@ class WSGIApp(object):
def portal_view(self, request): def portal_view(self, request):
# Default view. If domain is set to the default dummy domain, redirects to first-run setup instead. # 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 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): def setup_host_view(self, request):
# First-run setup view. # First-run setup view.
@ -106,18 +106,21 @@ class WSGIApp(object):
in_ipv6 = tools.get_local_ipv6() in_ipv6 = tools.get_local_ipv6()
is_letsencrypt = os.path.exists('/etc/periodic/daily/acme-sh') is_letsencrypt = os.path.exists('/etc/periodic/daily/acme-sh')
cert_info = tools.get_cert_info() 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): def setup_apps_view(self, request):
# Application manager view. # 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): def update_host_action(self, request):
# Update domain and port, then restart nginx (done via ClosingIterator in self.wsgi_app()) # Update domain and port, then restart nginx (done via ClosingIterator in self.wsgi_app())
try: try:
domain = request.form['domain'] domain = request.form['domain']
port = request.form['port'] 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'] server_name = request.environ['HTTP_X_FORWARDED_SERVER_NAME']
url = 'https://{}/setup-host'.format('{}:{}'.format(server_name, port) if port != '443' else 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)}) return self.render_json({'ok': self.lang.host_updated(url, url)})
@ -131,7 +134,8 @@ class WSGIApp(object):
def verify_dns_action(self, request): def verify_dns_action(self, request):
# Check if all FQDNs for all applications are resolvable and point to current external IP # 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() ipv4 = tools.get_external_ipv4()
ipv6 = tools.get_external_ipv6() ipv6 = tools.get_external_ipv6()
for domain in domains: for domain in domains:
@ -151,20 +155,22 @@ class WSGIApp(object):
def verify_http_action(self, request, **kwargs): def verify_http_action(self, request, **kwargs):
# Check if all applications are accessible from the internet using 3rd party ping service # Check if all applications are accessible from the internet using 3rd party ping service
proto = kwargs['proto'] 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: 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) url = '{}://{}/'.format(proto, host)
try: try:
if not tools.ping_url(url): if not tools.ping_url(url):
return self.render_json({'error': self.lang.http_host_not_reachable(url)}) return self.render_json({'error': self.lang.http_host_not_reachable(url)})
except: except:
return self.render_json({'error': self.lang.http_timeout()}) 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): def update_cert_action(self, request):
# Update certificate - either request via Let's Encrypt or manually upload files # Update certificate - either request via Let's Encrypt or manually upload files
try: try:
mgr = AppMgr()
if request.form['method'] not in ['auto', 'manual']: if request.form['method'] not in ['auto', 'manual']:
raise BadRequest() raise BadRequest()
if request.form['method'] == 'manual': if request.form['method'] == 'manual':
@ -174,11 +180,11 @@ class WSGIApp(object):
return self.render_json({'error': self.lang.key_file_missing()}) return self.render_json({'error': self.lang.key_file_missing()})
request.files['public'].save('/tmp/public.pem') request.files['public'].save('/tmp/public.pem')
request.files['private'].save('/tmp/private.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/public.pem')
os.unlink('/tmp/private.pem') os.unlink('/tmp/private.pem')
else: else:
self.mgr.request_cert() mgr.request_cert()
except BadRequest: except BadRequest:
return self.render_json({'error': self.lang.malformed_request()}) return self.render_json({'error': self.lang.malformed_request()})
except: except:
@ -187,34 +193,38 @@ class WSGIApp(object):
def update_common_action(self, request): def update_common_action(self, request):
try: 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: except BadRequest:
return self.render_json({'error': self.lang.malformed_request()}) return self.render_json({'error': self.lang.malformed_request()})
return self.render_json({'ok': self.lang.common_updated()}) return self.render_json({'ok': self.lang.common_updated()})
def update_app_visibility_action(self, request): def update_app_visibility_action(self, request):
try: try:
mgr = AppMgr()
if request.form['value'] == 'true': if request.form['value'] == 'true':
self.mgr.show_tiles(request.form['app']) mgr.show_tiles(request.form['app'])
else: else:
self.mgr.hide_tiles(request.form['app']) mgr.hide_tiles(request.form['app'])
except (BadRequest, InvalidValueException): except (BadRequest, InvalidValueException):
return self.render_json({'error': self.lang.malformed_request()}) return self.render_json({'error': self.lang.malformed_request()})
return self.render_json({'ok': 'ok'}) return self.render_json({'ok': 'ok'})
def update_app_autostart_action(self, request): def update_app_autostart_action(self, request):
try: try:
mgr = AppMgr()
if request.form['value'] == 'true': if request.form['value'] == 'true':
self.mgr.enable_autostart(request.form['app']) mgr.enable_autostart(request.form['app'])
else: else:
self.mgr.disable_autostart(request.form['app']) mgr.disable_autostart(request.form['app'])
except (BadRequest, InvalidValueException): except (BadRequest, InvalidValueException):
return self.render_json({'error': self.lang.malformed_request()}) return self.render_json({'error': self.lang.malformed_request()})
return self.render_json({'ok': 'ok'}) return self.render_json({'ok': 'ok'})
def start_app_action(self, request): def start_app_action(self, request):
try: try:
self.mgr.start_app(request.form['app']) mgr = AppMgr()
mgr.start_app(request.form['app'])
except (BadRequest, InvalidValueException): except (BadRequest, InvalidValueException):
return self.render_json({'error': self.lang.malformed_request()}) return self.render_json({'error': self.lang.malformed_request()})
except: except:
@ -223,7 +233,8 @@ class WSGIApp(object):
def stop_app_action(self, request): def stop_app_action(self, request):
try: try:
self.mgr.stop_app(request.form['app']) mgr = AppMgr()
mgr.stop_app(request.form['app'])
except (BadRequest, InvalidValueException): except (BadRequest, InvalidValueException):
return self.render_json({'error': self.lang.malformed_request()}) return self.render_json({'error': self.lang.malformed_request()})
except: except: