pipeline_cli.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 click
  17. from pathlib import Path
  18. from ruamel import yaml
  19. from flow_sdk.client import FlowClient
  20. default_config = Path(__file__).parent.joinpath("config.yaml").resolve()
  21. @click.group("Pipeline Config Tool")
  22. def cli():
  23. pass
  24. @click.command(name="init")
  25. @click.option("-c", "--pipeline-conf-path", "config_path", type=click.Path(exists=True),
  26. help="Path to pipeline configuration file.")
  27. @click.option("-d", "--log-directory", type=click.Path(),
  28. help="Path to pipeline logs directory.")
  29. @click.option("--ip", type=click.STRING, help="Fate Flow server ip address.")
  30. @click.option("--port", type=click.INT, help="Fate Flow server port.")
  31. @click.option("--app-key", type=click.STRING, help="app key for request to Fate Flow server")
  32. @click.option("--secret-key", type=click.STRING, help="secret key for request to Fate Flow server")
  33. @click.option("-r", "--system-user", type=click.STRING, help="system user role")
  34. def _init(**kwargs):
  35. """
  36. \b
  37. - DESCRIPTION:
  38. Pipeline Config Command. User can choose to provide path to conf file,
  39. or provide ip address and http port of a valid fate flow server. Optionally,
  40. pipeline log directory can be set to arbitrary location. Default log directory is
  41. pipeline/logs. Notice that, if both conf file and specifications are provided,
  42. settings in conf file are ignored.
  43. \b
  44. - USAGE:
  45. pipeline init -c config.yaml
  46. pipeline init --ip 10.1.2.3 --port 9380 --log-directory ./logs --system-user guest
  47. """
  48. config_path = kwargs.get("config_path")
  49. ip = kwargs.get("ip")
  50. port = kwargs.get("port")
  51. log_directory = kwargs.get("log_directory")
  52. system_user = kwargs.get("system_user")
  53. app_key = kwargs.get("app_key")
  54. secret_key = kwargs.get("secret_key")
  55. if config_path is None and (ip is None or port is None):
  56. print(
  57. "\nPipeline configuration failed. \nPlease provides configuration file path "
  58. "or server http ip address & port information."
  59. )
  60. return
  61. if config_path is None:
  62. config_path = default_config
  63. with Path(config_path).open("r") as fin:
  64. config = yaml.safe_load(fin)
  65. if ip:
  66. config["ip"] = ip
  67. if port:
  68. config["port"] = port
  69. if log_directory:
  70. config["log_directory"] = Path(log_directory).resolve().__str__()
  71. if app_key:
  72. config["app_key"] = app_key
  73. if secret_key:
  74. config["secret_key"] = secret_key
  75. if system_user:
  76. system_user = system_user.lower()
  77. if system_user not in ["guest", "host", "arbiter"]:
  78. raise ValueError(f"system_user {system_user} is not valid. Must be one of (guest, host, arbiter)")
  79. config["system_setting"] = {"role": system_user}
  80. with default_config.open("w") as fout:
  81. yaml.dump(config, fout, Dumper=yaml.RoundTripDumper)
  82. print("Pipeline configuration succeeded.")
  83. @click.group("config", help="pipeline config tool")
  84. def config_group():
  85. """
  86. pipeline config
  87. """
  88. pass
  89. @config_group.command(name="show")
  90. def _show():
  91. """
  92. \b
  93. - DESCRIPTION:
  94. Show pipeline config details for Flow server.
  95. \b
  96. - USAGE:
  97. pipeline config show
  98. """
  99. with Path(default_config).open("r") as fin:
  100. config = yaml.safe_load(fin)
  101. click.echo(f"\nPipeline Config: {yaml.dump(config)}")
  102. @config_group.command(name="check")
  103. def _check():
  104. """
  105. \b
  106. - DESCRIPTION:
  107. Check for Flow server status and Flow version.
  108. \b
  109. - USAGE:
  110. pipeline config check
  111. """
  112. from pipeline.backend import config as conf
  113. client = FlowClient(ip=conf.PipelineConfig.IP, port=conf.PipelineConfig.PORT, version=conf.SERVER_VERSION)
  114. version = client.remote_version.fate_flow()
  115. if version is None:
  116. click.echo(f"Flow server not responsive. Please check flow server ip and port setting.")
  117. else:
  118. click.echo(f"Flow server status normal, Flow version: {version}")
  119. cli.add_command(_init)
  120. cli.add_command(config_group)
  121. if __name__ == '__main__':
  122. cli()