main.py 7.5 KB

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