a
This commit is contained in:
parent
9c6ed5857a
commit
ed1c2cebe0
3 changed files with 88 additions and 35 deletions
34
app/api.py
34
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}"]
|
||||
op_result = GitOperation.clone("https://code.ungleich.ch/ungleich-public/ucloud-api.git")
|
||||
|
||||
env_file.writelines(lines)
|
||||
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}"
|
||||
|
||||
result = []
|
||||
result.append(clone_common())
|
||||
result.append(clone_etcd_wrapper())
|
||||
op_result.add(FileOperation.write,
|
||||
path=os.path.join("ucloud-api", ".env"),
|
||||
content=content)
|
||||
|
||||
if all(result):
|
||||
result.append(pipenv_install("ucloud-api"))
|
||||
op_result.add(GitOperation.clone, path="ucloud-api",
|
||||
url="https://code.ungleich.ch/ungleich-public/ucloud_common")
|
||||
|
||||
if all(result):
|
||||
print("Successfull installation :)")
|
||||
else:
|
||||
print("Unsuccessful installation. Please fix errors then run the installation again")
|
||||
op_result.add(GitOperation.clone, path="ucloud-api",
|
||||
url="https://code.ungleich.ch/ahmedbilal/etcd3_wrapper")
|
||||
|
||||
op_result.add(PipenvOperation.install, path="ucloud-api")
|
|
@ -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 = []
|
||||
|
||||
def execute(self, rc):
|
||||
self.result.append(rc)
|
||||
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 pipenv_install(_dir):
|
||||
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
|
||||
|
||||
|
||||
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:
|
||||
subprocess.check_output(command.split(), cwd=_dir)
|
||||
output = subprocess.check_output(command.split(), cwd=path)
|
||||
except subprocess.CalledProcessError as e:
|
||||
return False
|
||||
return Result(e, ResultType.failure, inspect.currentframe().f_code.co_name)
|
||||
else:
|
||||
return True
|
||||
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)
|
||||
|
1
ucloud-api
Submodule
1
ucloud-api
Submodule
|
@ -0,0 +1 @@
|
|||
Subproject commit 9aeb05987bd1b84e7ac851ce8b84f31647e436bd
|
Loading…
Reference in a new issue