Fix config synchronization in scheduler + Remove MULTISITE variables being fetched when MULTISITE is set to no

This commit is contained in:
Théophile Diot 2023-08-08 16:11:34 +00:00 committed by GitHub
parent 7f3f3ac7e3
commit 81c2c3187c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 64 additions and 53 deletions

View file

@ -879,52 +879,53 @@ class Database:
if setting.context == "multisite":
multisite.append(setting.id)
for service in session.query(Services).with_entities(Services.id).all():
checked_settings = []
for key, value in deepcopy(config).items():
original_key = key
if self.suffix_rx.search(key):
key = key[: -len(str(key.split("_")[-1])) - 1]
if config.get("MULTISITE", "no") == "yes":
for service in session.query(Services).with_entities(Services.id).all():
checked_settings = []
for key, value in deepcopy(config).items():
original_key = key
if self.suffix_rx.search(key):
key = key[: -len(str(key.split("_")[-1])) - 1]
if key not in multisite:
continue
elif f"{service.id}_{original_key}" not in config:
config[f"{service.id}_{original_key}"] = value
if key not in multisite:
continue
elif f"{service.id}_{original_key}" not in config:
config[f"{service.id}_{original_key}"] = value
if original_key not in checked_settings:
checked_settings.append(original_key)
else:
continue
if original_key not in checked_settings:
checked_settings.append(original_key)
else:
continue
service_settings = (
session.query(Services_settings)
.with_entities(
Services_settings.value,
Services_settings.suffix,
Services_settings.method,
)
.filter_by(service_id=service.id, setting_id=key)
.all()
)
for service_setting in service_settings:
config[
f"{service.id}_{key}"
+ (
f"_{service_setting.suffix}"
if service_setting.suffix > 0
else ""
service_settings = (
session.query(Services_settings)
.with_entities(
Services_settings.value,
Services_settings.suffix,
Services_settings.method,
)
] = (
service_setting.value
if methods is False
else {
"value": service_setting.value,
"global": False,
"method": service_setting.method,
}
.filter_by(service_id=service.id, setting_id=key)
.all()
)
for service_setting in service_settings:
config[
f"{service.id}_{key}"
+ (
f"_{service_setting.suffix}"
if service_setting.suffix > 0
else ""
)
] = (
service_setting.value
if methods is False
else {
"value": service_setting.value,
"global": False,
"method": service_setting.method,
}
)
if config["MULTISITE"] == "yes":
servers = " ".join(
service.id for service in session.query(Services).all()

View file

@ -47,6 +47,9 @@ SCHEDULER: Optional[JobScheduler] = None
GENERATE = False
INTEGRATION = "Linux"
CACHE_PATH = join(sep, "var", "cache", "bunkerweb")
SCHEDULER_TMP_ENV_PATH = Path(sep, "var", "tmp", "bunkerweb", "scheduler.env")
SCHEDULER_TMP_ENV_PATH.parent.mkdir(parents=True, exist_ok=True)
SCHEDULER_TMP_ENV_PATH.touch()
logger = setup_logger("Scheduler", getenv("LOG_LEVEL", "INFO"))
@ -422,8 +425,8 @@ if __name__ == "__main__":
external_plugin.pop("jobs", None)
tmp_external_plugins.append(external_plugin)
changes = {hash(dict_to_frozenset(d)) for d in tmp_external_plugins} != {
hash(dict_to_frozenset(d)) for d in db_plugins
changes = {dict_to_frozenset(d) for d in tmp_external_plugins} != {
dict_to_frozenset(d) for d in db_plugins
}
if changes:
@ -510,6 +513,10 @@ if __name__ == "__main__":
logger.info("All jobs in run_once() were successful")
if GENERATE:
content = ""
for k, v in env.items():
content += f"{k}={v}\n"
SCHEDULER_TMP_ENV_PATH.write_text(content)
# run the generator
proc = subprocess_run(
[
@ -521,12 +528,13 @@ if __name__ == "__main__":
join(sep, "usr", "share", "bunkerweb", "confs"),
"--output",
join(sep, "etc", "nginx"),
]
+ (
["--variables", str(tmp_variables_path)]
if args.variables and FIRST_RUN
else []
),
"--variables",
(
str(tmp_variables_path)
if args.variables and FIRST_RUN
else str(SCHEDULER_TMP_ENV_PATH)
),
],
stdin=DEVNULL,
stderr=STDOUT,
check=False,
@ -703,11 +711,9 @@ if __name__ == "__main__":
if CONFIGS_NEED_GENERATION:
CHANGES.append("custom_configs")
Thread(
target=generate_custom_configs,
args=(db.get_custom_configs(),),
kwargs={"original_path": configs_path},
).start()
generate_custom_configs(
db.get_custom_configs(), original_path=configs_path
)
if PLUGINS_NEED_GENERATION:
CHANGES.append("external_plugins")
@ -720,6 +726,10 @@ if __name__ == "__main__":
if CONFIG_NEED_GENERATION:
CHANGES.append("config")
env = db.get_config()
content = ""
for k, v in env.items():
content += f"{k}={v}\n"
SCHEDULER_TMP_ENV_PATH.write_text(content)
if INSTANCES_NEED_GENERATION:
CHANGES.append("instances")