-
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.
feat(config): Refactor config module
- Use nested models for configs - Promote hardcoded values to config variables
- Loading branch information
1 parent
2a59dff
commit 1c6f128
Showing
11 changed files
with
191 additions
and
89 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
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 |
---|---|---|
@@ -1,61 +1,163 @@ | ||
from pydantic import Field | ||
from pydantic_settings import BaseSettings | ||
from safir.logging import LogLevel, Profile | ||
from pydantic import BaseModel, Field | ||
from pydantic_settings import BaseSettings, SettingsConfigDict | ||
|
||
__all__ = ["Configuration", "config"] | ||
|
||
|
||
class Configuration(BaseSettings): | ||
"""Configuration for cm-service.""" | ||
class HTCondorConfiguration(BaseModel): | ||
condor_submit_bin: str = Field( | ||
description="Name of condor_submit client binary", | ||
default="condor_submit", | ||
) | ||
|
||
condor_q_bin: str = Field( | ||
description="Name of condor_q client binary", | ||
default="condor_q", | ||
) | ||
|
||
request_cpus: int = Field( | ||
description="Number of cores to request when submitting an htcondor job.", | ||
default=1, | ||
) | ||
|
||
request_mem: str = Field( | ||
description="Amount of memory requested when submitting an htcondor job.", | ||
default="512M", | ||
) | ||
|
||
request_disk: str = Field( | ||
description="Amount of disk space requested when submitting an htcondor job.", | ||
default="1G", | ||
) | ||
|
||
|
||
class SlurmConfiguration(BaseModel): | ||
"""Configuration settings for slurm client operations, especially sbatch. | ||
Set via SLURM__FIELD environment variables. | ||
Note | ||
---- | ||
Default SBATCH_* variables could work just as well, but it is useful to | ||
have this as a document of what settings are actually used. | ||
""" | ||
|
||
sacct_bin: str = Field( | ||
description="Name of sacct slurm client binary", | ||
default="sacct", | ||
) | ||
|
||
sbatch_bin: str = Field( | ||
description="Name of sbatch slurm client binary", | ||
default="sbatch", | ||
) | ||
|
||
memory: str = Field( | ||
description="Amount of memory requested when submitting a slurm job.", | ||
default="16448", | ||
) | ||
|
||
account: str = Field( | ||
description="Account used when submitting a slurm job.", | ||
default="rubin:production", | ||
) | ||
|
||
partition: str = Field( | ||
description="Partition requested when submitting a slurm job.", | ||
default="milano", | ||
) | ||
|
||
|
||
class AsgiConfiguration(BaseModel): | ||
title: str = Field( | ||
description="Title of the ASGI application", | ||
default="cm-service", | ||
) | ||
|
||
host: str = Field( | ||
description="The host address to which the asgi server should bind", | ||
default="0.0.0.0", | ||
) | ||
|
||
port: int = Field( | ||
description="Port number for the asgi server to listen on", | ||
default=8080, | ||
) | ||
|
||
prefix: str = Field( | ||
description="The URL prefix for the cm-service API", | ||
default="/cm-service/v1", | ||
title="The URL prefix for the cm-service API", | ||
validation_alias="CM_URL_PREFIX", | ||
) | ||
|
||
frontend_prefix: str = Field( | ||
description="The URL prefix for the frontend web app", | ||
default="/web_app", | ||
) | ||
|
||
|
||
class LoggingConfiguration(BaseModel): | ||
level: str = Field( | ||
default="INFO", | ||
title="Log level of the application's logger", | ||
validation_alias="LOGGING_LEVEL", | ||
) | ||
|
||
profile: str = Field( | ||
default="development", | ||
title="Application logging profile", | ||
validation_alias="SAFIR_LOGGING_PROFILE", | ||
) | ||
|
||
|
||
class DatabaseConfiguration(BaseModel): | ||
"""Database configuration nested model.""" | ||
|
||
database_url: str = Field( | ||
default="", | ||
title="The URL for the cm-service database", | ||
description="The URL for the cm-service database", | ||
validation_alias="CM_DATABASE_URL", | ||
) | ||
|
||
database_password: str | None = Field( | ||
default=None, | ||
title="The password for the cm-service database", | ||
description="The password for the cm-service database", | ||
validation_alias="CM_DATABASE_PASSWORD", | ||
) | ||
|
||
database_schema: str | None = Field( | ||
default=None, | ||
title="Schema to use for cm-service database", | ||
description="Schema to use for cm-service database", | ||
validation_alias="CM_DATABASE_SCHEMA", | ||
) | ||
|
||
database_echo: bool = Field( | ||
default=False, | ||
title="SQLAlchemy engine echo setting for the cm-service database", | ||
description="SQLAlchemy engine echo setting for the cm-service database", | ||
validation_alias="CM_DATABASE_ECHO", | ||
) | ||
|
||
profile: Profile = Field( | ||
default=Profile.development, | ||
title="Application logging profile", | ||
validation_alias="CM_LOG_PROFILE", | ||
) | ||
|
||
logger_name: str = Field( | ||
default="cmservice", | ||
title="The root name of the application's logger", | ||
validation_alias="CM_LOGGER", | ||
) | ||
class Configuration(BaseSettings): | ||
"""Configuration for cm-service. | ||
log_level: LogLevel = Field( | ||
default=LogLevel.INFO, | ||
title="Log level of the application's logger", | ||
validation_alias="CM_LOG_LEVEL", | ||
Base settings may be consumed from environment variables that are named | ||
according to the pattern 'CM_FIELD'; nested models may be consumed from | ||
environment variables named according to the pattern 'NESTED_MODEL__FIELD'. | ||
""" | ||
|
||
model_config = SettingsConfigDict( | ||
env_prefix="CM_", | ||
env_nested_delimiter="__", | ||
nested_model_default_partial_update=True, | ||
) | ||
|
||
# Nested Models | ||
asgi: AsgiConfiguration = AsgiConfiguration() | ||
db: DatabaseConfiguration = DatabaseConfiguration() | ||
slurm: SlurmConfiguration = SlurmConfiguration() | ||
htcondor: HTCondorConfiguration = HTCondorConfiguration() | ||
logging: LoggingConfiguration = LoggingConfiguration() | ||
|
||
|
||
config = Configuration() | ||
"""Configuration for cm-service.""" |
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
Oops, something went wrong.