websoft9/appmanage/api/service/manage.py

348 lines
13 KiB
Python
Raw Normal View History

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
import socket
2023-03-21 15:14:35 +08:00
import re
from threading import Thread
2023-03-24 15:35:58 +08:00
from api.utils import shell_execute, docker, const
from api.model.app import App
2023-03-07 16:55:23 +08:00
from api.model.response import Response
2023-03-17 16:31:03 +08:00
from api.utils import lock
2023-02-21 14:21:53 +08:00
2023-03-25 20:31:44 +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-03-27 10:24:54 +08:00
app_list, has_add = get_apps_from_compose(output_list)
list = get_apps_from_queue(app_list, has_add)
2023-03-24 16:07:53 +08:00
ret = Response(code=const.RETURN_SUCCESS, message="The app query is successful.", data=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-24 16:07:53 +08:00
ret = Response(code=const.RETURN_FAIL, message="App query failed!")
2023-02-21 14:21:53 +08:00
# get all info
2023-03-24 13:22:27 +08:00
cmd = "docker compose ls -a --format json"
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-03-27 10:24:54 +08:00
app_list, has_add = get_apps_from_compose(output_list)
list = get_apps_from_queue(app_list, has_add)
2023-03-24 15:23:39 +08:00
flag = 0
2023-03-27 14:17:13 +08:00
app_info = {}
2023-03-24 15:23:39 +08:00
for app in list:
if app["app_id"] == app_id:
list.clear()
list.append(app)
2023-03-27 14:17:13 +08:00
app_info = app
2023-03-24 15:23:39 +08:00
flag = 1
break
if flag == 1:
2023-03-24 16:07:53 +08:00
ret = Response(code=const.RETURN_SUCCESS, message="The app query is successful.", data=list)
2023-03-07 16:55:23 +08:00
ret = ret.dict()
2023-03-27 14:17:13 +08:00
return app_info
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):
app_name = split_app_id(app_id)
2023-03-25 16:59:04 +08:00
var_path = "/data/apps/" + app_name + "/variables.json"
real_name = docker.read_var(var_path, 'name')
2023-03-27 09:55:54 +08:00
if docker.check_app_directory(real_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-24 16:07:53 +08:00
ret = Response(code=const.RETURN_FAIL, message="This app is not currently installed.")
2023-03-09 10:33:26 +08:00
ret = ret.dict()
return ret
2023-03-25 20:31:44 +08:00
2023-03-21 17:57:43 +08:00
def install_app(app_name, customer_app_name, app_version):
ret = Response(code=const.RETURN_FAIL, message=" ")
2023-03-22 09:38:52 +08:00
ret.code, ret.message = check_app(app_name, customer_app_name, app_version)
if ret.code == const.RETURN_SUCCESS:
ret.code, ret.message = prepare_app(app_name, customer_app_name)
if ret.code == const.RETURN_SUCCESS:
t1 = Thread(target=install_app_job, args=(customer_app_name, app_version,))
2023-03-21 15:14:35 +08:00
t1.start()
2023-03-25 20:31:44 +08:00
ret.message = "The app is starting, please check again in a few minutes."
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-15 16:13:17 +08:00
app_name = split_app_id(app_id)
2023-03-16 14:50:06 +08:00
if if_app_exits(app_id, app_name):
2023-03-08 10:08:46 +08:00
docker.check_app_compose(app_name)
2023-03-25 20:31:44 +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
2023-03-24 16:07:53 +08:00
ret.message = "The app starts successfully."
2023-02-28 17:44:04 +08:00
else:
2023-03-24 16:07:53 +08:00
ret.message = "The app failed to start!"
2023-02-28 17:44:04 +08:00
else:
2023-03-24 16:07:53 +08:00
ret.message = "The app is not installed!"
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-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-15 16:13:17 +08:00
app_name = split_app_id(app_id)
2023-03-16 14:50:06 +08:00
if if_app_exits(app_id, app_name):
2023-03-25 20:31:44 +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
2023-03-24 16:07:53 +08:00
ret.message = "The app stopped successfully."
2023-02-28 17:44:04 +08:00
else:
2023-03-24 16:07:53 +08:00
ret.message = "App stop failed!"
2023-02-28 17:44:04 +08:00
else:
2023-03-24 16:07:53 +08:00
ret.message = "The app is not installed!"
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-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-15 16:13:17 +08:00
app_name = split_app_id(app_id)
2023-03-16 14:50:06 +08:00
if if_app_exits(app_id, app_name):
2023-03-25 20:31:44 +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
2023-03-24 16:07:53 +08:00
ret.message = "The app restarts successfully."
2023-02-28 17:44:04 +08:00
else:
2023-03-24 16:07:53 +08:00
ret.message = "App restart failed!"
2023-02-28 17:44:04 +08:00
else:
2023-03-24 16:07:53 +08:00
ret.message = "The app is not installed!"
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-03-15 15:26:02 +08:00
def uninstall_app(app_id):
2023-03-07 16:55:23 +08:00
ret = Response(code=const.RETURN_FAIL, message="")
2023-03-25 20:31:44 +08:00
if_stopped = stop_app(app_id) # stop_app
2023-03-15 16:13:17 +08:00
app_name = split_app_id(app_id)
2023-03-16 10:03:54 +08:00
real_name = app_id.split("_")[0]
2023-02-28 17:44:04 +08:00
if if_stopped["code"] == 0:
2023-03-25 20:31:44 +08:00
cmd = "docker compose -f /data/apps/" + app_name + "/docker-compose.yml down -v"
2023-03-16 10:03:54 +08:00
if real_name != app_name:
cmd = cmd + " && sudo rm -rf /data/apps/" + app_name
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
2023-03-24 16:07:53 +08:00
ret.message = "The app is deleted successfully"
2023-02-28 17:44:04 +08:00
else:
2023-03-24 16:07:53 +08:00
ret.message = "App deletion failed!"
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
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 = " "
code = const.RETURN_FAIL
2023-03-25 20:31:44 +08:00
if app_name == None or customer_app_name == None or app_version == None:
2023-03-24 16:07:53 +08:00
message = "Please fill in the APP information completely!"
2023-03-24 15:25:25 +08:00
elif not docker.check_app_directory(app_name):
2023-03-24 16:07:53 +08:00
message = "Installing the app is not supported!"
2023-03-25 20:31:44 +08:00
elif re.match('^[a-z0-9]+$', customer_app_name) == None:
2023-03-24 16:07:53 +08:00
message = "App names must be lowercase letters and numbers!"
2023-03-25 20:50:13 +08:00
elif docker.check_directory("/data/apps/" + customer_app_name):
2023-03-24 16:07:53 +08:00
message = "The APP name is already in use, please specify a different name to reinstall."
2023-03-24 15:25:25 +08:00
elif not docker.check_vm_resource(app_name):
2023-03-24 16:07:53 +08:00
message = "System resources (memory, CPU, disk) are insufficient, and continuing to install may cause the app to not run or the server to be abnormal!"
2023-03-24 15:25:25 +08:00
else:
code = const.RETURN_SUCCESS
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-03-25 20:31:44 +08:00
2023-03-24 15:25:25 +08:00
def install_app_job(customer_app_name, app_version):
2023-03-25 20:31:44 +08:00
# write running_apps.txt
2023-03-24 15:25:25 +08:00
file_path = "/data/apps/running_apps.txt"
2023-03-25 20:31:44 +08:00
shell_execute.execute_command_output_all("echo " + customer_app_name + " >> " + file_path)
2023-03-24 15:25:25 +08:00
# 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(customer_app_name)
# modify running_apps.txt
2023-03-24 15:27:19 +08:00
cmd = "cd /data/apps/" + customer_app_name + " && sudo docker compose up --pull always -d"
2023-03-24 15:25:25 +08:00
shell_execute.execute_command_output_all(cmd)
2023-03-25 20:31:44 +08:00
# delete
output = shell_execute.execute_command_output_all("sed -n \'/^" + customer_app_name + "/=\' " + file_path)
if int(output["code"]) == 0 and output["result"] != "":
line_num = output["result"].split("\n")[0]
shell_execute.execute_command_output_all("sed -i \'" + line_num + "d\' " + file_path)
2023-03-24 15:25:25 +08:00
def if_app_exits(app_id, app_name):
2023-03-25 20:31:44 +08:00
cmd = "docker compose ls -a | grep \'" + app_name + "\\b\'"
2023-03-24 15:25:25 +08:00
output = shell_execute.execute_command_output_all(cmd)
if int(output["code"]) == -1:
return False
else:
2023-03-25 16:59:04 +08:00
var_path = "/data/apps/" + app_name + "/variables.json"
real_name = docker.read_var(var_path, "name")
2023-03-24 15:25:25 +08:00
real_id = real_name + "_" + app_name
if app_id == real_id:
return True
else:
return False
2023-03-25 20:31:44 +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-25 20:31:44 +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
app_name = volume.split('/')[3]
2023-03-24 16:07:53 +08:00
app_path = "/data/apps/" + app_name
2023-03-25 16:59:04 +08:00
var_path = app_path + "/variables.json"
2023-03-24 16:41:45 +08:00
if not docker.check_directory(app_path):
2023-03-24 16:07:53 +08:00
continue
2023-03-25 16:59:04 +08:00
real_name = docker.read_var(var_path, 'name')
2023-03-24 15:23:39 +08:00
image_url = get_Image_url(real_name)
# get trade_mark
2023-03-25 16:59:04 +08:00
trade_mark = docker.read_var(var_path, 'trademark')
2023-03-24 15:23:39 +08:00
app_id = real_name + "_" + app_name # app_id
case = app_info["Status"].split("(")[0] # case
if case == "running":
case_code = const.RETURN_RUNNING # case_code
elif case == "exited":
case = "stop"
case_code = const.RETURN_STOP
elif case == "created":
case_code = const.RETURN_READY
case = "installing"
else:
case_code = const.RETURN_ERROR
# get env info
path = "/data/apps/" + app_name + "/.env"
port = 0
url = "-"
admin_url = "-"
# 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
user_name = "-"
try:
user_name = list(docker.read_env(path, "APP_USER").values())[0]
except IndexError:
pass
# get password
password = "-"
try:
password = list(docker.read_env(
path, "POWER_PASSWORD").values())[0]
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,
password=password)
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
def get_apps_from_queue(app_list, has_add):
2023-03-24 15:23:39 +08:00
file_path = "/data/apps/running_apps.txt"
2023-03-24 16:41:45 +08:00
if docker.check_directory(file_path):
2023-03-25 20:50:13 +08:00
output = shell_execute.execute_command_output_all("cat " + file_path)
apps = output["result"].split("\n")
for running_app_name in apps:
running_app_name = re.sub("\n", "", running_app_name)
if running_app_name not in has_add and running_app_name != "":
var_path = "/data/apps/" + running_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 + "_" + running_app_name, name=real_name, customer_name=running_app_name,
status_code=const.RETURN_READY, status="installing", port=0, volume="-",
url="-", image_url=image_url, admin_url="-", trade_mark=trade_mark, user_name="-",
password="-")
app_list.append(app.dict())
2023-03-24 15:23:39 +08:00
return app_list
2023-03-25 20:31:44 +08:00
def get_Image_url(app_name):
2023-03-24 15:23:39 +08:00
image_url = "/static/" + app_name + "-websoft9.png"
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
def get_admin_url(app_name, url):
admin_url = "-"
if app_name == "wordpress":
admin_url = url + "/wp-admin"
elif app_name == "other":
admin_url = url + "/admin"
else:
admin_url = "-"
return admin_url