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-02-22 17:19:27 +08:00
|
|
|
|
from api.utils import shell_execute
|
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-07 09:52:22 +08:00
|
|
|
|
rd1 = "code:请求操作内部响应码(0:成功 -1:失败)\n\nmessage:请求操作结果描述\n\ndata:返回请求结果内容\n\n" \
|
2023-04-06 09:18:30 +08:00
|
|
|
|
"[\n\n" \
|
|
|
|
|
"  app_id:应用ID,\n\n" \
|
|
|
|
|
"  name:应用名,\n\n" \
|
|
|
|
|
"  customer_name:自定义应用名,\n\n" \
|
|
|
|
|
"  trade_mark:应用商标,\n\n" \
|
|
|
|
|
"  status_code:应用运行状态码,\n\n"
|
|
|
|
|
|
|
|
|
|
status = '  status:应用运行状态,\n\n'
|
|
|
|
|
status_detail = "  status:应用运行状态,(running:正常运行,stop:停止,error:错误)\n\n"
|
|
|
|
|
status_list = "  status:应用运行状态,(waiting:等待安装,installing:安装中,running:正常运行,stop:停止,error:错误)\n\n"
|
|
|
|
|
|
|
|
|
|
rd2 = "  port:应用端口,\n\n" \
|
|
|
|
|
"  volume:yml文件路径,\n\n" \
|
|
|
|
|
"  url:应用网址,\n\n" \
|
|
|
|
|
"  image_url:图片路径,\n\n" \
|
|
|
|
|
"  admin_url:管理员网址,\n\n" \
|
|
|
|
|
"  user_name:用户名,\n\n" \
|
|
|
|
|
"  password:密码,\n\n" \
|
|
|
|
|
"  official_app:是否为官方应用\n\n" \
|
|
|
|
|
"]"
|
|
|
|
|
rd = rd1 + status + rd2
|
|
|
|
|
rd_detail = rd1 + status_detail + rd2
|
|
|
|
|
rd_list = rd1 + status_list + rd2
|
2023-04-07 09:52:22 +08:00
|
|
|
|
rd_process = "code:请求操作内部响应码(0:成功 -1:失败)\n\nmessage:请求操作结果描述\n\nstatus:应用运行状态," \
|
|
|
|
|
"(pulling:拉取镜像,creating:容器启动,inting:容器初始化,running:正常运行)"
|
|
|
|
|
rd_two = "code:请求操作内部响应码(0:成功 -1:失败)\n\nmessage:请求操作结果描述\n\ndata:None"
|
|
|
|
|
|
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-06 09:18:30 +08:00
|
|
|
|
response_description=rd_detail,
|
2023-04-02 21:51:14 +08:00
|
|
|
|
response_model=Response)
|
|
|
|
|
def app_detail(app_id: Optional[str] = Query(default=None, description="应用ID")):
|
2023-04-14 09:27:08 +08:00
|
|
|
|
myLogger.info_logger("Receive request: /AppStatus")
|
2023-03-16 14:27:42 +08:00
|
|
|
|
list = manage.get_app_detail(app_id)
|
2023-03-28 16:07:39 +08:00
|
|
|
|
return JSONResponse(list)
|
2023-03-16 14:09:48 +08:00
|
|
|
|
|
2023-04-02 21:51:14 +08:00
|
|
|
|
|
2023-04-14 09:27:08 +08:00
|
|
|
|
@router.api_route("/AppList", methods=["GET", "POST"], summary="获取所有APP的信息", response_description=rd_list,
|
2023-04-06 09:18:30 +08:00
|
|
|
|
response_model=Response)
|
2023-02-22 17:19:27 +08:00
|
|
|
|
def list_my_apps():
|
2023-04-14 09:27:08 +08:00
|
|
|
|
myLogger.info_logger("Receive request: /AppList")
|
2023-02-22 17:19:27 +08:00
|
|
|
|
list = manage.get_my_app()
|
|
|
|
|
return JSONResponse(content=list)
|
2023-02-21 14:21:53 +08:00
|
|
|
|
|
2023-04-02 21:51:14 +08:00
|
|
|
|
|
2023-04-14 09:27:08 +08:00
|
|
|
|
@router.api_route("/AppInstall", methods=["GET", "POST"], summary="安装APP", response_description=rd_two,
|
2023-04-02 21:51:14 +08:00
|
|
|
|
response_model=Response)
|
2023-04-14 10:51:29 +08:00
|
|
|
|
def AppInstall(request: Request, app_name: Optional[str] = Query(default=None, description="应用名"),
|
2023-04-12 17:11:53 +08:00
|
|
|
|
customer_app_name: Optional[str] = Query(default=None, description="应用自定义名字"),
|
|
|
|
|
app_version: Optional[str] = Query(default=None, description="应用版本")):
|
2023-04-14 10:51:29 +08:00
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
myLogger.info_logger("Receive request: /AppInstall")
|
|
|
|
|
getHeaders(request)
|
|
|
|
|
ret = manage.install_app(app_name, customer_app_name, app_version)
|
|
|
|
|
except CommandException as ce:
|
|
|
|
|
ret = {}
|
|
|
|
|
ret['ResponseData']['AppID'] = app_name + "_" + customer_app_name
|
|
|
|
|
ret['Error']=manage.get_error_info("Server.Container.Error","Docker returns the original error",str(ce))
|
|
|
|
|
except Exception as e:
|
|
|
|
|
ret = {}
|
|
|
|
|
ret['ResponseData']['AppID'] = app_name + "_" + customer_app_name
|
2023-04-14 10:52:05 +08:00
|
|
|
|
ret['Error']=manage.get_error_info("Server.SystemError","system original error",str(e))
|
2023-02-22 17:43:09 +08:00
|
|
|
|
|
2023-04-14 10:51:29 +08:00
|
|
|
|
return JSONResponse(content=ret)
|
2023-04-02 21:51:14 +08:00
|
|
|
|
|
2023-04-06 09:18:30 +08:00
|
|
|
|
@router.api_route("/process", methods=["GET", "POST"], summary="获取指定APP的安装进度",
|
|
|
|
|
response_description=rd_process,
|
2023-04-02 21:51:14 +08:00
|
|
|
|
response_model=Response)
|
|
|
|
|
def install_app_process(app_id: Optional[str] = Query(default=None, description="应用ID")):
|
2023-04-14 09:27:08 +08:00
|
|
|
|
myLogger.info_logger("Receive request: /process")
|
2023-03-15 11:43:04 +08:00
|
|
|
|
ret = manage.install_app_process(app_id)
|
2023-03-09 10:29:11 +08:00
|
|
|
|
return JSONResponse(content=ret)
|
|
|
|
|
|
2023-04-02 21:51:14 +08:00
|
|
|
|
|
2023-04-14 09:27:08 +08:00
|
|
|
|
@router.api_route("/AppStart", methods=["GET", "POST"], summary="启动APP", response_description=rd_two,
|
2023-04-12 17:11:53 +08:00
|
|
|
|
response_model=Response)
|
2023-04-02 21:51:14 +08:00
|
|
|
|
def start_app(app_id: Optional[str] = Query(default=None, description="应用ID")):
|
2023-04-14 09:27:08 +08:00
|
|
|
|
myLogger.info_logger("Receive request: /AppStart")
|
2023-03-15 11:43:04 +08:00
|
|
|
|
ret = manage.start_app(app_id)
|
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-14 09:27:08 +08:00
|
|
|
|
@router.api_route("/AppStop", methods=["GET", "POST"], summary="停止APP", response_description=rd_two,
|
2023-04-12 17:11:53 +08:00
|
|
|
|
response_model=Response)
|
2023-04-02 21:51:14 +08:00
|
|
|
|
def stop_app(app_id: Optional[str] = Query(default=None, description="应用ID")):
|
2023-04-14 09:27:08 +08:00
|
|
|
|
myLogger.info_logger("Receive request: /AppStop")
|
2023-03-15 11:43:04 +08:00
|
|
|
|
ret = manage.stop_app(app_id)
|
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-14 09:27:08 +08:00
|
|
|
|
@router.api_route("/AppRestart", methods=["GET", "POST"], summary="重启APP", response_description=rd_two,
|
2023-04-02 21:51:14 +08:00
|
|
|
|
response_model=Response)
|
|
|
|
|
def restart_app(app_id: Optional[str] = Query(default=None, description="应用ID")):
|
2023-04-14 09:27:08 +08:00
|
|
|
|
myLogger.info_logger("Receive request: /AppRestart")
|
2023-03-16 10:07:17 +08:00
|
|
|
|
ret = manage.restart_app(app_id)
|
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-14 09:27:08 +08:00
|
|
|
|
@router.api_route("/AppUninstall", methods=["GET", "POST"], summary="卸载APP", response_description=rd_two,
|
2023-04-02 21:51:14 +08:00
|
|
|
|
response_model=Response)
|
2023-04-14 09:27:08 +08:00
|
|
|
|
def uninstall_app(request: Request, app_id: Optional[str] = Query(default=None, description="应用ID"),
|
2023-04-12 17:11:53 +08:00
|
|
|
|
delete_image: bool = Query(default=False, description="是否删除镜像"),
|
|
|
|
|
delete_data: bool = Query(default=True, description='是否删除所有数据')):
|
2023-04-14 09:27:08 +08:00
|
|
|
|
myLogger.info_logger("Receive request: /AppUninstall")
|
2023-04-12 17:11:53 +08:00
|
|
|
|
getHeaders(request)
|
|
|
|
|
ret = manage.uninstall_app(app_id, delete_image, delete_data)
|
2023-02-28 17:44:04 +08:00
|
|
|
|
return JSONResponse(content=ret)
|
2023-04-12 17:11:53 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def getHeaders(request):
|
|
|
|
|
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
|