Implement app uninstall

This commit is contained in:
Disassembler 2020-03-12 21:54:18 +01:00
parent ac4b7f9995
commit d1e5b83186
No known key found for this signature in database
GPG Key ID: 524BD33A0EE29499

View File

@ -2,6 +2,7 @@
import json import json
import os import os
import shutil
import subprocess import subprocess
import tarfile import tarfile
import urllib.parse import urllib.parse
@ -17,6 +18,7 @@ class App:
def __init__(self, name, load_from_repo=True): def __init__(self, name, load_from_repo=True):
self.name = name self.name = name
self.version = None self.version = None
self.app_dir = os.path.join(APPS_DIR, name)
self.meta = {} self.meta = {}
self.containers = [] self.containers = []
if load_from_repo: if load_from_repo:
@ -50,15 +52,14 @@ class App:
def run_script(self, action): def run_script(self, action):
# Runs script for an app, if the script is present # Runs script for an app, if the script is present
app_dir = os.path.join(APPS_DIR, self.name) script_dir = os.path.join(self.app_dir, action)
script_dir = os.path.join(app_dir, action) script_path = os.path.join(self.app_dir, f'{script_dir}.sh')
script_path = os.path.join(app_dir, f'{script_dir}.sh')
if os.path.exists(script_path): if os.path.exists(script_path):
# Run the script in its working directory, if there is one, so it doesn't have to figure out paths to packaged files # Run the script in its working directory, if there is one, so it doesn't have to figure out paths to packaged files
env = os.environ.copy() env = os.environ.copy()
env['LAYERS_DIR'] = LAYERS_DIR env['LAYERS_DIR'] = LAYERS_DIR
env['VOLUMES_DIR'] = VOLUMES_DIR env['VOLUMES_DIR'] = VOLUMES_DIR
cwd = script_dir if os.path.exists(script_dir) else app_dir cwd = script_dir if os.path.exists(script_dir) else self.app_dir
subprocess.run(script_path, cwd=cwd, env=env, check=True) subprocess.run(script_path, cwd=cwd, env=env, check=True)
def create_container(self, name, definition): def create_container(self, name, definition):
@ -89,7 +90,18 @@ class App:
raise NotImplementedError() raise NotImplementedError()
def uninstall(self): def uninstall(self):
raise NotImplementedError() definition = repo_local.get_app(self.name)
self.run_script('uninstall')
# Remove containers
for container in definition['containers']:
Container(container, False).destroy()
# Unregister app and remove scripts
repo_local.unregister_app(self.name)
try:
shutil.rmtree(self.app_dir)
except FileNotFoundError:
pass
# TODO: Remove unused images
def start(self): def start(self):
for container in self.containers: for container in self.containers: