template.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 rrd import app
  17. from flask import g, render_template, request, jsonify
  18. from rrd.model.portal.template import Template
  19. from rrd.model.portal.strategy import Strategy
  20. from rrd.model.portal.action import Action
  21. from rrd.model.portal.grp_tpl import GrpTpl
  22. from rrd.model.portal.host_group import HostGroup
  23. from rrd.utils.logger import logging
  24. log = logging.getLogger(__file__)
  25. @app.route('/portal/template')
  26. def templates_get():
  27. page = int(request.args.get('p', 1))
  28. limit = int(request.args.get('limit', 10))
  29. query = request.args.get('q', '').strip()
  30. mine = request.args.get('mine', '1')
  31. me = g.user.name if mine == '1' else None
  32. vs, total = Template.query(page, limit, query, me)
  33. for v in vs:
  34. v.parent = Template.get(v.parent_id)
  35. return render_template(
  36. 'portal/template/list.html',
  37. data={
  38. 'vs': vs,
  39. 'total': total,
  40. 'query': query,
  41. 'limit': limit,
  42. 'page': page,
  43. 'mine': mine,
  44. }
  45. )
  46. @app.route('/portal/template/create', methods=['POST'])
  47. def template_create_post():
  48. name = request.form['name'].strip()
  49. if not name:
  50. return jsonify(msg='name is blank')
  51. if Template.read('tpl_name=%s', [name]):
  52. return jsonify(msg='name already existent')
  53. tpl_id = Template.insert({'tpl_name': name, 'create_user': g.user.name})
  54. if tpl_id:
  55. return jsonify(msg='', id=tpl_id)
  56. return jsonify(msg='create fail')
  57. @app.route('/portal/template/update/<tpl_id>')
  58. def template_update_get(tpl_id):
  59. tpl_id = int(tpl_id)
  60. t = Template.get(tpl_id)
  61. if not t:
  62. return jsonify(msg='no such template')
  63. t.parent = Template.get(t.parent_id)
  64. ss = Strategy.select_vs(where='tpl_id = %s', params=[tpl_id], order='metric')
  65. t.action = Action.get(t.action_id)
  66. log.debug(t)
  67. return render_template('portal/template/update.html', data={'tpl': t, 'ss': ss})
  68. @app.route('/portal/template/binds/<tpl_id>')
  69. def template_binds_get(tpl_id):
  70. tpl_id = int(tpl_id)
  71. t = Template.get(tpl_id)
  72. if not t:
  73. return jsonify(msg='no such template')
  74. groups = GrpTpl.grp_list(tpl_id)
  75. return render_template('portal/template/groups.html', data={
  76. "gs": groups,
  77. "tpl": t,
  78. })
  79. @app.route('/portal/template/unbind/group')
  80. def template_unbind_group_get():
  81. tpl_id = request.args.get('tpl_id', '')
  82. grp_id = request.args.get('grp_id', '')
  83. if not tpl_id:
  84. return jsonify(msg="tpl_id is blank")
  85. if not grp_id:
  86. return jsonify(msg="grp_id is blank")
  87. GrpTpl.unbind(grp_id, tpl_id)
  88. return jsonify(msg='')
  89. @app.route('/portal/template/unbind/node')
  90. def template_unbind_grp_name_get():
  91. tpl_id = request.args.get('tpl_id', '')
  92. if not tpl_id:
  93. return jsonify(msg="tpl_id is blank")
  94. grp_name = request.args.get('grp_name', '')
  95. if not grp_name:
  96. return jsonify(msg='grp_name is blank')
  97. hg = HostGroup.read('grp_name=%s', [grp_name])
  98. if not hg:
  99. return jsonify(msg='no such host group')
  100. GrpTpl.unbind(hg.id, tpl_id)
  101. return jsonify(msg='')
  102. @app.route('/portal/template/bind/node', methods=['POST'])
  103. def template_bind_node_post():
  104. node = request.form['node'].strip()
  105. tpl_id = request.form['tpl_id'].strip()
  106. if not node:
  107. return jsonify(msg='node is blank')
  108. if not tpl_id:
  109. return jsonify(msg='tpl id is blank')
  110. hg = HostGroup.read('grp_name=%s', [node])
  111. if not hg:
  112. return jsonify(msg='no such node')
  113. GrpTpl.bind(hg.id, tpl_id, g.user.name)
  114. return jsonify(msg="")
  115. @app.route('/portal/template/view/<tpl_id>')
  116. def template_view_get(tpl_id):
  117. tpl_id = int(tpl_id)
  118. t = Template.get(tpl_id)
  119. if not t:
  120. return jsonify(msg='no such template')
  121. t.parent = Template.get(t.parent_id)
  122. ss = Strategy.select_vs(where='tpl_id = %s', params=[tpl_id], order='metric')
  123. t.action = Action.get(t.action_id)
  124. return render_template('portal/template/view.html', data={'tpl': t, 'ss': ss})
  125. @app.route('/portal/template/fork/<tpl_id>')
  126. def template_fork_get(tpl_id):
  127. tpl_id = int(tpl_id)
  128. t = Template.get(tpl_id)
  129. if not t:
  130. return jsonify(msg='no such template')
  131. new_id = t.fork(g.user.name)
  132. if new_id == -1:
  133. return jsonify(msg='name[copy_of_%s] has already existent' % t.tpl_name)
  134. return jsonify(msg='', id=new_id)
  135. @app.route('/portal/template/help')
  136. def template_help_get():
  137. contact = app.config['CONTACT']
  138. return render_template('portal/template/help.html', contact=contact)
  139. @app.route('/portal/template/delete/<tpl_id>')
  140. def template_delete_get(tpl_id):
  141. tpl_id = int(tpl_id)
  142. t = Template.get(tpl_id)
  143. if not t:
  144. return jsonify(msg='no such template')
  145. if not t.writable(g.user):
  146. return jsonify(msg='no permission')
  147. Template.delete_one(tpl_id)
  148. action_id = t.action_id
  149. if action_id:
  150. Action.delete_one(action_id)
  151. Strategy.delete('tpl_id = %s', [tpl_id])
  152. GrpTpl.unbind_tpl(tpl_id)
  153. return jsonify(msg='')
  154. @app.route('/portal/template/rename/<tpl_id>', methods=['POST'])
  155. def template_rename_post(tpl_id):
  156. tpl_id = int(tpl_id)
  157. t = Template.get(tpl_id)
  158. if not t:
  159. return jsonify(msg='no such template')
  160. name = request.form['name'].strip()
  161. parent_id = request.form.get('parent_id', '')
  162. if not parent_id:
  163. parent_id = 0
  164. Template.update_dict({'tpl_name': name, 'parent_id': parent_id}, 'id=%s', [tpl_id])
  165. return jsonify(msg='')
  166. @app.route('/portal/template/action/update/<tpl_id>', methods=['POST'])
  167. def template_action_update_post(tpl_id):
  168. tpl_id = int(tpl_id)
  169. t = Template.get(tpl_id)
  170. if not t:
  171. return jsonify(msg='no such template')
  172. uic = request.form['uic'].strip()
  173. url = request.form['url'].strip()
  174. callback = request.form['callback'].strip()
  175. before_callback_sms = request.form['before_callback_sms'].strip()
  176. before_callback_mail = request.form['before_callback_mail'].strip()
  177. after_callback_sms = request.form['after_callback_sms'].strip()
  178. after_callback_mail = request.form['after_callback_mail'].strip()
  179. if t.action_id > 0:
  180. # update
  181. Action.update_dict(
  182. {
  183. 'uic': uic,
  184. 'url': url,
  185. 'callback': callback,
  186. 'before_callback_sms': before_callback_sms,
  187. 'before_callback_mail': before_callback_mail,
  188. 'after_callback_sms': after_callback_sms,
  189. 'after_callback_mail': after_callback_mail
  190. },
  191. 'id=%s',
  192. [t.action_id]
  193. )
  194. else:
  195. # insert
  196. action_id = Action.insert({
  197. 'uic': uic,
  198. 'url': url,
  199. 'callback': callback,
  200. 'before_callback_sms': before_callback_sms,
  201. 'before_callback_mail': before_callback_mail,
  202. 'after_callback_sms': after_callback_sms,
  203. 'after_callback_mail': after_callback_mail
  204. })
  205. if action_id <= 0:
  206. return jsonify(msg='insert action fail')
  207. Template.update_dict({'action_id': action_id}, 'id=%s', [t.id])
  208. return jsonify(msg='')