diff --git a/frigate/util/config.py b/frigate/util/config.py index 634016a331..1555a903b4 100644 --- a/frigate/util/config.py +++ b/frigate/util/config.py @@ -212,6 +212,14 @@ def find_config_file() -> str: return config_path +def _write_config(config_file: str, yaml: YAML, config: dict[str, Any]) -> None: + tmp_file = f"{config_file}.tmp" + with open(tmp_file, "w") as f: + yaml.dump(config, f) + shutil.copymode(config_file, tmp_file) + os.replace(tmp_file, config_file) + + def migrate_frigate_config(config_file: str): """handle migrating the frigate config.""" logger.info("Checking if frigate config needs migration...") @@ -259,8 +267,7 @@ def migrate_frigate_config(config_file: str): if previous_version < "0.14": logger.info(f"Migrating frigate config from {previous_version} to 0.14...") new_config = migrate_014(new_config) - with open(config_file, "w") as f: - yaml.dump(new_config, f) + _write_config(config_file, yaml, new_config) previous_version = "0.14" logger.info("Migrating export file names...") @@ -277,56 +284,48 @@ def migrate_frigate_config(config_file: str): if previous_version < "0.15-0": logger.info(f"Migrating frigate config from {previous_version} to 0.15-0...") new_config = migrate_015_0(new_config) - with open(config_file, "w") as f: - yaml.dump(new_config, f) + _write_config(config_file, yaml, new_config) previous_version = "0.15-0" if previous_version < "0.15-1": logger.info(f"Migrating frigate config from {previous_version} to 0.15-1...") new_config = migrate_015_1(new_config) - with open(config_file, "w") as f: - yaml.dump(new_config, f) + _write_config(config_file, yaml, new_config) previous_version = "0.15-1" if previous_version < "0.16-0": logger.info(f"Migrating frigate config from {previous_version} to 0.16-0...") new_config = migrate_016_0(new_config) - with open(config_file, "w") as f: - yaml.dump(new_config, f) + _write_config(config_file, yaml, new_config) previous_version = "0.16-0" if previous_version < "0.17-0": logger.info(f"Migrating frigate config from {previous_version} to 0.17-0...") new_config = migrate_017_0(new_config) - with open(config_file, "w") as f: - yaml.dump(new_config, f) + _write_config(config_file, yaml, new_config) previous_version = "0.17-0" if previous_version < "0.18-0": logger.info(f"Migrating frigate config from {previous_version} to 0.18-0...") new_config = migrate_018_0(new_config) - with open(config_file, "w") as f: - yaml.dump(new_config, f) + _write_config(config_file, yaml, new_config) previous_version = "0.18-0" if previous_version < "0.19-0": logger.info(f"Migrating frigate config from {previous_version} to 0.19-0...") new_config = migrate_019_0(new_config) - with open(config_file, "w") as f: - yaml.dump(new_config, f) + _write_config(config_file, yaml, new_config) previous_version = "0.19-0" if needs_models: logger.info("Migrating frigate detectors and model to models...") new_config = migrate_models(new_config) - with open(config_file, "w") as f: - yaml.dump(new_config, f) + _write_config(config_file, yaml, new_config) if needs_detector_rename: logger.info("Migrating renamed frigate detectors...") new_config = rename_hailo_detector(new_config) - with open(config_file, "w") as f: - yaml.dump(new_config, f) + _write_config(config_file, yaml, new_config) logger.info("Finished frigate config migration...")