diff --git a/basic/srv/vm/mgr/pkgmgr.py b/basic/srv/vm/mgr/pkgmgr.py index b390847..051f720 100644 --- a/basic/srv/vm/mgr/pkgmgr.py +++ b/basic/srv/vm/mgr/pkgmgr.py @@ -22,9 +22,7 @@ class PackageManager: # Load JSON configuration self.conf = conf self.online_packages = {} - self.pending = False - self.pending_to_download = 0 - self.pending_downloaded = 0 + self.pending = 0 def get_repo_resource(self, url, stream=False): return requests.get('{}/{}'.format(self.conf['repo']['url'], url), auth=(self.conf['repo']['user'], self.conf['repo']['pwd']), stream=stream) @@ -38,22 +36,21 @@ class PackageManager: pub_key.verify(packages_sig, packages, ec.ECDSA(hashes.SHA512())) self.online_packages = json.loads(packages) - def register_pending_installation(self): + def register_pending_installation(self, name): # Registers pending installation. Fetch online packages here instead of install_pacakges() to fail early if the repo isn't reachable self.fetch_online_packages() - self.pending = True - self.pending_to_download = 1 - self.pending_downloaded = 0 + self.pending = 1 + deps = [d for d in self.get_deps(name) if d not in self.conf['packages']] + return sum(self.online_packages[d]['size'] for d in deps) def install_package(self, name): # Main installation function. Wrapper for download, registration and install script - deps = d for d in self.get_deps(name) if d not in self.conf['packages'] - self.pending_to_download = sum(self.online_packages[d]['size'] for d in deps) + deps = [d for d in self.get_deps(name) if d not in self.conf['packages']] for dep in deps: self.download_package(dep) self.register_package(dep) self.run_install_script(dep) - self.pending = False + self.pending = 0 def uninstall_package(self, name): # Main uninstallation function. Wrapper for uninstall script, filesystem purge and unregistration @@ -69,7 +66,7 @@ class PackageManager: with open(tmp_archive, 'wb') as f: for chunk in r.iter_content(chunk_size=65536): if chunk: - self.pending_downloaded += f.write(chunk) + self.pending += f.write(chunk) # Verify hash if self.online_packages[name]['sha512'] != hash_file(tmp_archive): raise InvalidSignature(name) diff --git a/basic/srv/vm/mgr/wsgiapp.py b/basic/srv/vm/mgr/wsgiapp.py index d196c83..29c4793 100644 --- a/basic/srv/vm/mgr/wsgiapp.py +++ b/basic/srv/vm/mgr/wsgiapp.py @@ -141,13 +141,16 @@ class WSGIApp(object): def setup_apps_view(self, request): # Application manager view. - self.pkgmgr.fetch_online_packages() + try: + self.pkgmgr.fetch_online_packages() + except: + pass all_apps = sorted(set([k for k,v in self.pkgmgr.online_packages.items() if 'host' in v] + list(self.conf['apps'].keys()))) return self.render_template('setup-apps.html', request, all_apps=all_apps, online_packages=self.pkgmgr.online_packages) - def render_setup_apps_row(self, app, app_title, pending=False): + def render_setup_apps_row(self, app, app_title, total_size=None): t = self.jinja_env.get_template('setup-apps-row.html') - return t.render({'app': app, 'app_title': app_title, 'conf': self.conf, 'pending': pending}) + return t.render({'app': app, 'app_title': app_title, 'conf': self.conf, 'total_size': total_size}) def update_host_action(self, request): # Update domain and port, then restart nginx @@ -285,19 +288,19 @@ class WSGIApp(object): return self.render_json({'error': request.session.lang.installation_in_progress()}) try: app = request.form['app'] - self.pkgmgr.register_pending_installation() + total_size = self.pkgmgr.register_pending_installation(app) except (BadRequest, InvalidValueException): return self.render_json({'error': request.session.lang.malformed_request()}) except: return self.render_json({'error': request.session.lang.package_manager_error()}) app_title = self.pkgmgr.online_packages[app]['title'] - response = self.render_json({'ok': self.render_setup_apps_row(app, app_title, True)}) - response.call_on_close(lambda: pkgmgr.install_package(app)) + response = self.render_json({'ok': self.render_setup_apps_row(app, app_title, round(total_size / 1048576, 1))}) + response.call_on_close(lambda: self.pkgmgr.install_package(app)) return response def get_install_progress_action(self, request): if self.pkgmgr.pending: - return self.render_json({'progress': '{0:.1f}'.format(self.pkgmgr.pending_downloaded / self.pkgmgr.pending_to_download)}) + return self.render_json({'progress': round(self.pkgmgr.pending / 1048576, 1)}) self.conf.load() app = request.form['app'] app_title = self.conf['apps'][app]['title'] diff --git a/basic/srv/vm/static/js/admin.js b/basic/srv/vm/static/js/admin.js index 78208b3..47fe08e 100644 --- a/basic/srv/vm/static/js/admin.js +++ b/basic/srv/vm/static/js/admin.js @@ -12,11 +12,11 @@ $(function() { .on('click', '.app-start', start_app) .on('click', '.app-stop', stop_app) .on('click', '.app-install', install_app) - .on('click', '.app-uninstall', install_app); + .on('click', '.app-uninstall', uninstall_app); $('#update-password').on('submit', update_password); $('#reboot-vm').on('click', reboot_vm); $('#shutdown-vm').on('click', shutdown_vm); - window.setTimeout(check_progress, 1000); + window.setInterval(check_progress, 1000); }); function update_host() { @@ -146,7 +146,7 @@ function _do_app(action, ev) { $.post('/'+action+'-app', {'app': tr.data('app')}, function(data) { if (data.error) { td.attr('class','error').html(data.error); - } else { + } else if (action) { tr.replaceWith(data.ok); } }); @@ -166,7 +166,7 @@ function install_app(ev) { } function uninstall_app(ev) { - if (confirm('Do you really want to uninstall this applition?')) { + if (confirm('Do you really want to uninstall this application?')) { return _do_app('uninstall', ev); } return false; @@ -176,7 +176,7 @@ function check_progress() { var progress = $('#install-progress'); if (progress.length) { var tr = progress.closest('tr'); - $.get('/get-install-progress', {'app': tr.data('app')}, function(data) { + $.post('/get-install-progress', {'app': tr.data('app')}, function(data) { if (data.progress) { progress.text(data.progress); } else { diff --git a/basic/srv/vm/templates/setup-apps-row.html b/basic/srv/vm/templates/setup-apps-row.html index e0a0eb0..ad0d6d4 100644 --- a/basic/srv/vm/templates/setup-apps-row.html +++ b/basic/srv/vm/templates/setup-apps-row.html @@ -2,6 +2,6 @@ {{ app_title }} - {% if pending %}Instalace (0 %){% elif app not in conf['apps'] %} Není nainstalována{% elif is_service_started(app) %}Spuštěna{% else %}Zastavena{% endif %} - {% if app not in conf['apps'] %}Instalovat{% else %}{% if is_service_started(app) %}Zastavit{% else %}Spustit{% endif %}, Odinstalovat{% endif %} + {% if total_size %}Instalace (0 / {{ total_size }} MB){% elif app not in conf['apps'] %} Není nainstalována{% elif is_service_started(app) %}Spuštěna{% else %}Zastavena{% endif %} + {% if total_size %}
{% elif app not in conf['apps'] %}Instalovat{% else %}{% if is_service_started(app) %}Zastavit{% else %}Spustit{% endif %}, Odinstalovat{% endif %} diff --git a/basic/srv/vm/templates/setup-apps.html b/basic/srv/vm/templates/setup-apps.html index 4ecc441..4853d5d 100644 --- a/basic/srv/vm/templates/setup-apps.html +++ b/basic/srv/vm/templates/setup-apps.html @@ -51,6 +51,9 @@ {% endfor %} + {% if not online_packages %} +

Připojení k distribučnímu serveru se nezdařilo. Zkontrolujte přístupové údaje a připojení k síti.

+ {% endif %}
diff --git a/basic/srv/vm/wsgi.py b/basic/srv/vm/wsgi.py index 13b7ffe..4f5c9a8 100755 --- a/basic/srv/vm/wsgi.py +++ b/basic/srv/vm/wsgi.py @@ -13,4 +13,4 @@ if __name__ == '__main__': from werkzeug.contrib.fixers import ProxyFix from werkzeug.serving import run_simple - run_simple('127.0.0.1', 8080, ProxyFix(application)) + run_simple('127.0.0.1', 8080, ProxyFix(application), threaded=True) diff --git a/zz-build/usr/bin/lxc-pack b/zz-build/usr/bin/lxc-pack index 33f9000..10d8548 100755 --- a/zz-build/usr/bin/lxc-pack +++ b/zz-build/usr/bin/lxc-pack @@ -46,9 +46,9 @@ def pack(pkg_file): cwd = os.path.dirname(os.path.abspath(pkg_file)) subprocess.run(['tar', '--transform', 's|^|srv/{}/|'.format(pkg_name), '-rpf', tar_path, 'install', 'install.sh', 'upgrade', 'upgrade.sh', 'uninstall', 'uninstall.sh'], cwd=cwd) # Compress the tarball with xz (LZMA2) - print('Compressing', tar_path, '({0:.2f} MB)'.format(os.path.getsize(tar_path)/1048576)) + print('Compressing', tar_path, '({} MB)'.format(round(os.path.getsize(tar_path)/1048576, 2))) subprocess.run(['xz', '-9', tar_path]) - print('Compressed ', xz_path, '({0:.2f} MB)'.format(os.path.getsize(xz_path)/1048576)) + print('Compressed ', xz_path, '({} MB)'.format(round(os.path.getsize(xz_path)/1048576, 2))) # Register package print('Registering package')