mirror of
https://github.com/Chia-Network/chia-blockchain.git
synced 2026-08-24 10:05:29 -05:00
Fill missing services in config for data layer (#13220)
* fill missing services in config for data layer * adjust * here too
This commit is contained in:
@@ -3,7 +3,7 @@ from typing import Optional
|
||||
|
||||
import click
|
||||
|
||||
from chia.util.config import lock_and_load_config, save_config, str2bool
|
||||
from chia.util.config import load_defaults_for_missing_services, lock_and_load_config, save_config, str2bool
|
||||
|
||||
|
||||
def configure(
|
||||
@@ -24,7 +24,10 @@ def configure(
|
||||
seeder_nameserver: str,
|
||||
enable_data_server: str = "",
|
||||
):
|
||||
with lock_and_load_config(root_path, "config.yaml") as config:
|
||||
config_yaml = "config.yaml"
|
||||
with lock_and_load_config(root_path, config_yaml, fill_missing_services=True) as config:
|
||||
config.update(load_defaults_for_missing_services(config=config, config_name=config_yaml))
|
||||
|
||||
change_made = False
|
||||
if set_node_introducer:
|
||||
try:
|
||||
|
||||
@@ -59,8 +59,8 @@ def create_data_layer_service(
|
||||
|
||||
async def async_main() -> int:
|
||||
# TODO: refactor to avoid the double load
|
||||
config = load_config(DEFAULT_ROOT_PATH, "config.yaml")
|
||||
service_config = load_config_cli(DEFAULT_ROOT_PATH, "config.yaml", SERVICE_NAME)
|
||||
config = load_config(DEFAULT_ROOT_PATH, "config.yaml", fill_missing_services=True)
|
||||
service_config = load_config_cli(DEFAULT_ROOT_PATH, "config.yaml", SERVICE_NAME, fill_missing_services=True)
|
||||
config[SERVICE_NAME] = service_config
|
||||
initialize_logging(
|
||||
service_name=SERVICE_NAME,
|
||||
|
||||
+38
-4
@@ -59,9 +59,18 @@ def lock_config(root_path: Path, filename: Union[str, Path]) -> Iterator[None]:
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def lock_and_load_config(root_path: Path, filename: Union[str, Path]) -> Iterator[Dict[str, Any]]:
|
||||
def lock_and_load_config(
|
||||
root_path: Path,
|
||||
filename: Union[str, Path],
|
||||
fill_missing_services: bool = False,
|
||||
) -> Iterator[Dict[str, Any]]:
|
||||
with lock_config(root_path=root_path, filename=filename):
|
||||
config = _load_config_maybe_locked(root_path=root_path, filename=filename, acquire_lock=False)
|
||||
config = _load_config_maybe_locked(
|
||||
root_path=root_path,
|
||||
filename=filename,
|
||||
acquire_lock=False,
|
||||
fill_missing_services=fill_missing_services,
|
||||
)
|
||||
yield config
|
||||
|
||||
|
||||
@@ -83,6 +92,7 @@ def load_config(
|
||||
filename: Union[str, Path],
|
||||
sub_config: Optional[str] = None,
|
||||
exit_on_error: bool = True,
|
||||
fill_missing_services: bool = False,
|
||||
) -> Dict:
|
||||
return _load_config_maybe_locked(
|
||||
root_path=root_path,
|
||||
@@ -90,6 +100,7 @@ def load_config(
|
||||
sub_config=sub_config,
|
||||
exit_on_error=exit_on_error,
|
||||
acquire_lock=True,
|
||||
fill_missing_services=fill_missing_services,
|
||||
)
|
||||
|
||||
|
||||
@@ -99,6 +110,7 @@ def _load_config_maybe_locked(
|
||||
sub_config: Optional[str] = None,
|
||||
exit_on_error: bool = True,
|
||||
acquire_lock: bool = True,
|
||||
fill_missing_services: bool = False,
|
||||
) -> Dict:
|
||||
# This must be called under an acquired config lock, or acquire_lock should be True
|
||||
|
||||
@@ -123,6 +135,8 @@ def _load_config_maybe_locked(
|
||||
log.error(f"yaml.safe_load returned None: {path}")
|
||||
time.sleep(i * 0.1)
|
||||
continue
|
||||
if fill_missing_services:
|
||||
r.update(load_defaults_for_missing_services(config=r, config_name=path.name))
|
||||
if sub_config is not None:
|
||||
r = r.get(sub_config)
|
||||
return r
|
||||
@@ -133,14 +147,19 @@ def _load_config_maybe_locked(
|
||||
raise RuntimeError("Was not able to read config file successfully")
|
||||
|
||||
|
||||
def load_config_cli(root_path: Path, filename: str, sub_config: Optional[str] = None) -> Dict:
|
||||
def load_config_cli(
|
||||
root_path: Path,
|
||||
filename: str,
|
||||
sub_config: Optional[str] = None,
|
||||
fill_missing_services: bool = False,
|
||||
) -> Dict:
|
||||
"""
|
||||
Loads configuration from the specified filename, in the config directory,
|
||||
and then overrides any properties using the passed in command line arguments.
|
||||
Nested properties in the config file can be used in the command line with ".",
|
||||
for example --farmer_peer.host. Does not support lists.
|
||||
"""
|
||||
config = load_config(root_path, filename, sub_config)
|
||||
config = load_config(root_path, filename, sub_config, fill_missing_services=fill_missing_services)
|
||||
|
||||
flattened_props = flatten_properties(config)
|
||||
parser = argparse.ArgumentParser()
|
||||
@@ -278,3 +297,18 @@ def override_config(config: Dict[str, Any], config_overrides: Optional[Dict[str,
|
||||
def selected_network_address_prefix(config: Dict[str, Any]) -> str:
|
||||
address_prefix = config["network_overrides"]["config"][config["selected_network"]]["address_prefix"]
|
||||
return address_prefix
|
||||
|
||||
|
||||
def load_defaults_for_missing_services(config: Dict[str, Any], config_name: str) -> Dict[str, Any]:
|
||||
services = ["data_layer"]
|
||||
missing_services = [service for service in services if service not in config]
|
||||
defaulted = {}
|
||||
if len(missing_services) > 0:
|
||||
marshalled_default_config: str = initial_config_file(config_name)
|
||||
|
||||
unmarshalled_default_config = yaml.safe_load(marshalled_default_config)
|
||||
|
||||
for service in missing_services:
|
||||
defaulted[service] = unmarshalled_default_config[service]
|
||||
|
||||
return defaulted
|
||||
|
||||
Reference in New Issue
Block a user