forked from ray-project/ray
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Revert "[runtime env] plugin refactor[2/n]: support json sche… (
- Loading branch information
1 parent
adfdc26
commit 22dfd1f
Showing
13 changed files
with
283 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import os | ||
import jsonschema | ||
import logging | ||
from typing import List | ||
import json | ||
from ray._private.runtime_env.constants import ( | ||
RAY_RUNTIME_ENV_PLUGIN_SCHEMAS_ENV_VAR, | ||
RAY_RUNTIME_ENV_PLUGIN_SCHEMA_SUFFIX, | ||
) | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class RuntimeEnvPluginSchemaManager: | ||
"""This manager is used to load plugin json schemas.""" | ||
|
||
default_schema_path = os.path.join(os.path.dirname(__file__), "schemas") | ||
schemas = {} | ||
loaded = False | ||
|
||
@classmethod | ||
def _load_schemas(cls, schema_paths: List[str]): | ||
for schema_path in schema_paths: | ||
try: | ||
schema = json.load(open(schema_path)) | ||
except json.decoder.JSONDecodeError: | ||
logger.error("Invalid runtime env schema %s, skip it.", schema_path) | ||
if "title" not in schema: | ||
logger.error( | ||
"No valid title in runtime env schema %s, skip it.", schema_path | ||
) | ||
continue | ||
if schema["title"] in cls.schemas: | ||
logger.error( | ||
"The 'title' of runtime env schema %s conflicts with %s, skip it.", | ||
schema_path, | ||
cls.schemas[schema["title"]], | ||
) | ||
continue | ||
cls.schemas[schema["title"]] = schema | ||
|
||
@classmethod | ||
def _load_default_schemas(cls): | ||
schema_json_files = list() | ||
for root, _, files in os.walk(cls.default_schema_path): | ||
for f in files: | ||
if f.endswith(RAY_RUNTIME_ENV_PLUGIN_SCHEMA_SUFFIX): | ||
schema_json_files.append(os.path.join(root, f)) | ||
logger.info( | ||
f"Loading the default runtime env schemas: {schema_json_files}." | ||
) | ||
cls._load_schemas(schema_json_files) | ||
|
||
@classmethod | ||
def _load_schemas_from_env_var(cls): | ||
# The format of env var: | ||
# "/path/to/env_1_schema.json,/path/to/env_2_schema.json,/path/to/schemas_dir/" | ||
schema_paths = os.environ.get(RAY_RUNTIME_ENV_PLUGIN_SCHEMAS_ENV_VAR) | ||
if schema_paths: | ||
schema_json_files = list() | ||
for path in schema_paths.split(","): | ||
if path.endswith(RAY_RUNTIME_ENV_PLUGIN_SCHEMA_SUFFIX): | ||
schema_json_files.append(path) | ||
elif os.path.isdir(path): | ||
for root, _, files in os.walk(path): | ||
for f in files: | ||
if f.endswith(RAY_RUNTIME_ENV_PLUGIN_SCHEMA_SUFFIX): | ||
schema_json_files.append(os.path.join(root, f)) | ||
logger.info( | ||
f"Loading the runtime env schemas from env var: {schema_json_files}." | ||
) | ||
cls._load_schemas(schema_json_files) | ||
|
||
@classmethod | ||
def validate(cls, name, instance): | ||
if not cls.loaded: | ||
# Load the schemas lazily. | ||
cls._load_default_schemas() | ||
cls._load_schemas_from_env_var() | ||
cls.loaded = True | ||
# if no schema matches, skip the validation. | ||
if name in cls.schemas: | ||
jsonschema.validate(instance=instance, schema=cls.schemas[name]) | ||
|
||
@classmethod | ||
def clear(cls): | ||
cls.schemas.clear() | ||
cls.loaded = False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
{ | ||
"$schema": "https://json-schema.org/draft-07/schema#", | ||
"$id": "https://github.com/ray-project/ray/runtime_env/pip_schema.json", | ||
"title": "pip", | ||
"description": "A pip environment specification.", | ||
"oneOf": [ | ||
{ | ||
"type": "object", | ||
"properties": { | ||
"packages": { | ||
"oneOf": [ | ||
{ | ||
"type": "array", | ||
"items": { | ||
"type": "string" | ||
}, | ||
"description": "a list of pip packages" | ||
}, | ||
{ | ||
"type": "string", | ||
"description": "the path to a pip `requirements.txt` file" | ||
} | ||
] | ||
}, | ||
"pip_check": { | ||
"type": "boolean", | ||
"description": "whether to enable pip check at the end of pip install" | ||
}, | ||
"pip_version": { | ||
"type": "string", | ||
"description": "the version of pip" | ||
} | ||
}, | ||
"required": [ | ||
"packages" | ||
] | ||
}, | ||
{ | ||
"type": "string", | ||
"description": "the path to a pip `requirements.txt` file" | ||
}, | ||
{ | ||
"type": "array", | ||
"items": { | ||
"type": "string" | ||
}, | ||
"description": "a list of pip requirements specifiers" | ||
} | ||
] | ||
} |
7 changes: 7 additions & 0 deletions
7
python/ray/_private/runtime_env/schemas/working_dir_schema.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"$schema": "https://json-schema.org/draft-07/schema#", | ||
"$id": "https://github.com/ray-project/ray/runtime_env/working_dir_schema.json", | ||
"title": "working_dir", | ||
"type": "string", | ||
"description": "Specifies the working directory for the Ray workers." | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
python/ray/tests/test_runtime_env_validation_1_schema.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"$schema": "https://json-schema.org/draft-07/schema#", | ||
"$id": "https://github.com/ray-project/ray/runtime_env/pip_schema.json", | ||
"title": "test_env_1", | ||
"type": "object", | ||
"properties": { | ||
"array": { | ||
"type": "array", | ||
"items": { | ||
"type": "string" | ||
} | ||
}, | ||
"bool": { | ||
"type": "boolean" | ||
} | ||
}, | ||
"required": [ | ||
"array" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"$schema": "https://json-schema.org/draft-07/schema#", | ||
"$id": "https://github.com/ray-project/ray/runtime_env/working_dir_schema.json", | ||
"title": "test_env_2", | ||
"type": "string" | ||
} |
5 changes: 5 additions & 0 deletions
5
python/ray/tests/test_runtime_env_validation_bad_1_schema.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"$schema": "https://json-schema.org/draft-07/schema#", | ||
"$id": "https://github.com/ray-project/ray/runtime_env/working_dir_schema.json", | ||
"type": "string" | ||
} |
5 changes: 5 additions & 0 deletions
5
python/ray/tests/test_runtime_env_validation_bad_2_schema.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"$schema": "https://json-schema.org/draft-07/schema#", | ||
"$id": "https://github.com/ray-project/ray/runtime_env/working_dir_schema.json", | ||
"type": "string" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters