diff --git a/frigate/app.py b/frigate/app.py index 596abed72..a0722ecf2 100644 --- a/frigate/app.py +++ b/frigate/app.py @@ -63,6 +63,7 @@ from frigate.storage import StorageMaintainer from frigate.timeline import TimelineProcessor from frigate.types import CameraMetricsTypes, PTZMetricsTypes from frigate.util.builtin import save_default_config +from frigate.util.config import migrate_frigate_config from frigate.util.object import get_camera_regions_grid from frigate.version import VERSION from frigate.video import capture_camera, track_camera @@ -126,6 +127,9 @@ class FrigateApp: config_file = config_file_yaml save_default_config(config_file) + # check if the config file needs to be migrated + migrate_frigate_config(config_file) + user_config = FrigateConfig.parse_file(config_file) self.config = user_config.runtime_config(self.plus_api) diff --git a/frigate/util/config.py b/frigate/util/config.py new file mode 100644 index 000000000..2c1942926 --- /dev/null +++ b/frigate/util/config.py @@ -0,0 +1,105 @@ +"""configuration utils.""" + +import logging +import os +import shutil + +from ruamel.yaml import YAML + +from frigate.const import CONFIG_DIR + +logger = logging.getLogger(__name__) + +CURRENT_CONFIG_VERSION = 0.14 + +def migrate_frigate_config(config_file: str): + """handle migrating the frigate config.""" + logger.info("Checking if frigate config needs migration...") + version_file = os.path.join(CONFIG_DIR, ".version") + + if not os.path.isfile(version_file): + previous_version = 0.13 + else: + with open(version_file) as f: + try: + previous_version = float(f.readline()) + except Exception: + previous_version = 0.13 + + if previous_version == CURRENT_CONFIG_VERSION: + logger.info("frigate config does not need migration...") + return + + logger.info("copying config as backup...") + shutil.copy(config_file, os.path.join(CONFIG_DIR, "backup_config.yaml")) + + yaml = YAML() + yaml.indent(mapping=2, sequence=4, offset=2) + with open(config_file, "r") as f: + config: dict[str, dict[str, any]] = yaml.load(f) + + if previous_version < 0.14: + logger.info(f"Migrating frigate config from {previous_version} to 0.14...") + new_config = migrate_014(config) + with open(os.path.join(CONFIG_DIR, "new_config.yaml"), "w") as f: + yaml.dump(new_config, f) + previous_version = 0.14 + + #with open(version_file, "w") as f: + # f.write(str(CURRENT_CONFIG_VERSION)) + + logger.info("Finished frigate config migration...") + + +def migrate_014(config: dict[str, dict[str, any]]) -> dict[str, dict[str, any]]: + """Handle migrating frigate config to 0.14""" + # migrate record.events.required_zones to review.alerts.required_zones + new_config = config.copy() + global_required_zones = config.get("record", {}).get("events", {}).get("required_zones", []) + + if global_required_zones: + if not new_config.get("review"): + new_config["review"] = {} + + if not new_config["review"].get("alerts"): + new_config["review"]["alerts"] = {} + + if not new_config["review"]["alerts"].get("required_zones"): + new_config["review"]["alerts"]["required_zones"] = global_required_zones + + del new_config["record"]["events"]["required_zones"] + + if not new_config["record"]["events"]: + del new_config["record"]["events"] + + if not new_config["record"]: + del new_config["record"] + + for name, camera in config.get("cameras", {}).items(): + camera_config: dict[str, dict[str, any]] = camera.copy() + required_zones = camera_config.get("record", {}).get("events", {}).get("required_zones", []) + + if required_zones: + if not camera_config.get("review"): + camera_config["review"] = {} + + if not camera_config["review"].get("alerts"): + camera_config["review"]["alerts"] = {} + + if not camera_config["review"]["alerts"].get("required_zones"): + camera_config["review"]["alerts"]["required_zones"] = required_zones + + del camera_config["record"]["events"]["required_zones"] + + if not camera_config["record"]["events"]: + del camera_config["record"]["events"] + + if not camera_config["record"]: + del camera_config["record"] + + new_config["cameras"][name] = camera_config + + return new_config + + +