manage.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. import os, io, sys, platform, shutil, time, subprocess, json, datetime
  2. import socket
  3. from threading import Thread
  4. from api.utils import shell_execute, network, docker, const
  5. from api.model.app import App
  6. from api.model.response import Response
  7. # 获取所有app的信息
  8. def get_my_app(app_name=None):
  9. #{"name":"id",...}
  10. ret = Response(code=const.RETURN_FAIL, message="app查询失败")
  11. # get all info
  12. cmd = "sudo docker compose ls -a"
  13. output = shell_execute.execute_command_output_all(cmd)
  14. if int(output["code"]) == 0:
  15. output_list = output["result"].split("\n")
  16. list = []
  17. output_list = output_list[1:-1]
  18. list = set_app_info(output_list)
  19. flag = 0
  20. if app_name != None:
  21. for app in list:
  22. if app["name"] == app_name:
  23. list.clear()
  24. list.append(app)
  25. flag = 1
  26. break
  27. if app_name == None or flag == 1:
  28. ret = Response(code=const.RETURN_SUCCESS, message="app查询成功", data=list)
  29. ret = ret.dict()
  30. return ret
  31. def set_app_info(output_list):
  32. ip_result = shell_execute.execute_command_output_all("curl ifconfig.me")
  33. ip = ip_result["result"]
  34. app_list = []
  35. id_dic = get_id_dic()
  36. for app_info in output_list:
  37. app_name = app_info.split()[0] # app_name
  38. image_url = "https://libs.websoft9.com/Websoft9/logo/product/" + app_name + "-websoft9.png"
  39. # get trade_mark
  40. trade_mark = get_trade_mark(app_name)
  41. id = 0 # id
  42. id = "0" # id
  43. case = app_info.split()[1].split("(")[0] # case
  44. if case == "running":
  45. case_code = const.RETURN_RUNNING # case_code
  46. try:
  47. id = id_dic[app_name]
  48. except KeyError:
  49. pass
  50. elif case == "exited":
  51. case = "stop"
  52. case_code = const.RETURN_STOP
  53. elif case == "installing":
  54. case_code = const.RETURN_READY
  55. else:
  56. case_code = const.RETURN_ERROR
  57. volume = app_info.split()[-1] # volume
  58. # get env info
  59. path = "/data/apps/" + app_name + "/.env"
  60. port = 0
  61. url = "-"
  62. admin_url = "-"
  63. # get port and url
  64. try:
  65. http_port = list(docker.read_env(path, "APP_HTTP_PORT").values())[0]
  66. port = int(http_port)
  67. easy_url = "http://" + ip + ":" + str(port)
  68. url = get_url(app_name, easy_url)
  69. admin_url = get_admin_url(app_name, url)
  70. except IndexError:
  71. try:
  72. db_port = list(docker.read_env(path, "APP_DB.*_PORT").values())[0]
  73. port = int(db_port)
  74. except IndexError:
  75. pass
  76. # get user_name
  77. user_name = "-"
  78. try:
  79. user_name = list(docker.read_env(path, "APP_USER").values())[0]
  80. except IndexError:
  81. pass
  82. # get password
  83. password = "-"
  84. try:
  85. password = list(docker.read_env(path, "POWER_PASSWORD").values())[0]
  86. except IndexError:
  87. pass
  88. app = App(id=id, name=app_name, status_code=case_code, status=case, port=port, volume=volume, url=url,
  89. image_url=image_url, admin_url=admin_url, trade_mark=trade_mark, user_name=user_name, password=password)
  90. app_list.append(app.dict())
  91. file_path = "/data/apps/running_apps.txt"
  92. if os.path.exists(file_path) and os.path.getsize(file_path):
  93. with open(file_path, "r", encoding="utf-8") as f:
  94. for running_app_name in f:
  95. image_url = "https://libs.websoft9.com/Websoft9/logo/product/" + running_app_name + "-websoft9.png"
  96. trade_mark = get_trade_mark(app_name)
  97. app = App(id="0", name=running_app_name, status_code=const.RETURN_READY, status="installing", port=0, volume="-",
  98. url="-",image_url=image_url, admin_url="-", trade_mark=trade_mark, user_name="-",password="-")
  99. app_list.append(app.dict())
  100. return app_list
  101. def get_id_dic():
  102. output = shell_execute.execute_command_output_all("docker ps")
  103. id_dic = {}
  104. if int(output["code"]) == 0:
  105. id_op = output["result"].split("\n")
  106. id_op = id_op[1:-1]
  107. for i in id_op:
  108. id_name = i.split()[-1]
  109. id = i.split()[0]
  110. id_dic[id_name] = id
  111. return id_dic
  112. def get_trade_mark(app_name):
  113. trade_mark = ""
  114. var_path = "/data/apps/" + app_name + "/variables.json"
  115. try:
  116. f = open(var_path, 'r', encoding='utf-8')
  117. var = json.load(f)
  118. try:
  119. trade_mark = var["trademark"]
  120. except KeyError:
  121. pass
  122. except FileNotFoundError:
  123. pass
  124. return trade_mark
  125. def get_url(app_name,easy_url):
  126. url = easy_url
  127. if app_name == "joomla":
  128. url = easy_url + "/administrator"
  129. elif app_name == "other":
  130. url = easy_url + "/administrator"
  131. else:
  132. url = easy_url
  133. return url
  134. def get_admin_url(app_name,url):
  135. admin_url = "-"
  136. if app_name == "wordpress":
  137. admin_url = url + "/wp-admin"
  138. elif app_name == "other":
  139. admin_url = url + "/admin"
  140. else:
  141. admin_url = "-"
  142. return admin_url
  143. def install_app_process(app_name):
  144. if docker.check_app_directory(app_name):
  145. percentage = docker.get_process_perc(app_name)
  146. ret = Response(code=const.RETURN_SUCCESS, message=percentage)
  147. ret = ret.dict()
  148. else:
  149. ret = Response(code=const.RETURN_FAIL , message="目前没有安装此App")
  150. ret = ret.dict()
  151. return ret
  152. def install_app(app_name, customer_app_name, app_version):
  153. runnging_file_path = "/data/apps/running_apps.txt"
  154. unique_app_path = "/data/apps/" + str(customer_app_name)
  155. # 防止app名重复
  156. if os.path.exists(unique_app_path)
  157. ret = Response(code=const.RETURN_FAIL , message="APP名称已经使用,请指定其他名称重新安装。")
  158. ret = ret.dict()
  159. return ret
  160. if os.path.exists(runnging_file_path) and os.path.getsize(runnging_file_path):
  161. ret = Response(code=const.RETURN_SUCCESS, message="已有应用正在启动,请稍后再试")
  162. ret = ret.dict()
  163. elif docker.check_app_directory(app_name):
  164. if app_name != customer_app_name:
  165. output = shell_execute.execute_command_output_all("cp -r /data/apps/" + app_name + " /data/apps/" + customer_app_name)
  166. if int(output["code"]) != 0:
  167. ret.code = const.RETURN_FAIL
  168. ret.message = "创建" + customer_app_name + "目录失败."
  169. ret = ret.dict()
  170. return ret
  171. # check port
  172. docker.check_app_compose(customer_app_name)
  173. if app_version != None:
  174. path = "/data/apps/"+customer_app_name+"/.env"
  175. docker.modify_env(path, "APP_VERSION", app_version)
  176. t1 = Thread(target=record_and_install_app, args=(customer_app_name,))
  177. t1.start()
  178. ret = Response(code=const.RETURN_SUCCESS, message="应用正在启动中,请过几分钟再查询")
  179. ret = ret.dict()
  180. else:
  181. ret = Response(code=const.RETURN_FAIL , message="目前不支持安装此App")
  182. ret = ret.dict()
  183. return ret
  184. def record_and_install_app(app_name):
  185. # modify running_apps.txt
  186. file_path = "/data/apps/running_apps.txt"
  187. with open(file_path, "w", encoding="utf-8") as f:
  188. f.write(app_name)
  189. cmd = "cd /data/apps/" + app_name + " && sudo docker compose up -d"
  190. shell_execute.execute_command_output_all(cmd)
  191. with open(file_path, "a+", encoding="utf-8") as f:
  192. f.truncate(0)
  193. def if_app_exits(app_name):
  194. cmd = "docker compose ls -a | grep \'"+app_name+"\\b\'"
  195. output = shell_execute.execute_command_output_all(cmd)
  196. if int(output["code"]) == -1:
  197. return False
  198. else:
  199. return True
  200. def start_app(app_name):
  201. ret = Response(code=const.RETURN_FAIL, message="")
  202. if if_app_exits(app_name):
  203. docker.check_app_compose(app_name)
  204. cmd = "docker compose -f /data/apps/"+app_name+"/docker-compose.yml start"
  205. output = shell_execute.execute_command_output_all(cmd)
  206. if int(output["code"]) == 0:
  207. ret.code = const.RETURN_SUCCESS
  208. ret.message = "应用启动成功"
  209. else:
  210. ret.message = "应用启动失败"
  211. else:
  212. ret.message = "应用不存在"
  213. ret = ret.dict()
  214. return ret
  215. def stop_app(app_name):
  216. ret = Response(code=const.RETURN_FAIL, message="")
  217. if if_app_exits(app_name):
  218. cmd = "docker compose -f /data/apps/"+app_name+"/docker-compose.yml stop"
  219. output = shell_execute.execute_command_output_all(cmd)
  220. if int(output["code"]) == 0:
  221. ret.code = const.RETURN_SUCCESS
  222. ret.message = "应用停止成功"
  223. else:
  224. ret.message = "应用停止失败"
  225. else:
  226. ret.message = "应用不存在"
  227. ret = ret.dict()
  228. return ret
  229. def restart_app(app_name):
  230. ret = Response(code=const.RETURN_FAIL, message="")
  231. if if_app_exits(app_name):
  232. cmd = "docker compose -f /data/apps/"+app_name+"/docker-compose.yml restart"
  233. output = shell_execute.execute_command_output_all(cmd)
  234. if int(output["code"]) == 0:
  235. ret.code = const.RETURN_SUCCESS
  236. ret.message = "应用重启成功"
  237. else:
  238. ret.message = "应用重启失败"
  239. else:
  240. ret.message = "应用不存在"
  241. ret = ret.dict()
  242. return ret
  243. def delete_app(app_name, delete_flag):
  244. ret = Response(code=const.RETURN_FAIL, message="")
  245. if_stopped = stop_app(app_name)
  246. if if_stopped["code"] == 0:
  247. if delete_flag == 0:
  248. cmd = "docker compose -f /data/apps/"+app_name+"/docker-compose.yml down"
  249. elif delete_flag == 1:
  250. cmd = "docker compose -f /data/apps/"+app_name+"/docker-compose.yml down -v"
  251. else:
  252. cmd = "docker compose -f /data/apps/"+app_name+"/docker-compose.yml down"
  253. output = shell_execute.execute_command_output_all(cmd)
  254. if int(output["code"]) == 0:
  255. ret.code = 0
  256. ret.message = "应用删除成功"
  257. else:
  258. ret.message = "应用删除失败"
  259. else:
  260. ret.message = if_stopped["message"]
  261. ret = ret.dict()
  262. return ret