2023-02-22 09:35:21 +08:00
|
|
|
|
import os, io, sys, platform, shutil, time, json, datetime
|
2023-03-10 10:15:47 +08:00
|
|
|
|
import re,docker,requests
|
2023-02-22 17:18:24 +08:00
|
|
|
|
from api.utils import shell_execute
|
2023-03-13 15:37:47 +08:00
|
|
|
|
import psutil as p
|
2023-02-22 17:18:24 +08:00
|
|
|
|
from dotenv import load_dotenv, find_dotenv
|
2023-02-23 16:19:55 +08:00
|
|
|
|
import dotenv
|
2023-02-22 17:18:24 +08:00
|
|
|
|
from pathlib import Path
|
2023-03-15 17:55:05 +08:00
|
|
|
|
from api.utils.common_log import myLogger
|
2023-02-22 17:18:24 +08:00
|
|
|
|
|
2023-03-17 09:44:39 +08:00
|
|
|
|
def pull_images(app_name):
|
|
|
|
|
|
|
|
|
|
# 备用方法
|
|
|
|
|
# 为了防止安装前,用户服务器已经有了镜像。导致安装时镜像不重新拉取,镜像是老的(根据docker-compose.yml 和 .env 获取)
|
|
|
|
|
myLogger.info_logger("Pull images complete ...")
|
|
|
|
|
|
|
|
|
|
def delete_images(app_id):
|
|
|
|
|
|
|
|
|
|
# 备用方法
|
|
|
|
|
# 卸载APP时同时删除dockercompose里面对应的镜像(根据docker-compose.yml 和 .env 获取)
|
|
|
|
|
myLogger.info_logger("Delete images complete ...")
|
|
|
|
|
|
2023-03-15 10:26:52 +08:00
|
|
|
|
def get_process_perc(app_name, real_name):
|
2023-03-09 11:15:55 +08:00
|
|
|
|
|
2023-03-16 11:55:01 +08:00
|
|
|
|
process_now = "step1"
|
|
|
|
|
|
|
|
|
|
if if_app_exits(app_name):
|
|
|
|
|
process_now = "step2"
|
|
|
|
|
process_now = "step3"
|
|
|
|
|
|
2023-03-09 11:15:55 +08:00
|
|
|
|
return process_now
|
2023-03-09 10:37:49 +08:00
|
|
|
|
|
2023-03-16 11:55:01 +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-13 15:37:47 +08:00
|
|
|
|
def check_vm_resource(app_name):
|
2023-03-15 17:55:05 +08:00
|
|
|
|
myLogger.info_logger("Checking virtual memory resource ...")
|
2023-03-15 10:53:35 +08:00
|
|
|
|
cpu_count = p.cpu_count()
|
2023-03-13 15:37:47 +08:00
|
|
|
|
mem = p.virtual_memory()
|
|
|
|
|
mem_total = float(mem.total) / 1024 / 1024 / 1024
|
2023-03-15 14:26:03 +08:00
|
|
|
|
requirements_var = read_var(app_name, 'requirements')
|
|
|
|
|
need_cpu_count = int(requirements_var['cpu'])
|
|
|
|
|
need_mem = int(requirements_var['memory'])
|
2023-03-15 10:53:35 +08:00
|
|
|
|
if cpu_count<need_cpu_count or mem_total<need_mem:
|
|
|
|
|
return False
|
|
|
|
|
|
2023-03-13 15:37:47 +08:00
|
|
|
|
mem_free = float(mem.available) / 1024 / 1024 / 1024
|
2023-03-15 10:53:35 +08:00
|
|
|
|
if mem_total>=8 and mem_free<=4:
|
|
|
|
|
return False
|
|
|
|
|
|
2023-03-15 14:26:03 +08:00
|
|
|
|
need_disk = int(requirements_var['disk'])
|
2023-03-13 15:37:47 +08:00
|
|
|
|
disk = p.disk_usage('/')
|
2023-03-15 10:53:35 +08:00
|
|
|
|
disk_total = float(disk.total) / 1024 / 1024 / 1024
|
2023-03-13 15:37:47 +08:00
|
|
|
|
disk_free = float(disk.free) / 1024 / 1024 / 1024
|
2023-03-15 14:06:28 +08:00
|
|
|
|
if disk_total<need_disk or disk_free<2:
|
2023-03-13 15:37:47 +08:00
|
|
|
|
return False
|
2023-03-15 10:53:35 +08:00
|
|
|
|
|
2023-03-15 14:07:41 +08:00
|
|
|
|
return True
|
2023-03-08 16:33:24 +08:00
|
|
|
|
|
2023-03-08 16:31:25 +08:00
|
|
|
|
def check_app_directory(app_name):
|
2023-03-13 16:32:12 +08:00
|
|
|
|
# websoft9's support applist
|
2023-03-15 17:55:05 +08:00
|
|
|
|
myLogger.info_logger("Checking dir...")
|
2023-03-24 16:06:41 +08:00
|
|
|
|
path = "/data/library/apps/"+app_name
|
2023-03-21 15:14:35 +08:00
|
|
|
|
is_exists = os.path.exists(path)
|
|
|
|
|
return is_exists
|
2023-02-22 09:35:21 +08:00
|
|
|
|
|
|
|
|
|
def check_app_compose(app_name):
|
2023-03-15 17:55:05 +08:00
|
|
|
|
myLogger.info_logger("Checking port...")
|
2023-02-23 16:19:55 +08:00
|
|
|
|
path = "/data/apps/" + app_name + "/.env"
|
2023-03-10 16:47:11 +08:00
|
|
|
|
port_dic = read_env(path, "APP_.*_PORT")
|
2023-03-24 15:30:37 +08:00
|
|
|
|
#1.判断/data/apps/app_name/.env中的port是否占用,没有被占用,方法结束(get_start_port方法)
|
2023-03-10 16:47:11 +08:00
|
|
|
|
for port_name in port_dic:
|
2023-03-24 15:30:37 +08:00
|
|
|
|
port_value = get_start_port(port_dic[port_name])
|
2023-03-10 16:47:11 +08:00
|
|
|
|
modify_env(path, port_name, port_value)
|
2023-03-15 17:55:05 +08:00
|
|
|
|
myLogger.info_logger("Port check complete")
|
2023-02-23 16:19:55 +08:00
|
|
|
|
return
|
|
|
|
|
|
2023-03-16 16:49:31 +08:00
|
|
|
|
def check_app_url(customer_app_name):
|
|
|
|
|
myLogger.info_logger("Checking app url...")
|
|
|
|
|
|
|
|
|
|
# 如果app的.env文件中含有HTTP_URL项目,需要如此设置 HTTP_URL=ip:port
|
2023-03-16 17:20:11 +08:00
|
|
|
|
env_path = "/data/apps/" + customer_app_name + "/.env"
|
|
|
|
|
if read_env(env_path, "HTTP_URL") != {}:
|
|
|
|
|
ip = shell_execute.execute_command_output_all("curl ifconfig.me")["result"]
|
|
|
|
|
http_port = list(read_env(path, "APP_HTTP_PORT").values())[0]
|
|
|
|
|
url = ip + ":" + http_port
|
|
|
|
|
modify_env(path, "HTTP_URL", url)
|
|
|
|
|
|
2023-03-16 16:49:31 +08:00
|
|
|
|
myLogger.info_logger("App url check complete")
|
|
|
|
|
return
|
|
|
|
|
|
2023-02-27 11:07:25 +08:00
|
|
|
|
def read_env(path, key):
|
2023-03-15 17:55:05 +08:00
|
|
|
|
myLogger.info_logger("Read " + path)
|
2023-03-10 16:47:11 +08:00
|
|
|
|
output = shell_execute.execute_command_output_all("cat " + path + "|grep "+ key)
|
2023-02-27 11:07:25 +08:00
|
|
|
|
code = output["code"]
|
2023-03-10 16:47:11 +08:00
|
|
|
|
env_dic = {}
|
2023-02-27 11:07:25 +08:00
|
|
|
|
if int(code) == 0 and output["result"] != "":
|
|
|
|
|
ret = output["result"]
|
2023-03-10 16:47:11 +08:00
|
|
|
|
env_list = ret.split()
|
|
|
|
|
for env in env_list:
|
|
|
|
|
env_dic[env.split("=")[0]] = env.split("=")[1]
|
2023-03-17 16:31:03 +08:00
|
|
|
|
myLogger.info_logger("Read " + path + ": " + str(env_dic))
|
2023-03-10 16:47:11 +08:00
|
|
|
|
return env_dic
|
2023-02-27 11:07:25 +08:00
|
|
|
|
|
2023-03-09 11:30:22 +08:00
|
|
|
|
def modify_env(path, env_name, value):
|
2023-03-15 17:55:05 +08:00
|
|
|
|
myLogger.info_logger("Modify " + path + "...")
|
2023-03-08 10:08:46 +08:00
|
|
|
|
file_data = ""
|
|
|
|
|
with open(path, "r", encoding="utf-8") as f:
|
|
|
|
|
for line in f:
|
2023-03-09 15:37:18 +08:00
|
|
|
|
if re.match(env_name, line) != None:
|
|
|
|
|
env_name = line.split("=")[0]
|
2023-03-09 11:30:22 +08:00
|
|
|
|
line = line.replace(line, env_name + "=" + value+"\n")
|
2023-03-08 10:08:46 +08:00
|
|
|
|
file_data += line
|
|
|
|
|
with open(path, "w", encoding="utf-8") as f:
|
2023-03-15 17:55:05 +08:00
|
|
|
|
myLogger.info_logger("Modify " + path + ": Change " + env_name + " to " + value)
|
2023-03-08 10:08:46 +08:00
|
|
|
|
f.write(file_data)
|
2023-03-14 12:57:34 +08:00
|
|
|
|
|
|
|
|
|
def read_var(app_name, var_name):
|
|
|
|
|
value = "-"
|
|
|
|
|
var_path = "/data/apps/" + app_name + "/variables.json"
|
2023-03-15 17:55:05 +08:00
|
|
|
|
myLogger.info_logger("Read " + var_path)
|
2023-03-14 12:57:34 +08:00
|
|
|
|
try:
|
|
|
|
|
f = open(var_path, 'r', encoding='utf-8')
|
|
|
|
|
var = json.load(f)
|
|
|
|
|
try:
|
|
|
|
|
value = var[var_name]
|
|
|
|
|
except KeyError:
|
2023-03-15 17:55:05 +08:00
|
|
|
|
myLogger.warning_logger("Read " + var_path + ": No key " + var_name)
|
2023-03-14 12:57:34 +08:00
|
|
|
|
except FileNotFoundError:
|
2023-03-15 17:55:05 +08:00
|
|
|
|
myLogger.warning_logger(var_path + " not found")
|
2023-03-14 12:57:34 +08:00
|
|
|
|
return value
|
2023-03-24 15:30:37 +08:00
|
|
|
|
|
|
|
|
|
def get_start_port(port):
|
|
|
|
|
use_port = port
|
|
|
|
|
while True:
|
|
|
|
|
cmd = "netstat -ntlp | grep -v only"
|
|
|
|
|
output = shell_execute.execute_command_output_all(cmd)
|
|
|
|
|
if output["result"].find(use_port)==-1:
|
|
|
|
|
break
|
|
|
|
|
else:
|
|
|
|
|
use_port = str(int(use_port)+1)
|
|
|
|
|
|
|
|
|
|
return use_port
|