ucloud-setup/app/api.py

85 lines
2.1 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-30 01:23:45 +00:00
VirtualenvOperation
2019-08-28 11:42:50 +00:00
)
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/",
2019-08-30 01:29:31 +00:00
help="URL of ungleich OTP server")
2019-08-30 01:23:45 +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-30 01:23:45 +00:00
2019-08-28 15:42:57 +00:00
repo_name = "ucloud-api"
2019-08-30 01:23:45 +00:00
2019-08-28 11:42:50 +00:00
# Git Operation Depends on success of previous operation i.e PackageManagementOperation
2019-08-30 01:23:45 +00:00
op = GitOperation.clone(
2019-08-30 14:46:31 +00:00
url=f"https://code.ungleich.ch/ungleich-public/{repo_name}.git",
arguments="-b wip"
2019-08-30 01:23:45 +00:00
)
2019-08-28 11:42:50 +00:00
content = (
f"AUTH_NAME={auth_name}\n"
f"AUTH_SEED={auth_seed}\n"
2019-08-30 13:26:22 +00:00
f"AUTH_REALM={auth_realm}\n"
2019-08-28 11:42:50 +00:00
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
2019-08-30 01:23:45 +00:00
op.add(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-30 01:23:45 +00:00
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"),
2019-08-28 11:42:50 +00:00
)
2019-08-30 10:55:10 +00:00
op.add(PipenvOperation.create, path=repo_name, site_packages=True)
2019-08-30 06:18:10 +00:00
op.add(PipenvOperation.install, path=repo_name)