|
@@ -1,26 +1,25 @@
|
|
import os, io, sys, platform, shutil, time, json, datetime
|
|
import os, io, sys, platform, shutil, time, json, datetime
|
|
-import re,docker,requests
|
|
|
|
|
|
+import re, docker, requests
|
|
from api.utils import shell_execute
|
|
from api.utils import shell_execute
|
|
-import psutil as p
|
|
|
|
from dotenv import load_dotenv, find_dotenv
|
|
from dotenv import load_dotenv, find_dotenv
|
|
import dotenv
|
|
import dotenv
|
|
from pathlib import Path
|
|
from pathlib import Path
|
|
from api.utils.common_log import myLogger
|
|
from api.utils.common_log import myLogger
|
|
|
|
|
|
|
|
+
|
|
def pull_images(app_name):
|
|
def pull_images(app_name):
|
|
-
|
|
|
|
# 备用方法
|
|
# 备用方法
|
|
# 为了防止安装前,用户服务器已经有了镜像。导致安装时镜像不重新拉取,镜像是老的(根据docker-compose.yml 和 .env 获取)
|
|
# 为了防止安装前,用户服务器已经有了镜像。导致安装时镜像不重新拉取,镜像是老的(根据docker-compose.yml 和 .env 获取)
|
|
myLogger.info_logger("Pull images complete ...")
|
|
myLogger.info_logger("Pull images complete ...")
|
|
-
|
|
|
|
|
|
+
|
|
|
|
+
|
|
def delete_images(app_id):
|
|
def delete_images(app_id):
|
|
-
|
|
|
|
# 备用方法
|
|
# 备用方法
|
|
# 卸载APP时同时删除dockercompose里面对应的镜像(根据docker-compose.yml 和 .env 获取)
|
|
# 卸载APP时同时删除dockercompose里面对应的镜像(根据docker-compose.yml 和 .env 获取)
|
|
myLogger.info_logger("Delete images complete ...")
|
|
myLogger.info_logger("Delete images complete ...")
|
|
-
|
|
|
|
|
|
+
|
|
|
|
+
|
|
def get_process_perc(app_name, real_name):
|
|
def get_process_perc(app_name, real_name):
|
|
-
|
|
|
|
process_now = "step1"
|
|
process_now = "step1"
|
|
|
|
|
|
if if_app_exits(app_name):
|
|
if if_app_exits(app_name):
|
|
@@ -29,45 +28,52 @@ def get_process_perc(app_name, real_name):
|
|
|
|
|
|
return process_now
|
|
return process_now
|
|
|
|
|
|
|
|
+
|
|
def if_app_exits(app_name):
|
|
def if_app_exits(app_name):
|
|
- cmd = "docker compose ls -a | grep \'"+app_name+"\\b\'"
|
|
|
|
|
|
+ cmd = "docker compose ls -a | grep \'" + app_name + "\\b\'"
|
|
output = shell_execute.execute_command_output_all(cmd)
|
|
output = shell_execute.execute_command_output_all(cmd)
|
|
if int(output["code"]) == -1:
|
|
if int(output["code"]) == -1:
|
|
return False
|
|
return False
|
|
else:
|
|
else:
|
|
return True
|
|
return True
|
|
-
|
|
|
|
|
|
+
|
|
|
|
+
|
|
def check_vm_resource(app_name):
|
|
def check_vm_resource(app_name):
|
|
myLogger.info_logger("Checking virtual memory resource ...")
|
|
myLogger.info_logger("Checking virtual memory resource ...")
|
|
- cpu_count = p.cpu_count()
|
|
|
|
- mem = p.virtual_memory()
|
|
|
|
- mem_total = float(mem.total) / 1024 / 1024 / 1024
|
|
|
|
requirements_var = read_var(app_name, 'requirements')
|
|
requirements_var = read_var(app_name, 'requirements')
|
|
need_cpu_count = int(requirements_var['cpu'])
|
|
need_cpu_count = int(requirements_var['cpu'])
|
|
- need_mem = int(requirements_var['memory'])
|
|
|
|
- if cpu_count<need_cpu_count or mem_total<need_mem:
|
|
|
|
|
|
+ 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!")
|
|
return False
|
|
return False
|
|
-
|
|
|
|
- mem_free = float(mem.available) / 1024 / 1024 / 1024
|
|
|
|
- if mem_total>=8 and mem_free<=4:
|
|
|
|
|
|
+ need_mem_total = int(requirements_var['memory'])
|
|
|
|
+ mem_total = float(
|
|
|
|
+ shell_execute.execute_command_output_all("free -m | grep Mem | awk \'{print $2}\'")["result"]) / 1024
|
|
|
|
+ if mem_total < need_mem_total:
|
|
|
|
+ myLogger.info_logger("Check complete: The total amount of memory is insufficient!")
|
|
|
|
+ return False
|
|
|
|
+ mem_free = float(
|
|
|
|
+ shell_execute.execute_command_output_all("free -m | grep Mem | awk \'{print $4}\'")["result"]) / 1024
|
|
|
|
+ if need_mem_total > 4 and mem_free < 4:
|
|
|
|
+ myLogger.info_logger("Check complete: There is not enough memory left!")
|
|
return False
|
|
return False
|
|
-
|
|
|
|
need_disk = int(requirements_var['disk'])
|
|
need_disk = int(requirements_var['disk'])
|
|
- disk = p.disk_usage('/')
|
|
|
|
- disk_total = float(disk.total) / 1024 / 1024 / 1024
|
|
|
|
- disk_free = float(disk.free) / 1024 / 1024 / 1024
|
|
|
|
- if disk_total<need_disk or disk_free<2:
|
|
|
|
|
|
+ disk_free = float(shell_execute.execute_command_output_all("df -m | awk \'$NF==\"/\"{print $4}\'")["result"]) / 1024
|
|
|
|
+ if disk_free < need_disk:
|
|
|
|
+ myLogger.info_logger("Check complete: There are not enough disks left!")
|
|
return False
|
|
return False
|
|
-
|
|
|
|
|
|
+ myLogger.info_logger("Check complete.")
|
|
return True
|
|
return True
|
|
|
|
|
|
|
|
+
|
|
def check_app_directory(app_name):
|
|
def check_app_directory(app_name):
|
|
# websoft9's support applist
|
|
# websoft9's support applist
|
|
myLogger.info_logger("Checking dir...")
|
|
myLogger.info_logger("Checking dir...")
|
|
- path = "/data/library/apps/"+app_name
|
|
|
|
|
|
+ path = "/data/library/apps/" + app_name
|
|
is_exists = check_directory(path)
|
|
is_exists = check_directory(path)
|
|
return is_exists
|
|
return is_exists
|
|
|
|
|
|
|
|
+
|
|
def check_directory(path):
|
|
def check_directory(path):
|
|
output = shell_execute.execute_command_output_all("ls " + path)
|
|
output = shell_execute.execute_command_output_all("ls " + path)
|
|
if int(output["code"]) == 0:
|
|
if int(output["code"]) == 0:
|
|
@@ -75,20 +81,22 @@ def check_directory(path):
|
|
else:
|
|
else:
|
|
return False
|
|
return False
|
|
|
|
|
|
|
|
+
|
|
def check_app_compose(app_name):
|
|
def check_app_compose(app_name):
|
|
myLogger.info_logger("Checking port...")
|
|
myLogger.info_logger("Checking port...")
|
|
path = "/data/apps/" + app_name + "/.env"
|
|
path = "/data/apps/" + app_name + "/.env"
|
|
port_dic = read_env(path, "APP_.*_PORT")
|
|
port_dic = read_env(path, "APP_.*_PORT")
|
|
- #1.判断/data/apps/app_name/.env中的port是否占用,没有被占用,方法结束(get_start_port方法)
|
|
|
|
|
|
+ # 1.判断/data/apps/app_name/.env中的port是否占用,没有被占用,方法结束(get_start_port方法)
|
|
for port_name in port_dic:
|
|
for port_name in port_dic:
|
|
port_value = get_start_port(port_dic[port_name])
|
|
port_value = get_start_port(port_dic[port_name])
|
|
modify_env(path, port_name, port_value)
|
|
modify_env(path, port_name, port_value)
|
|
myLogger.info_logger("Port check complete")
|
|
myLogger.info_logger("Port check complete")
|
|
return
|
|
return
|
|
|
|
|
|
|
|
+
|
|
def check_app_url(customer_app_name):
|
|
def check_app_url(customer_app_name):
|
|
myLogger.info_logger("Checking app url...")
|
|
myLogger.info_logger("Checking app url...")
|
|
-
|
|
|
|
|
|
+
|
|
# 如果app的.env文件中含有HTTP_URL项目,需要如此设置 HTTP_URL=ip:port
|
|
# 如果app的.env文件中含有HTTP_URL项目,需要如此设置 HTTP_URL=ip:port
|
|
env_path = "/data/apps/" + customer_app_name + "/.env"
|
|
env_path = "/data/apps/" + customer_app_name + "/.env"
|
|
if read_env(env_path, "HTTP_URL") != {}:
|
|
if read_env(env_path, "HTTP_URL") != {}:
|
|
@@ -100,9 +108,10 @@ def check_app_url(customer_app_name):
|
|
myLogger.info_logger("App url check complete")
|
|
myLogger.info_logger("App url check complete")
|
|
return
|
|
return
|
|
|
|
|
|
|
|
+
|
|
def read_env(path, key):
|
|
def read_env(path, key):
|
|
myLogger.info_logger("Read " + path)
|
|
myLogger.info_logger("Read " + path)
|
|
- output = shell_execute.execute_command_output_all("cat " + path + "|grep "+ key)
|
|
|
|
|
|
+ output = shell_execute.execute_command_output_all("cat " + path + "|grep " + key)
|
|
code = output["code"]
|
|
code = output["code"]
|
|
env_dic = {}
|
|
env_dic = {}
|
|
if int(code) == 0 and output["result"] != "":
|
|
if int(code) == 0 and output["result"] != "":
|
|
@@ -113,6 +122,7 @@ def read_env(path, key):
|
|
myLogger.info_logger("Read " + path + ": " + str(env_dic))
|
|
myLogger.info_logger("Read " + path + ": " + str(env_dic))
|
|
return env_dic
|
|
return env_dic
|
|
|
|
|
|
|
|
+
|
|
def modify_env(path, env_name, value):
|
|
def modify_env(path, env_name, value):
|
|
myLogger.info_logger("Modify " + path + "...")
|
|
myLogger.info_logger("Modify " + path + "...")
|
|
output = shell_execute.execute_command_output_all("sed -n \'/^" + env_name + "/=\' " + path)
|
|
output = shell_execute.execute_command_output_all("sed -n \'/^" + env_name + "/=\' " + path)
|
|
@@ -123,6 +133,7 @@ def modify_env(path, env_name, value):
|
|
if int(output["code"]) == 0:
|
|
if int(output["code"]) == 0:
|
|
myLogger.info_logger("Modify " + path + ": Change " + env_name + " to " + value)
|
|
myLogger.info_logger("Modify " + path + ": Change " + env_name + " to " + value)
|
|
|
|
|
|
|
|
+
|
|
def read_var(app_name, var_name):
|
|
def read_var(app_name, var_name):
|
|
value = "-"
|
|
value = "-"
|
|
var_path = "/data/apps/" + app_name + "/variables.json"
|
|
var_path = "/data/apps/" + app_name + "/variables.json"
|
|
@@ -138,14 +149,15 @@ def read_var(app_name, var_name):
|
|
myLogger.warning_logger(var_path + " not found")
|
|
myLogger.warning_logger(var_path + " not found")
|
|
return value
|
|
return value
|
|
|
|
|
|
|
|
+
|
|
def get_start_port(port):
|
|
def get_start_port(port):
|
|
use_port = port
|
|
use_port = port
|
|
while True:
|
|
while True:
|
|
cmd = "netstat -ntlp | grep -v only"
|
|
cmd = "netstat -ntlp | grep -v only"
|
|
output = shell_execute.execute_command_output_all(cmd)
|
|
output = shell_execute.execute_command_output_all(cmd)
|
|
- if output["result"].find(use_port)==-1:
|
|
|
|
|
|
+ if output["result"].find(use_port) == -1:
|
|
break
|
|
break
|
|
else:
|
|
else:
|
|
- use_port = str(int(use_port)+1)
|
|
|
|
|
|
+ use_port = str(int(use_port) + 1)
|
|
|
|
|
|
return use_port
|
|
return use_port
|