host.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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_get(nodes_id):
  25. args_rules = [
  26. Rules.IDS.value
  27. ]
  28. try:
  29. ji.Check.previewing(args_rules, {args_rules[0][1]: nodes_id})
  30. ret = dict()
  31. ret['state'] = ji.Common.exchange_state(20000)
  32. ret['data'] = list()
  33. if -1 == nodes_id.find(','):
  34. node_id = nodes_id
  35. if db.r.hexists(app.config['hosts_info'], node_id):
  36. v = json.loads(db.r.hget(app.config['hosts_info'], node_id))
  37. v = Host.alive_check(v)
  38. v['node_id'] = node_id
  39. ret['data'] = v
  40. else:
  41. for node_id in nodes_id.split(','):
  42. if db.r.hexists(app.config['hosts_info'], node_id):
  43. v = json.loads(db.r.hget(app.config['hosts_info'], node_id))
  44. v = Host.alive_check(v)
  45. v['node_id'] = node_id
  46. ret['data'].append(v)
  47. if ret['data'].__len__() > 1:
  48. ret['data'].sort(key=lambda _k: _k['boot_time'])
  49. return ret
  50. except ji.PreviewingError, e:
  51. return json.loads(e.message)
  52. @Utils.dumps2response
  53. def r_get_by_filter():
  54. try:
  55. ret = dict()
  56. ret['state'] = ji.Common.exchange_state(20000)
  57. ret['data'] = list()
  58. alive = None
  59. if 'alive' in request.args:
  60. alive = request.args['alive']
  61. if str(alive).lower() in ['false', '0']:
  62. alive = False
  63. else:
  64. alive = True
  65. for k, v in db.r.hgetall(app.config['hosts_info']).items():
  66. v = json.loads(v)
  67. v = Host.alive_check(v)
  68. v['node_id'] = k
  69. if alive is not None and alive is not v['alive']:
  70. continue
  71. ret['data'].append(v)
  72. if ret['data'].__len__() > 1:
  73. ret['data'].sort(key=lambda _k: _k['boot_time'])
  74. return ret
  75. except ji.PreviewingError, e:
  76. return json.loads(e.message)
  77. @Utils.dumps2response
  78. def r_content_search():
  79. keyword = request.args.get('keyword', '')
  80. args_rules = [
  81. Rules.KEYWORD.value
  82. ]
  83. try:
  84. ji.Check.previewing(args_rules, {'keyword': keyword})
  85. ret = dict()
  86. ret['state'] = ji.Common.exchange_state(20000)
  87. ret['data'] = list()
  88. for k, v in db.r.hgetall(app.config['hosts_info']).items():
  89. v = json.loads(v)
  90. if -1 != v['hostname'].find(keyword):
  91. v = Host.alive_check(v)
  92. v['node_id'] = k
  93. ret['data'].append(v)
  94. if ret['data'].__len__() > 1:
  95. ret['data'].sort(key=lambda _k: _k['boot_time'])
  96. return ret
  97. except ji.PreviewingError, e:
  98. return json.loads(e.message)
  99. @Utils.dumps2response
  100. def r_delete(nodes_id):
  101. args_rules = [
  102. Rules.IDS.value
  103. ]
  104. try:
  105. ji.Check.previewing(args_rules, {args_rules[0][1]: nodes_id})
  106. ret = dict()
  107. ret['state'] = ji.Common.exchange_state(20000)
  108. ret['data'] = list()
  109. if -1 == nodes_id.find(','):
  110. node_id = nodes_id
  111. if db.r.hexists(app.config['hosts_info'], node_id):
  112. v = json.loads(db.r.hget(app.config['hosts_info'], node_id))
  113. v['node_id'] = node_id
  114. ret['data'] = v
  115. db.r.hdel(app.config['hosts_info'], node_id)
  116. db.r.srem(app.config['compute_nodes_hostname_key'], v['hostname'])
  117. else:
  118. for node_id in nodes_id.split(','):
  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'].append(v)
  123. db.r.hdel(app.config['hosts_info'], node_id)
  124. db.r.srem(app.config['compute_nodes_hostname_key'], v['hostname'])
  125. if ret['data'].__len__() > 1:
  126. ret['data'].sort(key=lambda _k: _k['boot_time'])
  127. return ret
  128. except ji.PreviewingError, e:
  129. return json.loads(e.message)