From 206589d5ad85903c73ecb4f6f520bc4a50291a10 Mon Sep 17 00:00:00 2001 From: def Date: Mon, 1 Aug 2022 21:32:20 +0200 Subject: [PATCH] add system nixos tasks --- .../graphql/mutations/system_mutations.py | 47 +++++++++++++++++++ ...os_tasks.py => test_system_nixos_tasks.py} | 6 +-- 2 files changed, 50 insertions(+), 3 deletions(-) rename tests/test_graphql/{_test_system_nixos_tasks.py => test_system_nixos_tasks.py} (98%) diff --git a/selfprivacy_api/graphql/mutations/system_mutations.py b/selfprivacy_api/graphql/mutations/system_mutations.py index 517a697..057c26f 100644 --- a/selfprivacy_api/graphql/mutations/system_mutations.py +++ b/selfprivacy_api/graphql/mutations/system_mutations.py @@ -1,10 +1,12 @@ """System management mutations""" # pylint: disable=too-few-public-methods +import subprocess import typing import pytz import strawberry from selfprivacy_api.graphql import IsAuthenticated from selfprivacy_api.graphql.mutations.mutation_interface import ( + GenericMutationReturn, MutationReturnInterface, ) from selfprivacy_api.utils import WriteUserData @@ -84,3 +86,48 @@ class SystemMutations: enableAutoUpgrade=auto_upgrade, allowReboot=allow_reboot, ) + + @strawberry.mutation(permission_classes=[IsAuthenticated]) + def run_system_rebuild(self) -> GenericMutationReturn: + rebuild_result = subprocess.Popen( + ["systemctl", "start", "sp-nixos-rebuild.service"], start_new_session=True + ) + rebuild_result.communicate()[0] + return GenericMutationReturn( + success=True, + message="Starting rebuild system", + code=200, + ) + + @strawberry.mutation(permission_classes=[IsAuthenticated]) + def run_system_rollback(self) -> GenericMutationReturn: + rollback_result = subprocess.Popen( + ["systemctl", "start", "sp-nixos-rollback.service"], start_new_session=True + ) + rollback_result.communicate()[0] + return GenericMutationReturn( + success=True, + message="Starting rebuild system", + code=200, + ) + + @strawberry.mutation(permission_classes=[IsAuthenticated]) + def run_system_upgrade(self) -> GenericMutationReturn: + upgrade_result = subprocess.Popen( + ["systemctl", "start", "sp-nixos-upgrade.service"], start_new_session=True + ) + upgrade_result.communicate()[0] + return GenericMutationReturn( + success=True, + message="Starting rebuild system", + code=200, + ) + + @strawberry.mutation(permission_classes=[IsAuthenticated]) + def reboot_system(self) -> GenericMutationReturn: + subprocess.Popen(["reboot"], start_new_session=True) + return GenericMutationReturn( + success=True, + message="System reboot has started", + code=200, + ) diff --git a/tests/test_graphql/_test_system_nixos_tasks.py b/tests/test_graphql/test_system_nixos_tasks.py similarity index 98% rename from tests/test_graphql/_test_system_nixos_tasks.py rename to tests/test_graphql/test_system_nixos_tasks.py index 3778887..601c353 100644 --- a/tests/test_graphql/_test_system_nixos_tasks.py +++ b/tests/test_graphql/test_system_nixos_tasks.py @@ -53,7 +53,7 @@ def mock_subprocess_check_output(mocker): API_REBUILD_SYSTEM_MUTATION = """ -mutation rebuildSystem() { +mutation rebuildSystem { runSystemRebuild { success message @@ -98,7 +98,7 @@ def test_graphql_system_rebuild(authorized_client, mock_subprocess_popen): API_UPGRADE_SYSTEM_MUTATION = """ -mutation upgradeSystem() { +mutation upgradeSystem { runSystemUpgrade { success message @@ -143,7 +143,7 @@ def test_graphql_system_upgrade(authorized_client, mock_subprocess_popen): API_ROLLBACK_SYSTEM_MUTATION = """ -mutation rollbackSystem() { +mutation rollbackSystem { runSystemRollback { success message