This commit is contained in:
chendelin1982 2021-11-02 20:54:10 +08:00
parent b1063250c7
commit cf777bc15c
3 changed files with 33 additions and 48 deletions

View file

@ -1,3 +1,4 @@
from typing import Any, Callable, Dict, List, Optional, Sequence, Tuple, Type, Union
import model, os, sys, subprocess, re import model, os, sys, subprocess, re
from model import GitOp from model import GitOp
@ -108,36 +109,41 @@ class Status:
'''The status operation of project, e.g Start | Stop | Restart | Erase''' '''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_name = project_name
self.project_path = project_path
self.dockerop = model.DockerOp() self.dockerop = model.DockerOp()
projectdict = self.dockerop.getProject() projectdict = self.dockerop.getProject()
if self.project_path == None: if self.project_name != None:
try: try:
self.project_path = projectdict[self.project_name] self.project_path = projectdict[self.project_name]
self.dockercomposeop = model.DockerComposeOp(self.project_path)
except: except:
print("No this application!") print("No this application!")
sys.exit(0) 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): def stopApp(self):
pass pass
def upApp(self): def upApp(self):
pass print("It up the application at directory: "+self.project_path)
self.dockercomposeop.execute()
def startApp(self): def startApp(self):
pass pass
def retartApp(self): def restartApp(self):
pass self.dockercomposeop.execute()
def eraseApp(self): def deleteApp(self):
self.dockercomposeop.down() self.dockercomposeop.execute()
def upApp(self):
self.dockercomposeop.up(self.project_path)

View file

@ -115,45 +115,27 @@ class SecurityOp:
class DockerComposeOp: class DockerComposeOp:
'''Docker Compose operation''' '''Docker Compose operation'''
def __init__(self, path: Optional[str] = ''): def __init__(self, command: str, **parameter):
self.cmd_up = "docker-compose up -d" self.cmd = "docker-compose "+ command
self.cmd_stop = "docker-compose stop"
self.cmd_down = "docker-compose down" for key, value in parameter.items():
self.path = path self.cmd = self.cmd + " --" + key + " " + value
try: try:
os.chdir(self.path) os.chdir(self.parameter[project-directory])
except: except:
print("Not found project directory") print("Not found project directory")
sys.exit(0) sys.exit(0)
def up(self): def execute(self):
'''docker-compose up''' '''docker-compose command executing'''
try: try:
os.system(self.cmd_up) os.system(self.cmd)
except: except:
print("Create failed") print("This operation execute failed")
os.system(self.cmd_up)
sys.exit(0) 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: class DockerOp:
''' Docker operation ''' ''' Docker operation '''
def __init__(self, status: Optional[str] = 'all'): def __init__(self, status: Optional[str] = 'all'):

View file

@ -26,8 +26,8 @@ def create(app_name: str, project_name: Optional[str] = None):
@app.command() @app.command()
def up(path: str): def up(path: str):
'''up one deleted application''' '''up one deleted application'''
status = controller.Status("application", path) status = controller.Status(None, path)
status.startApp() status.upApp()
@app.command() @app.command()
def start(project_name: str): def start(project_name: str):
@ -48,17 +48,16 @@ def restart(project_name: str):
status.retartApp() status.retartApp()
@app.command() @app.command()
def erase(project_name: str): def delete(project_name: str):
'''erase or delete an application''' '''erase or delete an application'''
status = controller.Status(project_name) status = controller.Status(project_name)
status.eraseApp() status.deleteApp()
@app.command() @app.command()
def update(project_name: str): def update(project_name: str):
'''update the local lists cache''' '''update the local lists cache'''
typer.echo(f"Hello {project_name}") typer.echo(f"Hello {project_name}")
@app.command() @app.command()
def upgrade(name: str): def upgrade(name: str):
'''upgrade one application''' '''upgrade one application'''
@ -70,7 +69,6 @@ def search(name: str):
'''Search application you want to install''' '''Search application you want to install'''
typer.echo(f"Hello {name}") typer.echo(f"Hello {name}")
@app.command() @app.command()
def show(name: str): def show(name: str):
'''show the detail of application''' '''show the detail of application'''
@ -81,6 +79,5 @@ def package(name: str):
'''package one application for no network environment''' '''package one application for no network environment'''
typer.echo(f"Hello {name}") typer.echo(f"Hello {name}")
if __name__ == "__main__": if __name__ == "__main__":
app() app()