ucloud-setup/app/api.py
2019-08-30 18:26:22 +05:00

83 lines
2.1 KiB
Python

import click
import subprocess
import os
import shutil
from app.helper import (
clone,
clone_common,
clone_etcd_wrapper,
GitOperation,
PipenvOperation,
FileOperation,
PackageManagementOperation,
VirtualenvOperation
)
@click.group()
def api():
pass
@api.command("setup")
@click.option("--path", required=True)
@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)
@click.option("--etcd_url", required=True)
@click.option("--etcd_password", required=True)
@click.option(
"--otp_server",
default="https://otp.ungleich.ch/ungleichotp/",
help="URL of ungleich OTP server")
def setup(
path,
auth_name,
auth_seed,
auth_realm,
realm_allowed,
otp_server,
etcd_url,
etcd_password,
):
os.chdir(path)
repo_name = "ucloud-api"
# Git Operation Depends on success of previous operation i.e PackageManagementOperation
op = GitOperation.clone(
url=f"https://code.ungleich.ch/ungleich-public/{repo_name}.git"
)
content = (
f"AUTH_NAME={auth_name}\n"
f"AUTH_SEED={auth_seed}\n"
f"AUTH_REALM={auth_realm}\n"
f"REALM_ALLOWED={list(realm_allowed)}\n"
f"OTP_SERVER={otp_server}\n"
f"ETCD_URL={etcd_url}\n"
f"ETCD_PASSWORD={etcd_password}\n"
)
# FileOperation depends on success of previos operation i.e GitOperation.clone
op.add(FileOperation.write, path=os.path.join(repo_name, ".env"), content=content)
op.add(
GitOperation.clone,
path=repo_name,
url="https://code.ungleich.ch/ungleich-public/ucloud_common",
)
op.add(
GitOperation.clone,
path=repo_name,
url="https://code.ungleich.ch/ahmedbilal/etcd3_wrapper",
)
shutil.copytree(
src=os.path.join(repo_name, "etcd3_wrapper"),
dst=os.path.join(repo_name, "ucloud_common", "etcd3_wrapper"),
)
op.add(PipenvOperation.create, path=repo_name, site_packages=True)
op.add(PipenvOperation.install, path=repo_name)