appmanage: complete check_vm_resource, add id_info

This commit is contained in:
Toyga 2023-03-13 15:37:47 +08:00
parent f8720c3b37
commit ed69bee7d9
3 changed files with 57 additions and 21 deletions

View File

@ -1,7 +1,7 @@
from pydantic import BaseModel from pydantic import BaseModel
class App(BaseModel): class App(BaseModel):
id: int id: str
name: str name: str
trade_mark: str trade_mark: str
status_code: int status_code: int

View File

@ -14,11 +14,10 @@ def get_my_app(app_name=None):
cmd = "sudo docker compose ls -a" cmd = "sudo docker compose ls -a"
output = shell_execute.execute_command_output_all(cmd) output = shell_execute.execute_command_output_all(cmd)
if int(output["code"]) == 0: if int(output["code"]) == 0:
output_list = output["result"].split() output_list = output["result"].split("\n")
list = [] list = []
output_list = output_list[4:] output_list = output_list[1:-1]
num = int(len(output_list) / 3) list = set_app_info(output_list)
list = set_app_info(output_list, num)
flag = 0 flag = 0
if app_name != None: if app_name != None:
for app in list: for app in list:
@ -32,19 +31,25 @@ def get_my_app(app_name=None):
ret = ret.dict() ret = ret.dict()
return ret return ret
def set_app_info(output_list, num): def set_app_info(output_list):
ip_result = shell_execute.execute_command_output_all("curl ifconfig.me") ip_result = shell_execute.execute_command_output_all("curl ifconfig.me")
ip = ip_result["result"] ip = ip_result["result"]
app_list = [] app_list = []
for i in range(0, num): id_dic = get_id_dic()
app_name = output_list[3 * i] # app_name for app_info in output_list:
app_name = app_info.split()[0] # app_name
image_url = "https://libs.websoft9.com/Websoft9/logo/product/" + app_name + "-websoft9.png" image_url = "https://libs.websoft9.com/Websoft9/logo/product/" + app_name + "-websoft9.png"
# get trade_mark # get trade_mark
trade_mark = get_trade_mark(app_name) trade_mark = get_trade_mark(app_name)
id = 0 # id id = 0 # id
case = output_list[3 * i + 1].split("(")[0] # case id = "0" # id
case = app_info.split()[1].split("(")[0] # case
if case == "running": if case == "running":
case_code = const.RETURN_RUNNING # case_code case_code = const.RETURN_RUNNING # case_code
try:
id = id_dic[app_name]
except KeyError:
pass
elif case == "exited": elif case == "exited":
case = "stop" case = "stop"
case_code = const.RETURN_STOP case_code = const.RETURN_STOP
@ -52,11 +57,7 @@ def set_app_info(output_list, num):
case_code = const.RETURN_READY case_code = const.RETURN_READY
else: else:
case_code = const.RETURN_ERROR case_code = const.RETURN_ERROR
volume = output_list[3 * i + 2] # volume volume = app_info.split()[-1] # volume
j = 2
while not volume.startswith("/"):
volume = output_list[3 * i + j]
j = j + 1
# get env info # get env info
path = "/data/apps/" + app_name + "/.env" path = "/data/apps/" + app_name + "/.env"
port = 0 port = 0
@ -98,13 +99,25 @@ def set_app_info(output_list, num):
for running_app_name in f: for running_app_name in f:
image_url = "https://libs.websoft9.com/Websoft9/logo/product/" + running_app_name + "-websoft9.png" image_url = "https://libs.websoft9.com/Websoft9/logo/product/" + running_app_name + "-websoft9.png"
trade_mark = get_trade_mark(app_name) trade_mark = get_trade_mark(app_name)
app = App(id=0, name=running_app_name, status_code=const.RETURN_READY, status="installing", port=0, volume="-", app = App(id="0", name=running_app_name, status_code=const.RETURN_READY, status="installing", port=0, volume="-",
url="-",image_url=image_url, admin_url="-", trade_mark=trade_mark, user_name="-",password="-") url="-",image_url=image_url, admin_url="-", trade_mark=trade_mark, user_name="-",password="-")
app_list.append(app.dict()) app_list.append(app.dict())
return app_list return app_list
def get_id_dic():
output = shell_execute.execute_command_output_all("docker ps")
id_dic = {}
if int(output["code"]) == 0:
id_op = output["result"].split("\n")
id_op = id_op[1:-1]
for i in id_op:
id_name = i.split()[-1]
id = i.split()[0]
id_dic[id_name] = id
return id_dic
def get_trade_mark(app_name): def get_trade_mark(app_name):
trade_mark = "" trade_mark = ""
var_path = "/data/apps/" + app_name + "/variables.json" var_path = "/data/apps/" + app_name + "/variables.json"
try: try:
@ -240,9 +253,9 @@ def delete_app(app_name, delete_flag):
ret = Response(code=const.RETURN_FAIL, message="") ret = Response(code=const.RETURN_FAIL, message="")
if_stopped = stop_app(app_name) if_stopped = stop_app(app_name)
if if_stopped["code"] == 0: if if_stopped["code"] == 0:
if delete_flag == 0 if delete_flag == 0:
cmd = "docker compose -f /data/apps/"+app_name+"/docker-compose.yml down" cmd = "docker compose -f /data/apps/"+app_name+"/docker-compose.yml down"
else if delete_flag == 1 elif delete_flag == 1:
cmd = "docker compose -f /data/apps/"+app_name+"/docker-compose.yml down -v" cmd = "docker compose -f /data/apps/"+app_name+"/docker-compose.yml down -v"
else: else:
cmd = "docker compose -f /data/apps/"+app_name+"/docker-compose.yml down" cmd = "docker compose -f /data/apps/"+app_name+"/docker-compose.yml down"

View File

@ -2,7 +2,7 @@ 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
from api.utils import network from api.utils import network
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
@ -22,9 +22,32 @@ def get_process_perc(app_name):
return process_now return process_now
def check_vm_resource(): def check_vm_resource(app_name):
# 服务器剩余资源是否足够安装如cpu内存硬盘 # 服务器剩余资源是否足够安装如cpu内存硬盘
p.cpu_percent(None) # 第一次返回的结果是0
time.sleep(0.5)
cpu_percent = p.cpu_percent(None)
mem = p.virtual_memory()
mem_total = float(mem.total) / 1024 / 1024 / 1024
mem_free = float(mem.available) / 1024 / 1024 / 1024
disk = p.disk_usage('/')
disk_free = float(disk.free) / 1024 / 1024 / 1024
if cpu_percent>70 or mem_free<1 or disk_free<5:
return False
# read variables.json
memory = ""
var_path = "/data/apps/" + app_name + "/variables.json"
try:
f = open(var_path, 'r', encoding='utf-8')
var = json.load(f)
try:
trade_mark = var["memory"]
except KeyError:
return False
except FileNotFoundError:
return False
if memory == "8" and mem_total<8 and mem_free<4:
return False
return true return true
def check_app_directory(app_name): def check_app_directory(app_name):