flow.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #
  2. # Copyright 2019 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 os
  17. from pathlib import Path
  18. import click
  19. from ruamel import yaml
  20. from flow_client.flow_cli.commands import (
  21. checkpoint, component, data, job, key, model, privilege, provider, queue,
  22. resource, server, service, table, tag, task, template, test, tracking,
  23. )
  24. from flow_client.flow_cli.utils.cli_utils import prettify
  25. CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])
  26. @click.group(short_help='Fate Flow Client', context_settings=CONTEXT_SETTINGS)
  27. @click.pass_context
  28. def flow_cli(ctx):
  29. '''
  30. Fate Flow Client
  31. '''
  32. ctx.ensure_object(dict)
  33. if ctx.invoked_subcommand == 'init':
  34. return
  35. with open(os.path.join(os.path.dirname(__file__), 'settings.yaml'), 'r') as fin:
  36. config = yaml.safe_load(fin)
  37. if not config.get('api_version'):
  38. raise ValueError('api_version in config is required')
  39. ctx.obj['api_version'] = config['api_version']
  40. is_server_conf_exist = False
  41. if config.get('server_conf_path'):
  42. conf_path = Path(config['server_conf_path'])
  43. is_server_conf_exist = conf_path.is_file()
  44. if is_server_conf_exist:
  45. server_conf = yaml.safe_load(conf_path.read_text('utf-8'))
  46. local_conf_path = conf_path.with_name(f'local.{conf_path.name}')
  47. if local_conf_path.is_file():
  48. server_conf.update(yaml.safe_load(local_conf_path.read_text('utf-8')))
  49. ctx.obj['ip'] = server_conf['fateflow']['host']
  50. ctx.obj['http_port'] = int(server_conf['fateflow']['http_port'])
  51. ctx.obj['server_url'] = f'http://{ctx.obj["ip"]}:{ctx.obj["http_port"]}/{ctx.obj["api_version"]}'
  52. http_app_key = None
  53. http_secret_key = None
  54. if server_conf.get('authentication', {}).get('client', {}).get('switch'):
  55. http_app_key = server_conf['authentication']['client']['http_app_key']
  56. http_secret_key = server_conf['authentication']['client']['http_secret_key']
  57. else:
  58. http_app_key = server_conf.get('fateflow', {}).get('http_app_key')
  59. http_secret_key = server_conf.get('fateflow', {}).get('http_secret_key')
  60. if http_app_key and http_secret_key:
  61. ctx.obj['app_key'] = http_app_key
  62. ctx.obj['secret_key'] = http_secret_key
  63. elif config.get('ip') and config.get('port'):
  64. ctx.obj['ip'] = config['ip']
  65. ctx.obj['http_port'] = int(config['port'])
  66. ctx.obj['server_url'] = f'http://{ctx.obj["ip"]}:{ctx.obj["http_port"]}/{config["api_version"]}'
  67. if config.get('app_key') and config.get('secret_key'):
  68. ctx.obj['app_key'] = config['app_key']
  69. ctx.obj['secret_key'] = config['secret_key']
  70. else:
  71. raise ValueError('Invalid configuration file. Did you run "flow init"?')
  72. ctx.obj['initialized'] = is_server_conf_exist or (config.get('ip') and config.get('port'))
  73. @flow_cli.command('init', short_help='Flow CLI Init Command')
  74. @click.option('-c', '--server-conf-path', type=click.Path(exists=True),
  75. help='Server configuration file absolute path.')
  76. @click.option('--ip', type=click.STRING, help='Fate flow server ip address.')
  77. @click.option('--port', type=click.INT, help='Fate flow server port.')
  78. @click.option('--app-key', type=click.STRING, help='APP key for sign requests.')
  79. @click.option('--secret-key', type=click.STRING, help='Secret key for sign requests.')
  80. @click.option('--reset', is_flag=True, default=False,
  81. help='If specified, initialization settings would be reset to none. Users should init flow again.')
  82. def initialization(**kwargs):
  83. '''
  84. \b
  85. - DESCRIPTION:
  86. Flow CLI Init Command. Custom can choose to provide an absolute path of server conf file,
  87. or provide ip address and http port of a valid fate flow server. Notice that, if custom
  88. provides both, the server conf would be loaded in priority. In this case, ip address and
  89. http port would be ignored.
  90. \b
  91. - USAGE:
  92. flow init -c /data/projects/fate/python/conf/service_conf.yaml
  93. flow init --ip 127.0.0.1 --port 9380
  94. '''
  95. with open(os.path.join(os.path.dirname(__file__), 'settings.yaml'), 'r') as fin:
  96. config = yaml.safe_load(fin)
  97. if kwargs.get('reset'):
  98. config['api_version'] = 'v1'
  99. for i in ('server_conf_path', 'ip', 'port', 'app_key', 'secret_key'):
  100. config[i] = None
  101. with open(os.path.join(os.path.dirname(__file__), 'settings.yaml'), 'w') as fout:
  102. yaml.dump(config, fout, Dumper=yaml.RoundTripDumper)
  103. prettify(
  104. {
  105. 'retcode': 0,
  106. 'retmsg': 'Fate Flow CLI has been reset successfully. '
  107. 'Please do initialization again before you using flow CLI v2.'
  108. }
  109. )
  110. else:
  111. config['api_version'] = 'v1'
  112. if kwargs.get('server_conf_path'):
  113. config['server_conf_path'] = os.path.abspath(kwargs['server_conf_path'])
  114. for i in ('ip', 'port', 'app_key', 'secret_key'):
  115. if kwargs.get(i):
  116. config[i] = kwargs[i]
  117. if config.get('server_conf_path') or (config.get('ip') and config.get('port')):
  118. with open(os.path.join(os.path.dirname(__file__), 'settings.yaml'), 'w') as fout:
  119. yaml.dump(config, fout, Dumper=yaml.RoundTripDumper)
  120. prettify(
  121. {
  122. 'retcode': 0,
  123. 'retmsg': 'Fate Flow CLI has been initialized successfully.'
  124. }
  125. )
  126. else:
  127. prettify(
  128. {
  129. 'retcode': 100,
  130. 'retmsg': 'Fate Flow CLI initialization failed. Please provides server configuration file path '
  131. 'or server http ip address and port information.'
  132. }
  133. )
  134. flow_cli.add_command(server.server)
  135. flow_cli.add_command(service.service)
  136. flow_cli.add_command(provider.provider)
  137. flow_cli.add_command(tracking.tracking)
  138. flow_cli.add_command(component.component)
  139. flow_cli.add_command(data.data)
  140. flow_cli.add_command(job.job)
  141. flow_cli.add_command(model.model)
  142. flow_cli.add_command(resource.resource)
  143. flow_cli.add_command(privilege.privilege)
  144. flow_cli.add_command(queue.queue)
  145. flow_cli.add_command(task.task)
  146. flow_cli.add_command(table.table)
  147. flow_cli.add_command(tag.tag)
  148. flow_cli.add_command(checkpoint.checkpoint)
  149. flow_cli.add_command(test.test)
  150. flow_cli.add_command(template.template)
  151. flow_cli.add_command(key.key)
  152. if __name__ == '__main__':
  153. flow_cli()