host.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. # -*- coding:utf-8 -*-
  2. __author__ = 'Ulric Qin'
  3. from flask import jsonify, request, render_template, g, make_response
  4. from rrd import app
  5. from rrd.model.portal.host_group import HostGroup
  6. from rrd.model.portal.group_host import GroupHost
  7. from rrd.model.portal.grp_tpl import GrpTpl
  8. from rrd.model.portal.host import Host
  9. from rrd.model.portal.template import Template
  10. @app.route('/portal/group/<group_id>/hosts.txt')
  11. def group_hosts_export(group_id):
  12. group_id = int(group_id)
  13. group = HostGroup.read(where='id = %s', params=[group_id])
  14. if not group:
  15. return jsonify(msg='no such group %s' % group_id)
  16. vs, _ = Host.query(1, 10000000, '', '0', group_id)
  17. names = [v.hostname for v in vs]
  18. response = make_response('\n'.join(names))
  19. response.headers["content-type"] = "text/plain"
  20. return response
  21. @app.route('/portal/group/<group_id>/hosts')
  22. def group_hosts_list(group_id):
  23. g.xbox = request.args.get('xbox', '')
  24. group_id = int(group_id)
  25. group = HostGroup.read(where='id = %s', params=[group_id])
  26. if not group:
  27. return jsonify(msg='no such group %s' % group_id)
  28. page = int(request.args.get('p', 1))
  29. limit = int(request.args.get('limit', 10))
  30. query = request.args.get('q', '')
  31. maintaining = request.args.get('maintaining', '0')
  32. vs, total = Host.query(page, limit, query, maintaining, group_id)
  33. return render_template(
  34. 'portal/host/index.html',
  35. data={
  36. 'vs': vs,
  37. 'total': total,
  38. 'query': query,
  39. 'limit': limit,
  40. 'page': page,
  41. 'maintaining': maintaining,
  42. 'group': group,
  43. }
  44. )
  45. @app.route('/portal/host/remove', methods=['POST'])
  46. def host_remove_post():
  47. group_id = int(request.form['grp_id'].strip())
  48. host_ids = request.form['host_ids'].strip()
  49. GroupHost.unbind(group_id, host_ids)
  50. return jsonify(msg='')
  51. @app.route('/portal/host/maintain', methods=['POST'])
  52. def host_maintain_post():
  53. begin = int(request.form['begin'].strip())
  54. end = int(request.form['end'].strip())
  55. host_ids = request.form['host_ids'].strip()
  56. if begin <= 0 or end <= 0:
  57. return jsonify(msg='begin or end is invalid')
  58. return jsonify(msg=Host.maintain(begin, end, host_ids))
  59. # 取消maintain时间
  60. @app.route('/portal/host/reset', methods=['POST'])
  61. def host_reset_post():
  62. host_ids = request.form['host_ids'].strip()
  63. return jsonify(msg=Host.no_maintain(host_ids))
  64. @app.route('/portal/host/add')
  65. def host_add_get():
  66. group_id = request.args.get('group_id', '')
  67. if not group_id:
  68. return jsonify(msg='no group_id given')
  69. group_id = int(group_id)
  70. group = HostGroup.read('id = %s', [group_id])
  71. if not group:
  72. return jsonify(msg='no such group')
  73. return render_template('portal/host/add.html', group=group)
  74. @app.route('/portal/host/add', methods=['POST'])
  75. def host_add_post():
  76. group_id = request.form['group_id']
  77. if not group_id:
  78. return jsonify(msg='no group_id given')
  79. group_id = int(group_id)
  80. group = HostGroup.read('id = %s', [group_id])
  81. if not group:
  82. return jsonify(msg='no such group')
  83. hosts = request.form['hosts'].strip()
  84. if not hosts:
  85. return jsonify(msg='hosts is blank')
  86. host_arr = hosts.splitlines()
  87. safe_host_arr = [h for h in host_arr if h]
  88. if not safe_host_arr:
  89. return jsonify(msg='hosts is blank')
  90. success = []
  91. failure = []
  92. for h in safe_host_arr:
  93. msg = GroupHost.bind(group_id, h)
  94. if not msg:
  95. success.append('%s<br>' % h)
  96. else:
  97. failure.append('%s %s<br>' % (h, msg))
  98. data = '<div class="alert alert-danger" role="alert">failure:<hr>' + ''.join(
  99. failure) + '</div><div class="alert alert-success" role="alert">success:<hr>' + ''.join(success) + '</div>'
  100. return jsonify(msg='', data=data)
  101. # 展示某个机器bind的group
  102. @app.route('/portal/host/<host_id>/groups')
  103. def host_groups_get(host_id):
  104. host_id = int(host_id)
  105. h = Host.read('id = %s', params=[host_id])
  106. if not h:
  107. return jsonify(msg='no such host')
  108. group_ids = GroupHost.group_ids(h.id)
  109. groups = [HostGroup.read('id = %s', [group_id]) for group_id in group_ids]
  110. return render_template('portal/host/groups.html', groups=groups, host=h)
  111. @app.route('/portal/host/<host_id>/templates')
  112. def host_templates_get(host_id):
  113. host_id = int(host_id)
  114. h = Host.read('id = %s', params=[host_id])
  115. if not h:
  116. return jsonify(msg='no such host')
  117. group_ids = GroupHost.group_ids(h.id)
  118. templates = GrpTpl.tpl_set(group_ids)
  119. for v in templates:
  120. v.parent = Template.get(v.parent_id)
  121. return render_template('portal/host/templates.html', **locals())
  122. @app.route('/portal/host/unbind')
  123. def host_unbind_get():
  124. host_id = request.args.get('host_id', '').strip()
  125. if not host_id:
  126. return jsonify(msg='host_id is blank')
  127. group_id = request.args.get('group_id', '').strip()
  128. if not group_id:
  129. return jsonify(msg='group_id is blank')
  130. GroupHost.unbind(int(group_id), host_id)
  131. return jsonify(msg='')