TooY há 2 anos atrás
pai
commit
b063175a18

+ 2 - 1
appmanage/api/model/app.py

@@ -8,6 +8,7 @@ class App(BaseModel):
     customer_name: str
     customer_name: str
     trade_mark: str
     trade_mark: str
     status: str
     status: str
+    status_reason: StatusReason
     official_app: bool
     official_app: bool
+    image_url: str
     running_info: RunningInfo
     running_info: RunningInfo
-    status_reason: StatusReason

+ 0 - 1
appmanage/api/model/running_info.py

@@ -4,7 +4,6 @@ class RunningInfo(BaseModel):
     port: int
     port: int
     compose_file: str
     compose_file: str
     url: str
     url: str
-    image_url: str
     admin_url: str
     admin_url: str
     user_name: str
     user_name: str
     password: str
     password: str

+ 51 - 44
appmanage/api/service/manage.py

@@ -28,16 +28,17 @@ redis_conn = Redis(host='websoft9-redis', port=6379)
 # 使用指定的 Redis 连接创建 RQ 队列
 # 使用指定的 Redis 连接创建 RQ 队列
 q = Queue(connection=redis_conn)
 q = Queue(connection=redis_conn)
 
 
+
 def AppList():
 def AppList():
     myLogger.info_logger("Install app ...")
     myLogger.info_logger("Install app ...")
     ret = {}
     ret = {}
     ret['ResponseData'] = {}
     ret['ResponseData'] = {}
     app_id = app_name + "_" + customer_name
     app_id = app_name + "_" + customer_name
-    ret['ResponseData']= get_my_app()
-    
-# 获取所有app的信息
-def get_my_app():
+    ret['ResponseData'] = get_my_app()
+
 
 
+# 获取所有app的信息
+def get_my_app(customer_app_name):
     # get all info
     # get all info
     cmd = "docker compose ls -a --format json"
     cmd = "docker compose ls -a --format json"
     output = shell_execute.execute_command_output_all(cmd)
     output = shell_execute.execute_command_output_all(cmd)
@@ -45,23 +46,37 @@ def get_my_app():
     installed_list, has_add = get_apps_from_compose(output_list)
     installed_list, has_add = get_apps_from_compose(output_list)
     installing_list = get_apps_from_queue()
     installing_list = get_apps_from_queue()
     app_list = installed_list + installing_list
     app_list = installed_list + installing_list
-    
-    return app_list
+    find = False
+    if customer_app_name != None:
+        for app in app_list:
+            if customer_app_name == app.customer_name:
+                ret = app
+                find = True
+                break
+        if not find:
+            raise CommandException(const.ERROR_CLIENT_PARAM_NOTEXIST, "This App doesn't exist!", "")
+    else:
+        ret = app_list
+
+    return ret
+
 
 
 # 获取具体某个app的信息
 # 获取具体某个app的信息
 def get_app_status(app_id):
 def get_app_status(app_id):
-    
-    myLogger.info_logger("Install app ...")
-    ret = {}
-    ret['ResponseData'] = {}
-    
     code, message = docker.check_app_id(app_id)
     code, message = docker.check_app_id(app_id)
-    
+    customer_app_name = app_id.split('_')[1]
     if code == None:
     if code == None:
-        app_list = get_my_app()
+        app = get_my_app(customer_app_name)
         # 将app_list 过滤出app_id的app,并缩减信息,使其符合文档的要求
         # 将app_list 过滤出app_id的app,并缩减信息,使其符合文档的要求
+        ret = {}
+        ret['app_id'] = app.app_id
+        app['status'] = app.status
+        app['status_reason'] = app.status_reason
     else:
     else:
-        ret['Error'] = get_error_info(code, message, "")
+        raise CommandException(code, message, '')
+
+    return ret
+
 
 
 def install_app(app_name, customer_name, app_version):
 def install_app(app_name, customer_name, app_version):
     myLogger.info_logger("Install app ...")
     myLogger.info_logger("Install app ...")
@@ -78,10 +93,8 @@ def install_app(app_name, customer_name, app_version):
 
 
     return ret
     return ret
 
 
+
 def start_app(app_id):
 def start_app(app_id):
-    ret = {}
-    ret['ResponseData'] = {}
-    ret['ResponseData']['app_id'] = app_id
     code, message = docker.check_app_id(app_id)
     code, message = docker.check_app_id(app_id)
     if code == None:
     if code == None:
         app_name = split_app_id(app_id)
         app_name = split_app_id(app_id)
@@ -91,16 +104,12 @@ def start_app(app_id):
             cmd = "docker compose -f " + app_path + "/docker-compose.yml start"
             cmd = "docker compose -f " + app_path + "/docker-compose.yml start"
             shell_execute.execute_command_output_all(cmd)
             shell_execute.execute_command_output_all(cmd)
         else:
         else:
-            ret['Error'] = get_error_info(const.ERROR_CLIENT_PARAM_NOTEXIST, "APP is not exist", "")
+            raise CommandException(const.ERROR_CLIENT_PARAM_NOTEXIST, "APP is not exist", "")
     else:
     else:
-        ret['Error'] = get_error_info(code, message, "")
+        raise CommandException(code, message, '')
 
 
-    return ret
 
 
 def stop_app(app_id):
 def stop_app(app_id):
-    ret = {}
-    ret['ResponseData'] = {}
-    ret['ResponseData']['app_id'] = app_id
     code, message = docker.check_app_id(app_id)
     code, message = docker.check_app_id(app_id)
     if code == None:
     if code == None:
         app_name = split_app_id(app_id)
         app_name = split_app_id(app_id)
@@ -110,17 +119,12 @@ def stop_app(app_id):
             cmd = "docker compose -f " + app_path + "/docker-compose.yml stop"
             cmd = "docker compose -f " + app_path + "/docker-compose.yml stop"
             shell_execute.execute_command_output_all(cmd)
             shell_execute.execute_command_output_all(cmd)
         else:
         else:
-            ret['Error'] = get_error_info(const.ERROR_CLIENT_PARAM_NOTEXIST, "APP is not exist", "")
+            raise CommandException(const.ERROR_CLIENT_PARAM_NOTEXIST, "APP is not exist", "")
     else:
     else:
-        ret['Error'] = get_error_info(code, message, "")
+        raise CommandException(code, message, "")
 
 
-    return ret
 
 
 def restart_app(app_id):
 def restart_app(app_id):
-    
-    ret = {}
-    ret['ResponseData'] = {}
-    ret['ResponseData']['app_id'] = app_id
     code, message = docker.check_app_id(app_id)
     code, message = docker.check_app_id(app_id)
     if code == None:
     if code == None:
         app_name = split_app_id(app_id)
         app_name = split_app_id(app_id)
@@ -130,20 +134,16 @@ def restart_app(app_id):
             cmd = "docker compose -f " + app_path + "/docker-compose.yml restart"
             cmd = "docker compose -f " + app_path + "/docker-compose.yml restart"
             shell_execute.execute_command_output_all(cmd)
             shell_execute.execute_command_output_all(cmd)
         else:
         else:
-            ret['Error'] = get_error_info(const.ERROR_CLIENT_PARAM_NOTEXIST, "APP is not exist", "")
+            raise CommandException(const.ERROR_CLIENT_PARAM_NOTEXIST, "APP is not exist", "")
     else:
     else:
-        ret['Error'] = get_error_info(code, message, "")
+        raise CommandException(code, message, "")
 
 
-    return ret
 
 
 def delete_app_failedjob(app_id):
 def delete_app_failedjob(app_id):
     myLogger.info_logger("delete_app_failedjob")
     myLogger.info_logger("delete_app_failedjob")
 
 
-def uninstall_app(app_id):
-    ret = {}
-    ret['ResponseData'] = {}
-    ret['ResponseData']['app_id'] = app_id
 
 
+def uninstall_app(app_id):
     code, message = docker.check_appid_include_rq(app_id)
     code, message = docker.check_appid_include_rq(app_id)
     if code == None:
     if code == None:
         app_name = split_app_id(app_id)
         app_name = split_app_id(app_id)
@@ -158,9 +158,10 @@ def uninstall_app(app_id):
         else:
         else:
             delete_app_failedjob(app_id)
             delete_app_failedjob(app_id)
     else:
     else:
-        ret['Error'] = get_error_info(code, message, "")
+        raise CommandException(code, message, "")
     return ret
     return ret
 
 
+
 def check_app(app_name, customer_name, app_version):
 def check_app(app_name, customer_name, app_version):
     message = ""
     message = ""
     code = None
     code = None
@@ -191,11 +192,13 @@ def check_app(app_name, customer_name, app_version):
         message = "Repeat installation: " + customer_name
         message = "Repeat installation: " + customer_name
     return code, message
     return code, message
 
 
+
 def prepare_app(app_name, customer_name):
 def prepare_app(app_name, customer_name):
     library_path = "/data/library/apps/" + app_name
     library_path = "/data/library/apps/" + app_name
     install_path = "/data/apps/" + customer_name
     install_path = "/data/apps/" + customer_name
     shell_execute.execute_command_output_all("cp -r " + library_path + " " + install_path)
     shell_execute.execute_command_output_all("cp -r " + library_path + " " + install_path)
 
 
+
 def install_app_delay(app_name, customer_name, app_version):
 def install_app_delay(app_name, customer_name, app_version):
     job_id = app_name + "_" + customer_name
     job_id = app_name + "_" + customer_name
 
 
@@ -230,6 +233,7 @@ def install_app_delay(app_name, customer_name, app_version):
         uninstall_app(job_id)
         uninstall_app(job_id)
         raise CommandException(const.ERROR_SERVER_SYSTEM, "system original error", str(e))
         raise CommandException(const.ERROR_SERVER_SYSTEM, "system original error", str(e))
 
 
+
 def app_exits_in_docker(app_id):
 def app_exits_in_docker(app_id):
     customer_name = app_id.split('_')[1]
     customer_name = app_id.split('_')[1]
     app_name = app_id.split('_')[0]
     app_name = app_id.split('_')[0]
@@ -250,9 +254,11 @@ def app_exits_in_docker(app_id):
     myLogger.info_logger("APP info: " + info)
     myLogger.info_logger("APP info: " + info)
     return info, flag
     return info, flag
 
 
+
 def split_app_id(app_id):
 def split_app_id(app_id):
     return app_id.split("_")[1]
     return app_id.split("_")[1]
 
 
+
 def get_apps_from_compose(output_list):
 def get_apps_from_compose(output_list):
     ip_result = shell_execute.execute_command_output_all("curl ifconfig.me")
     ip_result = shell_execute.execute_command_output_all("curl ifconfig.me")
     ip = ip_result["result"]
     ip = ip_result["result"]
@@ -322,12 +328,13 @@ def get_apps_from_compose(output_list):
 
 
         has_add.append(customer_name)
         has_add.append(customer_name)
 
 
-        running_info = RunningInfo(port=port, compose_file=volume, url=url, image_url=image_url, admin_url=admin_url,
+        running_info = RunningInfo(port=port, compose_file=volume, url=url, admin_url=admin_url,
                                    user_name=user_name, password=password, default_domain="", set_domain="")
                                    user_name=user_name, password=password, default_domain="", set_domain="")
         status_reason = StatusReason(Code="", Message="", Detail="")
         status_reason = StatusReason(Code="", Message="", Detail="")
 
 
         app = App(app_id=app_id, name=app_name, customer_name=customer_name, trade_mark=trade_mark, status=status,
         app = App(app_id=app_id, name=app_name, customer_name=customer_name, trade_mark=trade_mark, status=status,
-                  official_app=official_app, running_info=running_info, status_reason=status_reason)
+                  status_reason=status_reason, official_app=official_app, image_url=image_url,
+                  running_info=running_info)
         app_list.append(app.dict())
         app_list.append(app.dict())
     return app_list, has_add
     return app_list, has_add
 
 
