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

166 lines
4.6 KiB
Python
Raw Normal View History

2022-06-24 13:05:18 +00:00
"""Common system information and settings"""
# pylint: disable=too-few-public-methods
2022-06-24 18:14:20 +00:00
import subprocess
2022-06-24 12:26:51 +00:00
import typing
import strawberry
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
2022-06-24 18:14:20 +00:00
from selfprivacy_api.utils import ReadUserData
2022-06-24 12:26:51 +00:00
@strawberry.type
class DnsRecord:
2022-06-24 13:05:18 +00:00
"""DNS record"""
2022-06-24 18:14:20 +00:00
2022-06-24 12:26:51 +00:00
recordType: str
name: str
content: str
ttl: int
priority: typing.Optional[int]
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
required_dns_records: typing.List[DnsRecord]
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"],
provider=DnsProvider.CLOUDFLARE,
# TODO: get ip somehow
required_dns_records=[],
)
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"""
with ReadUserData() as user_data:
if "autoUpgrade" not in user_data:
return AutoUpgradeOptions(enable=True, allow_reboot=False)
if "enable" not in user_data["autoUpgrade"]:
user_data["autoUpgrade"]["enable"] = True
if "allowReboot" not in user_data["autoUpgrade"]:
user_data["autoUpgrade"]["allowReboot"] = False
return AutoUpgradeOptions(
enable=user_data["autoUpgrade"]["enable"],
allow_reboot=user_data["autoUpgrade"]["allowReboot"],
)
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"""
with ReadUserData() as user_data:
if "ssh" not in user_data:
return SshSettings(
enable=False, password_authentication=False, root_ssh_keys=[]
)
if "enable" not in user_data["ssh"]:
user_data["ssh"]["enable"] = False
if "passwordAuthentication" not in user_data["ssh"]:
user_data["ssh"]["passwordAuthentication"] = False
if "rootKeys" not in user_data["ssh"]:
user_data["ssh"]["rootKeys"] = []
return SshSettings(
enable=user_data["ssh"]["enable"],
password_authentication=user_data["ssh"]["passwordAuthentication"],
root_ssh_keys=user_data["ssh"]["rootKeys"],
)
def get_system_timezone() -> str:
"""Get system timezone"""
with ReadUserData() as user_data:
if "timezone" not in user_data:
return "Europe/Uzhgorod"
return user_data["timezone"]
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 subprocess.check_output(["uname", "-a"]).decode("utf-8").strip()
def get_python_version() -> str:
"""Get Python version"""
return subprocess.check_output(["python", "-V"]).decode("utf-8").strip()
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)
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"""
return SystemProviderInfo(provider=ServerProvider.HETZNER, id="UNKNOWN")
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)
2022-06-24 18:18:21 +00:00
settings: SystemSettings = SystemSettings()
info: SystemInfo = SystemInfo()
2022-06-24 18:14:20 +00:00
provider: SystemProviderInfo = strawberry.field(resolver=get_system_provider_info)
busy: bool = False