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):
|
||||
def __init__(self, message):
|
||||
self.message = message
|
||||
def __init__(self, code, message, detail):
|
||||
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 redis import Redis
|
||||
from rq import Queue, Worker, Connection
|
||||
from rq.registry import StartedJobRegistry, FinishedJobRegistry, DeferredJobRegistry, FailedJobRegistry, \
|
||||
ScheduledJobRegistry
|
||||
from rq.registry import StartedJobRegistry, FinishedJobRegistry, DeferredJobRegistry, FailedJobRegistry, ScheduledJobRegistry
|
||||
from api.exception.command_exception import CommandException
|
||||
|
||||
# 指定 Redis 容器的主机名和端口
|
||||
redis_conn = Redis(host='websoft9-redis', port=6379)
|
||||
|
@ -27,7 +27,6 @@ redis_conn = Redis(host='websoft9-redis', port=6379)
|
|||
# 使用指定的 Redis 连接创建 RQ 队列
|
||||
q = Queue(connection=redis_conn)
|
||||
|
||||
|
||||
# 获取所有app的信息
|
||||
def get_my_app():
|
||||
# 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):
|
||||
message = " "
|
||||
message = ""
|
||||
code = None
|
||||
app_id = app_name + "-" + customer_name
|
||||
if app_name == None:
|
||||
|
@ -215,55 +214,46 @@ def check_app(app_name, customer_name, app_version):
|
|||
message = "Repeat installation: " + customer_name
|
||||
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
|
||||
install_path = "/data/apps/" + customer_app_name
|
||||
message = " "
|
||||
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
|
||||
install_path = "/data/apps/" + customer_name
|
||||
shell_execute.execute_command_output_all("cp -r " + library_path + " " + install_path)
|
||||
|
||||
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:
|
||||
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"
|
||||
output = shell_execute.execute_command_output_all(cmd)
|
||||
myLogger.info_logger("install output")
|
||||
myLogger.info_logger(output["code"])
|
||||
myLogger.info_logger(output["result"])
|
||||
if int(output["code"]) != 0 or "error" in output["result"] or "fail" in output["result"]:
|
||||
raise Exception("installfailed!")
|
||||
else:
|
||||
return "success"
|
||||
else:
|
||||
raise Exception("prepare_app failed")
|
||||
code, message = check_app(app_name, customer_name, app_version)
|
||||
if code == None:
|
||||
|
||||
prepare_app(app_name, customer_name)
|
||||
|
||||
myLogger.info_logger("start JobID=" + job_id)
|
||||
# modify env
|
||||
env_path = "/data/apps/" + customer_name + "/.env"
|
||||
docker.modify_env(env_path, 'APP_NAME', customer_name)
|
||||
docker.modify_env(env_path, "APP_VERSION", app_version)
|
||||
# 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:
|
||||
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:
|
||||
myLogger.info_logger(customer_app_name + "install failed!")
|
||||
myLogger.info_logger(customer_name + "install failed!")
|
||||
myLogger.error_logger(e)
|
||||
job_id = app_name + "_" + customer_app_name
|
||||
try:
|
||||
uninstall_app(job_id)
|
||||
except Exception as e:
|
||||
myLogger.error_logger(e)
|
||||
raise Exception(e)
|
||||
|
||||
uninstall_app(job_id)
|
||||
raise CommandException(const.ERROR_SERVER_SYSTEM,"system original error",str(e))
|
||||
|
||||
def if_app_exits(app_id):
|
||||
app_name = app_id.split('_')[1]
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
import os, io, sys, platform, shutil, time, subprocess, json, datetime
|
||||
from api.utils.common_log import myLogger
|
||||
from api.exception.command_exception import CommandException
|
||||
from api.utils import const
|
||||
|
||||
def execute_command_output(cmd_str):
|
||||
print(cmd_str)
|
||||
|
@ -10,7 +11,7 @@ def execute_command_output(cmd_str):
|
|||
return out_str
|
||||
|
||||
# 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)
|
||||
execute_time = 0
|
||||
|
@ -22,8 +23,8 @@ def execute_command_output_all(cmd_str, max_time = 3):
|
|||
return {"code": "0", "result": process.stdout,}
|
||||
else:
|
||||
execute_time = execute_time + 1
|
||||
if execute_time > 3:
|
||||
raise CommandException(process.stdout)
|
||||
if execute_time > 2:
|
||||
raise CommandException(const.ERROR_SERVER_COMMAND,"Docker returns the original error",process.stdout)
|
||||
|
||||
def convert_command(cmd_str):
|
||||
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,
|
||||
response_model=Response)
|
||||
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="应用版本")):
|
||||
|
||||
try:
|
||||
myLogger.info_logger("Receive request: /AppInstall")
|
||||
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:
|
||||
ret = {}
|
||||
ret['ResponseData']['AppID'] = app_name + "_" + customer_app_name
|
||||
ret['Error']=manage.get_error_info(const.ERROR_SERVER_COMMAND,"Docker returns the original error",str(ce))
|
||||
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_app_name
|
||||
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)
|
||||
|
@ -99,7 +99,6 @@ def start_app(app_id: Optional[str] = Query(default=None, description="应用ID"
|
|||
ret = manage.start_app(app_id)
|
||||
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")):
|
||||
|
|
Loading…
Add table
Reference in a new issue