From cf777bc15cdd59b06045b3ae2b7a431283312526 Mon Sep 17 00:00:00 2001 From: chendelin1982 Date: Tue, 2 Nov 2021 20:54:10 +0800 Subject: [PATCH] compose --- cli/controller.py | 30 ++++++++++++++++++------------ cli/model.py | 40 +++++++++++----------------------------- cli/stackhub.py | 11 ++++------- 3 files changed, 33 insertions(+), 48 deletions(-) diff --git a/cli/controller.py b/cli/controller.py index b0176355..ba379327 100644 --- a/cli/controller.py +++ b/cli/controller.py @@ -1,3 +1,4 @@ +from typing import Any, Callable, Dict, List, Optional, Sequence, Tuple, Type, Union import model, os, sys, subprocess, re from model import GitOp @@ -108,36 +109,41 @@ class Status: '''The status operation of project, e.g Start | Stop | Restart | Erase''' - def __init__(self, project_name: str, project_path: Optional[str] = None): + def __init__(self, project_name: str, **parameter): self.project_name = project_name - self.project_path = project_path self.dockerop = model.DockerOp() projectdict = self.dockerop.getProject() - if self.project_path == None: + if self.project_name != None: try: self.project_path = projectdict[self.project_name] - self.dockercomposeop = model.DockerComposeOp(self.project_path) except: print("No this application!") sys.exit(0) + + else + try: + self.project_path = parameter[project-directory] + except: + print("Directory not exist, please check it") + sys.exit(0) + + self.dockercomposeop = model.DockerComposeOp(self.project_path) def stopApp(self): pass def upApp(self): - pass + print("It up the application at directory: "+self.project_path) + self.dockercomposeop.execute() def startApp(self): pass - def retartApp(self): - pass + def restartApp(self): + self.dockercomposeop.execute() - def eraseApp(self): - self.dockercomposeop.down() - - def upApp(self): - self.dockercomposeop.up(self.project_path) \ No newline at end of file + def deleteApp(self): + self.dockercomposeop.execute() \ No newline at end of file diff --git a/cli/model.py b/cli/model.py index 7df67cf4..61433782 100644 --- a/cli/model.py +++ b/cli/model.py @@ -115,45 +115,27 @@ class SecurityOp: class DockerComposeOp: '''Docker Compose operation''' - def __init__(self, path: Optional[str] = ''): + def __init__(self, command: str, **parameter): - self.cmd_up = "docker-compose up -d" - self.cmd_stop = "docker-compose stop" - self.cmd_down = "docker-compose down" - self.path = path + self.cmd = "docker-compose "+ command + + for key, value in parameter.items(): + self.cmd = self.cmd + " --" + key + " " + value try: - os.chdir(self.path) + os.chdir(self.parameter[project-directory]) except: print("Not found project directory") sys.exit(0) - def up(self): - '''docker-compose up''' + def execute(self): + '''docker-compose command executing''' try: - os.system(self.cmd_up) + os.system(self.cmd) except: - print("Create failed") - os.system(self.cmd_up) + print("This operation execute failed") sys.exit(0) - - def stop(self): - '''docker-compose stop''' - try: - os.system(self.cmd_stop) - except: - print("Stop failed, suggest try it again") - sys.exit(0) - - def down(self): - '''docker-compose down''' - try: - os.system(self.cmd_down) - except: - print("Down failed, suggest try it again") - sys.exit(0) - - + class DockerOp: ''' Docker operation ''' def __init__(self, status: Optional[str] = 'all'): diff --git a/cli/stackhub.py b/cli/stackhub.py index a028d887..4ab78a8a 100644 --- a/cli/stackhub.py +++ b/cli/stackhub.py @@ -26,8 +26,8 @@ def create(app_name: str, project_name: Optional[str] = None): @app.command() def up(path: str): '''up one deleted application''' - status = controller.Status("application", path) - status.startApp() + status = controller.Status(None, path) + status.upApp() @app.command() def start(project_name: str): @@ -48,17 +48,16 @@ def restart(project_name: str): status.retartApp() @app.command() -def erase(project_name: str): +def delete(project_name: str): '''erase or delete an application''' status = controller.Status(project_name) - status.eraseApp() + status.deleteApp() @app.command() def update(project_name: str): '''update the local lists cache''' typer.echo(f"Hello {project_name}") - @app.command() def upgrade(name: str): '''upgrade one application''' @@ -70,7 +69,6 @@ def search(name: str): '''Search application you want to install''' typer.echo(f"Hello {name}") - @app.command() def show(name: str): '''show the detail of application''' @@ -81,6 +79,5 @@ def package(name: str): '''package one application for no network environment''' typer.echo(f"Hello {name}") - if __name__ == "__main__": app() \ No newline at end of file