host.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import json
  4. import requests
  5. from flask import Blueprint, request, url_for
  6. import jimit as ji
  7. from jimvc.models import app_config
  8. from jimvc.models import Database as db
  9. from jimvc.models import Utils, Rules, Host
  10. __author__ = 'James Iter'
  11. __date__ = '2017/5/30'
  12. __contact__ = 'james.iter.cn@gmail.com'
  13. __copyright__ = '(c) 2017 by James Iter.'
  14. blueprint = Blueprint(
  15. 'api_host',
  16. __name__,
  17. url_prefix='/api/host'
  18. )
  19. blueprints = Blueprint(
  20. 'api_hosts',
  21. __name__,
  22. url_prefix='/api/hosts'
  23. )
  24. @Utils.dumps2response
  25. def r_nonrandom(hosts_name, random):
  26. args_rules = [
  27. Rules.HOSTS_NAME.value
  28. ]
  29. try:
  30. ji.Check.previewing(args_rules, {args_rules[0][1]: hosts_name})
  31. if str(random).lower() in ['false', '0']:
  32. random = False
  33. else:
  34. random = True
  35. ret = dict()
  36. ret['state'] = ji.Common.exchange_state(20000)
  37. Host.set_allocation_mode(hosts_name=hosts_name.split(','), random=random)
  38. ret['data'] = Host.get_all()
  39. return ret
  40. except ji.PreviewingError, e:
  41. return json.loads(e.message)
  42. @Utils.dumps2response
  43. def r_get(nodes_id):
  44. args_rules = [
  45. Rules.IDS.value
  46. ]
  47. try:
  48. ji.Check.previewing(args_rules, {args_rules[0][1]: nodes_id})
  49. ret = dict()
  50. ret['state'] = ji.Common.exchange_state(20000)
  51. ret['data'] = list()
  52. if -1 == nodes_id.find(','):
  53. node_id = nodes_id
  54. if db.r.hexists(app_config['hosts_info'], node_id):
  55. v = json.loads(db.r.hget(app_config['hosts_info'], node_id))
  56. v = Host.alive_check(v)
  57. v['node_id'] = node_id
  58. ret['data'] = v
  59. else:
  60. for node_id in nodes_id.split(','):
  61. if db.r.hexists(app_config['hosts_info'], node_id):
  62. v = json.loads(db.r.hget(app_config['hosts_info'], node_id))
  63. v = Host.alive_check(v)
  64. v['node_id'] = node_id
  65. ret['data'].append(v)
  66. if ret['data'].__len__() > 1:
  67. ret['data'].sort(key=lambda _k: _k['boot_time'])
  68. return ret
  69. except ji.PreviewingError, e:
  70. return json.loads(e.message)
  71. @Utils.dumps2response
  72. def r_get_by_filter():
  73. try:
  74. ret = dict()
  75. ret['state'] = ji.Common.exchange_state(20000)
  76. ret['data'] = list()
  77. alive = None
  78. if 'alive' in request.args:
  79. alive = request.args['alive']
  80. if str(alive).lower() in ['false', '0']:
  81. alive = False
  82. else:
  83. alive = True
  84. for host in Host.get_all():
  85. if alive is not None and alive is not host['alive']:
  86. continue
  87. ret['data'].append(host)
  88. return ret
  89. except ji.PreviewingError, e:
  90. return json.loads(e.message)
  91. @Utils.dumps2response
  92. def r_content_search():
  93. keyword = request.args.get('keyword', '')
  94. args_rules = [
  95. Rules.KEYWORD.value
  96. ]
  97. try:
  98. ji.Check.previewing(args_rules, {'keyword': keyword})
  99. ret = dict()
  100. ret['state'] = ji.Common.exchange_state(20000)
  101. ret['data'] = list()
  102. for host in Host.get_all():
  103. if -1 != host['hostname'].lower().find(keyword.lower()):
  104. ret['data'].append(host)
  105. return ret
  106. except ji.PreviewingError, e:
  107. return json.loads(e.message)
  108. @Utils.dumps2response
  109. def r_delete(nodes_id):
  110. args_rules = [
  111. Rules.IDS.value
  112. ]
  113. try:
  114. ji.Check.previewing(args_rules, {args_rules[0][1]: nodes_id})
  115. ret = dict()
  116. ret['state'] = ji.Common.exchange_state(20000)
  117. ret['data'] = list()
  118. if -1 == nodes_id.find(','):
  119. node_id = nodes_id
  120. if db.r.hexists(app_config['hosts_info'], node_id):
  121. v = json.loads(db.r.hget(app_config['hosts_info'], node_id))
  122. v['node_id'] = node_id
  123. ret['data'] = v
  124. db.r.hdel(app_config['hosts_info'], node_id)
  125. else:
  126. for node_id in nodes_id.split(','):
  127. if db.r.hexists(app_config['hosts_info'], node_id):
  128. v = json.loads(db.r.hget(app_config['hosts_info'], node_id))
  129. v['node_id'] = node_id
  130. ret['data'].append(v)
  131. db.r.hdel(app_config['hosts_info'], node_id)
  132. if ret['data'].__len__() > 1:
  133. ret['data'].sort(key=lambda _k: _k['boot_time'])
  134. return ret
  135. except ji.PreviewingError, e:
  136. return json.loads(e.message)
  137. @Utils.dumps2response
  138. def r_show():
  139. args = list()
  140. page = int(request.args.get('page', 1))
  141. page_size = int(request.args.get('page_size', 100))
  142. keyword = request.args.get('keyword', None)
  143. if page is not None:
  144. args.append('page=' + page.__str__())
  145. if page_size is not None:
  146. args.append('page_size=' + page_size.__str__())
  147. if keyword is not None:
  148. args.append('keyword=' + keyword.__str__())
  149. hosts_url = url_for('api_hosts.r_get_by_filter', _external=True)
  150. if keyword is not None:
  151. hosts_url = url_for('api_hosts.r_content_search', _external=True)
  152. if args.__len__() > 0:
  153. hosts_url = hosts_url + '?' + '&'.join(args)
  154. hosts_ret = requests.get(url=hosts_url, cookies=request.cookies)
  155. hosts_ret = json.loads(hosts_ret.content)
  156. ret = dict()
  157. ret['state'] = ji.Common.exchange_state(20000)
  158. ret['data'] = {
  159. 'hosts': hosts_ret['data'],
  160. 'keyword': keyword
  161. }
  162. return ret