feat: Add configuration options for all services

This commit is contained in:
Inex Code 2024-07-23 17:29:26 +03:00
parent 98f0ec10fc
commit 8c52384f16
6 changed files with 107 additions and 32 deletions

View file

@ -6,11 +6,42 @@ from typing import List
from selfprivacy_api.utils.systemd import get_service_status from selfprivacy_api.utils.systemd import get_service_status
from selfprivacy_api.services.service import Service, ServiceStatus from selfprivacy_api.services.service import Service, ServiceStatus
from selfprivacy_api.services.bitwarden.icon import BITWARDEN_ICON from selfprivacy_api.services.bitwarden.icon import BITWARDEN_ICON
from selfprivacy_api.services.config_item import (
StringServiceConfigItem,
BoolServiceConfigItem,
ServiceConfigItem,
)
from selfprivacy_api.utils.regex_strings import SUBDOMAIN_REGEX
class Bitwarden(Service): class Bitwarden(Service):
"""Class representing Bitwarden service.""" """Class representing Bitwarden service."""
config_items: dict[str, ServiceConfigItem] = {
"subdomain": StringServiceConfigItem(
id="subdomain",
default_value="password",
description="Subdomain",
regex=SUBDOMAIN_REGEX,
widget="subdomain",
),
"signupsAllowed": BoolServiceConfigItem(
id="signupsAllowed",
default_value=True,
description="Allow new user signups",
),
"sendsAllowed": BoolServiceConfigItem(
id="sendsAllowed",
default_value=True,
description="Allow users to use Bitwarden Send",
),
"emergencyAccessAllowed": BoolServiceConfigItem(
id="emergencyAccessAllowed",
default_value=True,
description="Allow users to enable Emergency Access",
),
}
@staticmethod @staticmethod
def get_id() -> str: def get_id() -> str:
"""Return service id.""" """Return service id."""

View file

@ -127,33 +127,6 @@ class Forgejo(Service):
def restart(): def restart():
subprocess.run(["systemctl", "restart", "forgejo.service"]) subprocess.run(["systemctl", "restart", "forgejo.service"])
@classmethod
def get_configuration(cls):
with ReadUserData() as user_data:
return {
key: cls.config_items[key].as_dict(
user_data.get("modules", {}).get(cls.get_id(), {})
)
for key in cls.config_items
}
@classmethod
def set_configuration(cls, config_items):
# First, validate the configuration
for key, value in config_items.items():
if cls.config_items[key].validate_value(value) is False:
raise ValueError(f"Value {value} is not valid for {key}")
with WriteUserData() as user_data:
if "modules" not in user_data:
user_data["modules"] = {}
if cls.get_id() not in user_data["modules"]:
user_data["modules"][cls.get_id()] = {}
for key, value in config_items.items():
cls.config_items[key].set_value(
value,
user_data["modules"][cls.get_id()],
)
@staticmethod @staticmethod
def get_logs(): def get_logs():
return "" return ""

View file

