This commit is contained in:
ahmadbilalkhalid 2019-08-28 16:42:50 +05:00
commit e57772f19e
8 changed files with 301 additions and 119 deletions

View file

@ -2,52 +2,84 @@ import click
import subprocess
import os
from app.helper import clone, clone_common,\
clone_etcd_wrapper, GitOperation,\
PipenvOperation, FileOperation,\
PackageManagementOperation
from app.helper import (
clone,
clone_common,
clone_etcd_wrapper,
GitOperation,
PipenvOperation,
FileOperation,
PackageManagementOperation,
)
@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("--otp_server", default="https://otp.ungleich.ch/ungleichotp/",
help="URL of ungleich OTP server")
@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):
os.chdir(path)
def setup(path, auth_name, auth_seed, auth_realm,
realm_allowed, otp_server):
os.chdir(path)
# Install Package
op = PackageManagementOperation.install("etcd")
# Git Operation Depends on success of previous operation i.e PackageManagementOperation
op.add(GitOperation.clone,
url="https://code.ungleich.ch/ungleich-public/ucloud-api.git",
path=path)
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"
# FileOperation depends on success of previos operation i.e GitOperation.clone
op.add(FileOperation.write,
path=os.path.join("ucloud-api", ".env"),
content=content)
op.add(GitOperation.clone, path="ucloud-api",
url="https://code.ungleich.ch/ungleich-public/ucloud_common")
op.add(GitOperation.clone, path="ucloud-api",
url="https://code.ungleich.ch/ahmedbilal/etcd3_wrapper")
op.add(PipenvOperation.install, path="ucloud-api")
# Install etcd
op = PackageManagementOperation.install("etcd")
# Add --pidfile argument to supervise daemon
op.add(
subprocess.check_output,
args=r"""sed -i -e 's/supervise_daemon_args="--chdir $ETCD_DATA_DIR"/supervise_daemon_args="--chdir $ETCD_DATA_DIR --pidfile /run/etcd.pid"/g' /etc/init.d/etcd""".split(),
)
# Change address of etcd
op.add(
subprocess.check_output,
args=r'sed -i -e "s/localhost/[2a0a:e5c0:0:2:0:b3ff:fe39:7994]/g" /etc/etcd/conf.yml'.split(),
)
# Start and Enable etcd service
op.add(subprocess.check_output,
args="service etcd start; rc-update add etcd".split())
# Git Operation Depends on success of previous operation i.e PackageManagementOperation
op.add(
GitOperation.clone,
url="https://code.ungleich.ch/ungleich-public/ucloud-api.git",
)
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"
)
# FileOperation depends on success of previos operation i.e GitOperation.clone
op.add(
FileOperation.write, path=os.path.join("ucloud-api", ".env"), content=content
)
op.add(
GitOperation.clone,
path="ucloud-api",
url="https://code.ungleich.ch/ungleich-public/ucloud_common",
)
op.add(
GitOperation.clone,
path="ucloud-api",
url="https://code.ungleich.ch/ahmedbilal/etcd3_wrapper",
)
op.add(PipenvOperation.install, path="ucloud-api")

View file

@ -2,34 +2,46 @@ import click
import subprocess
import os
from app.helper import clone, clone_common,\
clone_etcd_wrapper, GitOperation, PipenvOperation, FileOperation
from app.helper import (
clone,
clone_common,
clone_etcd_wrapper,
GitOperation,
PipenvOperation,
FileOperation,
)
@click.group()
def file_scan():
pass
@file_scan.command("setup")
@click.option("--path", required=True)
@click.option("--base_dir", required=True)
@click.option("--file_prefix", required=True)
def setup(path, base_dir, file_prefix):
os.chdir(path)
repo_name = "ucloud-file-scan"
op_result = GitOperation.clone(f"https://code.ungleich.ch/ungleich-public/{repo_name}.git")
content = f"BASE_DIR={base_dir}\n" \
f"FILE_PREFIX={file_prefix}\n"
op_result.add(FileOperation.write,
path=os.path.join(repo_name, ".env"),
content=content)
op_result = GitOperation.clone(
f"https://code.ungleich.ch/ungleich-public/{repo_name}.git"
)
content = f"BASE_DIR={base_dir}\n" f"FILE_PREFIX={file_prefix}\n"
op_result.add(
FileOperation.write, path=os.path.join(repo_name, ".env"), content=content
)
op_result.add(
GitOperation.clone,
path=repo_name,
url="https://code.ungleich.ch/ahmedbilal/etcd3_wrapper",
)
op_result.add(GitOperation.clone, path=repo_name,
url="https://code.ungleich.ch/ahmedbilal/etcd3_wrapper")
op_result.add(PipenvOperation.install, path=repo_name)
# Write Crontab entry
# Write Crontab entry

