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-02-27 11:07:25 +08:00
|
|
|
|
from threading import Thread
|
2023-03-07 16:55:23 +08:00
|
|
|
|
from api.utils import shell_execute, network, 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-02-21 14:21:53 +08:00
|
|
|
|
|
2023-02-22 17:45:54 +08:00
|
|
|
|
# 获取所有app的信息
|
2023-02-24 10:23:32 +08:00
|
|
|
|
def get_my_app(app_name=None):
|
2023-03-15 08:59:06 +08:00
|
|
|
|
|
2023-03-07 16:55:23 +08:00
|
|
|
|
ret = Response(code=const.RETURN_FAIL, message="app查询失败")
|
2023-02-21 14:21:53 +08:00
|
|
|
|
|
2023-02-24 10:23:32 +08:00
|
|
|
|
# get all info
|
2023-03-07 16:55:23 +08:00
|
|
|
|
cmd = "sudo docker compose ls -a"
|
2023-02-24 10:23:32 +08:00
|
|
|
|
output = shell_execute.execute_command_output_all(cmd)
|
|
|
|
|
if int(output["code"]) == 0:
|
2023-03-13 15:37:47 +08:00
|
|
|
|
output_list = output["result"].split("\n")
|
2023-02-24 10:23:32 +08:00
|
|
|
|
list = []
|
2023-03-13 15:37:47 +08:00
|
|
|
|
output_list = output_list[1:-1]
|
|
|
|
|
list = set_app_info(output_list)
|
2023-02-24 10:23:32 +08:00
|
|
|
|
flag = 0
|
|
|
|
|
if app_name != None:
|
|
|
|
|
for app in list:
|
|
|
|
|
if app["name"] == app_name:
|
|
|
|
|
list.clear()
|
|
|
|
|
list.append(app)
|
|
|
|
|
flag = 1
|
|
|
|
|
break
|
|
|
|
|
if app_name == None or flag == 1:
|
2023-03-15 08:59:06 +08:00
|
|
|
|
ret = Response(code=const.RETURN_SUCCESS,
|
|
|
|
|
message="app查询成功", data=list)
|
2023-03-07 16:55:23 +08:00
|
|
|
|
ret = ret.dict()
|
2023-02-23 16:19:55 +08:00
|
|
|
|
return ret
|
|
|
|
|
|
2023-03-15 08:59:06 +08:00
|
|
|
|
|
2023-03-13 15:37:47 +08:00
|
|
|
|
def set_app_info(output_list):
|
2023-03-08 17:10:59 +08:00
|
|
|
|
ip_result = shell_execute.execute_command_output_all("curl ifconfig.me")
|
|
|
|
|
ip = ip_result["result"]
|
2023-03-10 16:47:11 +08:00
|
|
|
|
app_list = []
|
2023-03-13 15:37:47 +08:00
|
|
|
|
for app_info in output_list:
|
|
|
|
|
app_name = app_info.split()[0] # app_name
|
2023-03-14 17:58:39 +08:00
|
|
|
|
|
2023-03-15 09:41:39 +08:00
|
|
|
|
real_name = docker.read_var(app_name, 'name')
|
|
|
|
|
image_url = "https://libs.websoft9.com/Websoft9/logo/product/" + real_name + "-websoft9.png"
|
2023-03-08 17:10:59 +08:00
|
|
|
|
# get trade_mark
|
2023-03-14 12:57:34 +08:00
|
|
|
|
trade_mark = docker.read_var(app_name, 'trademark')
|
2023-03-15 11:57:52 +08:00
|
|
|
|
app_id = real_name + "_" + app_name # app_id
|
2023-03-13 15:37:47 +08:00
|
|
|
|
case = app_info.split()[1].split("(")[0] # case
|
2023-03-09 11:30:22 +08:00
|
|
|
|
if case == "running":
|
2023-03-08 17:10:59 +08:00
|
|
|
|
case_code = const.RETURN_RUNNING # case_code
|
2023-03-09 11:30:22 +08:00
|
|
|
|
elif case == "exited":
|
2023-03-08 17:10:59 +08:00
|
|
|
|
case = "stop"
|
|
|
|
|
case_code = const.RETURN_STOP
|
2023-03-10 17:49:29 +08:00
|
|
|
|
elif case == "installing":
|
2023-03-09 11:30:22 +08:00
|
|
|
|
case_code = const.RETURN_READY
|
|
|
|
|
else:
|
|
|
|
|
case_code = const.RETURN_ERROR
|
2023-03-13 15:37:47 +08:00
|
|
|
|
volume = app_info.split()[-1] # volume
|
2023-03-08 17:10:59 +08:00
|
|
|
|
# get env info
|
2023-03-10 16:47:11 +08:00
|
|
|
|
path = "/data/apps/" + app_name + "/.env"
|
2023-03-08 17:10:59 +08:00
|
|
|
|
port = 0
|
|
|
|
|
url = "-"
|
2023-03-10 08:23:24 +08:00
|
|
|
|
admin_url = "-"
|
2023-03-10 16:47:11 +08:00
|
|
|
|
# get port and url
|
|
|
|
|
try:
|
2023-03-15 08:59:06 +08:00
|
|
|
|
http_port = list(docker.read_env(
|
|
|
|
|
path, "APP_HTTP_PORT").values())[0]
|
2023-03-08 17:10:59 +08:00
|
|
|
|
port = int(http_port)
|
2023-03-10 08:23:24 +08:00
|
|
|
|
easy_url = "http://" + ip + ":" + str(port)
|
2023-03-15 09:41:39 +08:00
|
|
|
|
url = get_url(real_name, easy_url)
|
|
|
|
|
admin_url = get_admin_url(real_name, url)
|
2023-03-10 16:47:11 +08:00
|
|
|
|
except IndexError:
|
|
|
|
|
try:
|
2023-03-15 08:59:06 +08:00
|
|
|
|
db_port = list(docker.read_env(
|
|
|
|
|
path, "APP_DB.*_PORT").values())[0]
|
2023-03-10 16:47:11 +08:00
|
|
|
|
port = int(db_port)
|
|
|
|
|
except IndexError:
|
|
|
|
|
pass
|
2023-03-08 17:10:59 +08:00
|
|
|
|
# get user_name
|
2023-03-10 16:47:11 +08:00
|
|
|
|
user_name = "-"
|
|
|
|
|
try:
|
|
|
|
|
user_name = list(docker.read_env(path, "APP_USER").values())[0]
|
|
|
|
|
except IndexError:
|
|
|
|
|
pass
|
2023-03-08 17:10:59 +08:00
|
|
|
|
# get password
|
2023-03-10 16:47:11 +08:00
|
|
|
|
password = "-"
|
|
|
|
|
try:
|
2023-03-15 08:59:06 +08:00
|
|
|
|
password = list(docker.read_env(
|
|
|
|
|
path, "POWER_PASSWORD").values())[0]
|
2023-03-10 16:47:11 +08:00
|
|
|
|
except IndexError:
|
|
|
|
|
pass
|
2023-03-08 17:10:59 +08:00
|
|
|
|
|
2023-03-15 11:57:52 +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,
|
2023-03-10 08:23:24 +08:00
|
|
|
|
image_url=image_url, admin_url=admin_url, trade_mark=trade_mark, user_name=user_name, password=password)
|
2023-03-10 16:47:11 +08:00
|
|
|
|
app_list.append(app.dict())
|
|
|
|
|
|
|
|
|
|
file_path = "/data/apps/running_apps.txt"
|
|
|
|
|
if os.path.exists(file_path) and os.path.getsize(file_path):
|
|
|
|
|
with open(file_path, "r", encoding="utf-8") as f:
|
|
|
|
|
for running_app_name in f:
|
2023-03-15 08:59:06 +08:00
|
|
|
|
image_url = "https://libs.websoft9.com/Websoft9/logo/product/" + \
|
|
|
|
|
running_app_name + "-websoft9.png"
|
2023-03-15 10:05:44 +08:00
|
|
|
|
trade_mark = docker.read_var(running_app_name, 'trademark')
|
2023-03-15 11:57:52 +08:00
|
|
|
|
real_name = docker.read_var(running_app_name, 'name')
|
|
|
|
|
app = App(app_id=real_name + "_" + running_app_name, name=real_name, customer_name=running_app_name, status_code=const.RETURN_READY, status="installing", port=0, volume="-",
|
2023-03-15 08:59:06 +08:00
|
|
|
|
url="-", image_url=image_url, admin_url="-", trade_mark=trade_mark, user_name="-", password="-")
|
2023-03-10 16:47:11 +08:00
|
|
|
|
app_list.append(app.dict())
|
|
|
|
|
return app_list
|
2023-03-08 17:10:59 +08:00
|
|
|
|
|
2023-03-15 08:59:06 +08:00
|
|
|
|
|
|
|
|
|
def get_url(app_name, easy_url):
|
|
|
|
|
|
2023-03-10 08:31:01 +08:00
|
|
|
|
url = easy_url
|
2023-03-10 08:23:24 +08:00
|
|
|
|
if app_name == "joomla":
|
2023-03-10 08:31:01 +08:00
|
|
|
|
url = easy_url + "/administrator"
|
|
|
|
|
elif app_name == "other":
|
|
|
|
|
url = easy_url + "/administrator"
|
2023-03-10 08:23:24 +08:00
|
|
|
|
else:
|
|
|
|
|
url = easy_url
|
2023-03-10 08:31:01 +08:00
|
|
|
|
return url
|
2023-03-10 08:23:24 +08:00
|
|
|
|
|
2023-03-15 08:59:06 +08:00
|
|
|
|
|
|
|
|
|
def get_admin_url(app_name, url):
|
|
|
|
|
|
2023-03-10 08:23:24 +08:00
|
|
|
|
admin_url = "-"
|
|
|
|
|
if app_name == "wordpress":
|
|
|
|
|
admin_url = url + "/wp-admin"
|
2023-03-10 08:31:01 +08:00
|
|
|
|
elif app_name == "other":
|
2023-03-10 08:23:24 +08:00
|
|
|
|
admin_url = url + "/admin"
|
|
|
|
|
else:
|
|
|
|
|
admin_url = "-"
|
|
|
|
|
return admin_url
|
|
|
|
|
|
2023-03-15 08:59:06 +08:00
|
|
|
|
|
2023-03-09 10:33:26 +08:00
|
|
|
|
def install_app_process(app_name):
|
|
|
|
|
|
2023-03-15 10:26:52 +08:00
|
|
|
|
real_name = docker.read_var(app_name, 'name')
|
2023-03-09 10:33:26 +08:00
|
|
|
|
if docker.check_app_directory(app_name):
|
2023-03-15 10:26:52 +08:00
|
|
|
|
percentage = docker.get_process_perc(app_name, real_name)
|
2023-03-09 11:08:24 +08:00
|
|
|
|
ret = Response(code=const.RETURN_SUCCESS, message=percentage)
|
2023-03-09 10:33:26 +08:00
|
|
|
|
ret = ret.dict()
|
|
|
|
|
else:
|
2023-03-15 08:59:06 +08:00
|
|
|
|
ret = Response(code=const.RETURN_FAIL, message="目前没有安装此App")
|
2023-03-09 10:33:26 +08:00
|
|
|
|
ret = ret.dict()
|
|
|
|
|
return ret
|
|
|
|
|
|
2023-03-14 17:24:29 +08:00
|
|
|
|
|
2023-03-15 15:24:24 +08:00
|
|
|
|
def install_app(app_name, customer_app_name, app_version):
|
2023-03-15 08:59:06 +08:00
|
|
|
|
|
2023-03-15 15:24:24 +08:00
|
|
|
|
if docker.check_vm_resource(app_name) == False:
|
|
|
|
|
ret = Response(code=const.RETURN_FAIL , message="系统资源(内存、CPU、磁盘)不足,继续安装可能导致应用无法运行或服务器异常!")
|
|
|
|
|
ret = ret.dict()
|
|
|
|
|
return ret
|
2023-03-14 17:24:29 +08:00
|
|
|
|
|
2023-03-14 12:57:34 +08:00
|
|
|
|
app_file_path = '/data/apps/'+app_name
|
|
|
|
|
running_file_path = "/data/apps/running_apps.txt"
|
2023-03-14 10:13:36 +08:00
|
|
|
|
unique_app_path = "/data/apps/"+customer_app_name
|
2023-03-14 15:12:38 +08:00
|
|
|
|
|
|
|
|
|
if os.path.exists(running_file_path) and os.path.getsize(running_file_path):
|
|
|
|
|
ret = Response(code=const.RETURN_SUCCESS, message="已有应用正在启动,请稍后再试")
|
|
|
|
|
ret = ret.dict()
|
|
|
|
|
|
2023-03-14 10:17:09 +08:00
|
|
|
|
# 防止app名重复
|
2023-03-14 15:12:38 +08:00
|
|
|
|
if if_app_exits(customer_app_name):
|
2023-03-15 08:59:06 +08:00
|
|
|
|
ret = Response(code=const.RETURN_FAIL,
|
|
|
|
|
message="APP名称已经使用,请指定其他名称重新安装。")
|
2023-03-14 10:17:09 +08:00
|
|
|
|
ret = ret.dict()
|
|
|
|
|
return ret
|
2023-03-13 17:50:27 +08:00
|
|
|
|
|
2023-03-10 16:47:11 +08:00
|
|
|
|
elif docker.check_app_directory(app_name):
|
2023-03-15 08:59:06 +08:00
|
|
|
|
|
2023-03-13 17:50:27 +08:00
|
|
|
|
if app_name != customer_app_name:
|
2023-03-15 08:59:06 +08:00
|
|
|
|
output = shell_execute.execute_command_output_all(
|
|
|
|
|
"cp -r /data/apps/" + app_name + " /data/apps/" + customer_app_name)
|
2023-03-13 17:50:27 +08:00
|
|
|
|
if int(output["code"]) != 0:
|
|
|
|
|
ret.code = const.RETURN_FAIL
|
|
|
|
|
ret.message = "创建" + customer_app_name + "目录失败."
|
|
|
|
|
ret = ret.dict()
|
|
|
|
|
return ret
|
2023-03-15 09:30:51 +08:00
|
|
|
|
env_file = unique_app_path + '/.env'
|
|
|
|
|
docker.modify_env(env_file, 'APP_NAME', customer_app_name)
|
|
|
|
|
|
2023-03-08 16:30:34 +08:00
|
|
|
|
# check port
|
2023-03-13 17:50:27 +08:00
|
|
|
|
docker.check_app_compose(customer_app_name)
|
2023-03-09 11:30:22 +08:00
|
|
|
|
if app_version != None:
|
2023-03-13 17:50:27 +08:00
|
|
|
|
path = "/data/apps/"+customer_app_name+"/.env"
|
2023-03-09 11:30:22 +08:00
|
|
|
|
docker.modify_env(path, "APP_VERSION", app_version)
|
2023-03-13 17:50:27 +08:00
|
|
|
|
t1 = Thread(target=record_and_install_app, args=(customer_app_name,))
|
2023-03-08 16:30:34 +08:00
|
|
|
|
t1.start()
|
|
|
|
|
ret = Response(code=const.RETURN_SUCCESS, message="应用正在启动中,请过几分钟再查询")
|
|
|
|
|
ret = ret.dict()
|
|
|
|
|
else:
|
2023-03-15 08:59:06 +08:00
|
|
|
|
ret = Response(code=const.RETURN_FAIL, message="目前不支持安装此App")
|
2023-03-09 08:40:51 +08:00
|
|
|
|
ret = ret.dict()
|
2023-02-23 16:19:55 +08:00
|
|
|
|
return ret
|
2023-02-28 17:44:04 +08:00
|
|
|
|
|
2023-03-15 08:59:06 +08:00
|
|
|
|
|
2023-03-10 16:47:11 +08:00
|
|
|
|
def record_and_install_app(app_name):
|
|
|
|
|
# modify 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"
|
|
|
|
|
shell_execute.execute_command_output_all(cmd)
|
|
|
|
|
with open(file_path, "a+", encoding="utf-8") as f:
|
|
|
|
|
f.truncate(0)
|
|
|
|
|
|
2023-03-15 08:59:06 +08:00
|
|
|
|
|
2023-02-28 17:44:04 +08:00
|
|
|
|
def if_app_exits(app_name):
|
|
|
|
|
cmd = "docker compose ls -a | grep \'"+app_name+"\\b\'"
|
|
|
|
|
output = shell_execute.execute_command_output_all(cmd)
|
|
|
|
|
if int(output["code"]) == -1:
|
|
|
|
|
return False
|
|
|
|
|
else:
|
|
|
|
|
return True
|
|
|
|
|
|
2023-03-15 08:59:06 +08:00
|
|
|
|
|
2023-03-15 11:40:19 +08:00
|
|
|
|
def start_app(app_name):
|
2023-03-07 16:55:23 +08:00
|
|
|
|
ret = Response(code=const.RETURN_FAIL, message="")
|
2023-02-28 17:44:04 +08:00
|
|
|
|
if if_app_exits(app_name):
|
2023-03-08 10:08:46 +08:00
|
|
|
|
docker.check_app_compose(app_name)
|
2023-03-06 16:54:17 +08:00
|
|
|
|
cmd = "docker compose -f /data/apps/"+app_name+"/docker-compose.yml start"
|
2023-02-28 17:44:04 +08:00
|
|
|
|
output = shell_execute.execute_command_output_all(cmd)
|
|
|
|
|
if int(output["code"]) == 0:
|
2023-03-07 16:55:23 +08:00
|
|
|
|
ret.code = const.RETURN_SUCCESS
|
|
|
|
|
ret.message = "应用启动成功"
|
2023-02-28 17:44:04 +08:00
|
|
|
|
else:
|
2023-03-07 16:55:23 +08:00
|
|
|
|
ret.message = "应用启动失败"
|
2023-02-28 17:44:04 +08:00
|
|
|
|
else:
|
2023-03-07 16:55:23 +08:00
|
|
|
|
ret.message = "应用不存在"
|
|
|
|
|
ret = ret.dict()
|
2023-02-28 17:44:04 +08:00
|
|
|
|
return ret
|
|
|
|
|
|
2023-03-15 08:59:06 +08:00
|
|
|
|
|
2023-03-15 11:40:19 +08:00
|
|
|
|
def stop_app(app_name):
|
2023-03-07 16:55:23 +08:00
|
|
|
|
ret = Response(code=const.RETURN_FAIL, message="")
|
2023-02-28 17:44:04 +08:00
|
|
|
|
if if_app_exits(app_name):
|
2023-03-06 16:54:17 +08:00
|
|
|
|
cmd = "docker compose -f /data/apps/"+app_name+"/docker-compose.yml stop"
|
2023-02-28 17:44:04 +08:00
|
|
|
|
output = shell_execute.execute_command_output_all(cmd)
|
|
|
|
|
if int(output["code"]) == 0:
|
2023-03-07 16:55:23 +08:00
|
|
|
|
ret.code = const.RETURN_SUCCESS
|
|
|
|
|
ret.message = "应用停止成功"
|
2023-02-28 17:44:04 +08:00
|
|
|
|
else:
|
2023-03-07 16:55:23 +08:00
|
|
|
|
ret.message = "应用停止失败"
|
2023-02-28 17:44:04 +08:00
|
|
|
|
else:
|
2023-03-07 16:55:23 +08:00
|
|
|
|
ret.message = "应用不存在"
|
|
|
|
|
ret = ret.dict()
|
2023-02-28 17:44:04 +08:00
|
|
|
|
return ret
|
|
|
|
|
|
2023-03-15 08:59:06 +08:00
|
|
|
|
|
2023-03-15 11:40:19 +08:00
|
|
|
|
def restart_app(app_name):
|
2023-03-07 16:55:23 +08:00
|
|
|
|
ret = Response(code=const.RETURN_FAIL, message="")
|
2023-02-28 17:44:04 +08:00
|
|
|
|
if if_app_exits(app_name):
|
2023-03-06 16:54:17 +08:00
|
|
|
|
cmd = "docker compose -f /data/apps/"+app_name+"/docker-compose.yml restart"
|
2023-02-28 17:44:04 +08:00
|
|
|
|
output = shell_execute.execute_command_output_all(cmd)
|
|
|
|
|
if int(output["code"]) == 0:
|
2023-03-07 16:55:23 +08:00
|
|
|
|
ret.code = const.RETURN_SUCCESS
|
|
|
|
|
ret.message = "应用重启成功"
|
2023-02-28 17:44:04 +08:00
|
|
|
|
else:
|
2023-03-07 16:55:23 +08:00
|
|
|
|
ret.message = "应用重启失败"
|
2023-02-28 17:44:04 +08:00
|
|
|
|
else:
|
2023-03-07 16:55:23 +08:00
|
|
|
|
ret.message = "应用不存在"
|
|
|
|
|
ret = ret.dict()
|
2023-02-28 17:44:04 +08:00
|
|
|
|
return ret
|
|
|
|
|
|
2023-03-15 08:59:06 +08:00
|
|
|
|
|
2023-03-15 11:35:21 +08:00
|
|
|
|
def uninstall_app(app_id, delete_flag):
|
2023-03-07 16:55:23 +08:00
|
|
|
|
ret = Response(code=const.RETURN_FAIL, message="")
|
2023-03-15 11:35:21 +08:00
|
|
|
|
if_stopped = stop_app(app_id)
|
2023-02-28 17:44:04 +08:00
|
|
|
|
if if_stopped["code"] == 0:
|
2023-03-13 15:37:47 +08:00
|
|
|
|
if delete_flag == 0:
|
2023-03-15 11:35:21 +08:00
|
|
|
|
cmd = "docker compose -f /data/apps/"+app_id+"/docker-compose.yml down"
|
2023-03-13 15:37:47 +08:00
|
|
|
|
elif delete_flag == 1:
|
2023-03-15 11:35:21 +08:00
|
|
|
|
cmd = "docker compose -f /data/apps/"+app_id+"/docker-compose.yml down -v"
|
|
|
|
|
cmd = cmd + " && sudo rm -rf /data/apps/" + app_id
|
2023-03-13 11:00:33 +08:00
|
|
|
|
else:
|
2023-03-15 11:35:21 +08:00
|
|
|
|
cmd = "docker compose -f /data/apps/"+app_id+"/docker-compose.yml down"
|
2023-02-28 17:44:04 +08:00
|
|
|
|
output = shell_execute.execute_command_output_all(cmd)
|
|
|
|
|
if int(output["code"]) == 0:
|
2023-03-07 16:55:23 +08:00
|
|
|
|
ret.code = 0
|
|
|
|
|
ret.message = "应用删除成功"
|
2023-02-28 17:44:04 +08:00
|
|
|
|
else:
|
2023-03-07 16:55:23 +08:00
|
|
|
|
ret.message = "应用删除失败"
|
2023-02-28 17:44:04 +08:00
|
|
|
|
else:
|
2023-03-07 16:55:23 +08:00
|
|
|
|
ret.message = if_stopped["message"]
|
|
|
|
|
ret = ret.dict()
|
2023-02-28 17:44:04 +08:00
|
|
|
|
return ret
|