docker.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import os, io, sys, platform, shutil, time, json, datetime
  2. import re,docker,requests
  3. from api.utils import shell_execute
  4. from api.utils import network
  5. from dotenv import load_dotenv, find_dotenv
  6. import dotenv
  7. from pathlib import Path
  8. def get_process_perc(app_name):
  9. process_now = "pulling"
  10. output = shell_execute.execute_command_output_all("sudo docker image list |grep " + app_name)
  11. code = output["code"]
  12. if int(code) == 0 and output["result"] != "":
  13. process_now = "starting"
  14. output = shell_execute.execute_command_output_all("sudo docker compose ls |grep " + app_name)
  15. code = output["code"]
  16. if int(code) == 0 and output["result"] != "":
  17. process_now = "running"
  18. return process_now
  19. def check_vm_resource():
  20. # 服务器剩余资源是否足够安装,如cpu,内存,硬盘
  21. return true
  22. def check_app_directory(app_name):
  23. # 判断/data/apps/app_name是否已经存在,如果已经存在,方法结束
  24. print("checking dir...")
  25. path = "/data/apps/"+app_name
  26. isexsits = os.path.exists(path)
  27. return isexsits
  28. def check_app_compose(app_name):
  29. print("checking port...")
  30. path = "/data/apps/" + app_name + "/.env"
  31. port_dic = read_env(path, "APP_.*_PORT")
  32. #1.判断/data/apps/app_name/.env中的port是否占用,没有被占用,方法结束(network.py的get_start_port方法)
  33. for port_name in port_dic:
  34. port_value = network.get_start_port(port_dic[port_name])
  35. modify_env(path, port_name, port_value)
  36. print("port check complete")
  37. return
  38. def read_env(path, key):
  39. output = shell_execute.execute_command_output_all("cat " + path + "|grep "+ key)
  40. code = output["code"]
  41. env_dic = {}
  42. if int(code) == 0 and output["result"] != "":
  43. ret = output["result"]
  44. env_list = ret.split()
  45. for env in env_list:
  46. env_dic[env.split("=")[0]] = env.split("=")[1]
  47. return env_dic
  48. def modify_env(path, env_name, value):
  49. file_data = ""
  50. with open(path, "r", encoding="utf-8") as f:
  51. for line in f:
  52. if re.match(env_name, line) != None:
  53. env_name = line.split("=")[0]
  54. line = line.replace(line, env_name + "=" + value+"\n")
  55. file_data += line
  56. with open(path, "w", encoding="utf-8") as f:
  57. f.write(file_data)