Changeset View
Changeset View
Standalone View
Standalone View
swh/vault/cookers/__init__.py
Show All 22 Lines | COOKER_TYPES = { | ||||
"revision_flat": RevisionFlatCooker, | "revision_flat": RevisionFlatCooker, | ||||
"revision_gitfast": RevisionGitfastCooker, | "revision_gitfast": RevisionGitfastCooker, | ||||
"snapshot_git_bare": GitBareCooker, | "snapshot_git_bare": GitBareCooker, | ||||
"revision_git_bare": GitBareCooker, | "revision_git_bare": GitBareCooker, | ||||
"directory_git_bare": GitBareCooker, | "directory_git_bare": GitBareCooker, | ||||
} | } | ||||
def get_cooker_cls(obj_type): | def get_cooker_cls(bundle_type): | ||||
return COOKER_TYPES[obj_type] | return COOKER_TYPES[bundle_type] | ||||
def check_config(cfg: Dict[str, Any]) -> Dict[str, Any]: | def check_config(cfg: Dict[str, Any]) -> Dict[str, Any]: | ||||
"""Ensure the configuration is ok to run a vault worker, and propagate defaults | """Ensure the configuration is ok to run a vault worker, and propagate defaults | ||||
Raises: | Raises: | ||||
EnvironmentError if the configuration is not for remote instance | EnvironmentError if the configuration is not for remote instance | ||||
ValueError if one of the following keys is missing: vault, storage | ValueError if one of the following keys is missing: vault, storage | ||||
Show All 21 Lines | if "storage" not in vcfg: | ||||
vcfg["storage"] = cfg.get("storage") | vcfg["storage"] = cfg.get("storage") | ||||
if not vcfg.get("storage"): | if not vcfg.get("storage"): | ||||
raise ValueError("invalid configuration: missing 'storage' config entry.") | raise ValueError("invalid configuration: missing 'storage' config entry.") | ||||
return cfg | return cfg | ||||
def get_cooker(obj_type: str, obj_id: str): | def get_cooker(bundle_type: str, obj_id: str): | ||||
"""Instantiate a cooker class of type obj_type. | """Instantiate a cooker class of type bundle_type. | ||||
Returns: | Returns: | ||||
Cooker class in charge of cooking the obj_type with id obj_id. | Cooker class in charge of cooking the bundle_type with id obj_id. | ||||
Raises: | Raises: | ||||
ValueError in case of a missing top-level vault key configuration or a storage | ValueError in case of a missing top-level vault key configuration or a storage | ||||
key. | key. | ||||
EnvironmentError in case the vault configuration reference a non remote class. | EnvironmentError in case the vault configuration reference a non remote class. | ||||
""" | """ | ||||
if "SWH_CONFIG_FILENAME" in os.environ: | if "SWH_CONFIG_FILENAME" in os.environ: | ||||
cfg = read_config(os.environ["SWH_CONFIG_FILENAME"], DEFAULT_CONFIG) | cfg = read_config(os.environ["SWH_CONFIG_FILENAME"], DEFAULT_CONFIG) | ||||
else: | else: | ||||
cfg = load_named_config(DEFAULT_CONFIG_PATH, DEFAULT_CONFIG) | cfg = load_named_config(DEFAULT_CONFIG_PATH, DEFAULT_CONFIG) | ||||
cooker_cls = get_cooker_cls(obj_type) | cooker_cls = get_cooker_cls(bundle_type) | ||||
cfg = check_config(cfg) | cfg = check_config(cfg) | ||||
vcfg = cfg["vault"] | vcfg = cfg["vault"] | ||||
storage = get_storage(**vcfg.pop("storage")) | storage = get_storage(**vcfg.pop("storage")) | ||||
backend = get_vault(**vcfg) | backend = get_vault(**vcfg) | ||||
try: | try: | ||||
from swh.graph.client import RemoteGraphClient # optional dependency | from swh.graph.client import RemoteGraphClient # optional dependency | ||||
graph = RemoteGraphClient(**vcfg["graph"]) if vcfg.get("graph") else None | graph = RemoteGraphClient(**vcfg["graph"]) if vcfg.get("graph") else None | ||||
except ModuleNotFoundError: | except ModuleNotFoundError: | ||||
if vcfg.get("graph"): | if vcfg.get("graph"): | ||||
raise EnvironmentError( | raise EnvironmentError( | ||||
"Graph configuration required but module is not installed." | "Graph configuration required but module is not installed." | ||||
) | ) | ||||
else: | else: | ||||
graph = None | graph = None | ||||
return cooker_cls( | return cooker_cls( | ||||
obj_type, | bundle_type, | ||||
obj_id, | obj_id, | ||||
backend=backend, | backend=backend, | ||||
storage=storage, | storage=storage, | ||||
graph=graph, | graph=graph, | ||||
max_bundle_size=cfg["max_bundle_size"], | max_bundle_size=cfg["max_bundle_size"], | ||||
) | ) |