2023-03-15 08:59:06 +08:00
|
|
|
|
import os
|
|
|
|
|
import io
|
|
|
|
|
import sys
|
|
|
|
|
import platform
|
|
|
|
|
import shutil
|
|
|
|
|
import time
|
|
|
|
|
import subprocess
|
|
|
|
|
import json
|
|
|
|
|
import datetime
|
2023-02-24 10:23:32 +08:00
|
|
|
|
import socket
|
2023-03-21 15:14:35 +08:00
|
|
|
|
import re
|
2023-02-27 11:07:25 +08:00
|
|
|
|
from threading import Thread
|
2023-03-24 15:35:58 +08:00
|
|
|
|
from api.utils import shell_execute, docker, const
|
2023-02-24 10:23:32 +08:00
|
|
|
|
from api.model.app import App
|
2023-03-07 16:55:23 +08:00
|
|
|
|
from api.model.response import Response
|
2023-03-28 17:16:07 +08:00
|
|
|
|
from api.utils.common_log import myLogger
|
2023-04-04 10:22:05 +08:00
|
|
|
|
from redis import Redis
|
2023-04-04 17:25:23 +08:00
|
|
|
|
from rq import Queue, Worker, Connection
|
2023-04-12 15:11:37 +08:00
|
|
|
|
from rq.registry import StartedJobRegistry, FinishedJobRegistry, DeferredJobRegistry, FailedJobRegistry, ScheduledJobRegistry
|
2023-04-04 10:22:05 +08:00
|
|
|
|
|
|
|
|
|
# 指定 Redis 容器的主机名和端口
|
2023-04-04 15:28:21 +08:00
|
|
|
|
redis_conn = Redis(host='websoft9-redis', port=6379)
|
2023-04-04 10:22:05 +08:00
|
|
|
|
|
|
|
|
|
# 使用指定的 Redis 连接创建 RQ 队列
|
|
|
|
|
q = Queue(connection=redis_conn)
|
|
|
|
|
|
2023-04-07 09:52:22 +08:00
|
|
|
|
|
2023-03-24 15:23:39 +08:00
|
|
|
|
# 获取所有app的信息
|
|
|
|
|
def get_my_app():
|
2023-03-24 16:07:53 +08:00
|
|
|
|
ret = Response(code=const.RETURN_FAIL, message="App query failed!")
|
2023-03-16 14:18:50 +08:00
|
|
|
|
|
|
|
|
|
# get all info
|
2023-03-24 13:22:27 +08:00
|
|
|
|
cmd = "docker compose ls -a --format json"
|
2023-03-16 14:18:50 +08:00
|
|
|
|
output = shell_execute.execute_command_output_all(cmd)
|
|
|
|
|
if int(output["code"]) == 0:
|
2023-03-24 13:22:27 +08:00
|
|
|
|
output_list = json.loads(output["result"])
|
2023-04-04 17:25:23 +08:00
|
|
|
|
installed_list, has_add = get_apps_from_compose(output_list)
|
|
|
|
|
installing_list = get_apps_from_queue()
|
|
|
|
|
app_list = installed_list + installing_list
|
2023-04-04 12:14:00 +08:00
|
|
|
|
ret = Response(code=const.RETURN_SUCCESS, message="The app query is successful.", data=app_list)
|
2023-03-16 14:18:50 +08:00
|
|
|
|
ret = ret.dict()
|
|
|
|
|
return ret
|
|
|
|
|
|
2023-03-25 20:31:44 +08:00
|
|
|
|
|
2023-03-24 15:23:39 +08:00
|
|
|
|
# 获取具体某个app的信息
|
|
|
|
|
def get_app_detail(app_id):
|
2023-03-27 15:34:00 +08:00
|
|
|
|
ret = {}
|
|
|
|
|
ret['code'] = const.RETURN_FAIL
|
|
|
|
|
ret['message'] = 'App query failed!'
|
|
|
|
|
ret['data'] = None
|
2023-02-21 14:21:53 +08:00
|
|
|
|
|
2023-03-27 16:26:03 +08:00
|
|
|
|
if docker.check_app_id(app_id):
|
|
|
|
|
# get all info
|
2023-03-31 16:44:16 +08:00
|
|
|
|
app_name = split_app_id(app_id)
|
|
|
|
|
info, code = if_app_exits(app_id)
|
|
|
|
|
if code:
|
|
|
|
|
cmd = "docker compose ls -a --format json"
|
|
|
|
|
output = shell_execute.execute_command_output_all(cmd)
|
|
|
|
|
if int(output["code"]) == 0:
|
|
|
|
|
output_list = json.loads(output["result"])
|
|
|
|
|
app_list, has_add = get_apps_from_compose(output_list)
|
|
|
|
|
flag = 0
|
|
|
|
|
app_info = None
|
2023-04-06 09:18:30 +08:00
|
|
|
|
for app in app_list:
|
2023-03-31 16:44:16 +08:00
|
|
|
|
if app["app_id"] == app_id:
|
|
|
|
|
app_info = app
|
|
|
|
|
flag = 1
|
|
|
|
|
break
|
|
|
|
|
if flag == 1:
|
|
|
|
|
ret['code'] = const.RETURN_SUCCESS
|
|
|
|
|
ret['message'] = "The app query is successful."
|
|
|
|
|
ret['data'] = app_info
|
2023-04-06 09:18:30 +08:00
|
|
|
|
else:
|
|
|
|
|
ret['message'] = 'This app is not currently installed.'
|
2023-03-31 16:44:16 +08:00
|
|
|
|
else:
|
|
|
|
|
ret['message'] = 'This app is not currently installed.'
|
2023-03-27 16:26:03 +08:00
|
|
|
|
else:
|
|
|
|
|
ret['message'] = "AppID is not legal!"
|
2023-03-27 15:34:00 +08:00
|
|
|
|
return ret
|
2023-02-23 16:19:55 +08:00
|
|
|
|
|
2023-03-25 20:31:44 +08:00
|
|
|
|
|
2023-03-27 10:24:54 +08:00
|
|
|
|
# 查询某个正在安装的app的 具体状态:waiting(等待安装)pulling(拉取镜像)initializing(初始化)running(正常运行)
|
2023-03-15 16:13:17 +08:00
|
|
|
|
def install_app_process(app_id):
|
2023-04-04 17:25:23 +08:00
|
|
|
|
ret = {}
|
|
|
|
|
ret['code'] = const.RETURN_FAIL
|
|
|
|
|
ret['message'] = ""
|
|
|
|
|
ret['status'] = ""
|
2023-03-15 16:13:17 +08:00
|
|
|
|
app_name = split_app_id(app_id)
|
2023-03-31 16:44:16 +08:00
|
|
|
|
if docker.check_app_id(app_id):
|
|
|
|
|
info, code = if_app_exits(app_id)
|
|
|
|
|
if code:
|
2023-04-07 09:18:47 +08:00
|
|
|
|
var_path = "/data/apps/" + app_name + "/variables.json"
|
|
|
|
|
real_name = docker.read_var(var_path, 'name')
|
2023-04-04 17:27:21 +08:00
|
|
|
|
app_status = docker.get_process_perc(app_name, real_name)
|
2023-04-04 17:25:23 +08:00
|
|
|
|
ret["code"] = const.RETURN_SUCCESS
|
|
|
|
|
ret['message'] = "This app is installing."
|
2023-04-04 17:27:21 +08:00
|
|
|
|
ret['status'] = app_status
|
2023-03-31 16:44:16 +08:00
|
|
|
|
else:
|
2023-04-04 17:25:23 +08:00
|
|
|
|
ret['message'] = "This app is not currently installed."
|
2023-03-09 10:33:26 +08:00
|
|
|
|
else:
|
2023-04-04 17:25:23 +08:00
|
|
|
|
ret['message'] = "AppID is not legal!"
|
2023-03-31 16:44:16 +08:00
|
|
|
|
|
|
|
|
|
ret = ret.dict()
|
2023-03-09 10:33:26 +08:00
|
|
|
|
return ret
|
|
|
|
|
|
2023-03-25 20:31:44 +08:00
|
|
|
|
|
2023-04-14 12:04:07 +08:00
|
|
|
|
def install_app(app_name, customer_name, app_version):
|
2023-04-04 10:22:05 +08:00
|
|
|
|
myLogger.info_logger("Install app ...")
|
2023-04-12 17:11:53 +08:00
|
|
|
|
ret = {}
|
|
|
|
|
ret['ResponseData'] = {}
|
2023-04-14 12:04:07 +08:00
|
|
|
|
app_id = app_name + "_" + customer_name
|
|
|
|
|
ret['ResponseData']['app_id'] = app_id
|
|
|
|
|
|
|
|
|
|
code, message, detail = check_app(app_name, customer_name, app_version)
|
|
|
|
|
|
|
|
|
|
q.enqueue(install_app_delay, app_name, customer_app_name, app_version, job_id=app_id, timeout=3600)
|
|
|
|
|
|
2023-03-21 15:14:35 +08:00
|
|
|
|
ret = ret.dict()
|
|
|
|
|
return ret
|
2023-02-28 17:44:04 +08:00
|
|
|
|
|
2023-03-25 20:31:44 +08:00
|
|
|
|
|
2023-03-15 16:13:17 +08:00
|
|
|
|
def start_app(app_id):
|
2023-03-07 16:55:23 +08:00
|
|
|
|
ret = Response(code=const.RETURN_FAIL, message="")
|
2023-03-27 16:26:03 +08:00
|
|
|
|
if docker.check_app_id(app_id):
|
|
|
|
|
app_name = split_app_id(app_id)
|
2023-03-31 16:44:16 +08:00
|
|
|
|
info, code = if_app_exits(app_id)
|
2023-03-28 16:07:39 +08:00
|
|
|
|
if code:
|
|
|
|
|
app_path = info.split()[-1].rsplit('/', 1)[0]
|
|
|
|
|
docker.check_app_compose(app_path + '/.env')
|
|
|
|
|
cmd = "docker compose -f " + app_path + "/docker-compose.yml start"
|
2023-03-27 16:26:03 +08:00
|
|
|
|
output = shell_execute.execute_command_output_all(cmd)
|
|
|
|
|
if int(output["code"]) == 0:
|
|
|
|
|
ret.code = const.RETURN_SUCCESS
|
|
|
|
|
ret.message = "The app starts successfully."
|
|
|
|
|
else:
|
|
|
|
|
ret.message = "The app failed to start!"
|
2023-02-28 17:44:04 +08:00
|
|
|
|
else:
|
2023-03-31 16:44:16 +08:00
|
|
|
|
ret.message = "This app is not currently installed."
|
2023-02-28 17:44:04 +08:00
|
|
|
|
else:
|
2023-03-27 16:26:03 +08:00
|
|
|
|
ret.message = "AppID is not legal!"
|
2023-03-07 16:55:23 +08:00
|
|
|
|
ret = ret.dict()
|
2023-02-28 17:44:04 +08:00
|
|
|
|
return ret
|
|
|
|
|
|
2023-04-07 09:52:22 +08:00
|
|
|
|
|
2023-03-15 16:13:17 +08:00
|
|
|
|
def stop_app(app_id):
|
2023-03-07 16:55:23 +08:00
|
|
|
|
ret = Response(code=const.RETURN_FAIL, message="")
|
2023-03-27 16:26:03 +08:00
|
|
|
|
if docker.check_app_id(app_id):
|
|
|
|
|
app_name = split_app_id(app_id)
|
2023-03-31 16:44:16 +08:00
|
|
|
|
info, code = if_app_exits(app_id)
|
2023-03-28 16:07:39 +08:00
|
|
|
|
if code:
|
|
|
|
|
app_path = info.split()[-1].rsplit('/', 1)[0]
|
|
|
|
|
cmd = "docker compose -f " + app_path + "/docker-compose.yml stop"
|
2023-03-27 16:26:03 +08:00
|
|
|
|
output = shell_execute.execute_command_output_all(cmd)
|
|
|
|
|
if int(output["code"]) == 0:
|
|
|
|
|
ret.code = const.RETURN_SUCCESS
|
|
|
|
|
ret.message = "The app stopped successfully."
|
|
|
|
|
else:
|
|
|
|
|
ret.message = "App stop failed!"
|
2023-02-28 17:44:04 +08:00
|
|
|
|
else:
|
2023-03-31 16:44:16 +08:00
|
|
|
|
ret.message = "This app is not currently installed."
|
2023-02-28 17:44:04 +08:00
|
|
|
|
else:
|
2023-03-27 16:26:03 +08:00
|
|
|
|
ret.message = 'AppID is not legal!'
|
2023-03-07 16:55:23 +08:00
|
|
|
|
ret = ret.dict()
|
2023-02-28 17:44:04 +08:00
|
|
|
|
return ret
|
|
|
|
|
|
2023-04-07 09:52:22 +08:00
|
|
|
|
|
2023-03-15 16:13:17 +08:00
|
|
|
|
def restart_app(app_id):
|
2023-03-07 16:55:23 +08:00
|
|
|
|
ret = Response(code=const.RETURN_FAIL, message="")
|
2023-03-27 16:26:03 +08:00
|
|
|
|
if docker.check_app_id(app_id):
|
|
|
|
|
app_name = split_app_id(app_id)
|
2023-03-31 16:44:16 +08:00
|
|
|
|
info, code = if_app_exits(app_id)
|
2023-03-28 16:07:39 +08:00
|
|
|
|
if code:
|
|
|
|
|
app_path = info.split()[-1].rsplit('/', 1)[0]
|
|
|
|
|
cmd = "docker compose -f " + app_path + "/docker-compose.yml restart"
|
2023-03-27 16:26:03 +08:00
|
|
|
|
output = shell_execute.execute_command_output_all(cmd)
|
|
|
|
|
if int(output["code"]) == 0:
|
|
|
|
|
ret.code = const.RETURN_SUCCESS
|
|
|
|
|
ret.message = "The app restarts successfully."
|
|
|
|
|
else:
|
|
|
|
|
ret.message = "App restart failed!"
|
2023-02-28 17:44:04 +08:00
|
|
|
|
else:
|
2023-03-31 16:44:16 +08:00
|
|
|
|
ret.message = "This app is not currently installed."
|
2023-02-28 17:44:04 +08:00
|
|
|
|
else:
|
2023-03-27 16:26:03 +08:00
|
|
|
|
ret.message = 'AppID is not legal!'
|
2023-03-07 16:55:23 +08:00
|
|
|
|
ret = ret.dict()
|
2023-02-28 17:44:04 +08:00
|
|
|
|
return ret
|
|
|
|
|
|
2023-03-15 08:59:06 +08:00
|
|
|
|
|
2023-04-12 17:11:53 +08:00
|
|
|
|
def uninstall_app(app_id, delete_image, delete_data):
|
2023-03-07 16:55:23 +08:00
|
|
|
|
ret = Response(code=const.RETURN_FAIL, message="")
|
2023-03-27 16:26:03 +08:00
|
|
|
|
if docker.check_app_id(app_id):
|
2023-03-30 14:31:18 +08:00
|
|
|
|
app_name = split_app_id(app_id)
|
2023-03-31 18:26:50 +08:00
|
|
|
|
if_stopped = stop_app(app_id) # stop_app
|
|
|
|
|
if if_stopped["code"] == 0:
|
2023-03-31 18:34:04 +08:00
|
|
|
|
info, code = if_app_exits(app_id)
|
|
|
|
|
app_path = info.split()[-1].rsplit('/', 1)[0]
|
2023-03-31 18:26:50 +08:00
|
|
|
|
cmd = "docker compose -f " + app_path + "/docker-compose.yml down -v"
|
|
|
|
|
lib_path = '/data/library/apps/' + app_name
|
|
|
|
|
if app_path != lib_path:
|
|
|
|
|
cmd = cmd + " && sudo rm -rf " + app_path
|
|
|
|
|
output = shell_execute.execute_command_output_all(cmd)
|
|
|
|
|
if int(output["code"]) == 0:
|
|
|
|
|
ret.code = 0
|
|
|
|
|
ret.message = "The app is deleted successfully"
|
2023-03-27 16:26:03 +08:00
|
|
|
|
else:
|
2023-03-31 18:26:50 +08:00
|
|
|
|
ret.message = "App deletion failed!"
|
2023-02-28 17:44:04 +08:00
|
|
|
|
else:
|
2023-03-31 18:26:50 +08:00
|
|
|
|
ret.message = if_stopped["message"]
|
|
|
|
|
else:
|
|
|
|
|
ret.message = 'AppID is not legal!'
|
2023-03-07 16:55:23 +08:00
|
|
|
|
ret = ret.dict()
|
2023-02-28 17:44:04 +08:00
|
|
|
|
return ret
|
2023-03-24 15:54:08 +08:00
|
|
|
|
|
2023-03-25 20:31:44 +08:00
|
|
|
|
|
2023-03-24 15:25:25 +08:00
|
|
|
|
def check_app(app_name, customer_app_name, app_version):
|
|
|
|
|
message = " "
|
2023-04-12 17:11:53 +08:00
|
|
|
|
code = None
|
2023-04-08 07:59:03 +08:00
|
|
|
|
app_id = app_name + "-" + customer_app_name
|
2023-04-12 17:11:53 +08:00
|
|
|
|
if not docker.check_app_websoft9(app_name):
|
|
|
|
|
code = 'Param.AppName.NotExis'
|
|
|
|
|
message = "不支持安装指定的App"
|
2023-03-25 20:31:44 +08:00
|
|
|
|
elif re.match('^[a-z0-9]+$', customer_app_name) == None:
|
2023-04-12 17:11:53 +08:00
|
|
|
|
code = 'Param.CustomerAppName.FormatError'
|
|
|
|
|
message = "用户自定义APP名称只能是数字和小写字母组成"
|
2023-03-25 20:50:13 +08:00
|
|
|
|
elif docker.check_directory("/data/apps/" + customer_app_name):
|
2023-04-12 17:11:53 +08:00
|
|
|
|
code = 'Param.CustomerAppName.Repeat'
|
|
|
|
|
message = "已经安装了此应用,请重新指定APP名称"
|
2023-03-24 15:25:25 +08:00
|
|
|
|
elif not docker.check_vm_resource(app_name):
|
2023-04-12 17:11:53 +08:00
|
|
|
|
code = 'Requirement.NotEnough'
|
|
|
|
|
message = "系统资源(cpu,内存,磁盘空间)不足"
|
2023-04-08 07:59:03 +08:00
|
|
|
|
elif check_app_wait(app_id):
|
2023-04-12 17:11:53 +08:00
|
|
|
|
code = 'Param.CustomerAppName.Wait'
|
|
|
|
|
message = "同名应用已经在安装等待中,请重新指定APP名称"
|
2023-03-24 15:25:25 +08:00
|
|
|
|
return code, message
|
2023-03-15 16:13:17 +08:00
|
|
|
|
|
|
|
|
|
|
2023-03-24 15:25:25 +08:00
|
|
|
|
def prepare_app(app_name, customer_app_name):
|
2023-03-24 16:02:35 +08:00
|
|
|
|
library_path = "/data/library/apps/" + app_name
|
2023-03-24 15:25:25 +08:00
|
|
|
|
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:
|
2023-03-24 16:07:53 +08:00
|
|
|
|
message = "creating" + customer_app_name + "directory failed!"
|
2023-03-24 15:25:25 +08:00
|
|
|
|
code = const.RETURN_FAIL
|
|
|
|
|
return code, message
|
|
|
|
|
|
2023-04-07 09:52:22 +08:00
|
|
|
|
def install_app_delay(app_name, customer_app_name, app_version):
|
2023-04-06 14:37:26 +08:00
|
|
|
|
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)
|
2023-04-06 14:10:32 +08:00
|
|
|
|
|
2023-04-06 14:37:26 +08:00
|
|
|
|
cmd = "cd /data/apps/" + customer_app_name + " && sudo docker compose pull && sudo docker compose up -d"
|
|
|
|
|
output = shell_execute.execute_command_output_all(cmd)
|
2023-04-06 16:38:40 +08:00
|
|
|
|
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"]:
|
2023-04-07 09:52:22 +08:00
|
|
|
|
raise Exception("installfailed!")
|
2023-04-06 16:38:40 +08:00
|
|
|
|
else:
|
2023-04-07 09:52:22 +08:00
|
|
|
|
return "success"
|
2023-04-06 14:37:26 +08:00
|
|
|
|
else:
|
|
|
|
|
raise Exception("prepare_app failed")
|
|
|
|
|
else:
|
2023-04-06 14:10:32 +08:00
|
|
|
|
raise Exception("resource check failed")
|
2023-04-06 14:37:26 +08:00
|
|
|
|
except Exception as e:
|
2023-04-07 09:52:22 +08:00
|
|
|
|
myLogger.info_logger(customer_app_name + "install failed!")
|
2023-04-06 14:37:26 +08:00
|
|
|
|
myLogger.error_logger(e)
|
|
|
|
|
job_id = app_name + "_" + customer_app_name
|
2023-04-06 16:38:40 +08:00
|
|
|
|
try:
|
|
|
|
|
uninstall_app(job_id)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
myLogger.error_logger(e)
|
2023-04-12 15:17:02 +08:00
|
|
|
|
raise Exception(e)
|
2023-04-04 17:25:23 +08:00
|
|
|
|
|
2023-04-07 09:52:22 +08:00
|
|
|
|
|
2023-03-31 16:44:16 +08:00
|
|
|
|
def if_app_exits(app_id):
|
|
|
|
|
app_name = app_id.split('_')[1]
|
2023-03-31 18:15:01 +08:00
|
|
|
|
real_name = app_id.split('_')[0]
|
2023-03-31 16:44:16 +08:00
|
|
|
|
flag = False
|
2023-03-28 16:07:39 +08:00
|
|
|
|
info = ""
|
|
|
|
|
cmd = "docker compose ls -a | grep \'/" + app_name + "/\'"
|
2023-03-24 15:25:25 +08:00
|
|
|
|
output = shell_execute.execute_command_output_all(cmd)
|
2023-03-31 16:44:16 +08:00
|
|
|
|
if int(output["code"]) == 0:
|
2023-03-28 17:31:06 +08:00
|
|
|
|
info = output["result"]
|
2023-03-31 16:44:16 +08:00
|
|
|
|
app_path = info.split()[-1].rsplit('/', 1)[0]
|
|
|
|
|
is_official = check_if_official_app(app_path + '/variables.json')
|
|
|
|
|
if is_official:
|
|
|
|
|
name = docker.read_var(app_path + '/variables.json', 'name')
|
|
|
|
|
if name == real_name:
|
|
|
|
|
flag = True
|
|
|
|
|
elif real_name == app_name:
|
2023-04-04 17:25:23 +08:00
|
|
|
|
flag = True
|
2023-03-31 16:44:16 +08:00
|
|
|
|
myLogger.info_logger("APP info: " + info)
|
|
|
|
|
return info, flag
|
2023-03-24 15:25:25 +08:00
|
|
|
|
|
2023-03-15 16:13:17 +08:00
|
|
|
|
def split_app_id(app_id):
|
2023-03-16 10:41:28 +08:00
|
|
|
|
return app_id.split("_")[1]
|
2023-03-24 15:23:39 +08:00
|
|
|
|
|
2023-03-27 10:24:54 +08:00
|
|
|
|
def get_apps_from_compose(output_list):
|
2023-03-24 15:23:39 +08:00
|
|
|
|
ip_result = shell_execute.execute_command_output_all("curl ifconfig.me")
|
|
|
|
|
ip = ip_result["result"]
|
|
|
|
|
app_list = []
|
|
|
|
|
has_add = []
|
|
|
|
|
for app_info in output_list:
|
|
|
|
|
volume = app_info["ConfigFiles"] # volume
|
2023-03-28 16:07:39 +08:00
|
|
|
|
app_path = volume.rsplit('/', 1)[0]
|
|
|
|
|
app_name = volume.split('/')[-2]
|
2023-03-29 14:48:18 +08:00
|
|
|
|
app_id = app_name + "_" + app_name # app_id
|
|
|
|
|
real_name = ""
|
|
|
|
|
trade_mark = ""
|
|
|
|
|
port = 0
|
|
|
|
|
url = ""
|
|
|
|
|
admin_url = ""
|
|
|
|
|
image_url = ""
|
|
|
|
|
user_name = ""
|
|
|
|
|
password = ""
|
|
|
|
|
official_app = False
|
|
|
|
|
|
2023-04-07 09:52:22 +08:00
|
|
|
|
if app_name in ['appmanage', 'nginxproxymanager',
|
|
|
|
|
'redis'] and app_path == '/data/apps/stackhub/docker/' + app_name:
|
2023-03-29 14:48:18 +08:00
|
|
|
|
continue
|
|
|
|
|
# get code
|
2023-03-24 15:23:39 +08:00
|
|
|
|
case = app_info["Status"].split("(")[0] # case
|
|
|
|
|
if case == "running":
|
2023-03-30 15:16:11 +08:00
|
|
|
|
case_code = const.APP_RUNNING # case_code
|
2023-03-24 15:23:39 +08:00
|
|
|
|
elif case == "exited":
|
|
|
|
|
case = "stop"
|
2023-03-30 15:16:11 +08:00
|
|
|
|
case_code = const.APP_STOP
|
2023-03-24 15:23:39 +08:00
|
|
|
|
elif case == "created":
|
2023-03-30 15:16:11 +08:00
|
|
|
|
case_code = const.APP_READY
|
2023-03-24 15:23:39 +08:00
|
|
|
|
case = "installing"
|
|
|
|
|
else:
|
2023-03-30 15:16:11 +08:00
|
|
|
|
case_code = const.APP_ERROR
|
2023-03-29 14:48:18 +08:00
|
|
|
|
|
|
|
|
|
var_path = app_path + "/variables.json"
|
|
|
|
|
official_app = check_if_official_app(var_path)
|
|
|
|
|
if official_app:
|
|
|
|
|
real_name = docker.read_var(var_path, 'name')
|
2023-03-29 14:52:37 +08:00
|
|
|
|
app_id = real_name + "_" + app_name # app_id
|
2023-03-29 14:48:18 +08:00
|
|
|
|
# get trade_mark
|
|
|
|
|
trade_mark = docker.read_var(var_path, 'trademark')
|
|
|
|
|
image_url = get_Image_url(real_name)
|
|
|
|
|
# get env info
|
|
|
|
|
path = app_path + "/.env"
|
|
|
|
|
# get port and url
|
|
|
|
|
try:
|
|
|
|
|
http_port = list(docker.read_env(
|
|
|
|
|
path, "APP_HTTP_PORT").values())[0]
|
|
|
|
|
port = int(http_port)
|
|
|
|
|
easy_url = "http://" + ip + ":" + str(port)
|
|
|
|
|
url = get_url(real_name, easy_url)
|
|
|
|
|
admin_url = get_admin_url(real_name, url)
|
|
|
|
|
except IndexError:
|
|
|
|
|
try:
|
|
|
|
|
db_port = list(docker.read_env(
|
|
|
|
|
path, "APP_DB.*_PORT").values())[0]
|
|
|
|
|
port = int(db_port)
|
|
|
|
|
except IndexError:
|
|
|
|
|
pass
|
|
|
|
|
# get user_name
|
|
|
|
|
try:
|
|
|
|
|
user_name = list(docker.read_env(path, "APP_USER").values())[0]
|
|
|
|
|
except IndexError:
|
|
|
|
|
pass
|
|
|
|
|
# get password
|
2023-03-24 15:23:39 +08:00
|
|
|
|
try:
|
2023-03-29 14:48:18 +08:00
|
|
|
|
password = list(docker.read_env(
|
|
|
|
|
path, "POWER_PASSWORD").values())[0]
|
2023-03-24 15:23:39 +08:00
|
|
|
|
except IndexError:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
has_add.append(app_name)
|
2023-03-25 20:31:44 +08:00
|
|
|
|
app = App(app_id=app_id, name=real_name, customer_name=app_name, status_code=case_code, status=case, port=port,
|
|
|
|
|
volume=volume, url=url,
|
|
|
|
|
image_url=image_url, admin_url=admin_url, trade_mark=trade_mark, user_name=user_name,
|
2023-03-28 16:07:39 +08:00
|
|
|
|
password=password, official_app=official_app)
|
2023-03-24 15:23:39 +08:00
|
|
|
|
app_list.append(app.dict())
|
2023-03-27 10:24:54 +08:00
|
|
|
|
return app_list, has_add
|
2023-03-24 15:23:39 +08:00
|
|
|
|
|
2023-03-27 10:24:54 +08:00
|
|
|
|
|
2023-03-29 14:48:18 +08:00
|
|
|
|
def check_if_official_app(var_path):
|
|
|
|
|
if docker.check_directory(var_path):
|
|
|
|
|
if docker.read_var(var_path, 'name') != "" and docker.read_var(var_path, 'trademark') != "" and docker.read_var(
|
|
|
|
|
var_path, 'requirements') != "":
|
2023-03-30 10:22:26 +08:00
|
|
|
|
requirements = docker.read_var(var_path, 'requirements')
|
|
|
|
|
try:
|
|
|
|
|
cpu = requirements['cpu']
|
|
|
|
|
mem = requirements['memory']
|
|
|
|
|
return True
|
|
|
|
|
except:
|
|
|
|
|
return False
|
2023-03-29 14:48:18 +08:00
|
|
|
|
else:
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
2023-04-08 07:59:03 +08:00
|
|
|
|
def check_app_wait(app_id):
|
|
|
|
|
myLogger.info_logger("check_app_wait")
|
|
|
|
|
deferred = DeferredJobRegistry(queue=q)
|
|
|
|
|
wait_job_ids = deferred.get_job_ids()
|
|
|
|
|
if app_id in wait_job_ids:
|
|
|
|
|
return True
|
|
|
|
|
else:
|
|
|
|
|
return False
|
|
|
|
|
|
2023-04-04 12:14:00 +08:00
|
|
|
|
def get_apps_from_queue():
|
2023-04-06 07:57:10 +08:00
|
|
|
|
myLogger.info_logger("get queque apps...")
|
2023-04-04 12:14:00 +08:00
|
|
|
|
# 获取 StartedJobRegistry 实例
|
|
|
|
|
registry = StartedJobRegistry(queue=q)
|
|
|
|
|
finish = FinishedJobRegistry(queue=q)
|
|
|
|
|
deferred = DeferredJobRegistry(queue=q)
|
2023-04-06 13:06:41 +08:00
|
|
|
|
failed = FailedJobRegistry(queue=q)
|
|
|
|
|
scheduled = ScheduledJobRegistry(queue=q)
|
|
|
|
|
|
2023-04-04 12:14:00 +08:00
|
|
|
|
# 获取正在执行的作业 ID 列表
|
|
|
|
|
run_job_ids = registry.get_job_ids()
|
|
|
|
|
finish_job_ids = finish.get_job_ids()
|
|
|
|
|
wait_job_ids = deferred.get_job_ids()
|
2023-04-06 13:06:41 +08:00
|
|
|
|
failed_jobs = failed.get_job_ids()
|
|
|
|
|
scheduled_jobs = scheduled.get_job_ids()
|
|
|
|
|
|
|
|
|
|
myLogger.info_logger(q.jobs)
|
2023-04-06 07:57:10 +08:00
|
|
|
|
myLogger.info_logger(wait_job_ids)
|
|
|
|
|
myLogger.info_logger(run_job_ids)
|
|
|
|
|
myLogger.info_logger(finish_job_ids)
|
2023-04-07 09:52:22 +08:00
|
|
|
|
myLogger.info_logger(failed_jobs)
|
2023-04-06 13:06:41 +08:00
|
|
|
|
myLogger.info_logger(scheduled_jobs)
|
2023-04-07 09:52:22 +08:00
|
|
|
|
|
2023-04-06 14:20:17 +08:00
|
|
|
|
myLogger.info_logger("----------------------------------------")
|
2023-04-06 16:38:40 +08:00
|
|
|
|
|
2023-04-04 17:25:23 +08:00
|
|
|
|
installing_list = []
|
2023-04-06 16:38:40 +08:00
|
|
|
|
for job in run_job_ids:
|
|
|
|
|
app = get_installing_app(job, const.APP_READY, 'installing')
|
2023-04-04 17:25:23 +08:00
|
|
|
|
installing_list.append(app)
|
2023-04-07 09:52:22 +08:00
|
|
|
|
for job in q.jobs:
|
2023-04-06 14:20:17 +08:00
|
|
|
|
app = get_installing_app(job.id, const.APP_WAIT, 'waiting')
|
2023-04-04 17:25:23 +08:00
|
|
|
|
installing_list.append(app)
|
2023-04-06 16:38:40 +08:00
|
|
|
|
for job_id in finish_job_ids:
|
|
|
|
|
job = q.fetch_job(job_id)
|
|
|
|
|
if job.result == "fail":
|
2023-04-07 09:52:22 +08:00
|
|
|
|
app = get_installing_app(job_id, const.APP_ERROR, 'error')
|
|
|
|
|
installing_list.append(app)
|
|
|
|
|
|
2023-04-04 17:25:23 +08:00
|
|
|
|
return installing_list
|
|
|
|
|
|
|
|
|
|
def get_installing_app(id, status_code, status):
|
|
|
|
|
real_name = id.split('_')[0]
|
|
|
|
|
app_name = id.split('_')[1]
|
|
|
|
|
var_path = "/data/apps/" + app_name + "/variables.json"
|
|
|
|
|
trade_mark = docker.read_var(var_path, 'trademark')
|
|
|
|
|
real_name = docker.read_var(var_path, 'name')
|
|
|
|
|
image_url = get_Image_url(real_name)
|
|
|
|
|
app = App(app_id=real_name + "_" + app_name, name=real_name, customer_name=app_name,
|
|
|
|
|
status_code=status_code, status=status, port=0, volume="",
|
|
|
|
|
url="", image_url=image_url, admin_url="", trade_mark=trade_mark, user_name="",
|
|
|
|
|
password="", official_app=True)
|
|
|
|
|
return app
|
2023-03-24 15:23:39 +08:00
|
|
|
|
|
2023-03-25 20:31:44 +08:00
|
|
|
|
def get_Image_url(app_name):
|
2023-03-30 13:19:54 +08:00
|
|
|
|
image_url = "static/images/" + app_name + "-websoft9.png"
|
2023-03-24 15:23:39 +08:00
|
|
|
|
return image_url
|
|
|
|
|
|
|
|
|
|
|
2023-03-25 20:31:44 +08:00
|
|
|
|
def get_url(app_name, easy_url):
|
2023-03-24 15:23:39 +08:00
|
|
|
|
url = easy_url
|
|
|
|
|
if app_name == "joomla":
|
|
|
|
|
url = easy_url + "/administrator"
|
|
|
|
|
elif app_name == "other":
|
|
|
|
|
url = easy_url + "/administrator"
|
|
|
|
|
else:
|
|
|
|
|
url = easy_url
|
|
|
|
|
return url
|
|
|
|
|
|
2023-04-04 17:25:23 +08:00
|
|
|
|
|
2023-03-24 15:23:39 +08:00
|
|
|
|
def get_admin_url(app_name, url):
|
2023-03-29 14:48:18 +08:00
|
|
|
|
admin_url = ""
|
2023-03-24 15:23:39 +08:00
|
|
|
|
if app_name == "wordpress":
|
|
|
|
|
admin_url = url + "/wp-admin"
|
|
|
|
|
elif app_name == "other":
|
|
|
|
|
admin_url = url + "/admin"
|
|
|
|
|
else:
|
2023-03-29 14:48:18 +08:00
|
|
|
|
admin_url = ""
|
2023-03-24 15:23:39 +08:00
|
|
|
|
return admin_url
|
2023-04-14 09:27:08 +08:00
|
|
|
|
|
|
|
|
|
|
2023-04-14 10:51:29 +08:00
|
|
|
|
def get_error_info(code, message, detail):
|
2023-04-14 09:27:08 +08:00
|
|
|
|
error = {}
|
2023-04-14 10:51:29 +08:00
|
|
|
|
error['Code'] = code
|
|
|
|
|
error['Message'] = message
|
|
|
|
|
error['Detail'] = detail
|
2023-04-14 09:27:08 +08:00
|
|
|
|
return error
|