2022-07-12 13:24:29 +00:00
|
|
|
"""System management mutations"""
|
|
|
|
# pylint: disable=too-few-public-methods
|
|
|
|
import typing
|
|
|
|
import strawberry
|
|
|
|
from selfprivacy_api.graphql import IsAuthenticated
|
|
|
|
from selfprivacy_api.graphql.mutations.mutation_interface import (
|
2022-08-01 19:32:20 +00:00
|
|
|
GenericMutationReturn,
|
2022-07-12 13:24:29 +00:00
|
|
|
MutationReturnInterface,
|
|
|
|
)
|
2022-08-25 17:03:56 +00:00
|
|
|
|
|
|
|
import selfprivacy_api.actions.system as system_actions
|
2023-12-15 09:46:59 +00:00
|
|
|
import selfprivacy_api.actions.ssh as ssh_actions
|
2022-07-12 13:24:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
@strawberry.type
|
|
|
|
class TimezoneMutationReturn(MutationReturnInterface):
|
|
|
|
"""Return type of the timezone mutation, contains timezone"""
|
|
|
|
|
|
|
|
timezone: typing.Optional[str]
|
|
|
|
|
|
|
|
|
|
|
|
@strawberry.type
|
|
|
|
class AutoUpgradeSettingsMutationReturn(MutationReturnInterface):
|
|
|
|
"""Return type autoUpgrade Settings"""
|
|
|
|
|
|
|
|
enableAutoUpgrade: bool
|
|
|
|
allowReboot: bool
|
|
|
|
|
|
|
|
|
2023-12-15 09:46:59 +00:00
|
|
|
@strawberry.type
|
|
|
|
class SSHSettingsMutationReturn(MutationReturnInterface):
|
|
|
|
"""A return type for after changing SSH settings"""
|
|
|
|
|
|
|
|
enable: bool
|
|
|
|
password_authentication: bool
|
|
|
|
|
|
|
|
|
|
|
|
@strawberry.input
|
|
|
|
class SSHSettingsInput:
|
|
|
|
"""Input type for SSH settings"""
|
|
|
|
|
|
|
|
enable: bool
|
|
|
|
password_authentication: bool
|
|
|
|
|
|
|
|
|
2022-07-12 13:24:29 +00:00
|
|
|
@strawberry.input
|
|
|
|
class AutoUpgradeSettingsInput:
|
|
|
|
"""Input type for auto upgrade settings"""
|
|
|
|
|
|
|
|
enableAutoUpgrade: typing.Optional[bool] = None
|
|
|
|
allowReboot: typing.Optional[bool] = None
|
|
|
|
|
|
|
|
|
|
|
|
@strawberry.type
|
|
|
|
class SystemMutations:
|
|
|
|
"""Mutations related to system settings"""
|
|
|
|
|
|
|
|
@strawberry.mutation(permission_classes=[IsAuthenticated])
|
|
|
|
def change_timezone(self, timezone: str) -> TimezoneMutationReturn:
|
|
|
|
"""Change the timezone of the server. Timezone is a tzdatabase name."""
|
2022-08-25 17:03:56 +00:00
|
|
|
try:
|
|
|
|
system_actions.change_timezone(timezone)
|
|
|
|
except system_actions.InvalidTimezone as e:
|
2022-07-12 13:24:29 +00:00
|
|
|
return TimezoneMutationReturn(
|
|
|
|
success=False,
|
2022-08-25 17:03:56 +00:00
|
|
|
message=str(e),
|
2022-07-12 13:24:29 +00:00
|
|
|
code=400,
|
|
|
|
timezone=None,
|
|
|
|
)
|
|
|
|
return TimezoneMutationReturn(
|
|
|
|
success=True,
|
|
|
|
message="Timezone changed",
|
|
|
|
code=200,
|
|
|
|
timezone=timezone,
|
|
|
|
)
|
|
|
|
|
|
|
|
@strawberry.mutation(permission_classes=[IsAuthenticated])
|
|
|
|
def change_auto_upgrade_settings(
|
|
|
|
self, settings: AutoUpgradeSettingsInput
|
|
|
|
) -> AutoUpgradeSettingsMutationReturn:
|
|
|
|
"""Change auto upgrade settings of the server."""
|
2022-08-25 17:03:56 +00:00
|
|
|
system_actions.set_auto_upgrade_settings(
|
|
|
|
settings.enableAutoUpgrade, settings.allowReboot
|
|
|
|
)
|
|
|
|
|
|
|
|
new_settings = system_actions.get_auto_upgrade_settings()
|
2022-07-12 13:24:29 +00:00
|
|
|
|
|
|
|
return AutoUpgradeSettingsMutationReturn(
|
|
|
|
success=True,
|
|
|
|
message="Auto-upgrade settings changed",
|
|
|
|
code=200,
|
2022-08-25 17:03:56 +00:00
|
|
|
enableAutoUpgrade=new_settings.enable,
|
|
|
|
allowReboot=new_settings.allowReboot,
|
2022-07-12 13:24:29 +00:00
|
|
|
)
|
2022-08-01 19:32:20 +00:00
|
|
|
|
2023-12-15 09:46:59 +00:00
|
|
|
@strawberry.mutation(permission_classes=[IsAuthenticated])
|
|
|
|
def change_ssh_settings(
|
|
|
|
self, settings: SSHSettingsInput
|
|
|
|
) -> SSHSettingsMutationReturn:
|
|
|
|
"""Change ssh settings of the server."""
|
|
|
|
ssh_actions.set_ssh_settings(
|
|
|
|
enable=settings.enable,
|
|
|
|
password_authentication=settings.password_authentication,
|
|
|
|
)
|
|
|
|
|
|
|
|
new_settings = ssh_actions.get_ssh_settings()
|
|
|
|
|
|
|
|
return SSHSettingsMutationReturn(
|
|
|
|
success=True,
|
|
|
|
message="SSH settings changed",
|
|
|
|
code=200,
|
|
|
|
enable=new_settings.enable,
|
|
|
|
password_authentication=new_settings.passwordAuthentication,
|
|
|
|
)
|
|
|
|
|
2022-08-01 19:32:20 +00:00
|
|
|
@strawberry.mutation(permission_classes=[IsAuthenticated])
|
|
|
|
def run_system_rebuild(self) -> GenericMutationReturn:
|
2023-12-29 13:11:03 +00:00
|
|
|
try:
|
|
|
|
system_actions.rebuild_system()
|
|
|
|
return GenericMutationReturn(
|
|
|
|
success=True,
|
|
|
|
message="Starting rebuild system",
|
|
|
|
code=200,
|
|
|
|
)
|
|
|
|
except system_actions.ShellException as e:
|
|
|
|
return GenericMutationReturn(
|
|
|
|
success=False,
|
|
|
|
message=str(e),
|
|
|
|
code=500,
|
|
|
|
)
|
2022-08-01 19:32:20 +00:00
|
|
|
|
|
|
|
@strawberry.mutation(permission_classes=[IsAuthenticated])
|
|
|
|
def run_system_rollback(self) -> GenericMutationReturn:
|
2022-08-25 17:03:56 +00:00
|
|
|
system_actions.rollback_system()
|
2023-12-29 13:11:03 +00:00
|
|
|
try:
|
|
|
|
return GenericMutationReturn(
|
|
|
|
success=True,
|
|
|
|
message="Starting rebuild system",
|
|
|
|
code=200,
|
|
|
|
)
|
|
|
|
except system_actions.ShellException as e:
|
|
|
|
return GenericMutationReturn(
|
|
|
|
success=False,
|
|
|
|
message=str(e),
|
|
|
|
code=500,
|
|
|
|
)
|
2022-08-01 19:32:20 +00:00
|
|
|
|
|
|
|
@strawberry.mutation(permission_classes=[IsAuthenticated])
|
|
|
|
def run_system_upgrade(self) -> GenericMutationReturn:
|
2022-08-25 17:03:56 +00:00
|
|
|
system_actions.upgrade_system()
|
2023-12-29 13:11:03 +00:00
|
|
|
try:
|
|
|
|
return GenericMutationReturn(
|
|
|
|
success=True,
|
|
|
|
message="Starting rebuild system",
|
|
|
|
code=200,
|
|
|
|
)
|
|
|
|
except system_actions.ShellException as e:
|
|
|
|
return GenericMutationReturn(
|
|
|
|
success=False,
|
|
|
|
message=str(e),
|
|
|
|
code=500,
|
|
|
|
)
|
2022-08-01 19:32:20 +00:00
|
|
|
|
|
|
|
@strawberry.mutation(permission_classes=[IsAuthenticated])
|
|
|
|
def reboot_system(self) -> GenericMutationReturn:
|
2022-08-25 17:03:56 +00:00
|
|
|
system_actions.reboot_system()
|
2023-12-29 13:11:03 +00:00
|
|
|
try:
|
|
|
|
return GenericMutationReturn(
|
|
|
|
success=True,
|
|
|
|
message="System reboot has started",
|
|
|
|
code=200,
|
|
|
|
)
|
|
|
|
except system_actions.ShellException as e:
|
|
|
|
return GenericMutationReturn(
|
|
|
|
success=False,
|
|
|
|
message=str(e),
|
|
|
|
code=500,
|
|
|
|
)
|
2022-08-25 17:03:56 +00:00
|
|
|
|
|
|
|
@strawberry.mutation(permission_classes=[IsAuthenticated])
|
|
|
|
def pull_repository_changes(self) -> GenericMutationReturn:
|
|
|
|
result = system_actions.pull_repository_changes()
|
|
|
|
if result.status == 0:
|
|
|
|
return GenericMutationReturn(
|
|
|
|
success=True,
|
|
|
|
message="Repository changes pulled",
|
|
|
|
code=200,
|
|
|
|
)
|
|
|
|
return GenericMutationReturn(
|
|
|
|
success=False,
|
|
|
|
message=f"Failed to pull repository changes:\n{result.data}",
|
|
|
|
code=500,
|
|
|
|
)
|