2023-04-19 17:00:43 +08:00
|
|
|
|
import os, io, sys, platform, shutil, time, json, datetime, psutil
|
2023-03-25 16:22:07 +08:00
|
|
|
|
import re, docker, requests
|
2023-02-22 17:18:24 +08:00
|
|
|
|
from api.utils import shell_execute
|
|
|
|
|
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-04-14 18:36:31 +08:00
|
|
|
|
from api.utils import shell_execute, const
|
2023-04-16 14:37:20 +08:00
|
|
|
|
from api.exception.command_exception import CommandException
|
2023-04-16 20:44:04 +08:00
|
|
|
|
from api.service import manage
|
2023-04-14 19:42:42 +08:00
|
|
|
|
|
|
|
|
|
|
2023-03-30 15:26:22 +08:00
|
|
|
|
# 已经是running的app怎么知道它已经能够访问,如页面能进入,如mysql能被客户端连接
|
|
|
|
|
def if_app_access(app_name):
|
|
|
|
|
return True
|
2023-04-14 19:42:42 +08:00
|
|
|
|
|
|
|
|
|
|
2023-03-16 11:55:01 +08:00
|
|
|
|
def if_app_exits(app_name):
|
2023-04-16 21:54:54 +08:00
|
|
|
|
cmd = "docker compose ls -a"
|
2023-03-16 11:55:01 +08:00
|
|
|
|
output = shell_execute.execute_command_output_all(cmd)
|
2023-04-16 21:54:54 +08:00
|
|
|
|
if int(output["code"]) == 0:
|
|
|
|
|
pattern = app_name + '$'
|
|
|
|
|
info_list = output['result'].split()
|
|
|
|
|
is_exist = False
|
|
|
|
|
for info in info_list:
|
|
|
|
|
if re.match(pattern, info) != None:
|
|
|
|
|
is_exist = True
|
|
|
|
|
break
|
|
|
|
|
return is_exist
|
2023-03-16 11:55:01 +08:00
|
|
|
|
else:
|
|
|
|
|
return True
|
2023-03-25 16:22:07 +08:00
|
|
|
|
|
2023-04-14 19:42:42 +08:00
|
|
|
|
|
2023-03-30 15:26:22 +08:00
|
|
|
|
def if_app_running(app_name):
|
2023-04-16 21:54:54 +08:00
|
|
|
|
cmd = "docker compose ls -a"
|
2023-03-30 15:26:22 +08:00
|
|
|
|
output = shell_execute.execute_command_output_all(cmd)
|
2023-04-16 21:54:54 +08:00
|
|
|
|
if int(output["code"]) == 0:
|
|
|
|
|
app_list = output['result'].split("\n")
|
|
|
|
|
pattern = app_name + '\s*'
|
|
|
|
|
if_running = False
|
|
|
|
|
for app in app_list:
|
|
|
|
|
if re.match(pattern, app) != None and re.match('running', app) != None:
|
|
|
|
|
if_running = True
|
|
|
|
|
break
|
|
|
|
|
return if_running
|
2023-03-30 15:26:22 +08:00
|
|
|
|
else:
|
2023-04-16 21:54:54 +08:00
|
|
|
|
return False
|
2023-04-14 19:42:42 +08:00
|
|
|
|
|
|
|
|
|
|
2023-04-14 18:36:31 +08:00
|
|
|
|
def check_appid_exist(app_id):
|
|
|
|
|
myLogger.info_logger("Checking check_appid_exist ...")
|
2023-04-15 16:39:33 +08:00
|
|
|
|
appList = manage.get_my_app()
|
|
|
|
|
find = False
|
|
|
|
|
for app in appList:
|
|
|
|
|
if app_id == app.app_id:
|
|
|
|
|
find = True
|
|
|
|
|
break
|
2023-03-27 16:26:03 +08:00
|
|
|
|
myLogger.info_logger("Check complete.")
|
2023-04-15 16:39:33 +08:00
|
|
|
|
return find
|
|
|
|
|
|
2023-03-27 16:26:03 +08:00
|
|
|
|
|
2023-04-15 08:20:29 +08:00
|
|
|
|
def check_appid_include_rq(app_id):
|
|
|
|
|
message = ""
|
|
|
|
|
code = None
|
2023-05-15 11:06:36 +08:00
|
|
|
|
if app_id == None or app_id == "undefine":
|
2023-04-15 08:20:29 +08:00
|
|
|
|
code = const.ERROR_CLIENT_PARAM_BLANK
|
|
|
|
|
message = "AppID is null"
|
2023-04-16 19:41:02 +08:00
|
|
|
|
elif re.match('^[a-z0-9]+_[a-z0-9]+$', app_id) == None:
|
2023-04-15 08:20:29 +08:00
|
|
|
|
code = const.ERROR_CLIENT_PARAM_Format
|
2023-05-15 11:05:32 +08:00
|
|
|
|
message = "App_id format error"
|
2023-04-16 20:30:13 +08:00
|
|
|
|
elif not check_appid_exist(app_id):
|
2023-04-15 08:20:29 +08:00
|
|
|
|
code = const.ERROR_CLIENT_PARAM_NOTEXIST
|
|
|
|
|
message = "AppID is not exist"
|
|
|
|
|
return code, message
|
|
|
|
|
|
2023-04-15 16:39:33 +08:00
|
|
|
|
|
2023-04-15 08:20:29 +08:00
|
|
|
|
def check_app_id(app_id):
|
|
|
|
|
message = ""
|
|
|
|
|
code = None
|
|
|
|
|
if app_id == None:
|
|
|
|
|
code = const.ERROR_CLIENT_PARAM_BLANK
|
|
|
|
|
message = "AppID is null"
|
2023-04-16 19:41:24 +08:00
|
|
|
|
elif re.match('^[a-z0-9]+_[a-z0-9]+$', app_id) == None:
|
2023-04-15 08:20:29 +08:00
|
|
|
|
code = const.ERROR_CLIENT_PARAM_Format
|
|
|
|
|
message = "APP name can only be composed of numbers and lowercase letters"
|
2023-04-16 19:05:31 +08:00
|
|
|
|
myLogger.info_logger(code)
|
2023-04-14 18:36:31 +08:00
|
|
|
|
return code, message
|
|
|
|
|
|
2023-03-27 16:26:03 +08:00
|
|
|
|
|
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-25 17:37:40 +08:00
|
|
|
|
var_path = "/data/library/apps/" + app_name + "/variables.json"
|
2023-03-25 16:59:04 +08:00
|
|
|
|
requirements_var = read_var(var_path, 'requirements')
|
2023-03-15 14:26:03 +08:00
|
|
|
|
need_cpu_count = int(requirements_var['cpu'])
|
2023-03-25 16:22:07 +08:00
|
|
|
|
cpu_count = int(shell_execute.execute_command_output_all("cat /proc/cpuinfo | grep \'core id\'| wc -l")["result"])
|
|
|
|
|
if cpu_count < need_cpu_count:
|
|
|
|
|
myLogger.info_logger("Check complete: The number of CPU cores is insufficient!")
|
2023-03-15 10:53:35 +08:00
|
|
|
|
return False
|
2023-03-25 16:22:07 +08:00
|
|
|
|
need_mem_total = int(requirements_var['memory'])
|
2023-04-19 17:00:43 +08:00
|
|
|
|
mem_free = float(psutil.virtual_memory().available) / 1024 / 1024 / 1024
|
2023-04-19 11:39:45 +08:00
|
|
|
|
if mem_free < need_mem_total * 1.2:
|
|
|
|
|
myLogger.info_logger("Check complete: The total amount of memory is insufficient!")
|
2023-03-15 10:53:35 +08:00
|
|
|
|
return False
|
2023-03-15 14:26:03 +08:00
|
|
|
|
need_disk = int(requirements_var['disk'])
|
2023-04-19 17:00:43 +08:00
|
|
|
|
disk_free = float(psutil.disk_usage('/').free) / 1024 / 1024 / 1024
|
2023-04-19 11:52:54 +08:00
|
|
|
|
if round(disk_free) < need_disk + 2:
|
2023-03-25 16:22:07 +08:00
|
|
|
|
myLogger.info_logger("Check complete: There are not enough disks left!")
|
2023-03-13 15:37:47 +08:00
|
|
|
|
return False
|
2023-03-25 16:22:07 +08:00
|
|
|
|
myLogger.info_logger("Check complete.")
|
2023-03-15 14:07:41 +08:00
|
|
|
|
return True
|
2023-03-08 16:33:24 +08:00
|
|
|
|
|
2023-03-25 16:22:07 +08:00
|
|
|
|
|
2023-04-08 07:59:03 +08:00
|
|
|
|
def check_app_websoft9(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-25 16:22:07 +08:00
|
|
|
|
path = "/data/library/apps/" + app_name
|
2023-03-24 16:41:45 +08:00
|
|
|
|
is_exists = check_directory(path)
|
2023-03-21 15:14:35 +08:00
|
|
|
|
return is_exists
|
2023-02-22 09:35:21 +08:00
|
|
|
|
|
2023-03-25 16:22:07 +08:00
|
|
|
|
|
2023-03-24 16:41:45 +08:00
|
|
|
|
def check_directory(path):
|
2023-04-16 14:33:53 +08:00
|
|
|
|
try:
|
|
|
|
|
shell_execute.execute_command_output_all("ls " + path)
|
2023-03-24 16:41:45 +08:00
|
|
|
|
return True
|
2023-04-16 14:33:53 +08:00
|
|
|
|
except CommandException as ce:
|
2023-03-24 16:41:45 +08:00
|
|
|
|
return False
|
|
|
|
|
|
2023-04-19 09:54:33 +08:00
|
|
|
|
def check_app_compose(app_name, customer_name):
|
2023-05-20 14:01:32 +08:00
|
|
|
|
myLogger.info_logger("Set port and random password ...")
|
2023-04-19 09:54:33 +08:00
|
|
|
|
library_path = "/data/library/apps/" + app_name
|
|
|
|
|
install_path = "/data/apps/" + customer_name
|
|
|
|
|
port_dic = read_env(library_path + '/.env', "APP_.*_PORT")
|
2023-03-25 16:22:07 +08:00
|
|
|
|
# 1.判断/data/apps/app_name/.env中的port是否占用,没有被占用,方法结束(get_start_port方法)
|
2023-04-19 09:54:33 +08:00
|
|
|
|
cmd1 = "docker container inspect $(docker ps -aq) | grep HostPort | awk \'{print $2}\' | sort -u"
|
|
|
|
|
cmd2 = "netstat -tunlp | grep \"LISTEN\" | awk '{print $4}' | awk -F \":\" '{print $NF}' | sort -u"
|
|
|
|
|
cmd3 = "grep -r \"APP_.*_PORT=\" /data/apps/*/.env | awk -F \"=\" '{print $2}' | sort -u"
|
|
|
|
|
s1 = shell_execute.execute_command_output_all(cmd1)['result'].replace('\"', '')
|
|
|
|
|
s2 = shell_execute.execute_command_output_all(cmd2)['result']
|
|
|
|
|
try:
|
|
|
|
|
s3 = ''
|
|
|
|
|
s3 = shell_execute.execute_command_output_all(cmd3)['result']
|
|
|
|
|
except:
|
|
|
|
|
pass
|
|
|
|
|
s = s1 + '\n' + s2 + '\n' + s3
|
|
|
|
|
|
|
|
|
|
shell_execute.execute_command_output_all("cp -r " + library_path + " " + install_path)
|
2023-03-10 16:47:11 +08:00
|
|
|
|
for port_name in port_dic:
|
2023-04-19 09:54:33 +08:00
|
|
|
|
port_value = get_start_port(s, port_dic[port_name])
|
|
|
|
|
modify_env(install_path + '/.env', port_name, port_value)
|
2023-05-20 11:06:15 +08:00
|
|
|
|
|
|
|
|
|
# set random password
|
|
|
|
|
power_password = shell_execute.execute_command_output_all("cat /data/apps/" + customer_name +"/.env")["result"]
|
|
|
|
|
if "POWER_PASSWORD" in power_password:
|
2023-05-20 14:14:46 +08:00
|
|
|
|
try:
|
|
|
|
|
shell_execute.execute_command_output_all("docker rm -f pwgen")
|
|
|
|
|
except Exception:
|
|
|
|
|
pass
|
2023-05-20 11:06:15 +08:00
|
|
|
|
new_password = shell_execute.execute_command_output_all("docker run --name pwgen backplane/pwgen 15")["result"].rstrip('\n') + "!"
|
2023-05-20 14:01:32 +08:00
|
|
|
|
modify_env(install_path + '/.env', 'POWER_PASSWORD', new_password)
|
2023-05-20 11:06:15 +08:00
|
|
|
|
shell_execute.execute_command_output_all("docker rm -f pwgen")
|
|
|
|
|
|
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"
|
2023-05-22 11:14:02 +08:00
|
|
|
|
env_map = get_map(env_path)
|
|
|
|
|
if env_map.get("APP_URL_REPLACE") == "true":
|
2023-05-19 14:20:37 +08:00
|
|
|
|
myLogger.info_logger(customer_app_name + "need to change app url...")
|
2023-05-19 14:36:08 +08:00
|
|
|
|
app_url = list(read_env(env_path, "APP_URL").values())[0]
|
2023-04-27 09:37:04 +08:00
|
|
|
|
ip = "localhost"
|
2023-05-19 14:20:37 +08:00
|
|
|
|
url = ""
|
2023-04-27 09:37:04 +08:00
|
|
|
|
try:
|
|
|
|
|
ip_result = shell_execute.execute_command_output_all("cat /data/apps/stackhub/docker/w9appmanage/public_ip")
|
2023-04-27 09:56:08 +08:00
|
|
|
|
ip = ip_result["result"].rstrip('\n')
|
2023-04-27 09:37:04 +08:00
|
|
|
|
except Exception:
|
|
|
|
|
ip = "127.0.0.1"
|
2023-05-19 14:12:21 +08:00
|
|
|
|
http_port = list(read_env(env_path, "APP_HTTP_PORT").values())[0]
|
2023-05-19 14:20:37 +08:00
|
|
|
|
|
2023-05-19 14:12:21 +08:00
|
|
|
|
if ":" in app_url:
|
|
|
|
|
url = ip + ":" + http_port
|
|
|
|
|
else:
|
|
|
|
|
url = ip
|
2023-05-19 14:46:37 +08:00
|
|
|
|
cmd = "sed -i 's/APP_URL=.*/APP_URL=" + url + "/g' /data/apps/" + customer_app_name +"/.env"
|
|
|
|
|
shell_execute.execute_command_output_all(cmd)
|
2023-03-16 17:20:11 +08:00
|
|
|
|
|
2023-03-16 16:49:31 +08:00
|
|
|
|
myLogger.info_logger("App url check complete")
|
|
|
|
|
return
|
|
|
|
|
|
2023-05-19 15:31:01 +08:00
|
|
|
|
def get_map(path):
|
|
|
|
|
myLogger.info_logger("Read env_dic" + path)
|
|
|
|
|
output = shell_execute.execute_command_output_all("cat " + path)
|
|
|
|
|
code = output["code"]
|
|
|
|
|
env_dic = {}
|
|
|
|
|
if int(code) == 0:
|
|
|
|
|
ret = output["result"]
|
|
|
|
|
env_list = ret.split("\n")
|
|
|
|
|
for env in env_list:
|
2023-05-19 15:41:50 +08:00
|
|
|
|
if "=" in env:
|
|
|
|
|
env_dic[env.split("=")[0]] = env.split("=")[1]
|
2023-05-19 16:17:53 +08:00
|
|
|
|
myLogger.info_logger(env_dic)
|
2023-05-19 15:31:01 +08:00
|
|
|
|
return env_dic
|
2023-03-25 16:22:07 +08:00
|
|
|
|
|
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-04-16 21:54:54 +08:00
|
|
|
|
output = shell_execute.execute_command_output_all("cat " + path)
|
2023-02-27 11:07:25 +08:00
|
|
|
|
code = output["code"]
|
2023-03-10 16:47:11 +08:00
|
|
|
|
env_dic = {}
|
2023-04-16 21:54:54 +08:00
|
|
|
|
if int(code) == 0:
|
2023-02-27 11:07:25 +08:00
|
|
|
|
ret = output["result"]
|
2023-04-16 21:54:54 +08:00
|
|
|
|
env_list = ret.split("\n")
|
2023-03-10 16:47:11 +08:00
|
|
|
|
for env in env_list:
|
2023-04-16 21:54:54 +08:00
|
|
|
|
if re.match(key, env) != None:
|
|
|
|
|
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-25 16:22:07 +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-24 18:40:31 +08:00
|
|
|
|
output = shell_execute.execute_command_output_all("sed -n \'/^" + env_name + "/=\' " + path)
|
|
|
|
|
if int(output["code"]) == 0 and output["result"] != "":
|
|
|
|
|
line_num = output["result"].split("\n")[0]
|
|
|
|
|
s = env_name + "=" + value
|
|
|
|
|
output = shell_execute.execute_command_output_all("sed -i \'" + line_num + "c " + s + "\' " + path)
|
|
|
|
|
if int(output["code"]) == 0:
|
|
|
|
|
myLogger.info_logger("Modify " + path + ": Change " + env_name + " to " + value)
|
2023-03-14 12:57:34 +08:00
|
|
|
|
|
2023-03-25 16:22:07 +08:00
|
|
|
|
|
2023-03-25 16:59:04 +08:00
|
|
|
|
def read_var(var_path, var_name):
|
2023-03-29 14:48:18 +08:00
|
|
|
|
value = ""
|
2023-03-15 17:55:05 +08:00
|
|
|
|
myLogger.info_logger("Read " + var_path)
|
2023-03-24 18:40:31 +08:00
|
|
|
|
output = shell_execute.execute_command_output_all("cat " + var_path)
|
|
|
|
|
if int(output["code"]) == 0:
|
|
|
|
|
var = json.loads(output["result"])
|
2023-03-14 12:57:34 +08:00
|
|
|
|
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-24 18:40:31 +08:00
|
|
|
|
else:
|
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
|
|
|
|
|
2023-03-25 16:22:07 +08:00
|
|
|
|
|
2023-04-19 09:54:33 +08:00
|
|
|
|
def get_start_port(s, port):
|
2023-03-24 15:30:37 +08:00
|
|
|
|
use_port = port
|
|
|
|
|
while True:
|
2023-04-19 09:54:33 +08:00
|
|
|
|
if s.find(use_port) == -1:
|
2023-03-24 15:30:37 +08:00
|
|
|
|
break
|
|
|
|
|
else:
|
2023-03-25 16:22:07 +08:00
|
|
|
|
use_port = str(int(use_port) + 1)
|
2023-03-24 15:30:37 +08:00
|
|
|
|
|
|
|
|
|
return use_port
|