Implement demo mode

This commit is contained in:
Disassembler 2020-08-26 18:06:27 +02:00
parent 3fe8ae2735
commit ccec8faff6
No known key found for this signature in database
GPG Key ID: 524BD33A0EE29499
3 changed files with 33 additions and 1 deletions

View File

@ -26,6 +26,7 @@ logging.basicConfig(level=logging.WARNING)
class WSGIApp: class WSGIApp:
def __init__(self): def __init__(self):
self.demo = config.get_common().get('demo')
self.queue = ActionQueue() self.queue = ActionQueue()
self.jinja_env = Environment(loader=FileSystemLoader('/usr/share/vmmgr/templates'), autoescape=True, lstrip_blocks=True, trim_blocks=True) self.jinja_env = Environment(loader=FileSystemLoader('/usr/share/vmmgr/templates'), autoescape=True, lstrip_blocks=True, trim_blocks=True)
self.url_map = Map(( self.url_map = Map((
@ -289,6 +290,9 @@ class WSGIApp:
return self.render_html('setup-remote.html', request, authorized_keys=authorized_keys, wg_conf=wg_conf, message=message) return self.render_html('setup-remote.html', request, authorized_keys=authorized_keys, wg_conf=wg_conf, message=message)
def update_host_action(self, request): def update_host_action(self, request):
# Do nothing in demo
if self.demo:
return self.render_json({'error': request.session.lang.not_available_in_demo()})
# Update domain and port, then restart nginx # Update domain and port, then restart nginx
domain = request.form['domain'] domain = request.form['domain']
port = request.form['port'] port = request.form['port']
@ -339,6 +343,9 @@ class WSGIApp:
return self.render_json({'ok': request.session.lang.http_hosts_ok(port)}) return self.render_json({'ok': request.session.lang.http_hosts_ok(port)})
def update_cert_action(self, request): def update_cert_action(self, request):
# Do nothing in demo
if self.demo:
return self.render_json({'error': request.session.lang.not_available_in_demo()})
# Update certificate - either request via Let's Encrypt or manually upload files # Update certificate - either request via Let's Encrypt or manually upload files
if request.form['method'] == 'selfsigned': if request.form['method'] == 'selfsigned':
vmmgr.create_selfsigned_cert() vmmgr.create_selfsigned_cert()
@ -361,6 +368,10 @@ class WSGIApp:
return self.render_json({'ok': request.session.lang.cert_installed(url, url)}) return self.render_json({'ok': request.session.lang.cert_installed(url, url)})
def update_common_action(self, request): def update_common_action(self, request):
# Do nothing in demo
if self.demo:
request.session['msg'] = f'common:error:{request.session.lang.not_available_in_demo()}'
return redirect('/setup-apps')
# Update common settings shared between apps - admin e-mail address, Google Maps API key # Update common settings shared between apps - admin e-mail address, Google Maps API key
email = request.form['email'] email = request.form['email']
if not validator.is_valid_email(email): if not validator.is_valid_email(email):
@ -371,6 +382,10 @@ class WSGIApp:
return redirect('/setup-apps') return redirect('/setup-apps')
def update_repo_action(self, request): def update_repo_action(self, request):
# Do nothing in demo
if self.demo:
request.session['msg'] = f'repo:error:{request.session.lang.not_available_in_demo()}'
return redirect('/setup-apps')
# Update repository URL and credentials # Update repository URL and credentials
url = request.form['repourl'] url = request.form['repourl']
if not validator.is_valid_repo_url(url): if not validator.is_valid_repo_url(url):
@ -428,6 +443,9 @@ class WSGIApp:
return self.render_json({'ok': self.render_setup_apps_table(request)}) return self.render_json({'ok': self.render_setup_apps_table(request)})
def update_password_action(self, request): def update_password_action(self, request):
# Do nothing in demo
if self.demo:
return self.render_json({'error': request.session.lang.not_available_in_demo()})
# Updates password for both HDD encryption (LUKS-on-LVM) and web interface admin account # Updates password for both HDD encryption (LUKS-on-LVM) and web interface admin account
if request.form['newpassword'] != request.form['newpassword2']: if request.form['newpassword'] != request.form['newpassword2']:
return self.render_json({'error': request.session.lang.password_mismatch()}) return self.render_json({'error': request.session.lang.password_mismatch()})
@ -441,18 +459,27 @@ class WSGIApp:
return self.render_json({'ok': request.session.lang.password_changed()}) return self.render_json({'ok': request.session.lang.password_changed()})
def reboot_vm_action(self, request): def reboot_vm_action(self, request):
# Do nothing in demo
if self.demo:
return self.render_json({'error': request.session.lang.not_available_in_demo()})
# Reboots VM # Reboots VM
response = self.render_json({'ok': request.session.lang.reboot_initiated()}) response = self.render_json({'ok': request.session.lang.reboot_initiated()})
response.call_on_close(vmmgr.reboot_vm) response.call_on_close(vmmgr.reboot_vm)
return response return response
def shutdown_vm_action(self, request): def shutdown_vm_action(self, request):
# Do nothing in demo
if self.demo:
return self.render_json({'error': request.session.lang.not_available_in_demo()})
# Shuts down VM # Shuts down VM
response = self.render_json({'ok': request.session.lang.shutdown_initiated()}) response = self.render_json({'ok': request.session.lang.shutdown_initiated()})
response.call_on_close(vmmgr.shutdown_vm) response.call_on_close(vmmgr.shutdown_vm)
return response return response
def update_ssh_keys_action(self, request): def update_ssh_keys_action(self, request):
# Do nothing in demo
if self.demo:
return self.render_json({'error': request.session.lang.not_available_in_demo()})
# Update authorized_keys file # Update authorized_keys file
remote.set_authorized_keys(request.form['ssh-keys'].replace('\r', '')) remote.set_authorized_keys(request.form['ssh-keys'].replace('\r', ''))
request.session['msg'] = f'ssh:info:{request.session.lang.ssh_keys_installed()}' request.session['msg'] = f'ssh:info:{request.session.lang.ssh_keys_installed()}'

View File

@ -54,6 +54,7 @@ class WSGILang:
'action_update': 'Aktualizovat', 'action_update': 'Aktualizovat',
'ssh_keys_installed': 'SSH klíče byly úspěšně změněny.', 'ssh_keys_installed': 'SSH klíče byly úspěšně změněny.',
'vpn_updated': 'Nastavení VPN bylo úspěšně změněno.', 'vpn_updated': 'Nastavení VPN bylo úspěšně změněno.',
'not_available_in_demo': 'Tato funkce není v demo verzi povolena',
} }
def __getattr__(self, key): def __getattr__(self, key):

View File

@ -205,7 +205,11 @@ function update_password() {
function _do_vm(action) { function _do_vm(action) {
$.get('/'+action+'-vm', function(data) { $.get('/'+action+'-vm', function(data) {
if (data.error) {
$('#vm-message').attr('class','error').html(data.error).show();
} else {
$('#vm-message').attr('class','info').html(data.ok).show(); $('#vm-message').attr('class','info').html(data.ok).show();
}
}).fail(lost_connection_alert); }).fail(lost_connection_alert);
} }