AppMgr visual improvements + removed few code repetitions

This commit is contained in:
Disassembler 2018-08-30 18:04:47 +02:00
parent 97c2c18ca3
commit 05c4fdd99d
No known key found for this signature in database
GPG Key ID: 524BD33A0EE29499
6 changed files with 30 additions and 27 deletions

View File

@ -108,7 +108,7 @@ ISSUE_TEMPLATE = '''
Pro přístup k aplikacím otevřete URL \x1b[1mhttps://{host}\x1b[0m ve Vašem
Pro přístup k aplikacím otevřete URL \x1b[1m{url}\x1b[0m ve Vašem
internetovém prohlížeči.
@ -254,25 +254,21 @@ class AppMgr:
def rebuild_issue(self):
# Compile the HTTPS host displayed in terminal banner
host = self.domain
domain = self.domain
# If the dummy host is used, take an IP address of a primary interface instead
if self.domain == 'spotter.vm':
host = tools.get_local_ipv4()
if not host:
host = tools.get_local_ipv6()
if not host:
host = '127.0.0.1'
# Show port number only when using the non-default HTTPS port
if self.port != '443':
host += ':{}'.format(self.port)
if domain == 'spotter.vm':
domain = tools.get_local_ipv4()
if not domain:
domain = tools.get_local_ipv6()
if not domain:
domain = '127.0.0.1'
# Rebuild the terminal banner
with open(ISSUE_FILE, 'w') as f:
f.write(ISSUE_TEMPLATE.format(host=host))
f.write(ISSUE_TEMPLATE.format(url=tools.compile_url(domain, self.port)))
def update_apps_urls(self):
# Update configuration for respective applications
host = '{}:{}'.format(self.domain, self.port) if self.port != '443' else self.domain
confupdater.update_url(host)
confupdater.update_url(tools.compile_url(self.domain, self.port))
# Restart currently running apps in order to update config and re-register nginx proxy
for app in self.conf['apps']:
if tools.is_service_started(app):

View File

@ -11,6 +11,10 @@ import subprocess
NULL_IP = '[100::1]'
def compile_url(domain, port, proto='https'):
port = ':{}'.format(port) if (proto == 'https' and port != '443') or (proto == 'http' and port != '80') else ''
return '{}://{}{}'.format(proto, domain, port)
def get_container_ip(app):
# Return an IP address of a container. If the container is not running, return address from IPv6 discard prefix instead
try:
@ -37,7 +41,7 @@ def get_external_ip(family):
allowed_gai_family = requests.packages.urllib3.util.connection.allowed_gai_family
try:
requests.packages.urllib3.util.connection.allowed_gai_family = lambda: family
return requests.get('http://tools.dasm.cz/myip.php', timeout=5).text
return requests.get('https://tools.dasm.cz/myip.php', timeout=5).text
except:
return None
finally:
@ -67,7 +71,7 @@ def resolve_ip(domain, type):
def ping_url(url):
try:
return requests.post('http://tools.dasm.cz/spotter-ping.php', data = {'url': url}, timeout=5).text == 'spotter-pong'
return requests.post('https://tools.dasm.cz/spotter-ping.php', data = {'url': url}, timeout=5).text == 'spotter-pong'
except requests.exceptions.Timeout:
raise
except:

View File

@ -129,13 +129,13 @@ class WSGIApp(object):
return self.render_template('setup-apps.html', 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
try:
domain = request.form['domain']
port = request.form['port']
request.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)
url = '{}/setup-host'.format(tools.compile_url(server_name, port))
response = self.render_json({'ok': request.session.lang.host_updated(url, url)})
response.call_on_close(tools.restart_nginx)
return response
@ -171,16 +171,16 @@ class WSGIApp(object):
# Check if all applications are accessible from the internet using 3rd party ping service
proto = kwargs['proto']
mgr = request.mgr
port = mgr.port if proto == 'https' else '80'
domains = [mgr.domain]+['{}.{}'.format(mgr.conf['apps'][app]['host'], mgr.domain) for app in mgr.conf['apps']]
for domain in domains:
host = '{}:{}'.format(domain, mgr.port) if proto == 'https' and mgr.port != '443' else domain
url = '{}://{}/'.format(proto, host)
url = tools.compile_url(domain, port, proto)
try:
if not tools.ping_url(url):
return self.render_json({'error': request.session.lang.http_host_not_reachable(url)})
except:
return self.render_json({'error': request.session.lang.http_timeout()})
return self.render_json({'ok': request.session.lang.http_hosts_ok(mgr.port if proto == 'https' else '80')})
return self.render_json({'ok': request.session.lang.http_hosts_ok(port)})
def update_cert_action(self, request):
# Update certificate - either request via Let's Encrypt or manually upload files
@ -203,7 +203,8 @@ class WSGIApp(object):
return self.render_json({'error': request.session.lang.malformed_request()})
except:
return self.render_json({'error': request.session.lang.cert_request_error()})
return self.render_json({'ok': request.session.lang.cert_installed()})
url = tools.compile_url(request.mgr.domain, request.mgr.port)
return self.render_json({'ok': request.session.lang.cert_installed(url, url)})
def update_common_action(self, request):
# Update common settings shared between apps - admin e-mail address, Google Maps API key

View File

@ -16,7 +16,7 @@ class WSGILang:
'cert_file_missing': 'Nebyl vybrán soubor s certifikátem.',
'key_file_missing': 'Nebyl vybrán soubor se soukromým klíčem.',
'cert_request_error': 'Došlo k chybě při žádosti o certifikát. Zkontrolujte, zda je virtuální stroj dostupný z internetu na portu 80.',
'cert_installed': 'Certifikát byl úspěšně nainstalován. Obnovte stránku nebo restartujte webový prohlížeč pro jeho načtení.',
'cert_installed': 'Certifikát byl úspěšně nainstalován. Přejděte na URL <a href="{}">{}</a> nebo restartujte webový prohlížeč pro jeho načtení.',
'common_updated': 'Nastavení aplikací bylo úspěšně změněno.',
'app_started': '<span class="info">Spuštěna</span> (<a href="#" class="app-stop">zastavit</a>)',
'app_stopped': '<span class="error">Zastavena</span> (<a href="#" class="app-start">spustit</a>)',

View File

@ -25,6 +25,8 @@ function update_host() {
$('#host-submit').show();
} else {
$('#host-message').attr('class','info').html(data.ok).show();
$('input').prop('disabled', true);
$('.setup-box').slice(1).css('opacity', '0.5');
}
});
return false;
@ -85,10 +87,10 @@ function update_cert() {
$.ajax({url: '/update-cert', type: 'POST', data: new FormData($('#update-cert')[0]), cache: false, contentType: false, processData: false, success: function(data) {
$('#cert-wait').hide();
if (data.error) {
$('#cert-message').attr('class','error').text(data.error).show();
$('#cert-message').attr('class','error').html(data.error).show();
$('#cert-submit').show();
} else {
$('#cert-message').attr('class','info').text(data.ok).show();
$('#cert-message').attr('class','info').html(data.ok).show();
}
}});
return false;

View File

@ -78,7 +78,7 @@
<div class="portal-box">
<h2><a href="https://odkbuild.{{ host }}"><img src="static/img/ODK.png" alt="Open Data Kit" title="Open Data Kit">ODK Build</a></h2>
<p><strong>Sběr dat s pomocí smartphone</strong>.<br>Aplikace pro návrh formulářů<br>
<p><a href="http://opendatakit.org/xiframe/">XLSForm</a> - online konverter XLS.<br>
<p><a href="https://opendatakit.org/xiframe/">XLSForm</a> - online konverter XLS.<br>
<a href="https://opendatakit.org/downloads/download-info/odk-formuploader/"><img src="static/img/icons/Java.png" class="ico" alt="ODK Form Uploader">ODK Form Uploader</a><br>
<a href="https://opendatakit.org/downloads/download-info/odk-validate-2/"><img src="static/img/icons/Java.png" class="ico" alt="ODK Validate">ODK Validate</a></p>
</div>
@ -333,7 +333,7 @@
</div>
<div class="portal-box">
<h2><a href="http://openid.net"><img src="static/img/OpenID.png" alt="OpenID" title="OpenID">OpenID</a></h2>
<h2><a href="https://openid.net"><img src="static/img/OpenID.png" alt="OpenID" title="OpenID">OpenID</a></h2>
<p>Pro ověření identity budete potřebovat účet OpenID. Zaregistrujte se. Registraci využijete v software Sahana EDEN.</p>
</div>