Get to the point of deploy successfully

This commit is contained in:
Luke Murphy 2020-07-02 14:20:03 +02:00
parent 409dfe5955
commit 86d252bcdd
No known key found for this signature in database
GPG Key ID: 5E2EF5A63E3718CC

View File

@ -2,12 +2,10 @@
from hashlib import md5 from hashlib import md5
from json import dumps, loads from json import dumps, loads
from os import environ from os import environ, mkdir
from os.path import exists from os.path import exists
from pathlib import Path from pathlib import Path
from random import choice
from shlex import split from shlex import split
from string import ascii_lowercase
from subprocess import run from subprocess import run
from typing import Dict from typing import Dict
@ -38,11 +36,6 @@ APPS_SPEC = {
DATA_DIR = Path("./data") DATA_DIR = Path("./data")
def get_secret(n: int) -> str:
"""Generate a ASCII secret that is n chars long."""
return "".join(choice(ascii_lowercase) for _ in range(n))
def clone_app_template(app_name: str) -> None: def clone_app_template(app_name: str) -> None:
"""Git clone an app template repository.""" """Git clone an app template repository."""
clone_path = DATA_DIR / app_name clone_path = DATA_DIR / app_name
@ -50,6 +43,12 @@ def clone_app_template(app_name: str) -> None:
run(split(f"git clone {clone_url} {clone_path}")) run(split(f"git clone {clone_url} {clone_path}"))
def create_docker_secret(key: str, value: str) -> None:
"""Load a docker secret into swarm."""
command = f"echo {value} | docker secret create {key} -"
run(command, shell=True)
def dump_db(db: Dict) -> None: def dump_db(db: Dict) -> None:
"""Dump the database.""" """Dump the database."""
with open(DATA_DIR / "db.json", "w") as handle: with open(DATA_DIR / "db.json", "w") as handle:
@ -72,18 +71,31 @@ def get_hash(value: str) -> str:
return hasher.hexdigest() return hasher.hexdigest()
def get_swarm_versions(app_name, form_data, env): def arrange_configs_and_secrets(app_name, form_data, env):
"""Version secrets and configs for swarm.""" """Version secrets and configs for swarm."""
values = [APPS_SPEC[app_name]["secrets"], APPS_SPEC[app_name]["configs"]]
for value in values: def _create_versions(values, secrets=False):
if value in form_data: """Step through listing and create an env var key/val.
hashed = get_hash(form_data[value])
env_key = f"{value.upper()}_VERSION" Takes "app_ini" and produces {"APP_INI_VERSION": "app_ini_laksjdklajsdkla"}.
env[env_key] = f"{value}_{hashed}" Hash is based on the value of the input passed in from the form.
"""
for value in values:
if value in form_data:
hashed = get_hash(form_data[value])
env_key = f"{value.upper()}_VERSION"
env_val = f"{value}_{hashed}"
env[env_key] = env_val
if secrets:
create_docker_secret(env_val, form_data[value])
_create_versions(APPS_SPEC[app_name]["configs"])
_create_versions(APPS_SPEC[app_name]["secrets"], secrets=True)
return env return env
def stack_deploy(stack_name, app_name, env): def stack_deploy(app_name, stack_name, env):
"""Depoy an application to the swarm.""" """Depoy an application to the swarm."""
compose_yml = DATA_DIR / app_name / "compose.yml" compose_yml = DATA_DIR / app_name / "compose.yml"
command = f"docker stack deploy -c {compose_yml} {stack_name}" command = f"docker stack deploy -c {compose_yml} {stack_name}"
@ -150,10 +162,17 @@ def deploy(app_name):
if not form.validate(): if not form.validate():
return render_template("second/install.html", app_name=app_name, form=form) return render_template("second/install.html", app_name=app_name, form=form)
env = get_loaded_env(app_name, form.data) try:
env = get_swarm_versions(app_name, form.data, env) mkdir(DATA_DIR)
except FileExistsError:
pass
dump_db({fork.data["stack_name"]: form.data}) clone_app_template(app_name)
env = get_loaded_env(app_name, form.data)
env = arrange_configs_and_secrets(app_name, form.data, env)
dump_db({form.data["stack_name"]: form.data})
stack_deploy(app_name, form.data["stack_name"], env) stack_deploy(app_name, form.data["stack_name"], env)