excepiton

This commit is contained in:
qiaofeng1227 2023-04-14 02:51:29 +00:00
parent adf6e14619
commit 364582825e
4 changed files with 29 additions and 18 deletions

View file

@ -0,0 +1,3 @@
class CommandException(Exception):
def __init__(self, message):
self.message = message

View file

@ -501,8 +501,9 @@ def get_admin_url(app_name, url):
return admin_url
def setErrorInfo(code, message):
def get_error_info(code, message, detail):
error = {}
error['code'] = code
error['message'] = message
error['Code'] = code
error['Message'] = message
error['Detail'] = detail
return error

View file

@ -1,6 +1,7 @@
#!/usr/bin/python3
import os, io, sys, platform, shutil, time, subprocess, json, datetime
from api.utils.common_log import myLogger
from api.exception.command_exception import CommandException
def execute_command_output(cmd_str):
print(cmd_str)
@ -14,16 +15,15 @@ def execute_command_output_all(cmd_str, max_time = 3):
myLogger.info_logger("Start to execute cmd: " + cmd_str)
execute_time = 0
while execute_time < max_time:
process = subprocess.run(f'nsenter -m -u -i -n -p -t 1 sh -c "{cmd_str}"', capture_output=True, check=False, text=True, shell=True)
myLogger.info_logger("execute success: " + cmd_str)
if process.returncode == 0:
if process.returncode == 0 and 'Fail' not in process.stdout and 'fail' not in process.stdout and 'Error' not in process.stdout and 'error' not in process.stdout:
return {"code": "0", "result": process.stdout,}
else:
execute_time = execute_time + 1
myLogger.error_logger("Command execute failed Commend: " + cmd_str)
return {"code": "-1", "result": "command execute failed, please check your command!"}
if execute_time > 3:
raise CommandException(process.stdout)
def convert_command(cmd_str):
convert_cmd = ""

View file

@ -10,6 +10,7 @@ from api.model.response import Response
from api.service import manage
from api.utils import shell_execute
from api.utils.common_log import myLogger
from api.exception.command_exception import CommandException
router = APIRouter()
@ -61,14 +62,24 @@ def list_my_apps():
@router.api_route("/AppInstall", methods=["GET", "POST"], summary="安装APP", response_description=rd_two,
response_model=Response)
def install_app(request: Request, app_name: Optional[str] = Query(default=None, description="应用名"),
def AppInstall(request: Request, app_name: Optional[str] = Query(default=None, description="应用名"),
customer_app_name: Optional[str] = Query(default=None, description="应用自定义名字"),
app_version: Optional[str] = Query(default=None, description="应用版本")):
myLogger.info_logger("Receive request: /AppInstall")
getHeaders(request)
ret = manage.install_app(app_name, customer_app_name, app_version)
return JSONResponse(content=ret)
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
ret['Error']=manage.get_error_info("Server.SystemError","system original error",str(ce))
return JSONResponse(content=ret)
@router.api_route("/process", methods=["GET", "POST"], summary="获取指定APP的安装进度",
response_description=rd_process,
@ -118,11 +129,7 @@ def getHeaders(request):
headers = request.headers
try:
version = headers.get('Version')
myLogger.info_logger("Version: " + version)
except:
pass
try:
language = headers.get('Language')
myLogger.info_logger("Language: " + language)
myLogger.info_logger("Version: " + version + ", Language: " + language)
except:
pass