appmanage
This commit is contained in:
parent
b8d6f1a2f4
commit
8522e525b2
4 changed files with 48 additions and 56 deletions
|
@ -1,3 +1,5 @@
|
||||||
class CommandException(Exception):
|
class CommandException(Exception):
|
||||||
def __init__(self, message):
|
def __init__(self, code, message, detail):
|
||||||
self.message = message
|
self.code = code
|
||||||
|
self.message = message
|
||||||
|
self.detail = detail
|
|
@ -18,8 +18,8 @@ from api.model.status_reason import StatusReason
|
||||||
from api.utils.common_log import myLogger
|
from api.utils.common_log import myLogger
|
||||||
from redis import Redis
|
from redis import Redis
|
||||||
from rq import Queue, Worker, Connection
|
from rq import Queue, Worker, Connection
|
||||||
from rq.registry import StartedJobRegistry, FinishedJobRegistry, DeferredJobRegistry, FailedJobRegistry, \
|
from rq.registry import StartedJobRegistry, FinishedJobRegistry, DeferredJobRegistry, FailedJobRegistry, ScheduledJobRegistry
|
||||||
ScheduledJobRegistry
|
from api.exception.command_exception import CommandException
|
||||||
|
|
||||||
# 指定 Redis 容器的主机名和端口
|
# 指定 Redis 容器的主机名和端口
|
||||||
redis_conn = Redis(host='websoft9-redis', port=6379)
|
redis_conn = Redis(host='websoft9-redis', port=6379)
|
||||||
|
@ -27,7 +27,6 @@ redis_conn = Redis(host='websoft9-redis', port=6379)
|
||||||
# 使用指定的 Redis 连接创建 RQ 队列
|
# 使用指定的 Redis 连接创建 RQ 队列
|
||||||
q = Queue(connection=redis_conn)
|
q = Queue(connection=redis_conn)
|
||||||
|
|
||||||
|
|
||||||
# 获取所有app的信息
|
# 获取所有app的信息
|
||||||
def get_my_app():
|
def get_my_app():
|
||||||
# get all info
|
# get all info
|
||||||
|
@ -186,7 +185,7 @@ def uninstall_app(app_id, delete_image, delete_data):
|
||||||
|
|
||||||
|
|
||||||
def check_app(app_name, customer_name, app_version):
|
def check_app(app_name, customer_name, app_version):
|
||||||
message = " "
|
message = ""
|
||||||
code = None
|
code = None
|
||||||
app_id = app_name + "-" + customer_name
|
app_id = app_name + "-" + customer_name
|
||||||
if app_name == None:
|
if app_name == None:
|
||||||
|
@ -215,55 +214,46 @@ 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_app_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_app_name
|
install_path = "/data/apps/" + customer_name
|
||||||
message = " "
|
shell_execute.execute_command_output_all("cp -r " + library_path + " " + install_path)
|
||||||
code = const.RETURN_SUCCESS
|
|
||||||
output = shell_execute.execute_command_output_all("cp -r " + library_path + " " + install_path)
|
|
||||||
if int(output["code"]) != 0:
|
|
||||||
message = "creating" + customer_app_name + "directory failed!"
|
|
||||||
code = const.RETURN_FAIL
|
|
||||||
return code, message
|
|
||||||
|
|
||||||
|
def install_app_delay(app_name, customer_name, app_version):
|
||||||
|
|
||||||
|
job_id = app_name + "_" + customer_name
|
||||||
|
|
||||||
def install_app_delay(app_name, customer_app_name, app_version):
|
|
||||||
try:
|
try:
|
||||||
code, message = check_app(app_name, customer_app_name, app_version)
|
|
||||||
if code == const.RETURN_SUCCESS:
|
|
||||||
code, message = prepare_app(app_name, customer_app_name)
|
|
||||||
if code == const.RETURN_SUCCESS:
|
|
||||||
myLogger.info_logger("start job=" + customer_app_name)
|
|
||||||
# modify env
|
|
||||||
env_path = "/data/apps/" + customer_app_name + "/.env"
|
|
||||||
docker.modify_env(env_path, 'APP_NAME', customer_app_name)
|
|
||||||
docker.modify_env(env_path, "APP_VERSION", app_version)
|
|
||||||
# check port
|
|
||||||
docker.check_app_compose(env_path)
|
|
||||||
|
|
||||||
cmd = "cd /data/apps/" + customer_app_name + " && sudo docker compose pull && sudo docker compose up -d"
|
code, message = check_app(app_name, customer_name, app_version)
|
||||||
output = shell_execute.execute_command_output_all(cmd)
|
if code == None:
|
||||||
myLogger.info_logger("install output")
|
|
||||||
myLogger.info_logger(output["code"])
|
prepare_app(app_name, customer_name)
|
||||||
myLogger.info_logger(output["result"])
|
|
||||||
if int(output["code"]) != 0 or "error" in output["result"] or "fail" in output["result"]:
|
myLogger.info_logger("start JobID=" + job_id)
|
||||||
raise Exception("installfailed!")
|
# modify env
|
||||||
else:
|
env_path = "/data/apps/" + customer_name + "/.env"
|
||||||
return "success"
|
docker.modify_env(env_path, 'APP_NAME', customer_name)
|
||||||
else:
|
docker.modify_env(env_path, "APP_VERSION", app_version)
|
||||||
raise Exception("prepare_app failed")
|
# check port
|
||||||
|
docker.check_app_compose(env_path)
|
||||||
|
|
||||||
|
cmd = "cd /data/apps/" + customer_name + " && sudo docker compose pull && sudo docker compose up -d"
|
||||||
|
output = shell_execute.execute_command_output_all(cmd)
|
||||||
|
myLogger.info_logger("-------Install result--------")
|
||||||
|
myLogger.info_logger(output["code"])
|
||||||
|
myLogger.info_logger(output["result"])
|
||||||
else:
|
else:
|
||||||
raise Exception("resource check failed")
|
raise CommandException(code,message,"")
|
||||||
|
except CommandException as ce:
|
||||||
|
uninstall_app(job_id)
|
||||||
|
raise CommandException(ce.code,ce.message,ce.detail)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
myLogger.info_logger(customer_app_name + "install failed!")
|
myLogger.info_logger(customer_name + "install failed!")
|
||||||
myLogger.error_logger(e)
|
myLogger.error_logger(e)
|
||||||
job_id = app_name + "_" + customer_app_name
|
uninstall_app(job_id)
|
||||||
try:
|
raise CommandException(const.ERROR_SERVER_SYSTEM,"system original error",str(e))
|
||||||
uninstall_app(job_id)
|
|
||||||
except Exception as e:
|
|
||||||
myLogger.error_logger(e)
|
|
||||||
raise Exception(e)
|
|
||||||
|
|
||||||
|
|
||||||
def if_app_exits(app_id):
|
def if_app_exits(app_id):
|
||||||
app_name = app_id.split('_')[1]
|
app_name = app_id.split('_')[1]
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
import os, io, sys, platform, shutil, time, subprocess, json, datetime
|
import os, io, sys, platform, shutil, time, subprocess, json, datetime
|
||||||
from api.utils.common_log import myLogger
|
from api.utils.common_log import myLogger
|
||||||
from api.exception.command_exception import CommandException
|
from api.exception.command_exception import CommandException
|
||||||
|
from api.utils import const
|
||||||
|
|
||||||
def execute_command_output(cmd_str):
|
def execute_command_output(cmd_str):
|
||||||
print(cmd_str)
|
print(cmd_str)
|
||||||
|
@ -10,7 +11,7 @@ def execute_command_output(cmd_str):
|
||||||
return out_str
|
return out_str
|
||||||
|
|
||||||
# cmd_str: 执行的command命令 times:如果不成功的重复次数
|
# cmd_str: 执行的command命令 times:如果不成功的重复次数
|
||||||
def execute_command_output_all(cmd_str, max_time = 3):
|
def execute_command_output_all(cmd_str, max_time = 2):
|
||||||
|
|
||||||
myLogger.info_logger("Start to execute cmd: " + cmd_str)
|
myLogger.info_logger("Start to execute cmd: " + cmd_str)
|
||||||
execute_time = 0
|
execute_time = 0
|
||||||
|
@ -22,8 +23,8 @@ def execute_command_output_all(cmd_str, max_time = 3):
|
||||||
return {"code": "0", "result": process.stdout,}
|
return {"code": "0", "result": process.stdout,}
|
||||||
else:
|
else:
|
||||||
execute_time = execute_time + 1
|
execute_time = execute_time + 1
|
||||||
if execute_time > 3:
|
if execute_time > 2:
|
||||||
raise CommandException(process.stdout)
|
raise CommandException(const.ERROR_SERVER_COMMAND,"Docker returns the original error",process.stdout)
|
||||||
|
|
||||||
def convert_command(cmd_str):
|
def convert_command(cmd_str):
|
||||||
convert_cmd = ""
|
convert_cmd = ""
|
||||||
|
|
|
@ -74,20 +74,20 @@ def list_my_apps(request: Request):
|
||||||
@router.api_route("/AppInstall", methods=["GET", "POST"], summary="安装APP", response_description=rd_two,
|
@router.api_route("/AppInstall", methods=["GET", "POST"], summary="安装APP", response_description=rd_two,
|
||||||
response_model=Response)
|
response_model=Response)
|
||||||
def AppInstall(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="应用自定义名字"),
|
customer_name: Optional[str] = Query(default=None, description="应用自定义名字"),
|
||||||
app_version: Optional[str] = Query(default=None, description="应用版本")):
|
app_version: Optional[str] = Query(default=None, description="应用版本")):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
myLogger.info_logger("Receive request: /AppInstall")
|
myLogger.info_logger("Receive request: /AppInstall")
|
||||||
get_headers(request)
|
get_headers(request)
|
||||||
ret = manage.install_app(app_name, customer_app_name, app_version)
|
ret = manage.install_app(app_name, customer_name, app_version)
|
||||||
except CommandException as ce:
|
except CommandException as ce:
|
||||||
ret = {}
|
ret = {}
|
||||||
ret['ResponseData']['AppID'] = app_name + "_" + customer_app_name
|
ret['ResponseData']['AppID'] = app_name + "_" + customer_name
|
||||||
ret['Error']=manage.get_error_info(const.ERROR_SERVER_COMMAND,"Docker returns the original error",str(ce))
|
ret['Error']=manage.get_error_info(ce.code,ce.message,ce.detail)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
ret = {}
|
ret = {}
|
||||||
ret['ResponseData']['AppID'] = app_name + "_" + customer_app_name
|
ret['ResponseData']['AppID'] = app_name + "_" + customer_name
|
||||||
ret['Error']=manage.get_error_info(const.ERROR_SERVER_SYSTEM,"system original error",str(e))
|
ret['Error']=manage.get_error_info(const.ERROR_SERVER_SYSTEM,"system original error",str(e))
|
||||||
|
|
||||||
return JSONResponse(content=ret)
|
return JSONResponse(content=ret)
|
||||||
|
@ -99,7 +99,6 @@ def start_app(app_id: Optional[str] = Query(default=None, description="应用ID"
|
||||||
ret = manage.start_app(app_id)
|
ret = manage.start_app(app_id)
|
||||||
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")):
|
||||||
|
|
Loading…
Add table
Reference in a new issue