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
 | 
					import os
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from app.helper import clone, clone_common,\
 | 
					from app.helper import clone, clone_common,\
 | 
				
			||||||
                       clone_etcd_wrapper, pipenv_install
 | 
					                       clone_etcd_wrapper, GitOperation, PipenvOperation, FileOperation
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@click.group()
 | 
					@click.group()
 | 
				
			||||||
def api():
 | 
					def api():
 | 
				
			||||||
| 
						 | 
					@ -22,24 +22,22 @@ def setup(path, auth_name, auth_seed, auth_realm,
 | 
				
			||||||
          realm_allowed, otp_server):
 | 
					          realm_allowed, otp_server):
 | 
				
			||||||
    os.chdir(path)
 | 
					    os.chdir(path)
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    if clone("https://code.ungleich.ch/ungleich-public/ucloud-api.git"):
 | 
					    op_result = GitOperation.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)
 | 
					    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 = []
 | 
					    op_result.add(FileOperation.write,
 | 
				
			||||||
        result.append(clone_common())
 | 
					                  path=os.path.join("ucloud-api", ".env"),
 | 
				
			||||||
        result.append(clone_etcd_wrapper())
 | 
					                  content=content)
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
        if all(result):
 | 
					    op_result.add(GitOperation.clone, path="ucloud-api",
 | 
				
			||||||
            result.append(pipenv_install("ucloud-api"))
 | 
					               url="https://code.ungleich.ch/ungleich-public/ucloud_common")
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    if all(result):
 | 
					    op_result.add(GitOperation.clone, path="ucloud-api",
 | 
				
			||||||
        print("Successfull installation :)")
 | 
					               url="https://code.ungleich.ch/ahmedbilal/etcd3_wrapper")
 | 
				
			||||||
    else:
 | 
					    
 | 
				
			||||||
        print("Unsuccessful installation. Please fix errors then run the installation again")
 | 
					    op_result.add(PipenvOperation.install, path="ucloud-api")
 | 
				
			||||||
| 
						 | 
					@ -1,5 +1,12 @@
 | 
				
			||||||
import subprocess
 | 
					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):
 | 
					def clone(repo):
 | 
				
			||||||
    command = f"git clone {repo}"
 | 
					    command = f"git clone {repo}"
 | 
				
			||||||
| 
						 | 
					@ -17,20 +24,67 @@ def clone_common():
 | 
				
			||||||
def clone_etcd_wrapper():
 | 
					def clone_etcd_wrapper():
 | 
				
			||||||
    return clone("https://code.ungleich.ch/ahmedbilal/etcd3_wrapper")
 | 
					    return clone("https://code.ungleich.ch/ahmedbilal/etcd3_wrapper")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class Result(object):
 | 
				
			||||||
class Operation(object):
 | 
					    def __init__(self, _result, _type: ResultType, _op=""):
 | 
				
			||||||
    def __init__(self):
 | 
					        self._type = _type
 | 
				
			||||||
        self.result = []
 | 
					        self._result = str(_result)
 | 
				
			||||||
    
 | 
					        self._op = _op
 | 
				
			||||||
    def execute(self, rc):
 | 
					        if self._type == ResultType.failure:
 | 
				
			||||||
        self.result.append(rc)
 | 
					            print(self._op, "failed")
 | 
				
			||||||
            
 | 
					            
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
def pipenv_install(_dir):
 | 
					    def add(self, operation, **kwargs):
 | 
				
			||||||
    command = f"pipenv install"
 | 
					        if self._type == ResultType.success:
 | 
				
			||||||
    try:
 | 
					            r = operation(**kwargs)
 | 
				
			||||||
        subprocess.check_output(command.split(), cwd=_dir)
 | 
					            self._type = r._type
 | 
				
			||||||
    except subprocess.CalledProcessError as e:
 | 
					            self._result = r._result
 | 
				
			||||||
        return False
 | 
					            return self
 | 
				
			||||||
    else:
 | 
					        else:
 | 
				
			||||||
        return True
 | 
					            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:
 | 
				
			||||||
 | 
					            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)
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
							
								
								
									
										1
									
								
								ucloud-api
									
										
									
									
									
										Submodule
									
								
							
							
						
						
									
										1
									
								
								ucloud-api
									
										
									
									
									
										Submodule
									
								
							| 
						 | 
					@ -0,0 +1 @@
 | 
				
			||||||
 | 
					Subproject commit 9aeb05987bd1b84e7ac851ce8b84f31647e436bd
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue