Esempio n. 1
0
 def do_register_package():
     if not runtime_support.package_exists(pkg_uri):
         tmp_path = os.path.join(_pkg_tmp(), "_tmp{}".format(pkg_name))
         runtime_support.create_project_package(
             working_dir=base_dir, py_modules=[], output_path=tmp_path)
         # TODO(ekl) does this get garbage collected correctly with the
         # current job id?
         runtime_support.push_package(pkg_uri, tmp_path)
         if not runtime_support.package_exists(pkg_uri):
             raise RuntimeError(
                 "Failed to upload package {}".format(pkg_uri))
Esempio n. 2
0
def load_package(config_path: str) -> "_RuntimePackage":
    """Load the code package given its config path.

    Args:
        config_path (str): The path to the configuration YAML that defines
            the package. For documentation on the packaging format, see the
            example YAML in ``example_pkg/ray_pkg.yaml``.

    Examples:
        >>> # Load from local.
        >>> my_pkg = load_package("~/path/to/my_pkg.yaml")

        >>> # Load from GitHub.
        >>> my_pkg = ray.util.load_package(
        ...   "https://raw.githubusercontent.com/user/repo/refspec"
        ...   "/path/to/package/my_pkg.yaml")

        >>> # Inspect the package runtime env.
        >>> print(my_pkg._runtime_env)
        ... {"conda": {...},
        ...  "docker": "anyscale-ml/ray-ml:nightly-py38-cpu",
        ...  "files": "https://github.com/demo/foo/blob/v3.0/project/"}

        >>> # Run remote functions from the package.
        >>> my_pkg.my_func.remote(1, 2)

        >>> # Create actors from the package.
        >>> actor = my_pkg.MyActor.remote(3, 4)

        >>> # Create new remote funcs in the same env as a package.
        >>> @ray.remote(runtime_env=my_pkg._runtime_env)
        >>> def f(): ...
    """

    if not ray.is_initialized():
        # TODO(ekl) lift this requirement?
        raise RuntimeError("Ray must be initialized first to load packages.")

    config_path = _download_from_github_if_needed(config_path)

    if not os.path.exists(config_path):
        raise ValueError("Config file does not exist: {}".format(config_path))

    # TODO(ekl) validate schema?
    config = yaml.safe_load(open(config_path).read())
    base_dir = os.path.abspath(os.path.dirname(config_path))
    runtime_env = config["runtime_env"]

    # Autofill working directory by uploading to GCS storage.
    if "files" not in runtime_env:
        pkg_name = runtime_support.get_project_package_name(
            working_dir=base_dir, modules=[])
        pkg_uri = runtime_support.Protocol.GCS.value + "://" + pkg_name
        if not runtime_support.package_exists(pkg_uri):
            tmp_path = os.path.join(_pkg_tmp(), "_tmp{}".format(pkg_name))
            runtime_support.create_project_package(
                working_dir=base_dir, modules=[], output_path=tmp_path)
            # TODO(ekl) does this get garbage collected correctly with the
            # current job id?
            runtime_support.push_package(pkg_uri, tmp_path)
            if not runtime_support.package_exists(pkg_uri):
                raise RuntimeError(
                    "Failed to upload package {}".format(pkg_uri))
        runtime_env["files"] = pkg_uri

    # Autofill conda config.
    conda_yaml = os.path.join(base_dir, "conda.yaml")
    if os.path.exists(conda_yaml):
        if "conda" in runtime_env:
            raise ValueError(
                "Both conda.yaml and conda: section found in package")
        runtime_env["conda"] = yaml.safe_load(open(conda_yaml).read())

    pkg = _RuntimePackage(
        name=config["name"],
        desc=config["description"],
        stub_file=os.path.join(base_dir, config["stub_file"]),
        runtime_env=runtime_env)
    return pkg