Exemplo n.º 1
0
def build(import_path: str, app_dir: str, output_path: Optional[str]):
    sys.path.insert(0, app_dir)

    node: Union[ClassNode, FunctionNode] = import_attr(import_path)
    if not isinstance(node, (ClassNode, FunctionNode)):
        raise TypeError(
            f"Expected '{import_path}' to be ClassNode or "
            f"FunctionNode, but got {type(node)}."
        )

    app = build_app(node)

    config = ServeApplicationSchema(
        deployments=[deployment_to_schema(d) for d in app.deployments.values()]
    ).dict()
    config["import_path"] = import_path

    if output_path is not None:
        if not output_path.endswith(".yaml"):
            raise ValueError("FILE_PATH must end with '.yaml'.")

        with open(output_path, "w") as f:
            yaml.safe_dump(config, stream=f, default_flow_style=False, sort_keys=False)
    else:
        print(yaml.safe_dump(config, default_flow_style=False, sort_keys=False), end="")
Exemplo n.º 2
0
    def to_dict(self) -> Dict:
        """Returns this Application's deployments as a dictionary.

        This dictionary adheres to the Serve REST API schema. It can be deployed
        via the Serve REST API.

        Returns:
            Dict: The Application's deployments formatted in a dictionary.
        """

        return ServeApplicationSchema(deployments=[
            deployment_to_schema(d) for d in self._deployments.values()
        ]).dict()
Exemplo n.º 3
0
def build(import_path: str, app_dir: str, output_path: Optional[str]):
    sys.path.insert(0, app_dir)

    node: Union[ClassNode, FunctionNode] = import_attr(import_path)
    if not isinstance(node, (ClassNode, FunctionNode)):
        raise TypeError(f"Expected '{import_path}' to be ClassNode or "
                        f"FunctionNode, but got {type(node)}.")

    app = build_app(node)

    config = ServeApplicationSchema(deployments=[
        deployment_to_schema(d) for d in app.deployments.values()
    ]).dict()
    config["import_path"] = import_path

    config_str = ("# This file was generated using the `serve build` command "
                  f"on Ray v{ray.__version__}.\n\n")
    config_str += yaml.dump(config,
                            Dumper=ServeBuildDumper,
                            default_flow_style=False,
                            sort_keys=False)

    with open(output_path, "w") if output_path else sys.stdout as f:
        f.write(config_str)