manage.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import os, io, sys, platform, shutil, time, subprocess, json, datetime
  2. from api.utils import shell_execute, network, docker
  3. # 获取所有app的信息
  4. def get_my_app():
  5. my_cmd = "sudo docker compose ls"
  6. output = shell_execute.execute_command_output_all(my_cmd)
  7. ret = read_output(output)
  8. return ret
  9. def install_app(app_name):
  10. path = "/data/apps/" + app_name
  11. isexsits = os.path.exists(path)
  12. ret = ""
  13. if not isexsits:
  14. ret = "app文件不存在,无法安装!"
  15. else:
  16. # check port
  17. docker.check_app_compose(app_name)
  18. cmd = "cd /data/apps/"+app_name+" && sudo docker compose up -d"
  19. shell_execute.execute_command_output_all(cmd)
  20. ret = get_app_status(app_name)
  21. return ret
  22. def get_app_status(app_name):
  23. cmd = "sudo docker compose ls | grep "+app_name
  24. output = shell_execute.execute_command_output_all(cmd)
  25. ret = read_output(output)
  26. return ret
  27. def read_output(output):
  28. if int(output["code"]) == 0:
  29. output_list = output["result"].split()
  30. print(output_list)
  31. ret = {}
  32. list = []
  33. num = int(len(output_list)/3)
  34. start = 1
  35. if num != 1:
  36. output_list = output_list[4:]
  37. num = num-1
  38. for i in range(0 ,num):
  39. app = {}
  40. app['name'] = output_list[3*i]
  41. app['status_code'] = output_list[3*i+1].split("(")[0]
  42. app['status'] = output_list[3*i+2]
  43. list.append(app)
  44. ret["code"] = 0
  45. ret["message"] = "app查询成功"
  46. ret["data"] = list
  47. else:
  48. ret = {}
  49. ret["code"] = -1
  50. ret["message"] = "app查询失败"
  51. ret["data"] = None
  52. return ret