2023-02-22 09:35:21 +08:00
|
|
|
|
import os, io, sys, platform, shutil, time, json, datetime
|
2023-02-27 11:07:25 +08:00
|
|
|
|
import re
|
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-02-22 09:35:21 +08:00
|
|
|
|
def create_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)
|
|
|
|
|
if isexsits:
|
|
|
|
|
return
|
2023-02-23 16:19:55 +08:00
|
|
|
|
|
2023-02-23 12:06:45 +08:00
|
|
|
|
if not os.path.exists("/tmp/docker-library"):
|
2023-02-24 10:23:32 +08:00
|
|
|
|
shell_execute.execute_command_output_all("git clone https://ghproxy.com/https://github.com/Websoft9/docker-library.git /tmp/docker-library")
|
2023-02-23 16:19:55 +08:00
|
|
|
|
|
2023-02-23 12:06:45 +08:00
|
|
|
|
shell_execute.execute_command_output_all("cp -r /tmp/docker-library/apps/"+app_name+" /data/apps")
|
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-02-27 11:07:25 +08:00
|
|
|
|
http_port_env, http_port = read_env(path, "APP_HTTP_PORT")
|
|
|
|
|
db_port_env, db_port = read_env(path, "APP_DB.*_PORT")
|
2023-02-22 09:35:21 +08:00
|
|
|
|
#1.判断/data/apps/app_name/.env中的port是否占用,没有被占用,方法结束(network.py的get_start_port方法)
|
2023-02-27 11:07:25 +08:00
|
|
|
|
if http_port != "":
|
|
|
|
|
print("check http port...")
|
2023-02-23 16:19:55 +08:00
|
|
|
|
http_port = network.get_start_port(http_port)
|
2023-03-08 10:08:46 +08:00
|
|
|
|
modify_port(path, http_port_env, http_port)
|
2023-02-27 11:07:25 +08:00
|
|
|
|
if db_port != "":
|
|
|
|
|
print("check db port...")
|
2023-02-23 16:19:55 +08:00
|
|
|
|
db_port = network.get_start_port(db_port)
|
2023-03-08 10:08:46 +08:00
|
|
|
|
modify_port(path, db_port_env, db_port)
|
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):
|
|
|
|
|
output = shell_execute.execute_command_output_all("cat " + path + "|grep "+ key+ "|head -1")
|
|
|
|
|
code = output["code"]
|
|
|
|
|
env = "" #the name of environment var
|
|
|
|
|
ret = "" #the value of environment var
|
|
|
|
|
if int(code) == 0 and output["result"] != "":
|
|
|
|
|
ret = output["result"]
|
|
|
|
|
env = ret.split("=")[0]
|
|
|
|
|
ret = ret.split("=")[1]
|
|
|
|
|
ret = re.sub("'","",ret)
|
|
|
|
|
ret = re.sub("\n","",ret)
|
|
|
|
|
return env, ret
|
|
|
|
|
|
2023-03-08 10:08:46 +08:00
|
|
|
|
def modify_port(path, env_name, port):
|
|
|
|
|
file_data = ""
|
|
|
|
|
with open(path, "r", encoding="utf-8") as f:
|
|
|
|
|
for line in f:
|
|
|
|
|
if env_name in line:
|
|
|
|
|
line = line.replace(line, env_name + "=" + port+"\n")
|
|
|
|
|
file_data += line
|
|
|
|
|
with open(path, "w", encoding="utf-8") as f:
|
|
|
|
|
f.write(file_data)
|