Skip to content

Commit

Permalink
Add services page to web UI and finish raw mode for service settings v1
Browse files Browse the repository at this point in the history
  • Loading branch information
TheophileDiot committed Sep 13, 2024
1 parent 37cdd32 commit 27152f0
Show file tree
Hide file tree
Showing 19 changed files with 1,110 additions and 133 deletions.
36 changes: 32 additions & 4 deletions src/common/db/Database.py
Original file line number Diff line number Diff line change
Expand Up @@ -1349,13 +1349,14 @@ def save_config(self, config: Dict[str, Any], method: str, changed: Optional[boo

for draft in drafts:
if draft not in db_drafts:
current_time = datetime.now().astimezone()
if draft not in db_ids:
self.logger.debug(f"Adding draft {draft}")
to_put.append(Services(id=draft, method=method, is_draft=True))
to_put.append(Services(id=draft, method=method, is_draft=True, creation_date=current_time, last_update=current_time))
db_ids[draft] = {"method": method, "is_draft": True}
elif method == db_ids[draft]["method"]:
self.logger.debug(f"Updating draft {draft}")
session.query(Services).filter(Services.id == draft).update({Services.is_draft: True})
session.query(Services).filter(Services.id == draft).update({Services.is_draft: True, Services.last_update: current_time})
changed_services = True

template = config.get("USE_TEMPLATE", "")
Expand Down Expand Up @@ -1385,7 +1386,12 @@ def save_config(self, config: Dict[str, Any], method: str, changed: Optional[boo

if server_name not in db_ids:
self.logger.debug(f"Adding service {server_name}")
to_put.append(Services(id=server_name, method=method, is_draft=server_name in drafts))
current_time = datetime.now().astimezone()
to_put.append(
Services(
id=server_name, method=method, is_draft=server_name in drafts, creation_date=current_time, last_update=current_time
)
)
db_ids[server_name] = {"method": method, "is_draft": server_name in drafts}
if server_name not in drafts:
changed_services = True
Expand Down Expand Up @@ -1425,6 +1431,7 @@ def save_config(self, config: Dict[str, Any], method: str, changed: Optional[boo
self.logger.debug(f"Adding setting {key} for service {server_name}")
changed_plugins.add(setting.plugin_id)
to_put.append(Services_settings(service_id=server_name, setting_id=key, value=value, suffix=suffix, method=method))
session.query(Services).filter(Services.id == server_name).update({Services.last_update: datetime.now().astimezone()})
elif (
method == service_setting.method or (service_setting.method not in ("scheduler", "autoconf") and method == "autoconf")
) and service_setting.value != value:
Expand All @@ -1446,6 +1453,7 @@ def save_config(self, config: Dict[str, Any], method: str, changed: Optional[boo

self.logger.debug(f"Updating setting {key} for service {server_name}")
query.update({Services_settings.value: value, Services_settings.method: method})
session.query(Services).filter(Services.id == server_name).update({Services.last_update: datetime.now().astimezone()})
elif setting and original_key not in global_values:
global_values.append(original_key)
global_value = (
Expand Down Expand Up @@ -1502,7 +1510,10 @@ def save_config(self, config: Dict[str, Any], method: str, changed: Optional[boo

if not session.query(Services).with_entities(Services.id).filter_by(id=first_server).first():
self.logger.debug(f"Adding service {first_server}")
to_put.append(Services(id=first_server, method=method))
current_time = datetime.now().astimezone()
to_put.append(
Services(id=first_server, method=method, is_draft=first_server in drafts, creation_date=current_time, last_update=current_time)
)
changed_services = True

for key, value in config.items():
Expand Down Expand Up @@ -2020,6 +2031,23 @@ def get_services_settings(self, methods: bool = False, with_drafts: bool = False

return services

def get_services(self, *, with_drafts: bool = False) -> List[Dict[str, Any]]:
"""Get the services from the database"""
with self._db_session() as session:
return [
{
"id": service.id,
"method": service.method,
"is_draft": service.is_draft,
"creation_date": service.creation_date,
"last_update": service.last_update,
}
for service in session.query(Services).with_entities(
Services.id, Services.method, Services.is_draft, Services.creation_date, Services.last_update
)
if with_drafts or not service.is_draft
]

def add_job_run(self, job_name: str, success: bool, start_date: datetime, end_date: Optional[datetime] = None) -> str:
"""Add a job run."""
with self._db_session() as session:
Expand Down
2 changes: 2 additions & 0 deletions src/common/db/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ class Services(Base):
id = Column(String(64), primary_key=True)
method = Column(METHODS_ENUM, nullable=False)
is_draft = Column(Boolean, default=False, nullable=False)
creation_date = Column(DateTime(timezone=True), nullable=False)
last_update = Column(DateTime(timezone=True), nullable=False)

settings = relationship("Services_settings", back_populates="service", cascade="all")
custom_configs = relationship("Custom_configs", back_populates="service", cascade="all")
Expand Down
25 changes: 17 additions & 8 deletions src/ui/app/models/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def __init__(self, db, data) -> None:
self.__db = db
self.__data = data

def __gen_conf(
def gen_conf(
self, global_conf: dict, services_conf: list[dict], *, check_changes: bool = True, changed_service: Optional[str] = None
) -> Union[str, Set[str]]:
"""Generates the nginx configuration file from the given configuration
Expand Down Expand Up @@ -108,7 +108,14 @@ def get_services(self, methods: bool = True, with_drafts: bool = False) -> list[
return self.__db.get_services_settings(methods=methods, with_drafts=with_drafts)

def check_variables(
self, variables: dict, config: dict, *, global_config: bool = False, ignored_multiples: Optional[Set[str]] = None, threaded: bool = False
self,
variables: dict,
config: dict,
*,
global_config: bool = False,
ignored_multiples: Optional[Set[str]] = None,
new: bool = False,
threaded: bool = False,
) -> dict:
"""Testify that the variables passed are valid
Expand All @@ -124,6 +131,8 @@ def check_variables(
"""
self.__data.load_from_file()
plugins_settings = self.get_plugins_settings()
blacklisted_settings = get_blacklisted_settings(global_config)

for k, v in variables.copy().items():
check = False

Expand All @@ -140,7 +149,7 @@ def check_variables(
variables.pop(k)
continue

if setting in get_blacklisted_settings(global_config):
if setting in blacklisted_settings:
message = f"Variable {k} is not editable, ignoring it"
if threaded:
self.__data["TO_FLASH"].append({"content": message, "type": "error"})
Expand All @@ -151,7 +160,7 @@ def check_variables(
elif setting not in config and plugins_settings[setting]["default"] == v:
variables.pop(k)
continue
elif config[setting]["method"] not in ("default", "ui"):
elif not new and setting != "IS_DRAFT" and config[setting]["method"] not in ("default", "ui"):
message = f"Variable {k} is not editable as is it managed by the {config[setting]['method']}, ignoring it"
if threaded:
self.__data["TO_FLASH"].append({"content": message, "type": "error"})
Expand Down Expand Up @@ -219,7 +228,7 @@ def new_service(self, variables: dict, is_draft: bool = False) -> Tuple[str, int
return f"Service {service['SERVER_NAME'].split(' ')[0]} already exists.", 1

services.append(variables | {"IS_DRAFT": "yes" if is_draft else "no"})
ret = self.__gen_conf(self.get_config(methods=False), services, check_changes=not is_draft)
ret = self.gen_conf(self.get_config(methods=False), services, check_changes=not is_draft)
if isinstance(ret, str):
return ret, 1
return f"Configuration for {variables['SERVER_NAME'].split(' ')[0]} has been generated.", 0
Expand Down Expand Up @@ -259,7 +268,7 @@ def edit_service(self, old_server_name: str, variables: dict, *, check_changes:
if k.startswith(old_server_name_splitted[0]):
config.pop(k)

ret = self.__gen_conf(config, services, check_changes=check_changes, changed_service=server_name_splitted[0])
ret = self.gen_conf(config, services, check_changes=check_changes, changed_service=server_name_splitted[0])
if isinstance(ret, str):
return ret, 1
return f"Configuration for {old_server_name_splitted[0]} has been edited.", 0
Expand All @@ -277,7 +286,7 @@ def edit_global_conf(self, variables: dict, *, check_changes: bool = True) -> Tu
str
the confirmation message
"""
ret = self.__gen_conf(self.get_config(methods=False) | variables, self.get_services(methods=False), check_changes=check_changes)
ret = self.gen_conf(self.get_config(methods=False) | variables, self.get_services(methods=False), check_changes=check_changes)
if isinstance(ret, str):
return ret, 1
return "The global configuration has been edited.", 0
Expand Down Expand Up @@ -327,7 +336,7 @@ def delete_service(self, service_name: str, *, check_changes: bool = True) -> Tu
if k in service:
service.pop(k)

ret = self.__gen_conf(new_env, new_services, check_changes=check_changes)
ret = self.gen_conf(new_env, new_services, check_changes=check_changes)
if isinstance(ret, str):
return ret, 1
return f"Configuration for {service_name} has been deleted.", 0
Loading

0 comments on commit 27152f0

Please sign in to comment.