host.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. for k, v in db.r.hgetall(app.config['hosts_info']).items():
  59. v = json.loads(v)
  60. v = Host.alive_check(v)
  61. v['node_id'] = k
  62. ret['data'].append(v)
  63. if ret['data'].__len__() > 1:
  64. ret['data'].sort(key=lambda _k: _k['boot_time'])
  65. return ret
  66. except ji.PreviewingError, e:
  67. return json.loads(e.message)
  68. @Utils.dumps2response
  69. def r_content_search():
  70. keyword = request.args.get('keyword', '')
  71. args_rules = [
  72. Rules.KEYWORD.value
  73. ]
  74. try:
  75. ji.Check.previewing(args_rules, {'keyword': keyword})
  76. ret = dict()
  77. ret['state'] = ji.Common.exchange_state(20000)
  78. ret['data'] = list()
  79. for k, v in db.r.hgetall(app.config['hosts_info']).items():
  80. v = json.loads(v)
  81. if -1 != v['hostname'].find(keyword):
  82. v = Host.alive_check(v)
  83. v['node_id'] = k
  84. ret['data'].append(v)
  85. if ret['data'].__len__() > 1:
  86. ret['data'].sort(key=lambda _k: _k['boot_time'])
  87. return ret
  88. except ji.PreviewingError, e:
  89. return json.loads(e.message)
  90. @Utils.dumps2response
  91. def r_delete(nodes_id):
  92. args_rules = [
  93. Rules.IDS.value
  94. ]
  95. try:
  96. ji.Check.previewing(args_rules, {args_rules[0][1]: nodes_id})
  97. ret = dict()
  98. ret['state'] = ji.Common.exchange_state(20000)
  99. ret['data'] = list()
  100. if -1 == nodes_id.find(','):
  101. node_id = nodes_id
  102. if db.r.hexists(app.config['hosts_info'], node_id):
  103. v = json.loads(db.r.hget(app.config['hosts_info'], node_id))
  104. v['node_id'] = node_id
  105. ret['data'] = v
  106. db.r.hdel(app.config['hosts_info'], node_id)
  107. db.r.srem(app.config['compute_nodes_hostname_key'], v['hostname'])
  108. else:
  109. for node_id in nodes_id.split(','):
  110. if db.r.hexists(app.config['hosts_info'], node_id):
  111. v = json.loads(db.r.hget(app.config['hosts_info'], node_id))
  112. v['node_id'] = node_id
  113. ret['data'].append(v)
  114. db.r.hdel(app.config['hosts_info'], node_id)
  115. db.r.srem(app.config['compute_nodes_hostname_key'], v['hostname'])
  116. if ret['data'].__len__() > 1:
  117. ret['data'].sort(key=lambda _k: _k['boot_time'])
  118. return ret
  119. except ji.PreviewingError, e:
  120. return json.loads(e.message)