mirror of
https://git.selfprivacy.org/SelfPrivacy/selfprivacy-rest-api.git
synced 2024-11-22 04:01:27 +00:00
feat: add flake services manager (#113)
Reviewed-on: https://git.selfprivacy.org/SelfPrivacy/selfprivacy-rest-api/pulls/113 Reviewed-by: Inex Code <inex.code@selfprivacy.org> Reviewed-by: houkime <houkime@protonmail.com> Co-authored-by: dettlaff <dettlaff@riseup.net> Co-committed-by: dettlaff <dettlaff@riseup.net>
This commit is contained in:
parent
a742e66cc3
commit
f90eb3fb4c
53
selfprivacy_api/services/flake_service_manager.py
Normal file
53
selfprivacy_api/services/flake_service_manager.py
Normal file
|
@ -0,0 +1,53 @@
|
|||
import re
|
||||
from typing import Tuple, Optional
|
||||
|
||||
FLAKE_CONFIG_PATH = "/etc/nixos/sp-modules/flake.nix"
|
||||
|
||||
|
||||
class FlakeServiceManager:
|
||||
def __enter__(self) -> "FlakeServiceManager":
|
||||
self.services = {}
|
||||
|
||||
with open(FLAKE_CONFIG_PATH, "r") as file:
|
||||
for line in file:
|
||||
service_name, url = self._extract_services(input_string=line)
|
||||
if service_name and url:
|
||||
self.services[service_name] = url
|
||||
|
||||
return self
|
||||
|
||||
def _extract_services(
|
||||
self, input_string: str
|
||||
) -> Tuple[Optional[str], Optional[str]]:
|
||||
pattern = r"inputs\.([\w-]+)\.url\s*=\s*([\S]+);"
|
||||
match = re.search(pattern, input_string)
|
||||
|
||||
if match:
|
||||
variable_name = match.group(1)
|
||||
url = match.group(2)
|
||||
return variable_name, url
|
||||
else:
|
||||
return None, None
|
||||
|
||||
def __exit__(self, exc_type, exc_value, traceback) -> None:
|
||||
with open(FLAKE_CONFIG_PATH, "w") as file:
|
||||
file.write(
|
||||
"""
|
||||
{
|
||||
description = "SelfPrivacy NixOS PoC modules/extensions/bundles/packages/etc";\n
|
||||
"""
|
||||
)
|
||||
|
||||
for key, value in self.services.items():
|
||||
file.write(
|
||||
f"""
|
||||
inputs.{key}.url = {value};
|
||||
"""
|
||||
)
|
||||
|
||||
file.write(
|
||||
"""
|
||||
outputs = _: { };
|
||||
}
|
||||
"""
|
||||
)
|
127
tests/test_flake_services_manager.py
Normal file
127
tests/test_flake_services_manager.py
Normal file
|
@ -0,0 +1,127 @@
|
|||
import pytest
|
||||
|
||||
from selfprivacy_api.services.flake_service_manager import FlakeServiceManager
|
||||
|
||||
all_services_file = """
|
||||
{
|
||||
description = "SelfPrivacy NixOS PoC modules/extensions/bundles/packages/etc";
|
||||
|
||||
|
||||
inputs.bitwarden.url = git+https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-config.git?ref=flakes&dir=sp-modules/bitwarden;
|
||||
|
||||
inputs.gitea.url = git+https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-config.git?ref=flakes&dir=sp-modules/gitea;
|
||||
|
||||
inputs.jitsi-meet.url = git+https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-config.git?ref=flakes&dir=sp-modules/jitsi-meet;
|
||||
|
||||
inputs.nextcloud.url = git+https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-config.git?ref=flakes&dir=sp-modules/nextcloud;
|
||||
|
||||
inputs.ocserv.url = git+https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-config.git?ref=flakes&dir=sp-modules/ocserv;
|
||||
|
||||
inputs.pleroma.url = git+https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-config.git?ref=flakes&dir=sp-modules/pleroma;
|
||||
|
||||
inputs.simple-nixos-mailserver.url = git+https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-config.git?ref=flakes&dir=sp-modules/simple-nixos-mailserver;
|
||||
|
||||
outputs = _: { };
|
||||
}
|
||||
"""
|
||||
|
||||
|
||||
some_services_file = """
|
||||
{
|
||||
description = "SelfPrivacy NixOS PoC modules/extensions/bundles/packages/etc";
|
||||
|
||||
|
||||
inputs.bitwarden.url = git+https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-config.git?ref=flakes&dir=sp-modules/bitwarden;
|
||||
|
||||
inputs.gitea.url = git+https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-config.git?ref=flakes&dir=sp-modules/gitea;
|
||||
|
||||
inputs.jitsi-meet.url = git+https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-config.git?ref=flakes&dir=sp-modules/jitsi-meet;
|
||||
|
||||
outputs = _: { };
|
||||
}
|
||||
"""
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def some_services_flake_mock(mocker, datadir):
|
||||
flake_config_path = datadir / "some_services.nix"
|
||||
mocker.patch(
|
||||
"selfprivacy_api.services.flake_service_manager.FLAKE_CONFIG_PATH",
|
||||
new=flake_config_path,
|
||||
)
|
||||
return flake_config_path
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def no_services_flake_mock(mocker, datadir):
|
||||
flake_config_path = datadir / "no_services.nix"
|
||||
mocker.patch(
|
||||
"selfprivacy_api.services.flake_service_manager.FLAKE_CONFIG_PATH",
|
||||
new=flake_config_path,
|
||||
)
|
||||
return flake_config_path
|
||||
|
||||
|
||||
# ---
|
||||
|
||||
|
||||
def test_read_services_list(some_services_flake_mock):
|
||||
with FlakeServiceManager() as manager:
|
||||
services = {
|
||||
"bitwarden": "git+https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-config.git?ref=flakes&dir=sp-modules/bitwarden",
|
||||
"gitea": "git+https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-config.git?ref=flakes&dir=sp-modules/gitea",
|
||||
"jitsi-meet": "git+https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-config.git?ref=flakes&dir=sp-modules/jitsi-meet",
|
||||
}
|
||||
assert manager.services == services
|
||||
|
||||
|
||||
def test_change_services_list(some_services_flake_mock):
|
||||
services = {
|
||||
"bitwarden": "git+https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-config.git?ref=flakes&dir=sp-modules/bitwarden",
|
||||
"gitea": "git+https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-config.git?ref=flakes&dir=sp-modules/gitea",
|
||||
"jitsi-meet": "git+https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-config.git?ref=flakes&dir=sp-modules/jitsi-meet",
|
||||
"nextcloud": "git+https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-config.git?ref=flakes&dir=sp-modules/nextcloud",
|
||||
"ocserv": "git+https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-config.git?ref=flakes&dir=sp-modules/ocserv",
|
||||
"pleroma": "git+https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-config.git?ref=flakes&dir=sp-modules/pleroma",
|
||||
"simple-nixos-mailserver": "git+https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-config.git?ref=flakes&dir=sp-modules/simple-nixos-mailserver",
|
||||
}
|
||||
|
||||
with FlakeServiceManager() as manager:
|
||||
manager.services = services
|
||||
|
||||
with FlakeServiceManager() as manager:
|
||||
assert manager.services == services
|
||||
|
||||
with open(some_services_flake_mock, "r", encoding="utf-8") as file:
|
||||
file_content = file.read().strip()
|
||||
|
||||
assert all_services_file.strip() == file_content
|
||||
|
||||
|
||||
def test_read_empty_services_list(no_services_flake_mock):
|
||||
with FlakeServiceManager() as manager:
|
||||
services = {}
|
||||
assert manager.services == services
|
||||
|
||||
|
||||
def test_change_empty_services_list(no_services_flake_mock):
|
||||
services = {
|
||||
"bitwarden": "git+https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-config.git?ref=flakes&dir=sp-modules/bitwarden",
|
||||
"gitea": "git+https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-config.git?ref=flakes&dir=sp-modules/gitea",
|
||||
"jitsi-meet": "git+https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-config.git?ref=flakes&dir=sp-modules/jitsi-meet",
|
||||
"nextcloud": "git+https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-config.git?ref=flakes&dir=sp-modules/nextcloud",
|
||||
"ocserv": "git+https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-config.git?ref=flakes&dir=sp-modules/ocserv",
|
||||
"pleroma": "git+https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-config.git?ref=flakes&dir=sp-modules/pleroma",
|
||||
"simple-nixos-mailserver": "git+https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-config.git?ref=flakes&dir=sp-modules/simple-nixos-mailserver",
|
||||
}
|
||||
|
||||
with FlakeServiceManager() as manager:
|
||||
manager.services = services
|
||||
|
||||
with FlakeServiceManager() as manager:
|
||||
assert manager.services == services
|
||||
|
||||
with open(no_services_flake_mock, "r", encoding="utf-8") as file:
|
||||
file_content = file.read().strip()
|
||||
|
||||
assert all_services_file.strip() == file_content
|
4
tests/test_flake_services_manager/no_services.nix
Normal file
4
tests/test_flake_services_manager/no_services.nix
Normal file
|
@ -0,0 +1,4 @@
|
|||
{
|
||||
description = "SelfPrivacy NixOS PoC modules/extensions/bundles/packages/etc";
|
||||
outputs = _: { };
|
||||
}
|
12
tests/test_flake_services_manager/some_services.nix
Normal file
12
tests/test_flake_services_manager/some_services.nix
Normal file
|
@ -0,0 +1,12 @@
|
|||
{
|
||||
description = "SelfPrivacy NixOS PoC modules/extensions/bundles/packages/etc";
|
||||
|
||||
|
||||
inputs.bitwarden.url = git+https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-config.git?ref=flakes&dir=sp-modules/bitwarden;
|
||||
|
||||
inputs.gitea.url = git+https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-config.git?ref=flakes&dir=sp-modules/gitea;
|
||||
|
||||
inputs.jitsi-meet.url = git+https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-config.git?ref=flakes&dir=sp-modules/jitsi-meet;
|
||||
|
||||
outputs = _: { };
|
||||
}
|
Loading…
Reference in a new issue