misc.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import jimit as ji
  4. import json
  5. from flask import Blueprint, request
  6. from jimvc.models import app_config, Rules, Token
  7. from jimvc.models import Utils
  8. from jimvc.models import Host
  9. __author__ = 'James Iter'
  10. __date__ = '2018-12-29'
  11. __contact__ = 'james.iter.cn@gmail.com'
  12. __copyright__ = '(c) 2018 by James Iter.'
  13. blueprint = Blueprint(
  14. 'api_misc',
  15. __name__,
  16. url_prefix='/api/misc'
  17. )
  18. blueprints = Blueprint(
  19. 'api_miscs',
  20. __name__,
  21. url_prefix='/api/miscs'
  22. )
  23. @Utils.dumps2response
  24. def r_join(node_id, _token):
  25. # 如果该 node_id 已经被加入,那么要用户先去计算节点处删除作废的节点后,再添加。
  26. ret = dict()
  27. ret['state'] = ji.Common.exchange_state(20000)
  28. ret['data'] = dict()
  29. args_rules = [
  30. Rules.NODE_ID.value,
  31. Rules.TOKEN.value
  32. ]
  33. try:
  34. ji.Check.previewing(args_rules, {'node_id': node_id, 'token': _token})
  35. token = Token()
  36. token.token = token
  37. # 检验 token 有效性
  38. if not token.valid():
  39. ret['state'] = ji.Common.exchange_state(41208)
  40. return ret
  41. nodes_id = list()
  42. hosts = Host.get_all()
  43. for host in hosts:
  44. nodes_id.append(host['node_id'])
  45. # 检测 node_id 是否已经存在
  46. if node_id in nodes_id:
  47. ret['state'] = ji.Common.exchange_state(40901)
  48. else:
  49. ret['data']['redis_host'] = request.host
  50. ret['data']['redis_port'] = app_config.get('redis_port', 6379)
  51. ret['data']['redis_password'] = app_config.get('redis_password', '')
  52. ret['data']['redis_dbid'] = app_config.get('redis_dbid', 0)
  53. return ret
  54. except ji.PreviewingError, e:
  55. return json.loads(e.message)