Enable deleting apps by using CLI

This commit is contained in:
Maarten de Waard 2022-09-22 16:14:49 +02:00
parent 5893ee39a3
commit caa9b2e79b
No known key found for this signature in database
GPG key ID: 1D3E893A657CC8DA
4 changed files with 95 additions and 33 deletions

View file

@ -58,9 +58,9 @@ class App(db.Model):
if ks_ready and hr_ready:
return "App installed and running"
if not hr_ready:
return f"App failed installing: {hr_message}"
return f"App HelmRelease status: {hr_message}"
if not ks_ready:
return f"App failed installing: {ks_message}"
return f"App Kustomization status: {ks_message}"
return "App is installing..."
@ -71,6 +71,32 @@ class App(db.Model):
# Create add-<app> kustomization
self.__create_kustomization()
def delete(self):
"""
Fully deletes an application
This includes user roles, all kubernetes objects and also PVCs, so your
data will be *gone*
"""
# Delete all roles first
for role in self.roles:
db.session.delete(role)
# Delete the kustomization
if self.__delete_kustomization():
# TODO: This is where we might want to poll for status changes in the
# app, so that only once the kustomization and all its stuff (other ks,
# helmrelease, etc.) is deleted, we continue
# If the kustomization delete went well, commit DB changes.
db.session.commit()
# Then delete the app
db.session.delete(self)
db.session.commit()
return True
return False
def __generate_secrets(self):
"""Generates passwords for app installation"""
# Create app variables secret
@ -90,11 +116,10 @@ class App(db.Model):
"add-app-kustomization.yaml.jinja")
k8s.store_kustomization(kustomization_template_filepath, self.slug)
def __delete_kustomization(self):
"""Deletes kustomization for this app"""
k8s.delete_kustomization(f"add-{self.slug}")
@staticmethod
def __get_templates_dir():
"""Returns directory that contains the Jinja templates used to create app secrets."""
return os.path.join(os.path.dirname(os.path.realpath(__file__)), "templates")
@property
def variables_template_filepath(self):
@ -116,6 +141,20 @@ class App(db.Model):
return 'stackspin-apps'
return 'stackspin'
@property
def roles(self):
"""
All roles that are linked to this app
"""
return AppRole.query.filter_by(
app_id=self.id
).all()
@staticmethod
def __get_templates_dir():
"""Returns directory that contains the Jinja templates used to create app secrets."""
return os.path.join(os.path.dirname(os.path.realpath(__file__)), "templates")
@staticmethod
def check_condition(status):
"""