diff --git a/cli/controller.py b/cli/controller.py index 2dab0e4e..27714600 100644 --- a/cli/controller.py +++ b/cli/controller.py @@ -83,10 +83,21 @@ class Create: def upRepo(self): '''docker-compose up repository''' - cmd = "docker-compose up -d" - print(cmd) + cmd_up = "docker-compose up -d" + cmd_down = "docker-compose down -v" os.chdir(self.folder) - os.system(cmd) + + try: + os.system(cmd_up) + except: + print("Create failed") + os.system(cmd_down) + sys.exit(0) def printResult(self): - pass \ No newline at end of file + pass + + +class Manage(model.DockerComposeOp): + pass + diff --git a/cli/model.py b/cli/model.py index 230717f8..0f4814d9 100644 --- a/cli/model.py +++ b/cli/model.py @@ -110,4 +110,53 @@ class SecurityOp: and any(c.isupper() for c in password) and sum(c.isdigit() for c in password) >= 3): break - return password \ No newline at end of file + return password + +class DockerComposeOp: + '''Docker Compose operation''' + + def __int__(self, path: Optional[str] = ""): + + self.cmd_up = "docker-compose up -d" + self.cmd_stop = "docker-compose stop" + self.cmd_down = "docker-compose down" + self.path = path + + try: + os.chdir(self.path) + except: + print("No found project directory") + sys.exit(0) + + def up(self): + '''docker-compose up''' + try: + os.system(cmd_up) + except: + print("Create failed") + os.system(cmd_up) + sys.exit(0) + + def stop(self): + '''docker-compose stop''' + try: + os.system(cmd_stop) + except: + print("Stop failed, suggest try it again") + sys.exit(0) + + def down(self): + '''docker-compose down''' + try: + os.system(cmd_down) + except: + print("Down failed, suggest try it again") + sys.exit(0) + + +class DockerOp: + ''' Docker operation ''' + def __init__(self): + pass + + \ No newline at end of file diff --git a/cli/requirements.txt b/cli/requirements.txt index 14fedd0b..96421bb6 100644 --- a/cli/requirements.txt +++ b/cli/requirements.txt @@ -1,3 +1,5 @@ typer psutil -jq \ No newline at end of file +jq +docker +docker-compose \ No newline at end of file diff --git a/cli/test.py b/cli/test.py index 226ee2e1..eb7f8317 100644 --- a/cli/test.py +++ b/cli/test.py @@ -1,41 +1,5 @@ -import model,re - -fileop=model.FileOp('c2/.env') -securityop=model.SecurityOp() -netop=model.NetOp() - - -env_dict = fileop.fileToDict() -env_str = fileop.fileToString() -port_list = [] - -for key in list(env_dict.keys()): - if env_dict[key] in ["", "True", "False"]: - del env_dict[key] - -for key,value in env_dict.items(): - # replace password - if re.match('\w*PASSWORD',key,re.I) != None: - env_str = env_str.replace(key+"="+value, key+"="+securityop.randomPass()) - - # replace port - if re.match('\w*PORT',key,re.I) != None: - port = int(value) - while port in port_list or not netop.checkPort(port): - port = port + 1 - port_list.append(port) - print(port_list) - env_str = env_str.replace(key+"="+value, key+"="+netop.setPort(int(port))) - - # replace app_container - if re.match('\w*APP_CONTAINER_NAME',key,re.I) != None: - env_str = env_str.replace(key+"="+value, key+"="+"hello") - - # replace app_container - if re.match('\w*APP_NETWORK',key,re.I) != None: - env_str = env_str.replace(key+"="+value, key+"="+"hello") - -fileop.stringToFile(env_str) - -print(env_str) +import docker +client = docker.from_env() +for image in client.images.list(): + print(image.id) \ No newline at end of file