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
|
|
|
|
|
from api.utils import network
|
|
|
|
|
|
|
|
|
|
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-09 10:37:49 +08:00
|
|
|
|
def get_process_perc(app_name):
|
2023-03-09 11:15:55 +08:00
|
|
|
|
|
2023-03-10 09:07:02 +08:00
|
|
|
|
process_now = "pulling"
|
2023-03-10 09:18:08 +08:00
|
|
|
|
output = shell_execute.execute_command_output_all("sudo docker image list |grep " + app_name)
|
2023-03-10 09:07:02 +08:00
|
|
|
|
code = output["code"]
|
|
|
|
|
if int(code) == 0 and output["result"] != "":
|
|
|
|
|
process_now = "starting"
|
2023-03-10 10:02:40 +08:00
|
|
|
|
|
2023-03-10 17:45:38 +08:00
|
|
|
|
output = shell_execute.execute_command_output_all("docker inspect " + app_name + "|grep error")
|
2023-03-10 10:22:05 +08:00
|
|
|
|
code = output["code"]
|
2023-03-10 17:45:38 +08:00
|
|
|
|
if int(code) == 0 and output["result"] == "":
|
|
|
|
|
process_now = "Initializing"
|
|
|
|
|
|
2023-03-09 11:15:55 +08:00
|
|
|
|
return process_now
|
2023-03-09 10:37:49 +08:00
|
|
|
|
|
2023-03-08 16:33:24 +08:00
|
|
|
|
def check_vm_resource():
|
|
|
|
|
# 服务器剩余资源是否足够安装,如cpu,内存,硬盘
|
|
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
|
2023-03-08 16:31:25 +08:00
|
|
|
|
def check_app_directory(app_name):
|
2023-02-23 11:52:21 +08:00
|
|
|
|
# 判断/data/apps/app_name是否已经存在,如果已经存在,方法结束
|
2023-02-24 10:23:32 +08:00
|
|
|
|
print("checking dir...")
|
2023-02-22 17:18:24 +08:00
|
|
|
|
path = "/data/apps/"+app_name
|
|
|
|
|
isexsits = os.path.exists(path)
|
2023-03-08 16:26:47 +08:00
|
|
|
|
return isexsits
|
2023-02-22 09:35:21 +08:00
|
|
|
|
|
|
|
|
|
def check_app_compose(app_name):
|
2023-02-24 10:23:32 +08:00
|
|
|
|
print("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-02-22 09:35:21 +08:00
|
|
|
|
#1.判断/data/apps/app_name/.env中的port是否占用,没有被占用,方法结束(network.py的get_start_port方法)
|
2023-03-10 16:47:11 +08:00
|
|
|
|
for port_name in port_dic:
|
|
|
|
|
port_value = network.get_start_port(port_dic[port_name])
|
|
|
|
|
modify_env(path, port_name, port_value)
|
2023-02-23 16:19:55 +08:00
|
|
|
|
print("port check complete")
|
|
|
|
|
return
|
|
|
|
|
|
2023-02-27 11:07:25 +08:00
|
|
|
|
def read_env(path, key):
|
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]
|
|
|
|
|
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-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:
|
|
|
|
|
f.write(file_data)
|