View file

@ -4,10 +4,12 @@ from dataclasses import dataclass
from abc import ABC
from enum import Enum
class ResultType(Enum):
success = 0
failure = 1
def clone(repo):
command = f"git clone {repo}"
try:
@ -16,14 +18,16 @@ def clone(repo):
return False
else:
return True
def clone_common():
return clone("https://code.ungleich.ch/ungleich-public/ucloud_common")
def clone_etcd_wrapper():
return clone("https://code.ungleich.ch/ahmedbilal/etcd3_wrapper")
class Result(object):
def __init__(self, _result, _type: ResultType, _op=""):
self._type = _type
@ -31,8 +35,7 @@ class Result(object):
self._op = _op
if self._type == ResultType.failure:
print(self._op, "failed")
def add(self, operation, **kwargs):
if self._type == ResultType.success:
r = operation(**kwargs)
@ -42,11 +45,11 @@ class Result(object):
else:
print("Dependency not satisfied")
exit(-1)
def __repr__(self):
return f"{self._type}, {self._result}"
class Operation(ABC):
pass
@ -58,10 +61,11 @@ class GitOperation(object):
try:
output = subprocess.check_output(command.split(), cwd=path)
except subprocess.CalledProcessError as e:
return Result(e, ResultType.failure, inspect.currentframe().f_code.co_name)
return Result(e, ResultType.failure, inspect.currentframe().f_code.co_name)
else:
return Result(output, ResultType.success)
class PipenvOperation(object):
@staticmethod
def install(path=".", package_name=None):
@ -72,7 +76,7 @@ class PipenvOperation(object):
try:
output = subprocess.check_output(command.split(), cwd=path)
except subprocess.CalledProcessError as e:
return Result(e, ResultType.failure, inspect.currentframe().f_code.co_name)
return Result(e, ResultType.failure, inspect.currentframe().f_code.co_name)
else:
return Result(output, ResultType.success)
@ -98,6 +102,7 @@ def get_distro_name():
distro_name = content[start:end]
return distro_name
class PackageManagementOperation(object):
@staticmethod
def install(package_name):
@ -107,7 +112,7 @@ class PackageManagementOperation(object):
command = f"apk add {package_name}"
else:
assert "Unknown Distro"
subprocess.check_output(command.split())
except Exception as e:
print(e)

View file

@ -2,8 +2,15 @@ import click
import subprocess
import os
from app.helper import clone, clone_common,\
clone_etcd_wrapper, GitOperation, PipenvOperation, FileOperation
from app.helper import (
clone,
clone_common,
clone_etcd_wrapper,
GitOperation,
PipenvOperation,
FileOperation,
)
@click.group()
def host():
@ -19,26 +26,35 @@ def host():
@click.option("--etcd_port", required=True)
def setup(path, ssh_username, ssh_key_path, ssh_key_pass, etcd_host, etcd_port):
os.chdir(path)
repo_name = "ucloud-vm"
op_result = GitOperation.clone(f"https://code.ungleich.ch/ungleich-public/{repo_name}.git")
content = f"ssh_username={ssh_username}\n" \
f"ssh_pkey={ssh_key_path}\n" \
f"ssh_private_key_password={ssh_key_pass}\n" \
f"ETCD_HOST={etcd_host}\n" \
f"ETCD_PORT={etcd_port}\n"
op_result.add(FileOperation.write,
path=os.path.join(repo_name, ".env"),
content=content)
op_result.add(GitOperation.clone, path=repo_name,
url="https://code.ungleich.ch/ungleich-public/ucloud_common")
op_result.add(GitOperation.clone, path=repo_name,
url="https://code.ungleich.ch/ahmedbilal/etcd3_wrapper")
op_result.add(PipenvOperation.install, path=repo_name)
op_result = GitOperation.clone(
f"https://code.ungleich.ch/ungleich-public/{repo_name}.git"
)
content = (
f"ssh_username={ssh_username}\n"
f"ssh_pkey={ssh_key_path}\n"
f"ssh_private_key_password={ssh_key_pass}\n"
f"ETCD_HOST={etcd_host}\n"
f"ETCD_PORT={etcd_port}\n"
)
op_result.add(
FileOperation.write, path=os.path.join(repo_name, ".env"), content=content
)
op_result.add(
GitOperation.clone,
path=repo_name,
url="https://code.ungleich.ch/ungleich-public/ucloud_common",
)
op_result.add(
GitOperation.clone,
path=repo_name,
url="https://code.ungleich.ch/ahmedbilal/etcd3_wrapper",
)
op_result.add(PipenvOperation.install, path=repo_name)

