mirror of
https://github.com/Websoft9/websoft9.git
synced 2025-02-03 01:28:39 +08:00
appmanage: add return info, add create directory
This commit is contained in:
parent
312d452a62
commit
1035484263
@ -1,58 +1,82 @@
|
|||||||
import os, io, sys, platform, shutil, time, subprocess, json, datetime
|
import os, io, sys, platform, shutil, time, subprocess, json, datetime
|
||||||
|
import socket
|
||||||
from api.utils import shell_execute, network, docker
|
from api.utils import shell_execute, network, docker
|
||||||
|
from api.model.app import App
|
||||||
|
import dotenv
|
||||||
|
from dotenv import load_dotenv, find_dotenv
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
# 获取所有app的信息
|
# 获取所有app的信息
|
||||||
def get_my_app():
|
def get_my_app(app_name=None):
|
||||||
|
#{"name":"id",...}
|
||||||
|
ip = socket.gethostbyname(socket.gethostname())
|
||||||
|
ret = {}
|
||||||
|
ret["code"] = -1
|
||||||
|
ret["message"] = "app查询失败"
|
||||||
|
ret["data"] = None
|
||||||
|
|
||||||
|
# get all info
|
||||||
|
cmd = "sudo docker compose ls"
|
||||||
|
output = shell_execute.execute_command_output_all(cmd)
|
||||||
|
if int(output["code"]) == 0:
|
||||||
|
output_list = output["result"].split()
|
||||||
|
list = []
|
||||||
|
output_list = output_list[4:]
|
||||||
|
num = int(len(output_list) / 3)
|
||||||
|
for i in range(0, num):
|
||||||
|
name = output_list[3 * i] #name
|
||||||
|
id = 0 #id
|
||||||
|
status = output_list[3 * i + 1].split("(")[0] #status
|
||||||
|
status_code = 0 #status_code
|
||||||
|
volume = output_list[3 * i+2] #volume
|
||||||
|
# get env info
|
||||||
|
path = "/data/apps/" + name + "/.env"
|
||||||
|
load_dotenv(find_dotenv(Path.cwd().joinpath(path)))
|
||||||
|
http_port = os.getenv('APP_HTTP_PORT')
|
||||||
|
db_port = os.getenv('APP_DB_PORT')
|
||||||
|
# get port and url
|
||||||
|
if http_port != None:
|
||||||
|
port = int(http_port)
|
||||||
|
url = "http://"+ip+":"+str(port)
|
||||||
|
elif db_port != None:
|
||||||
|
port = int(db_port)
|
||||||
|
url = "http://" + ip + ":" + str(port)
|
||||||
|
else:
|
||||||
|
port = 0
|
||||||
|
url = "-"
|
||||||
|
# get user_name
|
||||||
|
user_name = os.getenv('APP_USER')
|
||||||
|
if user_name == None:
|
||||||
|
user_name = "-"
|
||||||
|
# get password
|
||||||
|
password = os.getenv('POWER_PASSWORD')
|
||||||
|
if password == None:
|
||||||
|
password = "-"
|
||||||
|
|
||||||
|
app = App(id=id, name=name, status_code=status_code, status=status, port=port, volume=volume, url=url, user_name=user_name, password=password)
|
||||||
|
list.append(app.dict())
|
||||||
|
flag = 0
|
||||||
|
if app_name != None:
|
||||||
|
for app in list:
|
||||||
|
if app["name"] == app_name:
|
||||||
|
list.clear()
|
||||||
|
list.append(app)
|
||||||
|
flag = 1
|
||||||
|
break
|
||||||
|
if app_name == None or flag == 1:
|
||||||
|
ret["code"] = 0
|
||||||
|
ret["message"] = "app查询成功"
|
||||||
|
ret["data"] = list
|
||||||
|
|
||||||
my_cmd = "sudo docker compose ls"
|
|
||||||
output = shell_execute.execute_command_output_all(my_cmd)
|
|
||||||
ret = read_output(output)
|
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
def install_app(app_name):
|
def install_app(app_name):
|
||||||
path = "/data/apps/" + app_name
|
# check directory
|
||||||
isexsits = os.path.exists(path)
|
docker.create_app_directory(app_name)
|
||||||
ret = ""
|
# check port
|
||||||
if not isexsits:
|
docker.check_app_compose(app_name)
|
||||||
ret = "app文件不存在,无法安装!"
|
cmd = "cd /data/apps/"+app_name+" && sudo docker compose up -d"
|
||||||
else:
|
shell_execute.execute_command_output_all(cmd)
|
||||||
# check port
|
ret = get_my_app(app_name)
|
||||||
docker.check_app_compose(app_name)
|
|
||||||
cmd = "cd /data/apps/"+app_name+" && sudo docker compose up -d"
|
|
||||||
shell_execute.execute_command_output_all(cmd)
|
|
||||||
ret = get_app_status(app_name)
|
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
def get_app_status(app_name):
|
|
||||||
cmd = "sudo docker compose ls | grep "+app_name
|
|
||||||
output = shell_execute.execute_command_output_all(cmd)
|
|
||||||
ret = read_output(output)
|
|
||||||
return ret
|
|
||||||
|
|
||||||
def read_output(output):
|
|
||||||
if int(output["code"]) == 0:
|
|
||||||
output_list = output["result"].split()
|
|
||||||
print(output_list)
|
|
||||||
ret = {}
|
|
||||||
list = []
|
|
||||||
num = int(len(output_list)/3)
|
|
||||||
start = 1
|
|
||||||
if num != 1:
|
|
||||||
output_list = output_list[4:]
|
|
||||||
num = num-1
|
|
||||||
for i in range(0 ,num):
|
|
||||||
app = {}
|
|
||||||
app['name'] = output_list[3*i]
|
|
||||||
app['status_code'] = output_list[3*i+1].split("(")[0]
|
|
||||||
app['status'] = output_list[3*i+2]
|
|
||||||
list.append(app)
|
|
||||||
ret["code"] = 0
|
|
||||||
ret["message"] = "app查询成功"
|
|
||||||
ret["data"] = list
|
|
||||||
else:
|
|
||||||
ret = {}
|
|
||||||
ret["code"] = -1
|
|
||||||
ret["message"] = "app查询失败"
|
|
||||||
ret["data"] = None
|
|
||||||
return ret
|
|
@ -6,29 +6,9 @@ from dotenv import load_dotenv, find_dotenv
|
|||||||
import dotenv
|
import dotenv
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
def copy_dir(src_path, target_path):
|
|
||||||
if os.path.isdir(src_path) and os.path.isdir(target_path):
|
|
||||||
filelist_src = os.listdir(src_path)
|
|
||||||
for file in filelist_src:
|
|
||||||
path = os.path.join(os.path.abspath(src_path), file)
|
|
||||||
if os.path.isdir(path):
|
|
||||||
path1 = os.path.join(os.path.abspath(target_path), file)
|
|
||||||
if not os.path.exists(path1):
|
|
||||||
os.mkdir(path1)
|
|
||||||
copy_dir(path, path1)
|
|
||||||
else:
|
|
||||||
with open(path, 'rb') as read_stream:
|
|
||||||
contents = read_stream.read()
|
|
||||||
path1 = os.path.join(target_path, file)
|
|
||||||
with open(path1, 'wb') as write_stream:
|
|
||||||
write_stream.write(contents)
|
|
||||||
return True
|
|
||||||
|
|
||||||
else:
|
|
||||||
return False
|
|
||||||
|
|
||||||
def create_app_directory(app_name):
|
def create_app_directory(app_name):
|
||||||
# 判断/data/apps/app_name是否已经存在,如果已经存在,方法结束
|
# 判断/data/apps/app_name是否已经存在,如果已经存在,方法结束
|
||||||
|
print("checking dir...")
|
||||||
path = "/data/apps/"+app_name
|
path = "/data/apps/"+app_name
|
||||||
isexsits = os.path.exists(path)
|
isexsits = os.path.exists(path)
|
||||||
if isexsits:
|
if isexsits:
|
||||||
@ -39,12 +19,12 @@ def create_app_directory(app_name):
|
|||||||
os.makedirs("/data/apps")
|
os.makedirs("/data/apps")
|
||||||
|
|
||||||
if not os.path.exists("/tmp/docker-library"):
|
if not os.path.exists("/tmp/docker-library"):
|
||||||
shell_execute.execute_command_output_all("git clone https://ghproxy.com/https://github.com/Websoft9/docker-library.git /tmp")
|
shell_execute.execute_command_output_all("git clone https://ghproxy.com/https://github.com/Websoft9/docker-library.git /tmp/docker-library")
|
||||||
|
|
||||||
shell_execute.execute_command_output_all("cp -r /tmp/docker-library/apps/"+app_name+" /data/apps")
|
shell_execute.execute_command_output_all("cp -r /tmp/docker-library/apps/"+app_name+" /data/apps")
|
||||||
|
|
||||||
def check_app_compose(app_name):
|
def check_app_compose(app_name):
|
||||||
print("check port...")
|
print("checking port...")
|
||||||
path = "/data/apps/" + app_name + "/.env"
|
path = "/data/apps/" + app_name + "/.env"
|
||||||
load_dotenv(find_dotenv(Path.cwd().joinpath(path)))
|
load_dotenv(find_dotenv(Path.cwd().joinpath(path)))
|
||||||
http_port = os.getenv('APP_HTTP_PORT')
|
http_port = os.getenv('APP_HTTP_PORT')
|
||||||
|
Loading…
Reference in New Issue
Block a user