main.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import traceback
  4. import signal
  5. import time
  6. import jimit as ji
  7. import json
  8. import os
  9. import threading
  10. from flask import g, request, redirect, url_for, Response
  11. from models import Utils
  12. from models.event_processor import EventProcessor
  13. from models.initialize import app, logger, q_ws, Init
  14. import api_route_table
  15. import views_route_table
  16. from models import Database as db
  17. from models import Config
  18. from api.boot_job import blueprint as boot_job_blueprint
  19. from api.boot_job import blueprints as boot_job_blueprints
  20. from api.operate_rule import blueprint as operate_rule_blueprint
  21. from api.operate_rule import blueprints as operate_rule_blueprints
  22. from api.os_template import blueprint as os_template_blueprint
  23. from api.os_template import blueprints as os_template_blueprints
  24. from api.guest import blueprint as guest_blueprint
  25. from api.guest import blueprints as guest_blueprints
  26. from api.disk import blueprint as disk_blueprint
  27. from api.disk import blueprints as disk_blueprints
  28. from api.config import blueprint as config_blueprint
  29. from api.log import blueprint as log_blueprint
  30. from api.log import blueprints as log_blueprints
  31. from api.host import blueprint as host_blueprint
  32. from api.host import blueprints as host_blueprints
  33. from api.performance import blueprint as performance_blueprint
  34. from api.performance import blueprints as performance_blueprints
  35. from api.host_performance import blueprint as host_performance_blueprint
  36. from api.host_performance import blueprints as host_performance_blueprints
  37. from views.error_pages import *
  38. from views.config import blueprint as view_config_blueprint
  39. from views.misc import blueprint as view_misc_blueprint
  40. from views.dashboard import blueprint as view_dashboard_blueprint
  41. from views.guest import blueprint as view_guest_blueprint
  42. from views.guest import blueprints as view_guest_blueprints
  43. from views.disk import blueprint as view_disk_blueprint
  44. from views.disk import blueprints as view_disk_blueprints
  45. from views.log import blueprint as view_log_blueprint
  46. from views.log import blueprints as view_log_blueprints
  47. from views.os_template import blueprint as view_os_template_blueprint
  48. from views.os_template import blueprints as view_os_template_blueprints
  49. from views.boot_job import blueprint as view_boot_job_blueprint
  50. from views.boot_job import blueprints as view_boot_job_blueprints
  51. from views.operate_rule import blueprint as view_operate_rule_blueprint
  52. from views.operate_rule import blueprints as view_operate_rule_blueprints
  53. from views.host import blueprint as view_host_blueprint
  54. from views.host import blueprints as view_host_blueprints
  55. from websockify.websocketproxy import WebSocketProxy
  56. __author__ = 'James Iter'
  57. __date__ = '2017/3/31'
  58. __contact__ = 'james.iter.cn@gmail.com'
  59. __copyright__ = '(c) 2017 by James Iter.'
  60. def instantiation_ws_vnc(listen_port, target_host, target_port):
  61. # 用于 Web noVNC 代理
  62. ws = WebSocketProxy(listen_host="0.0.0.0", listen_port=listen_port, target_host=target_host,
  63. target_port=target_port, run_once=True, daemon=True, idle_timeout=10)
  64. ws.start_server()
  65. def ws_engine_for_vnc():
  66. while True:
  67. payload = q_ws.get()
  68. payload = json.loads(payload)
  69. c_pid = os.fork()
  70. if c_pid == 0:
  71. instantiation_ws_vnc(payload['listen_port'], payload['target_host'], payload['target_port'])
  72. # 因为 WebSocketProxy 使用了 daemon 参数,所以当执行到 ws.start_server() 时,会退出其所在的子进程,
  73. # 故而这里设置wait来处理结束的子进程的环境,避免出现僵尸进程。
  74. os.wait()
  75. q_ws.task_done()
  76. def is_not_need_to_auth(endpoint):
  77. not_auth_table = [
  78. 'api_config.r_get',
  79. 'api_config.r_create',
  80. 'v_config.create'
  81. ]
  82. if endpoint in not_auth_table:
  83. return True
  84. return False
  85. @app.before_request
  86. @Utils.dumps2response
  87. def r_before_request():
  88. try:
  89. g.ts = ji.Common.ts()
  90. if not is_not_need_to_auth(request.endpoint) and request.blueprint is not None and request.method != 'OPTIONS':
  91. g.config = Config()
  92. g.config.id = 1
  93. g.config.get()
  94. except ji.JITError, e:
  95. ret = json.loads(e.message)
  96. if ret['state']['code'] == '404':
  97. return redirect(location=url_for('v_config.create'), Response=Response)
  98. return ret
  99. # noinspection PyBroadException
  100. try:
  101. db.init_conn_mysql()
  102. db.init_conn_redis()
  103. app.register_blueprint(boot_job_blueprint)
  104. app.register_blueprint(boot_job_blueprints)
  105. app.register_blueprint(operate_rule_blueprint)
  106. app.register_blueprint(operate_rule_blueprints)
  107. app.register_blueprint(os_template_blueprint)
  108. app.register_blueprint(os_template_blueprints)
  109. app.register_blueprint(guest_blueprint)
  110. app.register_blueprint(guest_blueprints)
  111. app.register_blueprint(disk_blueprint)
  112. app.register_blueprint(disk_blueprints)
  113. app.register_blueprint(config_blueprint)
  114. app.register_blueprint(log_blueprint)
  115. app.register_blueprint(log_blueprints)
  116. app.register_blueprint(host_blueprint)
  117. app.register_blueprint(host_blueprints)
  118. app.register_blueprint(performance_blueprint)
  119. app.register_blueprint(performance_blueprints)
  120. app.register_blueprint(host_performance_blueprint)
  121. app.register_blueprint(host_performance_blueprints)
  122. app.register_blueprint(view_config_blueprint)
  123. app.register_blueprint(view_misc_blueprint)
  124. app.register_blueprint(view_dashboard_blueprint)
  125. app.register_blueprint(view_guest_blueprint)
  126. app.register_blueprint(view_guest_blueprints)
  127. app.register_blueprint(view_disk_blueprint)
  128. app.register_blueprint(view_disk_blueprints)
  129. app.register_blueprint(view_log_blueprint)
  130. app.register_blueprint(view_log_blueprints)
  131. app.register_blueprint(view_os_template_blueprint)
  132. app.register_blueprint(view_os_template_blueprints)
  133. app.register_blueprint(view_boot_job_blueprint)
  134. app.register_blueprint(view_boot_job_blueprints)
  135. app.register_blueprint(view_operate_rule_blueprint)
  136. app.register_blueprint(view_operate_rule_blueprints)
  137. app.register_blueprint(view_host_blueprint)
  138. app.register_blueprint(view_host_blueprints)
  139. except:
  140. logger.error(traceback.format_exc())
  141. if __name__ == '__main__':
  142. # noinspection PyBroadException
  143. try:
  144. signal.signal(signal.SIGTERM, Utils.signal_handle)
  145. signal.signal(signal.SIGINT, Utils.signal_handle)
  146. pid = os.fork()
  147. if pid == 0:
  148. ws_engine_for_vnc()
  149. else:
  150. # 父进程退出时,会清理所有提前退出的子进程的环境。所以这里无需对子进程做等待操作。
  151. # 即:即使子进程提前退出,且因父进程没有做wait处理,使其变成了僵尸进程。但当父进程退出时,会对因其所产生的僵尸进程做统一清理操作。
  152. threads = []
  153. t_ = threading.Thread(target=EventProcessor.launch, args=())
  154. threads.append(t_)
  155. t_ = threading.Thread(target=Init.pub_sub_ping_pong, args=())
  156. threads.append(t_)
  157. for t in threads:
  158. t.start()
  159. app.run(host=app.config['listen'], port=app.config['port'], use_reloader=False, threaded=True)
  160. while True:
  161. if Utils.exit_flag:
  162. # 主线程即将结束
  163. break
  164. time.sleep(1)
  165. # 等待子线程结束
  166. for t in threads:
  167. t.join()
  168. print 'Main say bye-bye!'
  169. except:
  170. logger.error(traceback.format_exc())
  171. exit(-1)