diff --git a/appmanage/api/service/manage.py b/appmanage/api/service/manage.py index d1e1b06a..f16fc796 100644 --- a/appmanage/api/service/manage.py +++ b/appmanage/api/service/manage.py @@ -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 diff --git a/appmanage/api/v1/routers/apps.py b/appmanage/api/v1/routers/apps.py index 8a71381e..7ec0e796 100644 --- a/appmanage/api/v1/routers/apps.py +++ b/appmanage/api/v1/routers/apps.py @@ -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)