Browse Source

install_app v1

Toyga 2 years ago
parent
commit
48bbdd7e3e

+ 35 - 6
appmanage/api/service/manage.py

@@ -1,29 +1,58 @@
 import os, io, sys, platform, shutil, time, subprocess, json, datetime
 
-from api.utils import shell_execute
+from api.utils import shell_execute, network, docker
 
 # 获取所有app的信息
 def get_my_app():
 
     my_cmd = "sudo docker compose ls"
     output = shell_execute.execute_command_output_all(my_cmd)
+    ret = read_output(output)
+    return ret
+
+def install_app(app_name):
+    path = "/data/apps/" + app_name
+    isexsits = os.path.exists(path)
+    ret = ""
+    if not isexsits:
+        ret = "app文件不存在,无法安装!"
+    else:
+        # check port
+        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
+
+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)
-        for i in range(1,num):
+        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+1]
-            app['status_code'] = output_list[3*i+2].split("(")[0]
-            app['status'] = output_list[3*i+3]
+            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
+    return ret

+ 22 - 6
appmanage/api/utils/docker.py

@@ -3,6 +3,7 @@ from api.utils import shell_execute
 from api.utils import network
 
 from dotenv import load_dotenv, find_dotenv
+import dotenv
 from pathlib import Path
 
 def copy_dir(src_path, target_path):
@@ -36,16 +37,31 @@ def create_app_directory(app_name):
     if not os.path.exists("/data"):
         os.makedirs("/data")
         os.makedirs("/data/apps")
-        
+
     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("cp -r /tmp/docker-library/apps/"+app_name+" /data/apps")
 
 def check_app_compose(app_name):
-    path = "/data/apps/" + app_name + ".env"
+    print("check port...")
+    path = "/data/apps/" + app_name + "/.env"
     load_dotenv(find_dotenv(Path.cwd().joinpath(path)))
-    port = os.getenv('APP_HTTP_PORT')
+    http_port = os.getenv('APP_HTTP_PORT')
+    db_port = os.getenv('APP_DB_PORT')
     #1.判断/data/apps/app_name/.env中的port是否占用,没有被占用,方法结束(network.py的get_start_port方法)
-    use_port = network.get_start_port(port)
-    return use_port
+    if http_port != None:
+        print("modify http port...")
+        http_port = network.get_start_port(http_port)
+        dotenv.set_key(path, "APP_HTTP_PORT", http_port)
+    if db_port != None:
+        print("modify db port")
+        db_port = network.get_start_port(db_port)
+        dotenv.set_key(path, "APP_DB_PORT", db_port)
+    print("port check complete")
+    return
+
+
+
+
+

+ 5 - 6
appmanage/api/utils/network.py

@@ -3,15 +3,14 @@ from api.utils import shell_execute
 
 # 根据.env文件提供的port,找出能正常启动的最小port
 def get_start_port(port):
-    print("目前检查"+port+"是否被占用")
     use_port = port
-    output = "ok"
     while True:
-        cmd = "netstat -anp|grep "+use_port
-        output = shell_execute.execute_CommandReturn(cmd)
-        if output == "":
+        print("check port: "+use_port)
+        cmd = "netstat -ntlp | grep -v only"
+        output = shell_execute.execute_command_output_all(cmd)
+        if output["result"].find(use_port)==-1:
             break
         else:
-            use_port = use_port+1
+            use_port = str(int(use_port)+1)
 
     return use_port

+ 2 - 2
appmanage/api/v1/routers/apps.py

@@ -18,8 +18,8 @@ def list_my_apps():
 
 @router.get("/install")
 def install_app(app_name: Optional[str] = None):
-
-    return {}
+    ret = manage.install_app(app_name)
+    return JSONResponse(content=ret)
 
 @router.get("/start")
 def start_app(app_name: Optional[str] = None):