appmanage:add some functions

This commit is contained in:
Toyga 2023-02-28 17:44:04 +08:00
parent 431e14cdf0
commit 1959c785b1
2 changed files with 77 additions and 8 deletions

View File

@ -81,3 +81,72 @@ def install_app(app_name):
ret["message"] = "应用正在启动中,请过几分钟再查询"
ret["data"] = ""
return ret
def if_app_exits(app_name):
cmd = "docker compose ls -a | grep \'"+app_name+"\\b\'"
output = shell_execute.execute_command_output_all(cmd)
if int(output["code"]) == -1:
return False
else:
return True
def start_app(app_name):
ret = {}
ret["code"] = -1
if if_app_exits(app_name):
cmd = "docker start "+app_name
output = shell_execute.execute_command_output_all(cmd)
if int(output["code"]) == 0:
ret["code"] = 0
ret["message"] = "应用启动成功"
else:
ret["message"] = "应用启动失败"
else:
ret["message"] = "应用不存在"
return ret
def stop_app(app_name):
ret = {}
ret["code"] = -1
if if_app_exits(app_name):
cmd = "docker stop " + app_name
output = shell_execute.execute_command_output_all(cmd)
if int(output["code"]) == 0:
ret["code"] = 0
ret["message"] = "应用停止成功"
else:
ret["message"] = "应用停止失败"
else:
ret["message"] = "应用不存在"
return ret
def restart_app(app_name):
ret = {}
ret["code"] = -1
if if_app_exits(app_name):
cmd = "docker restart " + app_name
output = shell_execute.execute_command_output_all(cmd)
if int(output["code"]) == 0:
ret["code"] = 0
ret["message"] = "应用重启成功"
else:
ret["message"] = "应用重启失败"
else:
ret["message"] = "应用不存在"
return ret
def delete_app(app_name):
ret = {}
ret["code"] = -1
if_stopped = stop_app(app_name)
if if_stopped["code"] == 0:
cmd = "docker rm "+app_name
output = shell_execute.execute_command_output_all(cmd)
if int(output["code"]) == 0:
ret["code"] = 0
ret["message"] = "应用删除成功"
else:
ret["message"] = "应用删除失败"
else:
ret["message"] = if_stopped["message"]
return ret

View File

@ -23,20 +23,20 @@ def install_app(app_name: Optional[str] = None):
@router.get("/start")
def start_app(app_name: Optional[str] = None):
return {}
ret = manage.start_app(app_name)
return JSONResponse(content=ret)
@router.get("/stop")
def stop_app(app_name: Optional[str] = None):
return {}
ret = manage.stop_app(app_name)
return JSONResponse(content=ret)
@router.get("/restart")
def restart_app(app_name: Optional[str] = None):
return {}
ret = manage.restart(app_name)
return JSONResponse(content=ret)
@router.get("/delete")
def delete_app(app_name: Optional[str] = None):
return {}
ret = manage.delete_app(app_name)
return JSONResponse(content=ret)