selfprivacy-rest-api/selfprivacy_api/graphql/queries/system.py

174 lines
4.7 KiB
Python
Raw Normal View History

2022-06-24 13:05:18 +00:00
"""Common system information and settings"""
2024-06-10 01:52:26 +00:00
2022-06-24 13:05:18 +00:00
# pylint: disable=too-few-public-methods
import os
2022-06-24 12:26:51 +00:00
import typing
import strawberry
2024-06-10 01:52:26 +00:00
from selfprivacy_api.graphql.common_types.dns import DnsRecord
2022-06-24 12:26:51 +00:00
2022-06-24 18:18:21 +00:00
from selfprivacy_api.graphql.queries.common import Alert, Severity
2022-06-24 12:26:51 +00:00
from selfprivacy_api.graphql.queries.providers import DnsProvider, ServerProvider
from selfprivacy_api.jobs import Jobs
from selfprivacy_api.jobs.migrate_to_binds import is_bind_migrated
from selfprivacy_api.services import get_all_required_dns_records
2022-06-24 18:14:20 +00:00
from selfprivacy_api.utils import ReadUserData
import selfprivacy_api.actions.system as system_actions
import selfprivacy_api.actions.ssh as ssh_actions
2022-06-24 12:26:51 +00:00
2022-06-24 18:14:20 +00:00
2022-06-24 12:26:51 +00:00
@strawberry.type
class SystemDomainInfo:
2022-06-24 13:05:18 +00:00
"""Information about the system domain"""
2022-06-24 18:14:20 +00:00
2022-06-24 12:26:51 +00:00
domain: str
hostname: str
provider: DnsProvider
@strawberry.field
def required_dns_records(self) -> typing.List[DnsRecord]:
"""Collect all required DNS records for all services"""
return [
DnsRecord(
record_type=record.type,
name=record.name,
content=record.content,
ttl=record.ttl,
priority=record.priority,
display_name=record.display_name,
)
for record in get_all_required_dns_records()
]
2022-06-24 12:26:51 +00:00
2022-06-24 18:14:20 +00:00
def get_system_domain_info() -> SystemDomainInfo:
"""Get basic system domain info"""
with ReadUserData() as user_data:
return SystemDomainInfo(
domain=user_data["domain"],
hostname=user_data["hostname"],
2022-10-28 12:34:58 +00:00
provider=user_data["dns"]["provider"],
2022-06-24 18:14:20 +00:00
)
2022-06-24 12:26:51 +00:00
@strawberry.type
class AutoUpgradeOptions:
2022-06-24 13:05:18 +00:00
"""Automatic upgrade options"""
2022-06-24 18:14:20 +00:00
2022-06-24 12:26:51 +00:00
enable: bool
allow_reboot: bool
2022-06-24 18:14:20 +00:00
def get_auto_upgrade_options() -> AutoUpgradeOptions:
"""Get automatic upgrade options"""
settings = system_actions.get_auto_upgrade_settings()
return AutoUpgradeOptions(
enable=settings.enable,
allow_reboot=settings.allowReboot,
)
2022-06-24 18:14:20 +00:00
2022-06-24 12:26:51 +00:00
@strawberry.type
class SshSettings:
2022-06-24 13:05:18 +00:00
"""SSH settings and root SSH keys"""
2022-06-24 18:14:20 +00:00
2022-06-24 12:26:51 +00:00
enable: bool
password_authentication: bool
root_ssh_keys: typing.List[str]
2022-06-24 18:14:20 +00:00
def get_ssh_settings() -> SshSettings:
"""Get SSH settings"""
settings = ssh_actions.get_ssh_settings()
return SshSettings(
enable=settings.enable,
password_authentication=settings.passwordAuthentication,
root_ssh_keys=settings.rootKeys,
)
2022-06-24 18:14:20 +00:00
def get_system_timezone() -> str:
"""Get system timezone"""
return system_actions.get_timezone()
2022-06-24 18:14:20 +00:00
2022-06-24 12:26:51 +00:00
@strawberry.type
class SystemSettings:
2022-06-24 13:05:18 +00:00
"""Common system settings"""
2022-06-24 18:14:20 +00:00
auto_upgrade: AutoUpgradeOptions = strawberry.field(
resolver=get_auto_upgrade_options
)
ssh: SshSettings = strawberry.field(resolver=get_ssh_settings)
timezone: str = strawberry.field(resolver=get_system_timezone)
def get_system_version() -> str:
"""Get system version"""
return system_actions.get_system_version()
2022-06-24 18:14:20 +00:00
def get_python_version() -> str:
"""Get Python version"""
return system_actions.get_python_version()
2022-06-24 18:14:20 +00:00
2022-06-24 12:26:51 +00:00
@strawberry.type
class SystemInfo:
2022-06-24 13:05:18 +00:00
"""System components versions"""
2022-06-24 18:14:20 +00:00
system_version: str = strawberry.field(resolver=get_system_version)
python_version: str = strawberry.field(resolver=get_python_version)
@strawberry.field
def using_binds(self) -> bool:
"""Check if the system is using BINDs"""
return is_bind_migrated()
2022-06-24 12:26:51 +00:00
@strawberry.type
class SystemProviderInfo:
2022-06-24 13:05:18 +00:00
"""Information about the VPS/Dedicated server provider"""
2022-06-24 18:14:20 +00:00
2022-06-24 12:26:51 +00:00
provider: ServerProvider
id: str
2022-06-24 18:14:20 +00:00
def get_system_provider_info() -> SystemProviderInfo:
"""Get system provider info"""
2022-10-31 10:52:38 +00:00
with ReadUserData() as user_data:
return SystemProviderInfo(
provider=user_data["server"]["provider"],
2022-11-28 12:49:48 +00:00
id="UNKNOWN",
2022-10-31 10:52:38 +00:00
)
2022-06-24 18:14:20 +00:00
2022-06-24 12:26:51 +00:00
@strawberry.type
class System:
"""
Base system type which represents common system status
"""
2022-06-24 18:14:20 +00:00
2022-07-07 13:53:19 +00:00
status: Alert = strawberry.field(
resolver=lambda: Alert(
severity=Severity.INFO,
title="Test message",
message="Test message",
timestamp=None,
)
)
2022-07-08 15:28:08 +00:00
domain_info: SystemDomainInfo = strawberry.field(resolver=get_system_domain_info)
2024-07-26 19:59:32 +00:00
settings: SystemSettings = strawberry.field(default_factory=SystemSettings)
info: SystemInfo = strawberry.field(default_factory=SystemInfo)
2022-06-24 18:14:20 +00:00
provider: SystemProviderInfo = strawberry.field(resolver=get_system_provider_info)
@strawberry.field
def busy(self) -> bool:
"""Check if the system is busy"""
return Jobs.is_busy()
@strawberry.field
def working_directory(self) -> str:
"""Get working directory"""
return os.getcwd()