manage.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. import os
  2. import io
  3. import sys
  4. import platform
  5. import shutil
  6. import time
  7. import subprocess
  8. import json
  9. import datetime
  10. import socket
  11. import re
  12. from threading import Thread
  13. from api.utils import shell_execute, docker, const
  14. from api.model.app import App
  15. from api.model.response import Response
  16. from api.utils import lock
  17. # 获取所有app的信息
  18. def get_my_app():
  19. ret = Response(code=const.RETURN_FAIL, message="App query failed!")
  20. # get all info
  21. cmd = "docker compose ls -a --format json"
  22. output = shell_execute.execute_command_output_all(cmd)
  23. if int(output["code"]) == 0:
  24. output_list = json.loads(output["result"])
  25. app_list, has_add = get_apps_from_compose(output_list)
  26. list = get_apps_from_queue(app_list, has_add)
  27. ret = Response(code=const.RETURN_SUCCESS, message="The app query is successful.", data=list)
  28. ret = ret.dict()
  29. return ret
  30. # 获取具体某个app的信息
  31. def get_app_detail(app_id):
  32. ret = {}
  33. ret['code'] = const.RETURN_FAIL
  34. ret['message'] = 'App query failed!'
  35. ret['data'] = None
  36. if docker.check_app_id(app_id):
  37. # get all info
  38. cmd = "docker compose ls -a --format json"
  39. output = shell_execute.execute_command_output_all(cmd)
  40. if int(output["code"]) == 0:
  41. output_list = json.loads(output["result"])
  42. app_list, has_add = get_apps_from_compose(output_list)
  43. list = get_apps_from_queue(app_list, has_add)
  44. flag = 0
  45. app_info = None
  46. for app in list:
  47. if app["app_id"] == app_id:
  48. list.clear()
  49. list.append(app)
  50. app_info = app
  51. flag = 1
  52. break
  53. if flag == 1:
  54. ret['code'] = const.RETURN_SUCCESS
  55. ret['message'] = "The app query is successful."
  56. ret['data'] = app_info
  57. else:
  58. ret['message'] = "AppID is not legal!"
  59. return ret
  60. # 查询某个正在安装的app的 具体状态:waiting(等待安装)pulling(拉取镜像)initializing(初始化)running(正常运行)
  61. def install_app_process(app_id):
  62. app_name = split_app_id(app_id)
  63. var_path = "/data/apps/" + app_name + "/variables.json"
  64. real_name = docker.read_var(var_path, 'name')
  65. if docker.check_app_directory(real_name):
  66. percentage = docker.get_process_perc(app_name, real_name)
  67. ret = Response(code=const.RETURN_SUCCESS, message=percentage)
  68. ret = ret.dict()
  69. else:
  70. ret = Response(code=const.RETURN_FAIL, message="This app is not currently installed.")
  71. ret = ret.dict()
  72. return ret
  73. def install_app(app_name, customer_app_name, app_version):
  74. ret = Response(code=const.RETURN_FAIL, message=" ")
  75. ret.code, ret.message = check_app(app_name, customer_app_name, app_version)
  76. if ret.code == const.RETURN_SUCCESS:
  77. ret.code, ret.message = prepare_app(app_name, customer_app_name)
  78. if ret.code == const.RETURN_SUCCESS:
  79. t1 = Thread(target=install_app_job, args=(customer_app_name, app_version,))
  80. t1.start()
  81. ret.message = "The app is starting, please check again in a few minutes."
  82. ret = ret.dict()
  83. return ret
  84. def start_app(app_id):
  85. ret = Response(code=const.RETURN_FAIL, message="")
  86. if docker.check_app_id(app_id):
  87. app_name = split_app_id(app_id)
  88. info, code = if_app_exits(app_name)
  89. if code:
  90. app_path = info.split()[-1].rsplit('/', 1)[0]
  91. docker.check_app_compose(app_path + '/.env')
  92. cmd = "docker compose -f " + app_path + "/docker-compose.yml start"
  93. output = shell_execute.execute_command_output_all(cmd)
  94. if int(output["code"]) == 0:
  95. ret.code = const.RETURN_SUCCESS
  96. ret.message = "The app starts successfully."
  97. else:
  98. ret.message = "The app failed to start!"
  99. else:
  100. ret.message = "The app is not installed!"
  101. else:
  102. ret.message = "AppID is not legal!"
  103. ret = ret.dict()
  104. return ret
  105. def stop_app(app_id):
  106. ret = Response(code=const.RETURN_FAIL, message="")
  107. if docker.check_app_id(app_id):
  108. app_name = split_app_id(app_id)
  109. info, code = if_app_exits(app_name)
  110. if code:
  111. app_path = info.split()[-1].rsplit('/', 1)[0]
  112. cmd = "docker compose -f " + app_path + "/docker-compose.yml stop"
  113. output = shell_execute.execute_command_output_all(cmd)
  114. if int(output["code"]) == 0:
  115. ret.code = const.RETURN_SUCCESS
  116. ret.message = "The app stopped successfully."
  117. else:
  118. ret.message = "App stop failed!"
  119. else:
  120. ret.message = "The app is not installed!"
  121. else:
  122. ret.message = 'AppID is not legal!'
  123. ret = ret.dict()
  124. return ret
  125. def restart_app(app_id):
  126. ret = Response(code=const.RETURN_FAIL, message="")
  127. if docker.check_app_id(app_id):
  128. app_name = split_app_id(app_id)
  129. info, code = if_app_exits(app_name)
  130. if code:
  131. app_path = info.split()[-1].rsplit('/', 1)[0]
  132. cmd = "docker compose -f " + app_path + "/docker-compose.yml restart"
  133. output = shell_execute.execute_command_output_all(cmd)
  134. if int(output["code"]) == 0:
  135. ret.code = const.RETURN_SUCCESS
  136. ret.message = "The app restarts successfully."
  137. else:
  138. ret.message = "App restart failed!"
  139. else:
  140. ret.message = "The app is not installed!"
  141. else:
  142. ret.message = 'AppID is not legal!'
  143. ret = ret.dict()
  144. return ret
  145. def uninstall_app(app_id):
  146. ret = Response(code=const.RETURN_FAIL, message="")
  147. if docker.check_app_id(app_id):
  148. info, code = if_app_exits(app_name)
  149. if code:
  150. app_path = info.split()[-1].rsplit('/', 1)[0]
  151. if_stopped = stop_app(app_id) # stop_app
  152. app_name = split_app_id(app_id)
  153. if if_stopped["code"] == 0:
  154. cmd = "docker compose -f " + app_path + "/docker-compose.yml down -v"
  155. lib_path = '/data/library/apps/' + app_name
  156. if app_path != lib_path:
  157. cmd = cmd + " && sudo rm -rf " + app_path
  158. output = shell_execute.execute_command_output_all(cmd)
  159. if int(output["code"]) == 0:
  160. ret.code = 0
  161. ret.message = "The app is deleted successfully"
  162. else:
  163. ret.message = "App deletion failed!"
  164. else:
  165. ret.message = if_stopped["message"]
  166. else:
  167. ret.message = 'AppID is not legal!'
  168. ret = ret.dict()
  169. return ret
  170. def check_app(app_name, customer_app_name, app_version):
  171. message = " "
  172. code = const.RETURN_FAIL
  173. if app_name == None or customer_app_name == None or app_version == None:
  174. message = "Please fill in the APP information completely!"
  175. elif not docker.check_app_directory(app_name):
  176. message = "Installing the app is not supported!"
  177. elif re.match('^[a-z0-9]+$', customer_app_name) == None:
  178. message = "App names must be lowercase letters and numbers!"
  179. elif docker.check_directory("/data/apps/" + customer_app_name):
  180. message = "The APP name is already in use, please specify a different name to reinstall."
  181. elif not docker.check_vm_resource(app_name):
  182. message = "System resources (memory, CPU, disk) are insufficient, and continuing to install may cause the app to not run or the server to be abnormal!"
  183. else:
  184. code = const.RETURN_SUCCESS
  185. return code, message
  186. def prepare_app(app_name, customer_app_name):
  187. library_path = "/data/library/apps/" + app_name
  188. install_path = "/data/apps/" + customer_app_name
  189. message = " "
  190. code = const.RETURN_SUCCESS
  191. output = shell_execute.execute_command_output_all("cp -r " + library_path + " " + install_path)
  192. if int(output["code"]) != 0:
  193. message = "creating" + customer_app_name + "directory failed!"
  194. code = const.RETURN_FAIL
  195. return code, message
  196. def install_app_job(customer_app_name, app_version):
  197. # write running_apps.txt
  198. file_path = "/data/apps/running_apps.txt"
  199. shell_execute.execute_command_output_all("echo " + customer_app_name + " >> " + file_path)
  200. # modify env
  201. env_path = "/data/apps/" + customer_app_name + "/.env"
  202. docker.modify_env(env_path, 'APP_NAME', customer_app_name)
  203. docker.modify_env(env_path, "APP_VERSION", app_version)
  204. # check port
  205. docker.check_app_compose('/data/apps/' + customer_app_name)
  206. # modify running_apps.txt
  207. cmd = "cd /data/apps/" + customer_app_name + " && sudo docker compose up --pull always -d"
  208. shell_execute.execute_command_output_all(cmd)
  209. # delete
  210. output = shell_execute.execute_command_output_all("sed -n \'/^" + customer_app_name + "/=\' " + file_path)
  211. if int(output["code"]) == 0 and output["result"] != "":
  212. line_num = output["result"].split("\n")[0]
  213. shell_execute.execute_command_output_all("sed -i \'" + line_num + "d\' " + file_path)
  214. def if_app_exits(app_name):
  215. info = ""
  216. cmd = "docker compose ls -a | grep \'/" + app_name + "/\'"
  217. output = shell_execute.execute_command_output_all(cmd)
  218. if int(output["code"]) == -1:
  219. info = output["result"]
  220. return info, False
  221. else:
  222. return info, True
  223. def split_app_id(app_id):
  224. return app_id.split("_")[1]
  225. def get_apps_from_compose(output_list):
  226. ip_result = shell_execute.execute_command_output_all("curl ifconfig.me")
  227. ip = ip_result["result"]
  228. official_app = False
  229. app_list = []
  230. has_add = []
  231. for app_info in output_list:
  232. volume = app_info["ConfigFiles"] # volume
  233. app_path = volume.rsplit('/', 1)[0]
  234. app_name = volume.split('/')[-2]
  235. official_app_path = "/data/apps/" + app_name
  236. var_path = app_path + "/variables.json"
  237. if app_path == official_app_path:
  238. official_app = True
  239. real_name = docker.read_var(var_path, 'name')
  240. image_url = get_Image_url(real_name)
  241. # get trade_mark
  242. trade_mark = docker.read_var(var_path, 'trademark')
  243. if official_app:
  244. app_id = real_name + "_" + app_name # app_id
  245. else:
  246. app_id = app_name + "_" + app_name # app_id
  247. case = app_info["Status"].split("(")[0] # case
  248. if case == "running":
  249. case_code = const.RETURN_RUNNING # case_code
  250. elif case == "exited":
  251. case = "stop"
  252. case_code = const.RETURN_STOP
  253. elif case == "created":
  254. case_code = const.RETURN_READY
  255. case = "installing"
  256. else:
  257. case_code = const.RETURN_ERROR
  258. # get env info
  259. path = "/data/apps/" + app_name + "/.env"
  260. port = 0
  261. url = "-"
  262. admin_url = "-"
  263. # get port and url
  264. try:
  265. http_port = list(docker.read_env(
  266. path, "APP_HTTP_PORT").values())[0]
  267. port = int(http_port)
  268. easy_url = "http://" + ip + ":" + str(port)
  269. url = get_url(real_name, easy_url)
  270. admin_url = get_admin_url(real_name, url)
  271. except IndexError:
  272. try:
  273. db_port = list(docker.read_env(
  274. path, "APP_DB.*_PORT").values())[0]
  275. port = int(db_port)
  276. except IndexError:
  277. pass
  278. # get user_name
  279. user_name = "-"
  280. try:
  281. user_name = list(docker.read_env(path, "APP_USER").values())[0]
  282. except IndexError:
  283. pass
  284. # get password
  285. password = "-"
  286. try:
  287. password = list(docker.read_env(
  288. path, "POWER_PASSWORD").values())[0]
  289. except IndexError:
  290. pass
  291. has_add.append(app_name)
  292. app = App(app_id=app_id, name=real_name, customer_name=app_name, status_code=case_code, status=case, port=port,
  293. volume=volume, url=url,
  294. image_url=image_url, admin_url=admin_url, trade_mark=trade_mark, user_name=user_name,
  295. password=password, official_app=official_app)
  296. app_list.append(app.dict())
  297. return app_list, has_add
  298. def get_apps_from_queue(app_list, has_add):
  299. file_path = "/data/apps/running_apps.txt"
  300. if docker.check_directory(file_path):
  301. output = shell_execute.execute_command_output_all("cat " + file_path)
  302. apps = output["result"].split("\n")
  303. for running_app_name in apps:
  304. running_app_name = re.sub("\n", "", running_app_name)
  305. if running_app_name not in has_add and running_app_name != "":
  306. var_path = "/data/apps/" + running_app_name + "/variables.json"
  307. trade_mark = docker.read_var(var_path, 'trademark')
  308. real_name = docker.read_var(var_path, 'name')
  309. image_url = get_Image_url(real_name)
  310. app = App(app_id=real_name + "_" + running_app_name, name=real_name, customer_name=running_app_name,
  311. status_code=const.RETURN_READY, status="installing", port=0, volume="-",
  312. url="-", image_url=image_url, admin_url="-", trade_mark=trade_mark, user_name="-",
  313. password="-", official_app=True)
  314. app_list.append(app.dict())
  315. return app_list
  316. def get_Image_url(app_name):
  317. image_url = "/static/" + app_name + "-websoft9.png"
  318. return image_url
  319. def get_url(app_name, easy_url):
  320. url = easy_url
  321. if app_name == "joomla":
  322. url = easy_url + "/administrator"
  323. elif app_name == "other":
  324. url = easy_url + "/administrator"
  325. else:
  326. url = easy_url
  327. return url
  328. def get_admin_url(app_name, url):
  329. admin_url = "-"
  330. if app_name == "wordpress":
  331. admin_url = url + "/wp-admin"
  332. elif app_name == "other":
  333. admin_url = url + "/admin"
  334. else:
  335. admin_url = "-"
  336. return admin_url