[#652] Fix error when deleting a service that have custom configs on web UI

This commit is contained in:
Théophile Diot 2023-10-10 18:05:05 +02:00
parent ff85f1c2bb
commit b063ac8a32
No known key found for this signature in database
GPG key ID: 248FEA4BAE400D06
4 changed files with 38 additions and 32 deletions

View file

@ -113,6 +113,11 @@ if __name__ == "__main__":
type=str,
help="The method that is used to save the config",
)
parser.add_argument(
"--no-check-changes",
action="store_true",
help="Set the changes to checked in the database",
)
args = parser.parse_args()
settings_path = Path(normpath(args.settings))
@ -361,10 +366,11 @@ if __name__ == "__main__":
changes.append("instances")
logger.info("Instance 127.0.0.1 successfully saved to database")
# update changes in db
ret = db.checked_changes(changes, value=True)
if ret:
logger.error(f"An error occurred when setting the changes to checked in the database : {ret}")
if not args.no_check_changes:
# update changes in db
ret = db.checked_changes(changes, value=True)
if ret:
logger.error(f"An error occurred when setting the changes to checked in the database : {ret}")
except SystemExit as e:
sys_exit(e.code)
except:

View file

@ -232,43 +232,42 @@ def manage_bunkerweb(method: str, *args, operation: str = "reloads"):
# Do the operation
error = False
if method == "services":
editing = operation == "edit"
service_custom_confs = glob(join(sep, "etc", "bunkerweb", "configs", "*", args[1]))
service_custom_confs = glob(join(sep, "etc", "bunkerweb", "configs", "*", args[1].split(" ")[0]))
moved = False
deleted = False
if operation == "new":
operation, error = app.config["CONFIG"].new_service(args[0])
elif operation == "edit":
if args[1] != args[2] and service_custom_confs:
if args[1].split(" ")[0] != args[2].split(" ")[0] and service_custom_confs:
for service_custom_conf in service_custom_confs:
if listdir(service_custom_conf):
move(
service_custom_conf,
service_custom_conf.replace(f"{sep}{args[1].split(' ')[0]}", f"{sep}{args[2].split(' ')[0]}").replace(join(sep, "etc"), join(sep, "var", "tmp")),
)
move(service_custom_conf, service_custom_conf.replace(f"{sep}{args[1].split(' ')[0]}", f"{sep}{args[2].split(' ')[0]}"))
moved = True
operation, error = app.config["CONFIG"].edit_service(args[1], args[0])
operation, error = app.config["CONFIG"].edit_service(args[1], args[0], check_changes=not moved)
elif operation == "delete":
operation, error = app.config["CONFIG"].delete_service(args[2])
for service_custom_conf in glob(join(sep, "etc", "bunkerweb", "configs", "*", args[2].split(" ")[0])):
if listdir(service_custom_conf):
rmtree(service_custom_conf, ignore_errors=True)
deleted = True
operation, error = app.config["CONFIG"].delete_service(args[2], check_changes=not deleted)
if error:
app.config["TO_FLASH"].append({"content": operation, "type": "error"})
else:
app.config["TO_FLASH"].append({"content": operation, "type": "success"})
if editing and moved and args[1] != args[2] and service_custom_confs:
for tmp_service_custom_conf in glob(join(sep, "var", "tmp", "bunkerweb", "configs", "*", args[2].split(" ")[0])):
move(
tmp_service_custom_conf,
tmp_service_custom_conf.replace(
join(sep, "var", "tmp"),
join(sep, "etc"),
),
)
error = app.config["CONFIGFILES"].save_configs()
if moved or deleted:
changes = ["config", "custom_configs"]
error = app.config["CONFIGFILES"].save_configs(check_changes=False)
if error:
app.config["TO_FLASH"].append({"content": error, "type": "error"})
rmtree(join(sep, "var", "tmp", "bunkerweb", "configs"), ignore_errors=True)
changes.pop()
# update changes in db
ret = db.checked_changes(changes, value=True)
if ret:
app.config["TO_FLASH"].append({"content": f"An error occurred when setting the changes to checked in the database : {ret}", "type": "error"})
if method == "global_config":
operation = app.config["CONFIG"].edit_global_conf(args[0])
elif method == "plugins":

View file

@ -17,7 +17,7 @@ class Config:
self.__settings = json_loads(Path(sep, "usr", "share", "bunkerweb", "settings.json").read_text(encoding="utf-8"))
self.__db = db
def __gen_conf(self, global_conf: dict, services_conf: list[dict]) -> None:
def __gen_conf(self, global_conf: dict, services_conf: list[dict], *, check_changes: bool = True) -> None:
"""Generates the nginx configuration file from the given configuration
Parameters
@ -62,7 +62,8 @@ class Config:
str(env_file),
"--method",
"ui",
],
]
+ (["--no-check-changes"] if not check_changes else []),
stdin=DEVNULL,
stderr=STDOUT,
check=False,
@ -196,7 +197,7 @@ class Config:
0,
)
def edit_service(self, old_server_name: str, variables: dict) -> Tuple[str, int]:
def edit_service(self, old_server_name: str, variables: dict, *, check_changes: bool = True) -> Tuple[str, int]:
"""Edits a service
Parameters
@ -234,7 +235,7 @@ class Config:
if k.startswith(old_server_name_splitted[0]):
config.pop(k)
self.__gen_conf(config, services)
self.__gen_conf(config, services, check_changes=check_changes)
return (
f"Configuration for {old_server_name_splitted[0]} has been edited.",
0,
@ -256,7 +257,7 @@ class Config:
self.__gen_conf(self.get_config(methods=False) | variables, self.get_services(methods=False))
return "The global configuration has been edited."
def delete_service(self, service_name: str) -> Tuple[str, int]:
def delete_service(self, service_name: str, *, check_changes: bool = True) -> Tuple[str, int]:
"""Deletes a service
Parameters
@ -301,5 +302,5 @@ class Config:
if k in service:
service.pop(k)
self.__gen_conf(new_env, new_services)
self.__gen_conf(new_env, new_services, check_changes=check_changes)
return f"Configuration for {service_name} has been deleted.", 0

View file

@ -50,7 +50,7 @@ class ConfigFiles:
generate_custom_configs(custom_configs)
self.__logger.info("Custom configs refreshed successfully")
def save_configs(self) -> str:
def save_configs(self, *, check_changes: bool = True) -> str:
custom_configs = []
configs_path = join(sep, "etc", "bunkerweb", "configs")
root_dirs = listdir(configs_path)
@ -70,7 +70,7 @@ class ConfigFiles:
}
)
err = self.__db.save_custom_configs(custom_configs, "ui")
err = self.__db.save_custom_configs(custom_configs, "ui", changed=check_changes)
if err:
self.__logger.error(f"Could not save custom configs: {err}")
return "Couldn't save custom configs to database"