@ -10,11 +10,31 @@ from selfprivacy_api.utils.systemd import (
from selfprivacy_api.services.service import Service, ServiceStatus from selfprivacy_api.services.service import Service, ServiceStatus
from selfprivacy_api.utils.block_devices import BlockDevice from selfprivacy_api.utils.block_devices import BlockDevice
from selfprivacy_api.services.jitsimeet.icon import JITSI_ICON from selfprivacy_api.services.jitsimeet.icon import JITSI_ICON
from selfprivacy_api.services.config_item import (
StringServiceConfigItem,
ServiceConfigItem,
)
from selfprivacy_api.utils.regex_strings import SUBDOMAIN_REGEX
class JitsiMeet(Service): class JitsiMeet(Service):
"""Class representing Jitsi service""" """Class representing Jitsi service"""
config_items: dict[str, ServiceConfigItem] = {
"subdomain": StringServiceConfigItem(
id="subdomain",
default_value="meet",
description="Subdomain",
regex=SUBDOMAIN_REGEX,
widget="subdomain",
),
"appName": StringServiceConfigItem(
id="appName",
default_value="Jitsi Meet",
description="The name displayed in the web interface",
),
}
@staticmethod @staticmethod
def get_id() -> str: def get_id() -> str:
"""Return service id.""" """Return service id."""

View file

@ -7,11 +7,26 @@ from selfprivacy_api.utils.systemd import get_service_status
from selfprivacy_api.services.service import Service, ServiceStatus from selfprivacy_api.services.service import Service, ServiceStatus
from selfprivacy_api.services.nextcloud.icon import NEXTCLOUD_ICON from selfprivacy_api.services.nextcloud.icon import NEXTCLOUD_ICON
from selfprivacy_api.services.config_item import (
StringServiceConfigItem,
ServiceConfigItem,
)
from selfprivacy_api.utils.regex_strings import SUBDOMAIN_REGEX
class Nextcloud(Service): class Nextcloud(Service):
"""Class representing Nextcloud service.""" """Class representing Nextcloud service."""
config_items: dict[str, ServiceConfigItem] = {
"subdomain": StringServiceConfigItem(
id="subdomain",
default_value="cloud",
description="Subdomain",
regex=SUBDOMAIN_REGEX,
widget="subdomain",
),
}
@staticmethod @staticmethod
def get_id() -> str: def get_id() -> str:
"""Return service id.""" """Return service id."""

View file

@ -11,11 +11,26 @@ from selfprivacy_api.utils.systemd import (
from selfprivacy_api.services.service import Service, ServiceStatus from selfprivacy_api.services.service import Service, ServiceStatus
from selfprivacy_api.utils.block_devices import BlockDevice from selfprivacy_api.utils.block_devices import BlockDevice
from selfprivacy_api.services.roundcube.icon import ROUNDCUBE_ICON from selfprivacy_api.services.roundcube.icon import ROUNDCUBE_ICON
from selfprivacy_api.services.config_item import (
StringServiceConfigItem,
ServiceConfigItem,
)
from selfprivacy_api.utils.regex_strings import SUBDOMAIN_REGEX
class Roundcube(Service): class Roundcube(Service):
"""Class representing roundcube service""" """Class representing roundcube service"""
config_items: dict[str, ServiceConfigItem] = {
"subdomain": StringServiceConfigItem(
id="subdomain",
default_value="cloud",
description="Subdomain",
regex=SUBDOMAIN_REGEX,
widget="subdomain",
),
}
@staticmethod @staticmethod
def get_id() -> str: def get_id() -> str:
"""Return service id.""" """Return service id."""

View file

@ -3,6 +3,7 @@ from abc import ABC, abstractmethod
from typing import List, Optional from typing import List, Optional
from selfprivacy_api import utils from selfprivacy_api import utils
from selfprivacy_api.services.config_item import ServiceConfigItem
from selfprivacy_api.utils.default_subdomains import DEFAULT_SUBDOMAINS from selfprivacy_api.utils.default_subdomains import DEFAULT_SUBDOMAINS
from selfprivacy_api.utils import ReadUserData, WriteUserData, get_domain from selfprivacy_api.utils import ReadUserData, WriteUserData, get_domain
from selfprivacy_api.utils.waitloop import wait_until_true from selfprivacy_api.utils.waitloop import wait_until_true
@ -34,6 +35,8 @@ class Service(ABC):
can be installed, configured and used by a user. can be installed, configured and used by a user.
""" """
config_items: dict[str, "ServiceConfigItem"] = {}
@staticmethod @staticmethod
@abstractmethod @abstractmethod
def get_id() -> str: def get_id() -> str:
@ -188,14 +191,32 @@ class Service(ABC):
pass pass
@classmethod @classmethod
@abstractmethod def get_configuration(cls):
def get_configuration(cls) -> dict: with ReadUserData() as user_data:
pass return {
key: cls.config_items[key].as_dict(
user_data.get("modules", {}).get(cls.get_id(), {})
)
for key in cls.config_items
}
@classmethod @classmethod
@abstractmethod
def set_configuration(cls, config_items): def set_configuration(cls, config_items):
pass for key, value in config_items.items():
if key not in cls.config_items:
raise ValueError(f"Key {key} is not valid for {cls.get_id()}")
if cls.config_items[key].validate_value(value) is False:
raise ValueError(f"Value {value} is not valid for {key}")
with WriteUserData() as user_data:
if "modules" not in user_data:
user_data["modules"] = {}
if cls.get_id() not in user_data["modules"]:
user_data["modules"][cls.get_id()] = {}
for key, value in config_items.items():
cls.config_items[key].set_value(
value,
user_data["modules"][cls.get_id()],
)
@staticmethod @staticmethod
@abstractmethod @abstractmethod