Ejemplo n.º 1
0
def sleep_setup_runtime_env(runtime_env: dict, session_dir):
    logger = get_hook_logger()
    logger.info(f"Setting up runtime environment {runtime_env}")
    logger.info("Simulating long runtime env setup.  Sleeping for 15s...")
    time.sleep(15)
    logger.info("Finished sleeping for 15s")
    return RuntimeEnvContext()
Ejemplo n.º 2
0
def setup_runtime_env(runtime_env: dict, session_dir):
    logger = get_hook_logger()
    logger.debug(f"Setting up runtime environment {runtime_env}")
    if runtime_env.get("conda") or runtime_env.get("pip"):
        conda_dict = get_conda_dict(runtime_env, session_dir)
        if isinstance(runtime_env.get("conda"), str):
            conda_env_name = runtime_env["conda"]
        else:
            assert conda_dict is not None
            ray_pip = current_ray_pip_specifier()
            if ray_pip:
                extra_pip_dependencies = [ray_pip, "ray[default]"]
            elif runtime_env.get("_inject_current_ray"):
                extra_pip_dependencies = (
                    _resolve_install_from_source_ray_dependencies())
            else:
                extra_pip_dependencies = []
            conda_dict = inject_dependencies(conda_dict, _current_py_version(),
                                             extra_pip_dependencies)
            logger.info(f"Setting up conda environment with {runtime_env}")
            # It is not safe for multiple processes to install conda envs
            # concurrently, even if the envs are different, so use a global
            # lock for all conda installs.
            # See https://github.com/ray-project/ray/issues/17086
            file_lock_name = "ray-conda-install.lock"
            with FileLock(os.path.join(session_dir, file_lock_name)):
                conda_dir = os.path.join(session_dir, "runtime_resources",
                                         "conda")
                try_to_create_directory(conda_dir)
                conda_yaml_path = os.path.join(conda_dir, "environment.yml")
                with open(conda_yaml_path, "w") as file:
                    # Sort keys because we hash based on the file contents,
                    # and we don't want the hash to depend on the order
                    # of the dependencies.
                    yaml.dump(conda_dict, file, sort_keys=True)
                conda_env_name = get_or_create_conda_env(
                    conda_yaml_path, conda_dir)

            if runtime_env.get("_inject_current_ray"):
                conda_path = os.path.join(conda_dir, conda_env_name)
                _inject_ray_to_conda_site(conda_path)
        logger.info(
            f"Finished setting up runtime environment at {conda_env_name}")

        return RuntimeEnvContext(conda_env_name)

    return RuntimeEnvContext()
Ejemplo n.º 3
0
def setup_runtime_env(runtime_env: dict, session_dir):
    if runtime_env.get("conda") or runtime_env.get("pip"):
        conda_dict = get_conda_dict(runtime_env, session_dir)
        if isinstance(runtime_env.get("conda"), str):
            conda_env_name = runtime_env["conda"]
        else:
            assert conda_dict is not None
            py_version = ".".join(map(str,
                                      sys.version_info[:3]))  # like 3.6.10
            ray_pip = current_ray_pip_specifier()
            if ray_pip and not runtime_env.get("_skip_inject_ray"):
                extra_pip_dependencies = [ray_pip, "ray[default]"]
            else:
                extra_pip_dependencies = []
            conda_dict = inject_dependencies(conda_dict, py_version,
                                             extra_pip_dependencies)
            # Locking to avoid multiple processes installing concurrently
            conda_hash = hashlib.sha1(
                json.dumps(conda_dict,
                           sort_keys=True).encode("utf-8")).hexdigest()
            conda_hash_str = f"conda-generated-{conda_hash}"
            file_lock_name = f"ray-{conda_hash_str}.lock"
            with FileLock(os.path.join(session_dir, file_lock_name)):
                conda_dir = os.path.join(session_dir, "runtime_resources",
                                         "conda")
                try_to_create_directory(conda_dir)
                conda_yaml_path = os.path.join(conda_dir, "environment.yml")
                with open(conda_yaml_path, "w") as file:
                    # Sort keys because we hash based on the file contents,
                    # and we don't want the hash to depend on the order
                    # of the dependencies.
                    yaml.dump(conda_dict, file, sort_keys=True)
                conda_env_name = get_or_create_conda_env(
                    conda_yaml_path, conda_dir)

        return RuntimeEnvContext(conda_env_name)

    return RuntimeEnvContext()
