ucloud-setup/app/api.py

74 lines
2 KiB
Python
Raw Normal View History

2019-08-27 08:31:22 +00:00
import click
import subprocess
2019-08-27 08:57:22 +00:00
import os
2019-08-28 15:42:57 +00:00
import shutil
2019-08-27 08:31:22 +00:00
2019-08-28 11:42:50 +00:00
from app.helper import (
clone,
clone_common,
clone_etcd_wrapper,
GitOperation,
PipenvOperation,
FileOperation,
PackageManagementOperation,
)
2019-08-27 08:31:22 +00:00
@click.group()
def api():
pass
2019-08-28 11:42:50 +00:00
2019-08-27 08:31:22 +00:00
@api.command("setup")
2019-08-27 08:57:22 +00:00
@click.option("--path", required=True)
2019-08-27 08:31:22 +00:00
@click.option("--auth_name", required=True)
@click.option("--auth_seed", required=True)
@click.option("--auth_realm", required=True)
@click.option("--realm_allowed", multiple=True, required=True)
2019-08-28 15:23:18 +00:00
@click.option("--etcd_url", required=True)
@click.option("--etcd_password", required=True)
2019-08-28 11:42:50 +00:00
@click.option(
"--otp_server",
default="https://otp.ungleich.ch/ungleichotp/",
help="URL of ungleich OTP server",
)
2019-08-28 15:23:18 +00:00
def setup(path, auth_name, auth_seed, auth_realm,
realm_allowed, otp_server, etcd_url,
etcd_password):
2019-08-28 11:42:50 +00:00
os.chdir(path)
2019-08-28 15:42:57 +00:00
repo_name = "ucloud-api"
2019-08-28 11:42:50 +00:00
# Git Operation Depends on success of previous operation i.e PackageManagementOperation
2019-08-28 15:42:57 +00:00
op = GitOperation.clone(url=f"https://code.ungleich.ch/ungleich-public/{repo_name}.git")
2019-08-28 11:42:50 +00:00
content = (
f"AUTH_NAME={auth_name}\n"
f"AUTH_SEED={auth_seed}\n"
f"AUTH_REALM={auth_seed}\n"
f"REALM_ALLOWED={list(realm_allowed)}\n"
f"OTP_SERVER={otp_server}\n"
2019-08-28 15:23:18 +00:00
f"ETCD_URL={etcd_url}\n"
f"ETCD_PASSWORD={etcd_password}\n"
2019-08-28 11:42:50 +00:00
)
# FileOperation depends on success of previos operation i.e GitOperation.clone
op.add(
2019-08-28 15:42:57 +00:00
FileOperation.write, path=os.path.join(repo_name, ".env"), content=content
2019-08-28 11:42:50 +00:00
)
op.add(
GitOperation.clone,
2019-08-28 15:42:57 +00:00
path=repo_name,
2019-08-28 11:42:50 +00:00
url="https://code.ungleich.ch/ungleich-public/ucloud_common",
)
op.add(
GitOperation.clone,
2019-08-28 15:42:57 +00:00
path=repo_name,
2019-08-28 15:44:09 +00:00
url="https://code.ungleich.ch/ahmedbilal/etcd3_wrapper"
2019-08-28 11:42:50 +00:00
)
2019-08-28 15:44:09 +00:00
op.add(shutil.copytree, src=os.path.join(repo_name, "etcd3_wrapper"),
2019-08-28 15:42:57 +00:00
dest=os.path.join(repo_name, "ucloud_common"))
2019-08-28 11:42:50 +00:00
op.add(PipenvOperation.install, path="ucloud-api")