websoft9/appmanage/api/v1/routers/apps.py
2023-04-16 13:57:32 +08:00

197 lines
8.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from typing import Optional, List
from fastapi import APIRouter, status, Depends, Query, Request
from pydantic import BaseModel, Field
from starlette.responses import JSONResponse
import os, io, sys, platform, shutil, time, subprocess, json, datetime
from api.model.app import App
from api.model.response import Response
from api.service import manage
from api.utils import shell_execute, const
from api.utils.common_log import myLogger
from api.exception.command_exception import CommandException
router = APIRouter()
rd1 = "code请求操作内部响应码0成功 -1失败\n\nmessage请求操作结果描述\n\ndata返回请求结果内容\n\n" \
"[\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" \
"  volumeyml文件路径,\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
rd_process = "code请求操作内部响应码0成功 -1失败\n\nmessage请求操作结果描述\n\nstatus应用运行状态," \
"pulling拉取镜像creating容器启动inting容器初始化running:正常运行)"
rd_two = "code请求操作内部响应码0成功 -1失败\n\nmessage请求操作结果描述\n\ndataNone"
@router.api_route("/AppStatus", methods=["GET", "POST"], summary="获取指定APP的信息",
response_description=rd_detail,
response_model=Response)
def AppStatus(app_id: Optional[str] = Query(default=None, description="应用ID")):
try:
myLogger.info_logger("Receive request: /AppStatus")
ret = {}
ret['ResponseData'] = manage.get_app_status(app_id)
except CommandException as ce:
ret = {}
ret['ResponseData'] = None
ret['Error'] = manage.get_error_info(ce.code, ce.message, str(ce))
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)
@router.api_route("/AppList", methods=["GET", "POST"], summary="获取所有APP的信息", response_description=rd_list,
response_model=Response)
def AppList(request: Request, app_id: Optional[str] = Query(default=None, description="应用ID")):
try:
myLogger.info_logger("Receive request: /AppList")
get_headers(request)
ret = {}
ret['ResponseData'] = manage.get_my_app
except CommandException as ce:
ret = {}
ret['ResponseData'] = None
ret['Error'] = manage.get_error_info(ce.code, ce.message, str(ce))
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(content=ret)
@router.api_route("/AppInstall", methods=["GET", "POST"], summary="安装APP", response_description=rd_two,
response_model=Response)
def AppInstall(request: Request, app_name: Optional[str] = Query(default=None, description="应用名"),
customer_name: Optional[str] = Query(default=None, description="应用自定义名字"),
app_version: Optional[str] = Query(default=None, description="应用版本")):
try:
myLogger.info_logger("Receive request: /AppInstall")
get_headers(request)
ret = manage.install_app(app_name, customer_name, app_version)
except CommandException as ce:
myLogger.error_logger(ce.code)
ret = {}
ret['ResponseData']['AppID'] = app_name + "_" + customer_name
ret['Error'] = manage.get_error_info(ce.code, ce.message, ce.detail)
except Exception as e:
myLogger.error_logger(str(e))
ret = {}
ret['ResponseData']['AppID'] = app_name + "_" + customer_name
ret['Error'] = manage.get_error_info(const.ERROR_SERVER_SYSTEM, "system original error", str(e))
myLogger.info_logger(ret)
return JSONResponse(content=ret)
@router.api_route("/AppStart", methods=["GET", "POST"], summary="启动APP", response_description=rd_two,
response_model=Response)
def start_app(app_id: Optional[str] = Query(default=None, description="应用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)
@router.api_route("/AppStop", methods=["GET", "POST"], summary="停止APP", response_description=rd_two,
response_model=Response)
def stop_app(app_id: Optional[str] = Query(default=None, description="应用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)
@router.api_route("/AppRestart", methods=["GET", "POST"], summary="重启APP", response_description=rd_two,
response_model=Response)
def AppRestart(app_id: Optional[str] = Query(default=None, description="应用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)
@router.api_route("/AppUninstall", methods=["GET", "POST"], summary="卸载APP", response_description=rd_two,
response_model=Response)
def AppUninstall(request: Request, app_id: Optional[str] = Query(default=None, description="应用ID"),
delete_image: bool = Query(default=False, description="是否删除镜像"),
delete_data: bool = Query(default=True, description='是否删除所有数据')):
try:
myLogger.info_logger("Receive request: /AppUninstall")
get_headers(request)
manage.uninstall_app(app_name, customer_name, app_version)
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)
def get_headers(request):
headers = request.headers
try:
version = headers.get('Version')
language = headers.get('Language')
myLogger.info_logger("Version: " + version + ", Language: " + language)
except:
pass