update install app

This commit is contained in:
TooY 2023-03-17 16:31:03 +08:00
parent fe6a55a4ac
commit 97c74e1af0
3 changed files with 54 additions and 39 deletions

View File

@ -12,6 +12,7 @@ from threading import Thread
from api.utils import shell_execute, network, docker, const from api.utils import shell_execute, network, docker, const
from api.model.app import App from api.model.app import App
from api.model.response import Response from api.model.response import Response
from api.utils import lock
def get_app_detail(app_id): def get_app_detail(app_id):
@ -185,13 +186,14 @@ def install_app_process(app_id):
def install_app(app_name, customer_app_name, app_version): def install_app(app_name, customer_app_name, app_version):
app_file_path = '/data/apps/'+app_name app_file_path = '/data/apps/' + app_name
running_file_path = "/data/apps/running_apps.txt" running_file_path = "/data/apps/running_apps.txt"
unique_app_path = "/data/apps/"+customer_app_name unique_app_path = "/data/apps/" + customer_app_name
is_lock = lock.install_mutex.acquire(False)
if os.path.exists(running_file_path) and os.path.getsize(running_file_path): if is_lock and not (os.path.exists(running_file_path) and os.path.getsize(running_file_path)):
ret = Response(code=const.RETURN_SUCCESS, message="已有应用正在启动,请稍后再试") # if os.path.exists(running_file_path) and os.path.getsize(running_file_path):
ret = ret.dict() # ret = Response(code=const.RETURN_SUCCESS, message="已有应用正在启动,请稍后再试")
# ret = ret.dict()
# 防止app名重复 # 防止app名重复
app_id = app_name + "_" + customer_app_name app_id = app_name + "_" + customer_app_name
@ -199,12 +201,14 @@ def install_app(app_name, customer_app_name, app_version):
ret = Response(code=const.RETURN_FAIL, ret = Response(code=const.RETURN_FAIL,
message="APP名称已经使用请指定其他名称重新安装。") message="APP名称已经使用请指定其他名称重新安装。")
ret = ret.dict() ret = ret.dict()
lock.install_mutex.release()
return ret return ret
elif docker.check_app_directory(app_name): elif docker.check_app_directory(app_name):
if docker.check_vm_resource(app_name) == False: if docker.check_vm_resource(app_name) == False:
ret = Response(code=const.RETURN_FAIL, message="系统资源(内存、CPU、磁盘)不足,继续安装可能导致应用无法运行或服务器异常!") ret = Response(code=const.RETURN_FAIL, message="系统资源(内存、CPU、磁盘)不足,继续安装可能导致应用无法运行或服务器异常!")
ret = ret.dict() ret = ret.dict()
lock.install_mutex.release()
return ret return ret
if app_name != customer_app_name: if app_name != customer_app_name:
@ -214,6 +218,7 @@ def install_app(app_name, customer_app_name, app_version):
ret.code = const.RETURN_FAIL ret.code = const.RETURN_FAIL
ret.message = "创建" + customer_app_name + "目录失败." ret.message = "创建" + customer_app_name + "目录失败."
ret = ret.dict() ret = ret.dict()
lock.install_mutex.release()
return ret return ret
env_file = unique_app_path + '/.env' env_file = unique_app_path + '/.env'
docker.modify_env(env_file, 'APP_NAME', customer_app_name) docker.modify_env(env_file, 'APP_NAME', customer_app_name)
@ -223,6 +228,9 @@ def install_app(app_name, customer_app_name, app_version):
if app_version != None: if app_version != None:
path = "/data/apps/"+customer_app_name+"/.env" path = "/data/apps/"+customer_app_name+"/.env"
docker.modify_env(path, "APP_VERSION", app_version) docker.modify_env(path, "APP_VERSION", app_version)
file_path = "/data/apps/running_apps.txt"
with open(file_path, "w", encoding="utf-8") as f:
f.write(app_name)
t1 = Thread(target=record_and_install_app, args=(customer_app_name,)) t1 = Thread(target=record_and_install_app, args=(customer_app_name,))
t1.start() t1.start()
ret = Response(code=const.RETURN_SUCCESS, message="应用正在启动中,请过几分钟再查询") ret = Response(code=const.RETURN_SUCCESS, message="应用正在启动中,请过几分钟再查询")
@ -230,14 +238,17 @@ def install_app(app_name, customer_app_name, app_version):
else: else:
ret = Response(code=const.RETURN_FAIL, message="目前不支持安装此App") ret = Response(code=const.RETURN_FAIL, message="目前不支持安装此App")
ret = ret.dict() ret = ret.dict()
lock.install_mutex.release()
return ret
else:
ret = Response(code=const.RETURN_SUCCESS, message="已有应用正在启动,请稍后再试")
ret = ret.dict()
return ret return ret
def record_and_install_app(app_name): def record_and_install_app(app_name):
# modify running_apps.txt # modify running_apps.txt
file_path = "/data/apps/running_apps.txt" file_path = "/data/apps/running_apps.txt"
with open(file_path, "w", encoding="utf-8") as f:
f.write(app_name)
cmd = "cd /data/apps/" + app_name + " && sudo docker compose up -d" cmd = "cd /data/apps/" + app_name + " && sudo docker compose up -d"
shell_execute.execute_command_output_all(cmd) shell_execute.execute_command_output_all(cmd)
with open(file_path, "a+", encoding="utf-8") as f: with open(file_path, "a+", encoding="utf-8") as f:

View File

@ -104,6 +104,7 @@ def read_env(path, key):
env_list = ret.split() env_list = ret.split()
for env in env_list: for env in env_list:
env_dic[env.split("=")[0]] = env.split("=")[1] env_dic[env.split("=")[0]] = env.split("=")[1]
myLogger.info_logger("Read " + path + ": " + str(env_dic))
return env_dic return env_dic
def modify_env(path, env_name, value): def modify_env(path, env_name, value):

View File

@ -0,0 +1,3 @@
from threading import Lock
install_mutex = Lock()