@@ -414,12 +421,12 @@ def get_installing_app(id, status, code, message, detail):
     trade_mark = docker.read_var(var_path, 'trademark')
     trade_mark = docker.read_var(var_path, 'trademark')
     app_name = docker.read_var(var_path, 'name')
     app_name = docker.read_var(var_path, 'name')
     image_url = get_Image_url(app_name)
     image_url = get_Image_url(app_name)
-    running_info = RunningInfo(port=0, compose_file="", url="", image_url=image_url, admin_url="",
+    running_info = RunningInfo(port=0, compose_file="", url="", admin_url="",
                                user_name="", password="", default_domain="", set_domain="")
                                user_name="", password="", default_domain="", set_domain="")
     status_reason = StatusReason(Code=code, Message=message, Detail=detail)
     status_reason = StatusReason(Code=code, Message=message, Detail=detail)
-    app = App(app_id=app_name + "_" + customer_name, name=app_name, customer_name=customer_name,
-              trade_mark=trade_mark, status=status, official_app=True, running_info=running_info,
-              status_reason=status_reason)
+    app = App(app_id=app_name + "_" + customer_name, name=app_name, customer_name=customer_name, trade_mark=trade_mark,
+              status=status, status_reason=status_reason, official_app=True, image_url=image_url,
+              running_info=running_info)
     return app
     return app
 
 
 
 

+ 10 - 4
appmanage/api/utils/docker.py

@@ -59,11 +59,15 @@ def if_app_running(app_name):
 
 
 def check_appid_exist(app_id):
 def check_appid_exist(app_id):
     myLogger.info_logger("Checking check_appid_exist ...")
     myLogger.info_logger("Checking check_appid_exist ...")
-    appList=shell_execute.get_my_app(app_id)        # ----------
-    if len(appList) == 0:
-        return False
+    appList = manage.get_my_app()
+    find = False
+    for app in appList:
+        if app_id == app.app_id:
+            find = True
+            break
     myLogger.info_logger("Check complete.")
     myLogger.info_logger("Check complete.")
-    return True
+    return find
+
 
 
 def check_appid_include_rq(app_id):
 def check_appid_include_rq(app_id):
     message = ""
     message = ""
@@ -79,6 +83,7 @@ def check_appid_include_rq(app_id):
         message = "AppID is not exist"
         message = "AppID is not exist"
     return code, message
     return code, message
 
 
+
 def check_app_id(app_id):
 def check_app_id(app_id):
     message = ""
     message = ""
     code = None
     code = None
@@ -93,6 +98,7 @@ def check_app_id(app_id):
     #     message = "AppID is not exist"
     #     message = "AppID is not exist"
     return code, message
     return code, message
 
 
+
 def check_app_id(app_id):
 def check_app_id(app_id):
     message = ""
     message = ""
     code = None
     code = None

+ 44 - 8
appmanage/api/v1/routers/apps.py

