Optimize the way the UI handles services creation and edition

This commit is contained in:
Théophile Diot 2023-09-28 14:27:48 +01:00
parent c0816bb119
commit 48096d711c
No known key found for this signature in database
GPG key ID: 248FEA4BAE400D06
2 changed files with 77 additions and 43 deletions

View file

@ -245,25 +245,27 @@ LOG_RX = re_compile(
def manage_bunkerweb(method: str, *args, operation: str = "reloads"):
# Do the operation
error = False
if method == "services":
error = False
editing = False
editing = operation == "edit"
service_custom_confs = glob(
join(sep, "etc", "bunkerweb", "configs", "*", args[1])
)
moved = False
if operation == "new":
operation, error = app.config["CONFIG"].new_service(args[0])
elif operation == "edit":
editing = True
if args[1] != args[2] and service_custom_confs:
for service_custom_conf in service_custom_confs:
move(
service_custom_conf,
service_custom_conf.replace(
f"{sep}{args[1]}", f"{sep}{args[2]}"
).replace(join(sep, "etc"), join(sep, "var", "tmp")),
)
if listdir(service_custom_conf):
move(
service_custom_conf,
service_custom_conf.replace(
f"{sep}{args[1]}", f"{sep}{args[2]}"
).replace(join(sep, "etc"), join(sep, "var", "tmp")),
)
moved = True
operation, error = app.config["CONFIG"].edit_service(args[1], args[0])
elif operation == "delete":
operation, error = app.config["CONFIG"].delete_service(args[2])
@ -273,7 +275,7 @@ def manage_bunkerweb(method: str, *args, operation: str = "reloads"):
else:
app.config["TO_FLASH"].append({"content": operation, "type": "success"})
if editing and args[1] != args[2] and service_custom_confs:
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])
):
@ -292,7 +294,6 @@ def manage_bunkerweb(method: str, *args, operation: str = "reloads"):
)
if method == "global_config":
operation = app.config["CONFIG"].edit_global_conf(args[0])
app.config["TO_FLASH"].append({"content": operation, "type": "success"})
elif method == "plugins":
app.config["CONFIG"].reload_config()
@ -304,18 +305,21 @@ def manage_bunkerweb(method: str, *args, operation: str = "reloads"):
operation = app.config["INSTANCES"].stop_instance(args[0])
elif operation == "restart":
operation = app.config["INSTANCES"].restart_instance(args[0])
else:
elif not error:
operation = "The scheduler will be in charge of reloading the instances."
if isinstance(operation, list):
for op in operation:
app.config["TO_FLASH"].append(
{"content": f"Reload failed for the instance {op}", "type": "error"}
)
elif operation.startswith("Can't"):
app.config["TO_FLASH"].append({"content": operation, "type": "error"})
else:
app.config["TO_FLASH"].append({"content": operation, "type": "success"})
operation = ""
if operation:
if isinstance(operation, list):
for op in operation:
app.config["TO_FLASH"].append(
{"content": f"Reload failed for the instance {op}", "type": "error"}
)
elif operation.startswith("Can't"):
app.config["TO_FLASH"].append({"content": operation, "type": "error"})
else:
app.config["TO_FLASH"].append({"content": operation, "type": "success"})
app.config["RELOADING"] = False
@ -530,6 +534,18 @@ def services():
):
del variables[variable]
if (
request.form["operation"] == "edit"
and len(variables) == 1
and "SERVER_NAME" in variables
and variables["SERVER_NAME"] == request.form.get("OLD_SERVER_NAME", "")
):
flash(
"The service was not edited because no values were changed.",
"error",
)
return redirect(url_for("loading", next=url_for("services")))
error = app.config["CONFIG"].check_variables(variables)
if error:

View file

@ -76,11 +76,10 @@ class Config:
check=False,
)
env_file.unlink()
if proc.returncode != 0:
raise Exception(f"Error from generator (return code = {proc.returncode})")
env_file.unlink()
def get_plugins_settings(self) -> dict:
return {
**{k: v for x in self.get_plugins() for k, v in x["settings"].items()},
@ -180,7 +179,7 @@ class Config:
self.get_config(methods=False), self.get_services(methods=False)
)
def new_service(self, variables: dict, edit: bool = False) -> Tuple[str, int]:
def new_service(self, variables: dict) -> Tuple[str, int]:
"""Creates a new service from the given variables
Parameters
@ -199,17 +198,16 @@ class Config:
raise this if the service already exists
"""
services = self.get_services(methods=False)
for i, service in enumerate(services):
if service["SERVER_NAME"] == variables["SERVER_NAME"] or service[
"SERVER_NAME"
] in variables["SERVER_NAME"].split(" "):
if not edit:
return (
f"Service {service['SERVER_NAME'].split(' ')[0]} already exists.",
1,
)
services.pop(i)
server_name_splitted = variables["SERVER_NAME"].split(" ")
for service in services:
if (
service["SERVER_NAME"] == variables["SERVER_NAME"]
or service["SERVER_NAME"] in server_name_splitted
):
return (
f"Service {service['SERVER_NAME'].split(' ')[0]} already exists.",
1,
)
services.append(variables)
self.__gen_conf(self.get_config(methods=False), services)
@ -233,19 +231,39 @@ class Config:
str
the confirmation message
"""
message, error = self.delete_service(old_server_name)
services = self.get_services(methods=False)
changed_server_name = old_server_name != variables["SERVER_NAME"]
server_name_splitted = variables["SERVER_NAME"].split(" ")
old_server_name_splitted = old_server_name.split(" ")
for i, service in enumerate(deepcopy(services)):
if (
service["SERVER_NAME"] == variables["SERVER_NAME"]
or service["SERVER_NAME"] in server_name_splitted
):
if changed_server_name:
return (
f"Service {service['SERVER_NAME'].split(' ')[0]} already exists.",
1,
)
services.pop(i)
elif changed_server_name and (
service["SERVER_NAME"] == old_server_name
or service["SERVER_NAME"] in old_server_name_splitted
):
services.pop(i)
if error:
return message, error
services.append(variables)
config = self.get_config(methods=False)
message, error = self.new_service(variables, edit=True)
if error:
return message, error
if changed_server_name:
for k in deepcopy(config):
if k.startswith(old_server_name):
config.pop(k)
self.__gen_conf(config, services)
return (
f"Configuration for {old_server_name.split(' ')[0]} has been edited.",
error,
0,
)
def edit_global_conf(self, variables: dict) -> str: