Spotter-VM/basic/srv/vm/mgr/appmgr.py

219 lines
8.0 KiB
Python
Raw Normal View History

2018-10-02 22:13:39 +02:00
# -*- coding: utf-8 -*-
2018-10-15 15:40:40 +02:00
import hashlib
2018-10-02 22:13:39 +02:00
import json
2018-10-15 14:58:24 +02:00
import os
2018-10-02 22:13:39 +02:00
import requests
import shutil
2018-10-15 14:58:24 +02:00
import subprocess
import time
import uuid
2018-10-02 22:13:39 +02:00
from cryptography.exceptions import InvalidSignature
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import ec
from cryptography.hazmat.primitives.serialization import load_pem_public_key
from threading import Lock
from . import tools
2018-10-02 22:13:39 +02:00
PUB_FILE = '/srv/vm/packages.pub'
2018-10-02 22:13:39 +02:00
LXC_ROOT = '/var/lib/lxc'
class ActionItem:
def __init__(self, action, app):
self.timestamp = int(time.time())
self.action = action
self.app = app
self.started = False
self.finished = False
self.data = None
class InstallItem:
def __init__(self, total):
# Stage 0 = download, 1 = deps install, 2 = app install
self.stage = 0
self.total = total
self.downloaded = 0
def __str__(self):
# Limit the disaplyed percentage between 1 - 99 for aestethical and psychological reasons
return str(max(1, min(99, round(self.downloaded / self.total * 100))))
class AppMgr:
def __init__(self, vmmgr):
self.vmmgr = vmmgr
self.conf = vmmgr.conf
2018-10-02 22:13:39 +02:00
self.online_packages = {}
self.action_queue = {}
self.lock = Lock()
2018-10-02 22:13:39 +02:00
2018-10-26 15:38:35 +02:00
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)
2018-10-15 15:40:40 +02:00
def fetch_online_packages(self):
2018-10-02 22:13:39 +02:00
# Fetches and verifies online packages. Can raise InvalidSignature
2018-10-27 12:49:22 +02:00
self.online_packages = {}
2018-10-26 15:38:35 +02:00
packages = self.get_repo_resource('packages').content
packages_sig = self.get_repo_resource('packages.sig').content
with open(PUB_FILE, 'rb') as f:
2018-10-02 22:13:39 +02:00
pub_key = load_pem_public_key(f.read(), default_backend())
pub_key.verify(packages_sig, packages, ec.ECDSA(hashes.SHA512()))
2018-10-15 15:40:40 +02:00
self.online_packages = json.loads(packages)
2018-10-02 22:13:39 +02:00
def enqueue_action(self, action, app):
# Remove actions older than 1 day
for id,item in self.action_queue.items():
if item.timestamp < time.time() - 86400:
del self.item[id]
# Enqueue action
id = '{}:{}'.format(app, uuid.uuid4())
item = ActionItem(action, app)
self.action_queue[id] = item
return id,item
def get_actions(self, ids):
# Return list of requested actions
result = {}
for id in ids:
result[id] = self.action_queue[id] if id in self.action_queue else None
return result
def process_action(self, id):
# Main method for deferred queue processing called by WSGI close handler
item = self.action_queue[id]
with self.lock:
item.started = True
try:
# Call the action method inside exclusive lock
getattr(self, item.action)(item)
except BaseException as e:
item.data = e
finally:
item.finished = True
2018-10-26 17:31:40 +02:00
def start_app(self, item):
if not tools.is_service_started(item.app):
self.vmmgr.start_app(item.app)
def stop_app(self, app):
if tools.is_service_started(item.app):
self.vmmgr.stop_app(item.app)
def install_app(self, item):
2018-10-26 15:38:35 +02:00
# Main installation function. Wrapper for download, registration and install script
deps = [d for d in self.get_install_deps(item.app) if d not in self.conf['packages']]
item.data = InstallItem(sum(self.online_packages[d]['size'] for d in deps))
for dep in deps:
self.download_package(dep, item.data)
for dep in deps:
item.data.stage = 2 if dep == deps[-1] else 1
self.unpack_package(dep)
# Run uninstall script before installation to purge previous failed installation
self.run_uninstall_script(dep)
self.register_package(dep)
self.run_install_script(dep)
def uninstall_app(self, item):
2018-10-26 14:12:12 +02:00
# Main uninstallation function. Wrapper for uninstall script, filesystem purge and unregistration
deps = self.get_install_deps(item.app, False)[::-1]
2018-10-26 21:52:06 +02:00
for dep in deps:
if dep not in self.get_uninstall_deps():
self.run_uninstall_script(dep)
self.purge_package(dep)
self.unregister_package(dep)
2018-10-02 22:13:39 +02:00
def download_package(self, name, installitem):
tmp_archive = '/tmp/{}.tar.xz'.format(name)
2018-10-26 15:38:35 +02:00
r = self.get_repo_resource('{}.tar.xz'.format(name), True)
2018-10-15 14:58:24 +02:00
with open(tmp_archive, 'wb') as f:
for chunk in r.iter_content(chunk_size=65536):
2018-10-02 22:13:39 +02:00
if chunk:
installitem.downloaded += f.write(chunk)
def unpack_package(self, name):
tmp_archive = '/tmp/{}.tar.xz'.format(name)
2018-10-02 22:13:39 +02:00
# Verify hash
2018-10-15 14:58:24 +02:00
if self.online_packages[name]['sha512'] != hash_file(tmp_archive):
2018-10-02 22:13:39 +02:00
raise InvalidSignature(name)
# Unpack
2018-10-28 18:20:27 +01:00
subprocess.run(['tar', 'xJf', tmp_archive], cwd='/', check=True)
2018-10-15 14:58:24 +02:00
os.unlink(tmp_archive)
2018-10-02 22:13:39 +02:00
2018-10-26 14:12:12 +02:00
def purge_package(self, name):
# Removes package and shared data from filesystem
shutil.rmtree(os.path.join(LXC_ROOT, self.conf['packages'][name]['lxcpath']))
srv_dir = os.path.join('/srv/', name)
2018-10-26 21:52:06 +02:00
if os.path.exists(srv_dir):
2018-10-26 14:12:12 +02:00
shutil.rmtree(srv_dir)
2018-10-15 14:58:24 +02:00
def register_package(self, name):
2018-10-21 10:09:02 +02:00
# Registers a package in local configuration
2018-10-15 14:58:24 +02:00
metadata = self.online_packages[name]
self.conf['packages'][name] = {
2018-10-26 14:12:12 +02:00
'deps': metadata['deps'],
'lxcpath': metadata['lxcpath'],
'version': metadata['version']
2018-10-02 22:13:39 +02:00
}
2018-10-21 10:09:02 +02:00
# If host definition is present, register the package as application
2018-10-15 15:40:40 +02:00
if 'host' in metadata:
self.conf['apps'][name] = {
'title': metadata['title'],
'host': metadata['host'],
'login': 'N/A',
'password': 'N/A',
'visible': False
}
2018-10-21 10:09:02 +02:00
self.conf.save()
2018-10-02 22:13:39 +02:00
2018-10-26 14:12:12 +02:00
def unregister_package(self, name):
# Removes a package from local configuration
del self.conf['packages'][name]
if name in self.conf['apps']:
del self.conf['apps'][name]
self.conf.save()
2018-10-02 22:13:39 +02:00
2018-10-28 18:20:27 +01:00
def run_install_script(self, name):
# Runs install.sh for a package, if the script is present
install_dir = os.path.join('/srv/', name, 'install')
install_script = os.path.join('/srv/', name, 'install.sh')
if os.path.exists(install_script):
subprocess.run(install_script, check=True)
os.unlink(install_script)
if os.path.exists(install_dir):
shutil.rmtree(install_dir)
def run_uninstall_script(self, name):
# Runs uninstall.sh for a package, if the script is present
uninstall_script = os.path.join('/srv/', name, 'uninstall.sh')
if os.path.exists(uninstall_script):
subprocess.run(uninstall_script, check=True)
2018-10-26 21:52:06 +02:00
def get_install_deps(self, name, online=True):
# Flatten dependency tree for a package while preserving the dependency order
packages = self.online_packages if online else self.conf['packages']
deps = packages[name]['deps'].copy()
for dep in deps[::-1]:
deps[:0] = [d for d in self.get_install_deps(dep, online)]
deps = list(dict.fromkeys(deps + [name]))
return deps
def get_uninstall_deps(self):
# Create reverse dependency tree for all installed packages
deps = {}
for pkg in self.conf['packages']:
for d in self.conf['packages'][pkg]['deps']:
deps.setdefault(d, []).append(pkg)
2018-10-02 22:13:39 +02:00
return deps
def hash_file(file_path):
sha512 = hashlib.sha512()
with open(file_path, 'rb') as f:
while True:
data = f.read(65536)
if not data:
break
sha512.update(data)
return sha512.hexdigest()