Ejemplo n.º 4
0
def setup_runtime_env(runtime_env: dict, session_dir):
    if runtime_env.get("conda") or runtime_env.get("pip"):
        conda_dict = get_conda_dict(runtime_env, session_dir)
        if isinstance(runtime_env.get("conda"), str):
            conda_env_name = runtime_env["conda"]
        else:
            assert conda_dict is not None
            py_version = ".".join(map(str,
                                      sys.version_info[:3]))  # like 3.6.10
            ray_pip = current_ray_pip_specifier()
            if ray_pip and not runtime_env.get("_skip_inject_ray"):
                extra_pip_dependencies = [ray_pip, "ray[default]"]
            else:
                extra_pip_dependencies = []
            conda_dict = inject_dependencies(conda_dict, py_version,
                                             extra_pip_dependencies)
            # It is not safe for multiple processes to install conda envs
            # concurrently, even if the envs are different, so use a global
            # lock for all conda installs.
            # See https://github.com/ray-project/ray/issues/17086
            file_lock_name = "ray-conda-install.lock"
            with FileLock(os.path.join(session_dir, file_lock_name)):
                conda_dir = os.path.join(session_dir, "runtime_resources",
                                         "conda")
                try_to_create_directory(conda_dir)
                conda_yaml_path = os.path.join(conda_dir, "environment.yml")
                with open(conda_yaml_path, "w") as file:
                    # Sort keys because we hash based on the file contents,
                    # and we don't want the hash to depend on the order
                    # of the dependencies.
                    yaml.dump(conda_dict, file, sort_keys=True)
                conda_env_name = get_or_create_conda_env(
                    conda_yaml_path, conda_dir)

        return RuntimeEnvContext(conda_env_name)

    return RuntimeEnvContext()
Ejemplo n.º 5
0
def setup_worker(input_args):
    # remaining_args contains the arguments to the original worker command,
    # minus the python executable, e.g. default_worker.py --node-ip-address=...
    args, remaining_args = parser.parse_known_args(args=input_args)

    commands = []
    py_executable: str = sys.executable
    runtime_env: dict = json.loads(args.serialized_runtime_env or "{}")
    runtime_env_context: RuntimeEnvContext = None

    # Ray client server setups runtime env by itself instead of agent.
    if runtime_env.get("conda") or runtime_env.get("pip"):
        if not args.serialized_runtime_env_context:
            runtime_env_context = setup_runtime_env(runtime_env,
                                                    args.session_dir)
        else:
            runtime_env_context = RuntimeEnvContext.deserialize(
                args.serialized_runtime_env_context)

    # activate conda
    if runtime_env_context and runtime_env_context.conda_env_name:
        py_executable = "python"
        conda_activate_commands = get_conda_activate_commands(
            runtime_env_context.conda_env_name)
        if (conda_activate_commands):
            commands += conda_activate_commands
    elif runtime_env.get("conda"):
        logger.warning(
            "Conda env name is not found in context, "
            "but conda exists in runtime env. The runtime env %s, "
            "the context %s.", args.serialized_runtime_env,
            args.serialized_runtime_env_context)

    commands += [
        " ".join(
            [f"exec {py_executable}"] + remaining_args +
            # Pass the runtime for working_dir setup.
            # We can't do it in shim process here because it requires
            # connection to gcs.
            ["--serialized-runtime-env", f"'{args.serialized_runtime_env}'"])
    ]
    command_separator = " && "
    command_str = command_separator.join(commands)

    # update env vars
    if runtime_env.get("env_vars"):
        env_vars = runtime_env["env_vars"]
        os.environ.update(env_vars)
    os.execvp("bash", ["bash", "-c", command_str])