websoft9/appmanage/api/v1/routers/apps.py

317 lines
14 KiB
Python
Raw Normal View History

2023-02-21 14:21:53 +08:00
from typing import Optional, List
2023-04-12 17:11:53 +08:00
from fastapi import APIRouter, status, Depends, Query, Request
2023-04-02 21:51:14 +08:00
from pydantic import BaseModel, Field
2023-02-21 14:21:53 +08:00
from starlette.responses import JSONResponse
2023-02-22 17:19:27 +08:00
import os, io, sys, platform, shutil, time, subprocess, json, datetime
2023-02-21 14:21:53 +08:00
from api.model.app import App
2023-04-02 21:51:14 +08:00
from api.model.response import Response
2023-02-21 14:21:53 +08:00
from api.service import manage
2023-04-14 11:50:18 +08:00
from api.utils import shell_execute, const
2023-03-15 17:55:05 +08:00
from api.utils.common_log import myLogger
2023-04-14 10:51:29 +08:00
from api.exception.command_exception import CommandException
2023-02-21 14:21:53 +08:00
router = APIRouter()
2023-04-17 15:40:44 +08:00
rd_s = "ResponseData: 各个接口的业务数据\n\n{\n\n"
rd_m = "  app_id应用ID\n\n}\n\n"
rd_e = "Error错误code和错误信息\n\n{\n\n" \
"  Code错误码,\n\n" \
"  Message错误信息,\n\n" \
"  Detail错误详情\n\n}"
rd_status = "  app_id应用ID\n\n" \
"  status应用运行状态,[installing(创建中)running(运行中)exited(停止)restarting(反复重启)failed(失败)]\n\n" \
"  StatusReason{\n\n" \
"    Code错误代码,\n\n" \
"    Message错误提示信息,\n\n" \
"    Detail错误真实信息\n\n  }\n\n}\n\n"
info = "  app_id应用ID,\n\n  name应用名,\n\n" \
"  customer_name自定义应用名,\n\n  trade_mark应用商标,\n\n" \
"  status应用运行状态,[installing(创建中)running(运行中)exited(停止)restarting(反复重启)failed(失败)]\n\n" \
"  StatusReason{\n\n" \
"    Code错误代码,\n\n" \
"    Message错误提示信息,\n\n" \
2023-04-18 08:30:47 +08:00
"    Detail错误真实信息\n\n  }\n\n" \
2023-04-17 15:40:44 +08:00
"  official_app是否为官方应用,\n\n" \
"  image_url图片路径,\n\n" \
2023-04-18 14:27:51 +08:00
"  config{\n\n" \
2023-04-17 15:40:44 +08:00
"    port应用端口,\n\n    compose_filedocker compose 文件路径,\n\n" \
"    url应用网址,\n\n    admin_url管理员网址,\n\n" \
"    user_name用户名,\n\n    password密码,\n\n" \
2023-04-18 14:27:51 +08:00
"    default_domain默认域名,\n\n    set_domain用户自定义域名\n\n  }\n\n}\n\n"
2023-04-17 15:40:44 +08:00
rd = rd_s + rd_m + rd_e
rd_info = rd_s + info + rd_e
rd_status = rd_s + rd_status + rd_e
2023-04-07 09:52:22 +08:00
2023-04-06 09:18:30 +08:00
2023-04-14 09:27:08 +08:00
@router.api_route("/AppStatus", methods=["GET", "POST"], summary="获取指定APP的信息",
2023-04-17 15:40:44 +08:00
response_description=rd_status,
2023-04-02 21:51:14 +08:00
response_model=Response)
2023-04-16 19:49:10 +08:00
def AppStatus(request: Request,app_id: Optional[str] = Query(default=None, description="应用ID")):
2023-04-14 19:42:42 +08:00
try:
myLogger.info_logger("Receive request: /AppStatus")
2023-04-16 19:49:10 +08:00
get_headers(request)
2023-04-14 19:42:42 +08:00
ret = {}
2023-04-15 07:43:13 +08:00
ret['ResponseData'] = manage.get_app_status(app_id)
2023-04-14 19:42:42 +08:00
except CommandException as ce:
ret = {}
ret['ResponseData'] = None
2023-04-17 14:36:03 +08:00
ret['Error'] = manage.get_error_info(ce.code, ce.message, ce.detail)
2023-04-14 19:42:42 +08:00
except Exception as e:
ret = {}
ret['ResponseData'] = None
ret['Error'] = manage.get_error_info(const.ERROR_SERVER_SYSTEM, "system original error", str(e))
return JSONResponse(ret)
2023-03-16 14:09:48 +08:00
2023-04-02 21:51:14 +08:00
2023-04-17 15:40:44 +08:00
@router.api_route("/AppList", methods=["GET", "POST"], summary="获取所有APP的信息", response_description=rd_info,
2023-04-06 09:18:30 +08:00
response_model=Response)
2023-04-14 19:42:42 +08:00
def AppList(request: Request, app_id: Optional[str] = Query(default=None, description="应用ID")):
2023-04-14 14:36:37 +08:00
try:
myLogger.info_logger("Receive request: /AppList")
get_headers(request)
2023-04-16 23:26:20 +08:00
app_list = manage.get_my_app(app_id)
myLogger.info_logger(len(app_list))
2023-04-16 23:37:51 +08:00
response = JSONResponse({'ResponseData': app_list})
2023-04-14 14:36:37 +08:00
except CommandException as ce:
ret = {}
ret['ResponseData'] = None
2023-04-17 15:09:24 +08:00
ret['Error'] = manage.get_error_info(ce.code, ce.message, ce.detail)
2023-04-16 23:32:29 +08:00
response = JSONResponse(content=ret)
2023-04-14 14:36:37 +08:00
except Exception as e:
ret = {}
ret['ResponseData'] = None
2023-04-14 19:42:42 +08:00
ret['Error'] = manage.get_error_info(const.ERROR_SERVER_SYSTEM, "system original error", str(e))
2023-04-16 23:32:29 +08:00
response = JSONResponse(content=ret)
return response
2023-02-21 14:21:53 +08:00
2023-04-02 21:51:14 +08:00
2023-04-17 15:40:44 +08:00
@router.api_route("/AppInstall", methods=["GET", "POST"], summary="安装APP", response_description=rd,
2023-04-02 21:51:14 +08:00
response_model=Response)
2023-04-17 15:40:44 +08:00
def AppInstall(request: Request, app_name: Optional[str] = Query(default=None, description="应用名称"),
customer_app_name: Optional[str] = Query(default=None, description="用户自定义应用名称"),
2023-04-12 17:11:53 +08:00
app_version: Optional[str] = Query(default=None, description="应用版本")):
2023-04-14 10:51:29 +08:00
try:
myLogger.info_logger("Receive request: /AppInstall")
2023-04-14 10:53:07 +08:00
get_headers(request)
2023-04-17 15:40:44 +08:00
ret = manage.install_app(app_name, customer_app_name, app_version)
2023-04-14 10:51:29 +08:00
except CommandException as ce:
ret = {}
2023-04-16 14:23:54 +08:00
ret['ResponseData'] = {}
2023-04-19 17:00:43 +08:00
ret['ResponseData']['AppID'] = app_name + "_" + customer_app_name
2023-04-14 19:42:42 +08:00
ret['Error'] = manage.get_error_info(ce.code, ce.message, ce.detail)
2023-04-16 14:03:29 +08:00
myLogger.error_logger("Ready for return fail message")
2023-04-14 10:51:29 +08:00
except Exception as e:
2023-04-16 13:36:46 +08:00
myLogger.error_logger(str(e))
2023-04-14 10:51:29 +08:00
ret = {}
2023-04-19 17:00:43 +08:00
ret['ResponseData'] = {}
ret['ResponseData']['AppID'] = app_name + "_" + customer_app_name
2023-04-14 19:42:42 +08:00
ret['Error'] = manage.get_error_info(const.ERROR_SERVER_SYSTEM, "system original error", str(e))
2023-04-17 15:40:44 +08:00
2023-04-16 13:57:32 +08:00
myLogger.info_logger(ret)
2023-04-14 10:51:29 +08:00
return JSONResponse(content=ret)
2023-04-02 21:51:14 +08:00
2023-04-14 19:42:42 +08:00
2023-04-17 15:40:44 +08:00
@router.api_route("/AppStart", methods=["GET", "POST"], summary="启动APP", response_description=rd,
2023-04-12 17:11:53 +08:00
response_model=Response)
2023-04-16 19:49:10 +08:00
def AppStart(request: Request,app_id: Optional[str] = Query(default=None, description="应用ID")):
2023-04-15 16:39:33 +08:00
try:
myLogger.info_logger("Receive request: /AppStart")
2023-04-17 15:40:44 +08:00
get_headers(request)
2023-04-15 16:39:33 +08:00
ret = {}
2023-04-16 18:59:09 +08:00
ret['ResponseData'] = {}
manage.start_app(app_id)
2023-04-16 19:25:56 +08:00
ret['ResponseData']['AppID'] = app_id
2023-04-15 16:39:33 +08:00
except CommandException as ce:
ret = {}
2023-04-16 19:12:44 +08:00
ret['ResponseData'] = {}
2023-04-16 19:25:56 +08:00
ret['ResponseData']['AppID'] = app_id
2023-04-18 16:53:08 +08:00
myLogger.info_logger("AppStart commond error")
myLogger.info_logger(ce.detail)
2023-04-15 16:39:33 +08:00
ret['Error'] = manage.get_error_info(ce.code, ce.message, ce.detail)
except Exception as e:
ret = {}
2023-04-16 19:12:44 +08:00
ret['ResponseData'] = {}
2023-04-16 19:25:56 +08:00
ret['ResponseData']['AppID'] = app_id
2023-04-15 16:39:33 +08:00
ret['Error'] = manage.get_error_info(const.ERROR_SERVER_SYSTEM, "system original error", str(e))
2023-02-28 17:44:04 +08:00
return JSONResponse(content=ret)
2023-02-25 11:37:39 +08:00
2023-04-14 19:42:42 +08:00
2023-04-17 15:40:44 +08:00
@router.api_route("/AppStop", methods=["GET", "POST"], summary="停止APP", response_description=rd,
2023-04-12 17:11:53 +08:00
response_model=Response)
2023-04-16 19:49:10 +08:00
def AppStop(request: Request,app_id: Optional[str] = Query(default=None, description="应用ID")):
2023-04-15 16:39:33 +08:00
try:
myLogger.info_logger("Receive request: /AppStop")
2023-04-16 19:49:10 +08:00
get_headers(request)
2023-04-15 16:39:33 +08:00
ret = {}
2023-04-16 18:59:09 +08:00
ret['ResponseData'] = {}
manage.stop_app(app_id)
2023-04-16 19:25:56 +08:00
ret['ResponseData']['AppID'] = app_id
2023-04-15 16:39:33 +08:00
except CommandException as ce:
ret = {}
2023-04-16 19:12:44 +08:00
ret['ResponseData'] = {}
2023-04-16 19:25:56 +08:00
ret['ResponseData']['AppID'] = app_id
2023-04-15 16:39:33 +08:00
ret['Error'] = manage.get_error_info(ce.code, ce.message, ce.detail)
except Exception as e:
ret = {}
2023-04-16 19:25:56 +08:00
ret['ResponseData'] = {}
ret['ResponseData']['AppID'] = app_id
2023-04-15 16:39:33 +08:00
ret['Error'] = manage.get_error_info(const.ERROR_SERVER_SYSTEM, "system original error", str(e))
2023-02-28 17:44:04 +08:00
return JSONResponse(content=ret)
2023-02-25 11:37:39 +08:00
2023-04-02 21:51:14 +08:00
2023-04-17 15:40:44 +08:00
@router.api_route("/AppRestart", methods=["GET", "POST"], summary="重启APP", response_description=rd,
2023-04-02 21:51:14 +08:00
response_model=Response)
2023-04-16 19:49:10 +08:00
def AppRestart(request: Request,app_id: Optional[str] = Query(default=None, description="应用ID")):
2023-04-15 16:39:33 +08:00
try:
myLogger.info_logger("Receive request: /AppRestart")
2023-04-16 19:49:10 +08:00
get_headers(request)
2023-04-15 16:39:33 +08:00
ret = {}
2023-04-16 18:59:09 +08:00
ret['ResponseData'] = {}
manage.restart_app(app_id)
2023-04-16 19:25:56 +08:00
ret['ResponseData']['AppID'] = app_id
2023-04-15 16:39:33 +08:00
except CommandException as ce:
ret = {}
2023-04-16 19:12:44 +08:00
ret['ResponseData'] = {}
2023-04-16 19:25:56 +08:00
ret['ResponseData']['AppID'] = app_id
2023-04-15 16:39:33 +08:00
ret['Error'] = manage.get_error_info(ce.code, ce.message, ce.detail)
except Exception as e:
ret = {}
2023-04-16 19:12:44 +08:00
ret['ResponseData'] = {}
2023-04-16 19:25:56 +08:00
ret['ResponseData']['AppID'] = app_id
2023-04-15 16:39:33 +08:00
ret['Error'] = manage.get_error_info(const.ERROR_SERVER_SYSTEM, "system original error", str(e))
2023-02-28 17:44:04 +08:00
return JSONResponse(content=ret)
2023-02-25 11:37:39 +08:00
2023-04-02 21:51:14 +08:00
2023-04-17 15:40:44 +08:00
@router.api_route("/AppUninstall", methods=["GET", "POST"], summary="卸载APP", response_description=rd,
2023-04-02 21:51:14 +08:00
response_model=Response)
2023-04-16 20:26:41 +08:00
def AppUninstall(request: Request, app_id: Optional[str] = Query(default=None, description="应用ID")):
2023-04-12 17:11:53 +08:00
2023-04-15 07:47:11 +08:00
try:
myLogger.info_logger("Receive request: /AppUninstall")
get_headers(request)
2023-04-15 16:39:33 +08:00
ret = {}
2023-04-16 18:59:09 +08:00
ret['ResponseData'] = {}
2023-04-16 20:09:36 +08:00
manage.uninstall_app(app_id)
2023-04-16 19:25:56 +08:00
ret['ResponseData']['AppID'] = app_id
2023-04-15 07:47:11 +08:00
except CommandException as ce:
ret = {}
2023-04-16 19:12:44 +08:00
ret['ResponseData'] = {}
2023-04-16 19:25:56 +08:00
ret['ResponseData']['AppID'] = app_id
2023-04-15 07:47:11 +08:00
ret['Error'] = manage.get_error_info(ce.code, ce.message, ce.detail)
except Exception as e:
ret = {}
2023-04-16 19:12:44 +08:00
ret['ResponseData'] = {}
2023-04-16 19:25:56 +08:00
ret['ResponseData']['AppID'] = app_id
2023-04-15 07:47:11 +08:00
ret['Error'] = manage.get_error_info(const.ERROR_SERVER_SYSTEM, "system original error", str(e))
return JSONResponse(content=ret)
2023-04-12 17:11:53 +08:00
2023-05-10 14:19:24 +08:00
@router.api_route("/AppDomainAdd", methods=["GET", "POST"], summary="绑定域名", response_model=Response)
2023-05-11 10:42:32 +08:00
def AppDomainAdd(request: Request, app_id: Optional[str] = Query(default=None, description="应用ID"), domains: Optional[List[str]] = Query(default=None, description="域名列表")):
2023-05-10 14:19:24 +08:00
try:
myLogger.info_logger("Receive request: /AppDomainAdd")
2023-05-11 16:49:30 +08:00
myLogger.info_logger(domains)
2023-05-10 14:19:24 +08:00
get_headers(request)
ret = {}
ret['ResponseData'] = {}
manage.app_domain_add(app_id,domains)
2023-05-11 10:09:48 +08:00
ret['ResponseData'] = domains
2023-05-10 14:19:24 +08:00
except CommandException as ce:
ret = {}
ret['ResponseData'] = {}
ret['ResponseData']['AppID'] = app_id
ret['Error'] = manage.get_error_info(ce.code, ce.message, ce.detail)
except Exception as e:
ret = {}
ret['ResponseData'] = {}
ret['ResponseData']['AppID'] = app_id
ret['Error'] = manage.get_error_info(const.ERROR_SERVER_SYSTEM, "system original error", str(e))
return JSONResponse(content=ret)
@router.api_route("/AppDomainUpdate", methods=["GET", "POST"], summary="修改域名", response_model=Response)
2023-05-11 10:42:32 +08:00
def AppDomainUpdate(request: Request, app_id: Optional[str] = Query(default=None, description="应用ID"), domains: Optional[List[str]] = Query(default=None, description="域名列表")):
2023-05-10 14:19:24 +08:00
try:
myLogger.info_logger("Receive request: /AppDomainUpdate")
get_headers(request)
ret = {}
ret['ResponseData'] = {}
manage.app_domain_update(app_id,domains)
2023-05-11 10:09:48 +08:00
ret['ResponseData'] = domains
2023-05-10 14:19:24 +08:00
except CommandException as ce:
ret = {}
ret['ResponseData'] = {}
ret['ResponseData']['AppID'] = app_id
ret['Error'] = manage.get_error_info(ce.code, ce.message, ce.detail)
except Exception as e:
ret = {}
ret['ResponseData'] = {}
ret['ResponseData']['AppID'] = app_id
ret['Error'] = manage.get_error_info(const.ERROR_SERVER_SYSTEM, "system original error", str(e))
return JSONResponse(content=ret)
@router.api_route("/AppDomainDelete", methods=["GET", "POST"], summary="删除域名", response_model=Response)
def AppDomainDelete(request: Request, app_id: Optional[str] = Query(default=None, description="应用ID")):
try:
myLogger.info_logger("Receive request: /AppDomainDelete")
get_headers(request)
ret = {}
ret['ResponseData'] = {}
manage.app_domain_delete(app_id)
ret['ResponseData']['AppID'] = app_id
except CommandException as ce:
ret = {}
ret['ResponseData'] = {}
ret['ResponseData']['AppID'] = app_id
ret['Error'] = manage.get_error_info(ce.code, ce.message, ce.detail)
except Exception as e:
ret = {}
ret['ResponseData'] = {}
ret['ResponseData']['AppID'] = app_id
ret['Error'] = manage.get_error_info(const.ERROR_SERVER_SYSTEM, "system original error", str(e))
return JSONResponse(content=ret)
2023-05-10 14:58:19 +08:00
@router.api_route("/AppDomainList", methods=["GET", "POST"], summary="查询App对应域名", response_model=Response)
2023-05-10 14:19:24 +08:00
def AppDomainList(request: Request, app_id: Optional[str] = Query(default=None, description="应用ID")):
try:
myLogger.info_logger("Receive request: /AppDomainList")
get_headers(request)
ret = {}
ret['ResponseData'] = {}
2023-05-11 10:09:48 +08:00
domains = manage.app_domain_list(app_id)
response = JSONResponse({'ResponseData': domains})
2023-05-10 14:19:24 +08:00
except CommandException as ce:
ret = {}
ret['ResponseData'] = {}
ret['ResponseData']['AppID'] = app_id
ret['Error'] = manage.get_error_info(ce.code, ce.message, ce.detail)
2023-05-11 10:09:48 +08:00
response = JSONResponse(content=ret)
2023-05-10 14:19:24 +08:00
except Exception as e:
ret = {}
ret['ResponseData'] = {}
ret['ResponseData']['AppID'] = app_id
ret['Error'] = manage.get_error_info(const.ERROR_SERVER_SYSTEM, "system original error", str(e))
2023-05-11 10:09:48 +08:00
response = JSONResponse(content=ret)
2023-05-10 14:19:24 +08:00
2023-05-11 10:09:48 +08:00
return response
2023-05-10 14:19:24 +08:00
2023-04-14 10:53:07 +08:00
def get_headers(request):
2023-04-12 17:11:53 +08:00
headers = request.headers
try:
version = headers.get('Version')
language = headers.get('Language')
2023-04-14 10:51:29 +08:00
myLogger.info_logger("Version: " + version + ", Language: " + language)
2023-04-12 17:11:53 +08:00
except:
pass