From ed1c2cebe0dbb1acd2a2f41cb4ed8383f016beba Mon Sep 17 00:00:00 2001 From: Ahmed Bilal Khalid Date: Tue, 27 Aug 2019 16:19:54 +0500 Subject: [PATCH] a --- app/api.py | 40 ++++++++++++------------- app/helper.py | 82 ++++++++++++++++++++++++++++++++++++++++++--------- ucloud-api | 1 + 3 files changed, 88 insertions(+), 35 deletions(-) create mode 160000 ucloud-api diff --git a/app/api.py b/app/api.py index 4206829..661f4e1 100644 --- a/app/api.py +++ b/app/api.py @@ -3,7 +3,7 @@ import subprocess import os from app.helper import clone, clone_common,\ - clone_etcd_wrapper, pipenv_install + clone_etcd_wrapper, GitOperation, PipenvOperation, FileOperation @click.group() def api(): @@ -22,24 +22,22 @@ def setup(path, auth_name, auth_seed, auth_realm, realm_allowed, otp_server): os.chdir(path) - if clone("https://code.ungleich.ch/ungleich-public/ucloud-api.git"): - with open(".env", "w") as env_file: - lines = [f"AUTH_NAME={auth_name}", - f"AUTH_SEED={auth_seed}", - f"AUTH_REALM={auth_seed}", - f"REALM_ALLOWED={realm_allowed}", - f"OTP_SERVER={otp_server}"] - - env_file.writelines(lines) - - result = [] - result.append(clone_common()) - result.append(clone_etcd_wrapper()) - - if all(result): - result.append(pipenv_install("ucloud-api")) + op_result = GitOperation.clone("https://code.ungleich.ch/ungleich-public/ucloud-api.git") - if all(result): - print("Successfull installation :)") - else: - print("Unsuccessful installation. Please fix errors then run the installation again") \ No newline at end of file + content = f"AUTH_NAME={auth_name}" \ + f"AUTH_SEED={auth_seed}" \ + f"AUTH_REALM={auth_seed}" \ + f"REALM_ALLOWED={list(realm_allowed)}" \ + f"OTP_SERVER={otp_server}" + + op_result.add(FileOperation.write, + path=os.path.join("ucloud-api", ".env"), + content=content) + + op_result.add(GitOperation.clone, path="ucloud-api", + url="https://code.ungleich.ch/ungleich-public/ucloud_common") + + op_result.add(GitOperation.clone, path="ucloud-api", + url="https://code.ungleich.ch/ahmedbilal/etcd3_wrapper") + + op_result.add(PipenvOperation.install, path="ucloud-api") \ No newline at end of file diff --git a/app/helper.py b/app/helper.py index 8c7811d..a45fc02 100644 --- a/app/helper.py +++ b/app/helper.py @@ -1,5 +1,12 @@ import subprocess +import inspect +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}" @@ -17,20 +24,67 @@ def clone_common(): def clone_etcd_wrapper(): return clone("https://code.ungleich.ch/ahmedbilal/etcd3_wrapper") - -class Operation(object): - def __init__(self): - self.result = [] +class Result(object): + def __init__(self, _result, _type: ResultType, _op=""): + self._type = _type + self._result = str(_result) + self._op = _op + if self._type == ResultType.failure: + print(self._op, "failed") + - def execute(self, rc): - self.result.append(rc) + def add(self, operation, **kwargs): + if self._type == ResultType.success: + r = operation(**kwargs) + self._type = r._type + self._result = r._result + return self + else: + print("Dependency not satisfied") + exit(-1) + + + def __repr__(self): + return f"{self._type}, {self._result}" + +class Operation(ABC): + pass -def pipenv_install(_dir): - command = f"pipenv install" - try: - subprocess.check_output(command.split(), cwd=_dir) - except subprocess.CalledProcessError as e: - return False - else: - return True \ No newline at end of file +class GitOperation(object): + @staticmethod + def clone(url, path="."): + command = f"git clone {url}" + 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) + else: + return Result(output, ResultType.success) + +class PipenvOperation(object): + @staticmethod + def install(path=".", package_name=None): + if package_name: + command = f"pipenv install {package_name}" + else: + command = f"pipenv install" + 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) + else: + return Result(output, ResultType.success) + + +class FileOperation(object): + @staticmethod + def write(path, content, mode="w"): + try: + with open(path, mode) as file: + file.write(content) + except Exception as e: + return Result(e, ResultType.failure, inspect.currentframe().f_code.co_name) + else: + return Result("", ResultType.success) + \ No newline at end of file diff --git a/ucloud-api b/ucloud-api new file mode 160000 index 0000000..9aeb059 --- /dev/null +++ b/ucloud-api @@ -0,0 +1 @@ +Subproject commit 9aeb05987bd1b84e7ac851ce8b84f31647e436bd