View file

@ -2,26 +2,38 @@ import click
import subprocess
import os
from app.helper import clone, clone_common,\
clone_etcd_wrapper, GitOperation, PipenvOperation, FileOperation
from app.helper import (
clone,
clone_common,
clone_etcd_wrapper,
GitOperation,
PipenvOperation,
FileOperation,
)
@click.group()
def image():
pass
@image.command("setup")
@click.option("--path", required=True)
def setup(path):
os.chdir(path)
repo_name = "ucloud-image-scanner"
op_result = GitOperation.clone(f"https://code.ungleich.ch/ungleich-public/{repo_name}.git")
op_result = GitOperation.clone(
f"https://code.ungleich.ch/ungleich-public/{repo_name}.git"
)
op_result.add(
GitOperation.clone,
path=repo_name,
url="https://code.ungleich.ch/ahmedbilal/etcd3_wrapper",
)
op_result.add(GitOperation.clone, path=repo_name,
url="https://code.ungleich.ch/ahmedbilal/etcd3_wrapper")
op_result.add(PipenvOperation.install, path=repo_name)
# Write Crontab entry
# Write Crontab entry

View file

@ -2,8 +2,15 @@ import click
import subprocess
import os
from app.helper import clone, clone_common,\
clone_etcd_wrapper, GitOperation, PipenvOperation, FileOperation
from app.helper import (
clone,
clone_common,
clone_etcd_wrapper,
GitOperation,
PipenvOperation,
FileOperation,
)
@click.group()
def scheduler():
@ -17,28 +24,37 @@ def scheduler():
@click.option("--request_prefix", required=True)
@click.option("--etcd_host", required=True)
@click.option("--etcd_port", required=True)
def setup(path, vm_prefix, host_prefix, request_prefix, etcd_host, etcd_port):
os.chdir(path)
repo_name = "ucloud-scheduler"
op_result = GitOperation.clone(f"https://code.ungleich.ch/ungleich-public/{repo_name}.git")
content = f"VM_PREFIX={vm_prefix}\n" \
f"HOST_PREFIX={host_prefix}\n" \
f"REQUEST_PREFIX={request_prefix}\n" \
f"ETCD_HOST={etcd_host}\n" \
f"ETCD_PORT={etcd_port}\n"
op_result.add(FileOperation.write,
path=os.path.join(repo_name, ".env"),
content=content)
op_result.add(GitOperation.clone, path=repo_name,
url="https://code.ungleich.ch/ungleich-public/ucloud_common")
op_result.add(GitOperation.clone, path=repo_name,
url="https://code.ungleich.ch/ahmedbilal/etcd3_wrapper")
op_result.add(PipenvOperation.install, path=repo_name)
op_result = GitOperation.clone(
f"https://code.ungleich.ch/ungleich-public/{repo_name}.git"
)
content = (
f"VM_PREFIX={vm_prefix}\n"
f"HOST_PREFIX={host_prefix}\n"
f"REQUEST_PREFIX={request_prefix}\n"
f"ETCD_HOST={etcd_host}\n"
f"ETCD_PORT={etcd_port}\n"
)
op_result.add(
FileOperation.write, path=os.path.join(repo_name, ".env"), content=content
)
op_result.add(
GitOperation.clone,
path=repo_name,
url="https://code.ungleich.ch/ungleich-public/ucloud_common",
)
op_result.add(
GitOperation.clone,
path=repo_name,
url="https://code.ungleich.ch/ahmedbilal/etcd3_wrapper",
)
op_result.add(PipenvOperation.install, path=repo_name)