host.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. # -*- coding:utf-8 -*-
  2. # Copyright 2017 Xiaomi, Inc.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. __author__ = 'Ulric Qin'
  16. from flask import jsonify, request, render_template, g, make_response
  17. from rrd import app
  18. from rrd.model.portal.host_group import HostGroup
  19. from rrd.model.portal.group_host import GroupHost
  20. from rrd.model.portal.grp_tpl import GrpTpl
  21. from rrd.model.portal.host import Host
  22. from rrd.model.portal.template import Template
  23. @app.route('/portal/group/<group_id>/hosts.txt')
  24. def group_hosts_export(group_id):
  25. group_id = int(group_id)
  26. group = HostGroup.read(where='id = %s', params=[group_id])
  27. if not group:
  28. return jsonify(msg='no such group %s' % group_id)
  29. vs, _ = Host.query(1, 10000000, '', '0', group_id)
  30. names = [v.hostname for v in vs]
  31. response = make_response('\n'.join(names))
  32. response.headers["content-type"] = "text/plain"
  33. return response
  34. @app.route('/portal/group/<group_id>/hosts')
  35. def group_hosts_list(group_id):
  36. g.xbox = request.args.get('xbox', '')
  37. group_id = int(group_id)
  38. group = HostGroup.read(where='id = %s', params=[group_id])
  39. if not group:
  40. return jsonify(msg='no such group %s' % group_id)
  41. page = int(request.args.get('p', 1))
  42. limit = int(request.args.get('limit', 10))
  43. query = request.args.get('q', '')
  44. maintaining = request.args.get('maintaining', '0')
  45. vs, total = Host.query(page, limit, query, maintaining, group_id)
  46. return render_template(
  47. 'portal/host/index.html',
  48. data={
  49. 'vs': vs,
  50. 'total': total,
  51. 'query': query,
  52. 'limit': limit,
  53. 'page': page,
  54. 'maintaining': maintaining,
  55. 'group': group,
  56. }
  57. )
  58. @app.route('/portal/host/remove', methods=['POST'])
  59. def host_remove_post():
  60. group_id = int(request.form['grp_id'].strip())
  61. host_ids = request.form['host_ids'].strip()
  62. GroupHost.unbind(group_id, host_ids)
  63. return jsonify(msg='')
  64. @app.route('/portal/host/maintain', methods=['POST'])
  65. def host_maintain_post():
  66. begin = int(request.form['begin'].strip())
  67. end = int(request.form['end'].strip())
  68. host_ids = request.form['host_ids'].strip()
  69. if begin <= 0 or end <= 0:
  70. return jsonify(msg='begin or end is invalid')
  71. return jsonify(msg=Host.maintain(begin, end, host_ids))
  72. # 取消maintain时间
  73. @app.route('/portal/host/reset', methods=['POST'])
  74. def host_reset_post():
  75. host_ids = request.form['host_ids'].strip()
  76. return jsonify(msg=Host.no_maintain(host_ids))
  77. @app.route('/portal/host/add')
  78. def host_add_get():
  79. group_id = request.args.get('group_id', '')
  80. if not group_id:
  81. return jsonify(msg='no group_id given')
  82. group_id = int(group_id)
  83. group = HostGroup.read('id = %s', [group_id])
  84. if not group:
  85. return jsonify(msg='no such group')
  86. return render_template('portal/host/add.html', group=group)
  87. @app.route('/portal/host/add', methods=['POST'])
  88. def host_add_post():
  89. group_id = request.form['group_id']
  90. if not group_id:
  91. return jsonify(msg='no group_id given')
  92. group_id = int(group_id)
  93. group = HostGroup.read('id = %s', [group_id])
  94. if not group:
  95. return jsonify(msg='no such group')
  96. hosts = request.form['hosts'].strip()
  97. if not hosts:
  98. return jsonify(msg='hosts is blank')
  99. host_arr = hosts.splitlines()
  100. safe_host_arr = [h.strip() for h in host_arr if h]
  101. if not safe_host_arr:
  102. return jsonify(msg='hosts is blank')
  103. success = []
  104. failure = []
  105. for h in safe_host_arr:
  106. msg = GroupHost.bind(group_id, h)
  107. if not msg:
  108. success.append('%s<br>' % h)
  109. else:
  110. failure.append('%s %s<br>' % (h, msg))
  111. data = '<div class="alert alert-danger" role="alert">failure:<hr>' + ''.join(
  112. failure) + '</div><div class="alert alert-success" role="alert">success:<hr>' + ''.join(success) + '</div>'
  113. return jsonify(msg='', data=data)
  114. # 展示某个机器bind的group
  115. @app.route('/portal/host/<host_id>/groups')
  116. def host_groups_get(host_id):
  117. host_id = int(host_id)
  118. h = Host.read('id = %s', params=[host_id])
  119. if not h:
  120. return jsonify(msg='no such host')
  121. group_ids = GroupHost.group_ids(h.id)
  122. groups = [HostGroup.read('id = %s', [group_id]) for group_id in group_ids]
  123. return render_template('portal/host/groups.html', groups=groups, host=h)
  124. @app.route('/portal/host/<host_id>/templates')
  125. def host_templates_get(host_id):
  126. host_id = int(host_id)
  127. h = Host.read('id = %s', params=[host_id])
  128. if not h:
  129. return jsonify(msg='no such host')
  130. group_ids = GroupHost.group_ids(h.id)
  131. templates = GrpTpl.tpl_set(group_ids)
  132. for v in templates:
  133. v.parent = Template.get(v.parent_id)
  134. return render_template('portal/host/templates.html', **locals())
  135. @app.route('/portal/host/unbind')
  136. def host_unbind_get():
  137. host_id = request.args.get('host_id', '').strip()
  138. if not host_id:
  139. return jsonify(msg='host_id is blank')
  140. group_id = request.args.get('group_id', '').strip()
  141. if not group_id:
  142. return jsonify(msg='group_id is blank')
  143. GroupHost.unbind(int(group_id), host_id)
  144. return jsonify(msg='')