Merge branch '43-install-remove-app-from-cli' into 45-allow-database-to-have-external-apps

This commit is contained in:
Mart van Santen 2022-09-23 23:59:08 +08:00
commit 6e427f275f
13 changed files with 1244 additions and 16 deletions

View file

@ -13,11 +13,11 @@ from flask.cli import AppGroup
from ory_kratos_client.api import v0alpha2_api as kratos_api
from sqlalchemy import func
from config import *
from config import HYDRA_ADMIN_URL, KRATOS_ADMIN_URL, KRATOS_PUBLIC_URL
from helpers import KratosUser
from cliapp import cli
from areas.roles import Role
from areas.apps import AppRole, App
from areas.apps import AppRole, App, APP_NOT_INSTALLED_STATUS
from database import db
# APIs
@ -66,7 +66,7 @@ def create_app(slug, name):
if app_obj:
current_app.logger.info(f"App definition: {name} ({slug}) already exists in database")
return
db.session.add(obj)
db.session.commit()
current_app.logger.info(f"App definition: {name} ({slug}) created")
@ -92,20 +92,92 @@ def delete_app(slug):
:param slug: str Slug of app to remove
"""
current_app.logger.info(f"Trying to delete app: {slug}")
obj = App.query.filter_by(slug=slug).first()
app_obj = App.query.filter_by(slug=slug).first()
if not obj:
if not app_obj:
current_app.logger.info("Not found")
return
# Deleting will (probably) fail if there are still roles attached. This is a
# PoC implementation only. Actually management of apps and roles will be
# done by the backend application
db.session.delete(obj)
db.session.commit()
current_app.logger.info("Success")
deleted = app_obj.delete()
current_app.logger.info(f"Success: {deleted}")
return
@app_cli.command(
"uninstall",
)
@click.argument("slug")
def uninstall_app(slug):
"""Uninstalls the app from the cluster
:param slug: str Slug of app to remove
"""
current_app.logger.info(f"Trying to delete app: {slug}")
app_obj = App.query.filter_by(slug=slug).first()
if not app_obj:
current_app.logger.info("Not found")
return
uninstalled = app_obj.uninstall()
current_app.logger.info(f"Success: {uninstalled}")
return
@app_cli.command("status")
@click.argument("slug")
def status_app(slug):
"""Gets the current app status from the Kubernetes cluster
:param slug: str Slug of app to remove
"""
current_app.logger.info(f"Getting status for app: {slug}")
app = App.query.filter_by(slug=slug).first()
if not app:
current_app.logger.error(f"App {slug} does not exist")
return
current_app.logger.info(f"Status: {app.get_status()}")
@app_cli.command("install")
@click.argument("slug")
def install_app(slug):
"""Installs app into Kubernetes cluster
:param slug: str Slug of app to install
"""
current_app.logger.info(f"Installing app: {slug}")
app = App.query.filter_by(slug=slug).first()
if not app:
current_app.logger.error(f"App {slug} does not exist")
return
current_status = app.get_status()
if current_status == APP_NOT_INSTALLED_STATUS:
app.install()
current_app.logger.info(
f"App {slug} installing... use `status` to see status")
else:
current_app.logger.error("App {slug} should have status"
f" {APP_NOT_INSTALLED_STATUS} but has status: {current_status}")
@app_cli.command("roles")
@click.argument("slug")
def roles_app(slug):
"""Gets a list of roles for this app
:param slug: str Slug of app queried
"""
current_app.logger.info(f"Getting roles for app: {slug}")
app = App.query.filter_by(slug=slug).first()
if not app:
current_app.logger.error(f"App {slug} does not exist")
return
current_app.logger.info("Roles: ")
for role in app.roles:
current_app.logger.info(role)
cli.cli.add_command(app_cli)
@ -282,7 +354,7 @@ def setpassword_user(email, password):
# Execute UI sequence to set password, given we have a recovery URL
result = kratos_user.ui_set_password(KRATOS_PUBLIC_URL, url, password)
except Exception as error:
except Exception as error: # pylint: disable=broad-except
current_app.logger.error(f"Error while setting password: {error}")
return False
@ -321,7 +393,7 @@ def recover_user(email):
url = kratos_user.get_recovery_link()
print(url)
except Exception as error:
except Exception as error: # pylint: disable=broad-except
current_app.logger.error(f"Error while getting reset link: {error}")