host.py 4.6 KB

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