info_app.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #
  2. # Copyright 2021 The FATE Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import socket
  17. from flask import request
  18. from flask.json import jsonify
  19. from fate_arch.common import FederatedMode
  20. from fate_flow.db.runtime_config import RuntimeConfig
  21. from fate_flow.db.service_registry import ServerRegistry
  22. from fate_flow.settings import API_VERSION, GRPC_PORT, HOST, HTTP_PORT, PARTY_ID
  23. from fate_flow.utils.api_utils import error_response, federated_api, get_json_result
  24. @manager.route('/common', methods=['POST'])
  25. def get_common_info():
  26. return get_json_result(data={
  27. 'version': RuntimeConfig.get_env('FATE'),
  28. 'host': HOST,
  29. 'http_port': HTTP_PORT,
  30. 'grpc_port': GRPC_PORT,
  31. 'party_id': PARTY_ID,
  32. })
  33. @manager.route('/fateboard', methods=['POST'])
  34. def get_fateboard_info():
  35. host = ServerRegistry.FATEBOARD.get('host')
  36. port = ServerRegistry.FATEBOARD.get('port')
  37. if not host or not port:
  38. return error_response(404, 'fateboard is not configured')
  39. return get_json_result(data={
  40. 'host': host,
  41. 'port': port,
  42. })
  43. # TODO: send greetings message using grpc protocol
  44. @manager.route('/eggroll', methods=['POST'])
  45. def get_eggroll_info():
  46. conf = ServerRegistry.FATE_ON_EGGROLL['rollsite']
  47. with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
  48. r = s.connect_ex((conf['host'], conf['port']))
  49. if r != 0:
  50. return error_response(503)
  51. return error_response(200)
  52. @manager.route('/version', methods=['POST'])
  53. @app.route(f'/{API_VERSION}/version/get', methods=['POST'])
  54. def get_version():
  55. module = request.json['module'] if isinstance(request.json, dict) and request.json.get('module') else 'FATE'
  56. version = RuntimeConfig.get_env(module)
  57. if version is None:
  58. return error_response(404, f'unknown module {module}')
  59. return get_json_result(data={
  60. module: version,
  61. 'API': API_VERSION,
  62. })
  63. @manager.route('/party/<dest_party_id>', methods=['POST'])
  64. def get_party_info(dest_party_id):
  65. response = federated_api(
  66. 'party_info', 'POST', '/info/common',
  67. PARTY_ID, dest_party_id, '',
  68. {}, FederatedMode.MULTIPLE,
  69. )
  70. return jsonify(response)
  71. @manager.route('/party/<proxy_party_id>/<dest_party_id>', methods=['POST'])
  72. def get_party_info_from_another_party(proxy_party_id, dest_party_id):
  73. response = federated_api(
  74. 'party_info', 'POST', f'/info/party/{dest_party_id}',
  75. PARTY_ID, proxy_party_id, '',
  76. {}, FederatedMode.MULTIPLE,
  77. )
  78. return jsonify(response)