mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-02-15 07:35:27 +03:00
Install config global state early
This commit is contained in:
parent
66c6f8130d
commit
9e68719c08
@ -32,7 +32,7 @@ def main() -> None:
|
|||||||
|
|
||||||
# Load the configuration.
|
# Load the configuration.
|
||||||
try:
|
try:
|
||||||
config = FrigateConfig.load()
|
config = FrigateConfig.load(install=True)
|
||||||
except ValidationError as e:
|
except ValidationError as e:
|
||||||
print("*************************************************************")
|
print("*************************************************************")
|
||||||
print("*************************************************************")
|
print("*************************************************************")
|
||||||
|
|||||||
@ -569,7 +569,6 @@ class FrigateApp:
|
|||||||
|
|
||||||
# Ensure global state.
|
# Ensure global state.
|
||||||
self.ensure_dirs()
|
self.ensure_dirs()
|
||||||
self.config.install()
|
|
||||||
|
|
||||||
# Start frigate services.
|
# Start frigate services.
|
||||||
self.init_camera_metrics()
|
self.init_camera_metrics()
|
||||||
|
|||||||
@ -137,6 +137,17 @@ def validate_env_string(v: str) -> str:
|
|||||||
EnvString = Annotated[str, AfterValidator(validate_env_string)]
|
EnvString = Annotated[str, AfterValidator(validate_env_string)]
|
||||||
|
|
||||||
|
|
||||||
|
def validate_env_vars(v: dict[str, str], info: ValidationInfo) -> dict[str, str]:
|
||||||
|
if isinstance(info.context, dict) and info.context.get("install", False):
|
||||||
|
for k, v in v:
|
||||||
|
os.environ[k] = v
|
||||||
|
|
||||||
|
return v
|
||||||
|
|
||||||
|
|
||||||
|
EnvVars = Annotated[dict[str, str], AfterValidator(validate_env_vars)]
|
||||||
|
|
||||||
|
|
||||||
class UIConfig(FrigateBaseModel):
|
class UIConfig(FrigateBaseModel):
|
||||||
timezone: Optional[str] = Field(default=None, title="Override UI timezone.")
|
timezone: Optional[str] = Field(default=None, title="Override UI timezone.")
|
||||||
time_format: TimeFormatEnum = Field(
|
time_format: TimeFormatEnum = Field(
|
||||||
@ -1311,8 +1322,9 @@ class LoggerConfig(FrigateBaseModel):
|
|||||||
default_factory=dict, title="Log level for specified processes."
|
default_factory=dict, title="Log level for specified processes."
|
||||||
)
|
)
|
||||||
|
|
||||||
def install(self):
|
@model_validator(mode="after")
|
||||||
"""Install global logging state."""
|
def post_validation(self, info: ValidationInfo) -> Self:
|
||||||
|
if isinstance(info.context, dict) and info.context.get("install", False):
|
||||||
logging.getLogger().setLevel(self.default.value.upper())
|
logging.getLogger().setLevel(self.default.value.upper())
|
||||||
|
|
||||||
log_levels = {
|
log_levels = {
|
||||||
@ -1466,6 +1478,15 @@ def verify_motion_and_detect(camera_config: CameraConfig) -> ValueError | None:
|
|||||||
|
|
||||||
|
|
||||||
class FrigateConfig(FrigateBaseModel):
|
class FrigateConfig(FrigateBaseModel):
|
||||||
|
# Fields that install global state should be defined first, so that their validators run first.
|
||||||
|
environment_vars: EnvVars = Field(
|
||||||
|
default_factory=dict, title="Frigate environment variables."
|
||||||
|
)
|
||||||
|
logger: LoggerConfig = Field(
|
||||||
|
default_factory=LoggerConfig, title="Logging configuration."
|
||||||
|
)
|
||||||
|
|
||||||
|
# Rest of the fields
|
||||||
mqtt: MqttConfig = Field(title="MQTT configuration.")
|
mqtt: MqttConfig = Field(title="MQTT configuration.")
|
||||||
database: DatabaseConfig = Field(
|
database: DatabaseConfig = Field(
|
||||||
default_factory=DatabaseConfig, title="Database configuration."
|
default_factory=DatabaseConfig, title="Database configuration."
|
||||||
@ -1475,9 +1496,6 @@ class FrigateConfig(FrigateBaseModel):
|
|||||||
default_factory=ProxyConfig, title="Proxy configuration."
|
default_factory=ProxyConfig, title="Proxy configuration."
|
||||||
)
|
)
|
||||||
auth: AuthConfig = Field(default_factory=AuthConfig, title="Auth configuration.")
|
auth: AuthConfig = Field(default_factory=AuthConfig, title="Auth configuration.")
|
||||||
environment_vars: Dict[str, str] = Field(
|
|
||||||
default_factory=dict, title="Frigate environment variables."
|
|
||||||
)
|
|
||||||
ui: UIConfig = Field(default_factory=UIConfig, title="UI configuration.")
|
ui: UIConfig = Field(default_factory=UIConfig, title="UI configuration.")
|
||||||
notifications: NotificationConfig = Field(
|
notifications: NotificationConfig = Field(
|
||||||
default_factory=NotificationConfig, title="Notification Config"
|
default_factory=NotificationConfig, title="Notification Config"
|
||||||
@ -1492,9 +1510,6 @@ class FrigateConfig(FrigateBaseModel):
|
|||||||
default=DEFAULT_DETECTORS,
|
default=DEFAULT_DETECTORS,
|
||||||
title="Detector hardware configuration.",
|
title="Detector hardware configuration.",
|
||||||
)
|
)
|
||||||
logger: LoggerConfig = Field(
|
|
||||||
default_factory=LoggerConfig, title="Logging configuration."
|
|
||||||
)
|
|
||||||
record: RecordConfig = Field(
|
record: RecordConfig = Field(
|
||||||
default_factory=RecordConfig, title="Global record configuration."
|
default_factory=RecordConfig, title="Global record configuration."
|
||||||
)
|
)
|
||||||
@ -1873,12 +1888,9 @@ class FrigateConfig(FrigateBaseModel):
|
|||||||
return cls.parse(config_yaml, is_json=False, **context)
|
return cls.parse(config_yaml, is_json=False, **context)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def parse_object(cls, obj: Any, *, plus_api: Optional[PlusApi] = None):
|
def parse_object(
|
||||||
return cls.model_validate(obj, context={"plus_api": plus_api})
|
cls, obj: Any, *, plus_api: Optional[PlusApi] = None, install: bool = False
|
||||||
|
):
|
||||||
def install(self):
|
return cls.model_validate(
|
||||||
"""Install global state from the config."""
|
obj, context={"plus_api": plus_api, "install": install}
|
||||||
self.logger.install()
|
)
|
||||||
|
|
||||||
for key, value in self.environment_vars.items():
|
|
||||||
os.environ[key] = value
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user