@@ -69,7 +69,7 @@ def AppList(request: Request, app_id: Optional[str] = Query(default=None, descri
         myLogger.info_logger("Receive request: /AppList")
         myLogger.info_logger("Receive request: /AppList")
         get_headers(request)
         get_headers(request)
         ret = {}
         ret = {}
-        ret['ResponseData'] = manage.AppList()
+        ret['ResponseData'] = manage.get_my_app
     except CommandException as ce:
     except CommandException as ce:
         ret = {}
         ret = {}
         ret['ResponseData'] = None
         ret['ResponseData'] = None
@@ -105,24 +105,58 @@ def AppInstall(request: Request, app_name: Optional[str] = Query(default=None, d
 @router.api_route("/AppStart", methods=["GET", "POST"], summary="启动APP", response_description=rd_two,
 @router.api_route("/AppStart", methods=["GET", "POST"], summary="启动APP", response_description=rd_two,
                   response_model=Response)
                   response_model=Response)
 def start_app(app_id: Optional[str] = Query(default=None, description="应用ID")):
 def start_app(app_id: Optional[str] = Query(default=None, description="应用ID")):
-    myLogger.info_logger("Receive request: /AppStart")
-    ret = manage.start_app(app_id)
+    try:
+        myLogger.info_logger("Receive request: /AppStart")
+        get_headers(request)
+        manage.start_app(app_id)
+        ret = {}
+        ret['ResponseData']['AppID'] = app_name + "_" + customer_name
+    except CommandException as ce:
+        ret = {}
+        ret['ResponseData']['AppID'] = app_name + "_" + customer_name
+        ret['Error'] = manage.get_error_info(ce.code, ce.message, ce.detail)
+    except Exception as e:
+        ret = {}
+        ret['ResponseData']['AppID'] = app_name + "_" + customer_name
+        ret['Error'] = manage.get_error_info(const.ERROR_SERVER_SYSTEM, "system original error", str(e))
     return JSONResponse(content=ret)
     return JSONResponse(content=ret)
 
 
 
 
 @router.api_route("/AppStop", methods=["GET", "POST"], summary="停止APP", response_description=rd_two,
 @router.api_route("/AppStop", methods=["GET", "POST"], summary="停止APP", response_description=rd_two,
                   response_model=Response)
                   response_model=Response)
 def stop_app(app_id: Optional[str] = Query(default=None, description="应用ID")):
 def stop_app(app_id: Optional[str] = Query(default=None, description="应用ID")):
-    myLogger.info_logger("Receive request: /AppStop")
-    ret = manage.stop_app(app_id)
+    try:
+        myLogger.info_logger("Receive request: /AppStop")
+        manage.stop_app(app_id)
+        ret = {}
+        ret['ResponseData']['AppID'] = app_name + "_" + customer_name
+    except CommandException as ce:
+        ret = {}
+        ret['ResponseData']['AppID'] = app_name + "_" + customer_name
+        ret['Error'] = manage.get_error_info(ce.code, ce.message, ce.detail)
+    except Exception as e:
+        ret = {}
+        ret['ResponseData']['AppID'] = app_name + "_" + customer_name
+        ret['Error'] = manage.get_error_info(const.ERROR_SERVER_SYSTEM, "system original error", str(e))
     return JSONResponse(content=ret)
     return JSONResponse(content=ret)
 
 
 
 
 @router.api_route("/AppRestart", methods=["GET", "POST"], summary="重启APP", response_description=rd_two,
 @router.api_route("/AppRestart", methods=["GET", "POST"], summary="重启APP", response_description=rd_two,
                   response_model=Response)
                   response_model=Response)
 def AppRestart(app_id: Optional[str] = Query(default=None, description="应用ID")):
 def AppRestart(app_id: Optional[str] = Query(default=None, description="应用ID")):
-    myLogger.info_logger("Receive request: /AppRestart")
-    ret = manage.restart_app(app_id)
+    try:
+        myLogger.info_logger("Receive request: /AppRestart")
+        ret = manage.restart_app(app_id)
+        ret = {}
+        ret['ResponseData']['AppID'] = app_name + "_" + customer_name
+    except CommandException as ce:
+        ret = {}
+        ret['ResponseData']['AppID'] = app_name + "_" + customer_name
+        ret['Error'] = manage.get_error_info(ce.code, ce.message, ce.detail)
+    except Exception as e:
+        ret = {}
+        ret['ResponseData']['AppID'] = app_name + "_" + customer_name
+        ret['Error'] = manage.get_error_info(const.ERROR_SERVER_SYSTEM, "system original error", str(e))
     return JSONResponse(content=ret)
     return JSONResponse(content=ret)
 
 
 
 
@@ -135,7 +169,9 @@ def AppUninstall(request: Request, app_id: Optional[str] = Query(default=None, d
     try:
     try:
         myLogger.info_logger("Receive request: /AppUninstall")
         myLogger.info_logger("Receive request: /AppUninstall")
         get_headers(request)
         get_headers(request)
-        ret = manage.uninstall_app(app_name, customer_name, app_version)
+        manage.uninstall_app(app_name, customer_name, app_version)
+        ret = {}
+        ret['ResponseData']['AppID'] = app_name + "_" + customer_name
     except CommandException as ce:
     except CommandException as ce:
         ret = {}
         ret = {}
         ret['ResponseData']['AppID'] = app_name + "_" + customer_name
         ret['ResponseData']['AppID'] = app_name + "_" + customer_name