From f961f75784b89622ccdbd4b9f32cb462acd9f796 Mon Sep 17 00:00:00 2001 From: Disassembler Date: Sun, 24 Jan 2021 11:27:06 +0100 Subject: [PATCH] Implement OS update control --- usr/lib/python3.8/vmmgr/vmmgr.py | 12 ++++++++++++ usr/lib/python3.8/vmmgr/wsgiapp.py | 15 ++++++++++++++- usr/lib/python3.8/vmmgr/wsgilang.py | 2 ++ usr/share/vmmgr/static/js/admin.js | 21 +++++++++++++++++++-- usr/share/vmmgr/templates/setup-apps.html | 9 ++++++++- 5 files changed, 55 insertions(+), 4 deletions(-) diff --git a/usr/lib/python3.8/vmmgr/vmmgr.py b/usr/lib/python3.8/vmmgr/vmmgr.py index f5d7180..f522815 100644 --- a/usr/lib/python3.8/vmmgr/vmmgr.py +++ b/usr/lib/python3.8/vmmgr/vmmgr.py @@ -15,6 +15,7 @@ from spoc.image import Image from . import config, crypto, net, paths, templates BIN_ACME_SH = '/usr/bin/acme.sh' +BIN_APK = '/sbin/apk' BIN_BLKID = '/sbin/blkid' BIN_CRYPTSETUP = '/sbin/cryptsetup' BIN_NGINX = '/usr/sbin/nginx' @@ -149,6 +150,17 @@ def install_manual_cert(public_file, private_file): # Reload nginx reload_nginx() +def check_for_vm_updates(): + try: + lines = subprocess.run([BIN_APK, '--no-cache', 'upgrade', '-s'], check=True, stdout=subprocess.PIPE).stdout.decode().splitlines() + # 4 lines means we've got just repo list and total size + return len(lines) > 4 + except subprocess.CalledProcessError: + return False + +def update_vm(): + subprocess.run([BIN_APK, '--no-cache', 'upgrade'], check=True) + def shutdown_vm(): subprocess.run([BIN_POWEROFF]) diff --git a/usr/lib/python3.8/vmmgr/wsgiapp.py b/usr/lib/python3.8/vmmgr/wsgiapp.py index 9ada7de..d8b16e1 100644 --- a/usr/lib/python3.8/vmmgr/wsgiapp.py +++ b/usr/lib/python3.8/vmmgr/wsgiapp.py @@ -60,6 +60,7 @@ class WSGIApp: Rule('/uninstall-app', endpoint='uninstall_app_action'), Rule('/update-app', endpoint='update_app_action'), Rule('/update-password', endpoint='update_password_action'), + Rule('/update-vm', endpoint='update_vm_action'), Rule('/shutdown-vm', endpoint='shutdown_vm_action'), Rule('/reboot-vm', endpoint='reboot_vm_action'), Rule('/update-ssh-keys', endpoint='update_ssh_keys_action'), @@ -179,7 +180,8 @@ class WSGIApp: message = self.get_session_message(request) repo_conf = vmmgr.get_repo_conf() common = config.get_common() - return self.render_html('setup-apps.html', request, repo_conf=repo_conf, repo_error=repo_error, table=table, message=message, common=common) + has_vm_updates = False if self.demo else vmmgr.check_for_vm_updates() + return self.render_html('setup-apps.html', request, repo_conf=repo_conf, repo_error=repo_error, table=table, message=message, common=common, has_vm_updates=has_vm_updates) def render_setup_apps_table(self, request): lang = request.session.lang @@ -456,6 +458,17 @@ class WSGIApp: return self.render_json({'error': request.session.lang.bad_password()}) return self.render_json({'ok': request.session.lang.password_changed()}) + def update_vm_action(self, request): + # Do nothing in demo + if self.demo: + return self.render_json({'error': request.session.lang.not_available_in_demo()}) + # Updates VM + try: + vmmgr.update_vm() + except subprocess.CalledProcessError: + return self.render_json({'error': request.session.lang.vm_update_failed()}) + return self.render_json({'ok': request.session.lang.vm_updated()}) + def reboot_vm_action(self, request): # Do nothing in demo if self.demo: diff --git a/usr/lib/python3.8/vmmgr/wsgilang.py b/usr/lib/python3.8/vmmgr/wsgilang.py index 28b5f28..4159f88 100644 --- a/usr/lib/python3.8/vmmgr/wsgilang.py +++ b/usr/lib/python3.8/vmmgr/wsgilang.py @@ -55,6 +55,8 @@ class WSGILang: 'ssh_keys_installed': 'SSH klíče byly úspěšně změněny.', 'vpn_updated': 'Nastavení VPN bylo úspěšně změněno.', 'not_available_in_demo': 'Tato funkce není v demo verzi povolena', + 'vm_updated': 'Balíky úspěšně aktualizovány. Pro dokončení aktualizace restartujte virtuální stroj.', + 'vm_update_failed': 'Došlo k chybě při aktualizaci balíků. Zkuste restartovat virtuální stroj a akci opakovat.', } def __getattr__(self, key): diff --git a/usr/share/vmmgr/static/js/admin.js b/usr/share/vmmgr/static/js/admin.js index 6a1abd1..c1517ba 100644 --- a/usr/share/vmmgr/static/js/admin.js +++ b/usr/share/vmmgr/static/js/admin.js @@ -18,6 +18,7 @@ $(function() { .on('click', '.app-update', update_app) .on('click', '.app-clear-status', clear_app_status); $('#update-password').on('submit', update_password); + $('#update-vm').on('click', update_vm); $('#reboot-vm').on('click', reboot_vm); $('#shutdown-vm').on('click', shutdown_vm); if ($('#app-manager').length) { @@ -206,9 +207,25 @@ function update_password() { function _do_vm(action) { $.get('/'+action+'-vm', function(data) { if (data.error) { - $('#vm-message').attr('class','error').html(data.error).show(); + $('#power-vm-message').attr('class','error').html(data.error).show(); } else { - $('#vm-message').attr('class','info').html(data.ok).show(); + $('#power-vm-message').attr('class','info').html(data.ok).show(); + } + }).fail(lost_connection_alert); +} + +function update_vm() { + $('#update-vm').hide(); + $('#update-vm-message').hide(); + $('#update-vm-wait').show(); + $.get('/update-vm', function(data) { + $('#update-vm-wait').hide(); + if (data.error) { + $('#update-vm-message').attr('class','error').html(data.error).show(); + $('#update-vm').show(); + } else { + $('#update-vm-message').attr('class','info').html(data.ok).show(); + $('#update-vm').show().prop('disabled', true); } }).fail(lost_connection_alert); } diff --git a/usr/share/vmmgr/templates/setup-apps.html b/usr/share/vmmgr/templates/setup-apps.html index 1a8e3ac..a3ef1c0 100644 --- a/usr/share/vmmgr/templates/setup-apps.html +++ b/usr/share/vmmgr/templates/setup-apps.html @@ -107,9 +107,16 @@ +

Aktualizace balíků operačního systému virtuálního stroje.

+ +
{% if has_vm_updates %}Jsou k dispozici aktualizace{% else %}Balíky jsou aktuální{% endif %}
+
+
+ Provádí se aktualizace balíků, prosím čekejte... +

Restartování nebo vypnutí virtuálního stroje.

-
+
{% endblock %}