mirror of
https://git.selfprivacy.org/SelfPrivacy/selfprivacy-rest-api.git
synced 2025-01-07 16:41:08 +00:00
Add services to volumes
This commit is contained in:
parent
19b5c06fc6
commit
9abc11f187
108
selfprivacy_api/graphql/common_types/service.py
Normal file
108
selfprivacy_api/graphql/common_types/service.py
Normal file
|
@ -0,0 +1,108 @@
|
||||||
|
from enum import Enum
|
||||||
|
import typing
|
||||||
|
import strawberry
|
||||||
|
from selfprivacy_api.graphql.common_types.dns import DnsRecord
|
||||||
|
|
||||||
|
from selfprivacy_api.graphql.common_types.storage_usage import (
|
||||||
|
ServiceStorageUsage,
|
||||||
|
StorageVolume,
|
||||||
|
)
|
||||||
|
from selfprivacy_api.graphql.queries.services import get_volume_by_id
|
||||||
|
from selfprivacy_api.services import get_service_by_id, get_services_by_location
|
||||||
|
from selfprivacy_api.services import Service as ServiceInterface
|
||||||
|
from selfprivacy_api.utils.block_devices import BlockDevices
|
||||||
|
|
||||||
|
|
||||||
|
@strawberry.enum
|
||||||
|
class ServiceStatusEnum(Enum):
|
||||||
|
RUNNING = "RUNNING"
|
||||||
|
DEGRADED = "DEGRADED"
|
||||||
|
ERROR = "ERROR"
|
||||||
|
STOPPED = "STOPPED"
|
||||||
|
OFF = "OFF"
|
||||||
|
|
||||||
|
|
||||||
|
def get_storage_usage(root: "Service") -> ServiceStorageUsage:
|
||||||
|
"""Get storage usage for a service"""
|
||||||
|
service = get_service_by_id(root.id)
|
||||||
|
if service is None:
|
||||||
|
return ServiceStorageUsage(
|
||||||
|
service=service,
|
||||||
|
title="Not found",
|
||||||
|
used_space="0",
|
||||||
|
volume=get_volume_by_id("sda1"),
|
||||||
|
)
|
||||||
|
return ServiceStorageUsage(
|
||||||
|
service=service_to_graphql_service(service),
|
||||||
|
title=service.get_display_name(),
|
||||||
|
used_space=str(service.get_storage_usage()),
|
||||||
|
volume=get_volume_by_id(service.get_location()),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@strawberry.type
|
||||||
|
class Service:
|
||||||
|
storage_usage: ServiceStorageUsage = strawberry.field(resolver=get_storage_usage)
|
||||||
|
id: str
|
||||||
|
display_name: str
|
||||||
|
description: str
|
||||||
|
svg_icon: str
|
||||||
|
is_movable: bool
|
||||||
|
is_required: bool
|
||||||
|
is_enabled: bool
|
||||||
|
status: ServiceStatusEnum
|
||||||
|
url: typing.Optional[str]
|
||||||
|
dns_records: typing.Optional[typing.List[DnsRecord]]
|
||||||
|
|
||||||
|
|
||||||
|
def service_to_graphql_service(service: ServiceInterface) -> Service:
|
||||||
|
"""Convert service to graphql service"""
|
||||||
|
return Service(
|
||||||
|
id=service.get_id(),
|
||||||
|
display_name=service.get_display_name(),
|
||||||
|
description=service.get_description(),
|
||||||
|
svg_icon=service.get_svg_icon(),
|
||||||
|
is_movable=service.is_movable(),
|
||||||
|
is_required=service.is_required(),
|
||||||
|
is_enabled=service.is_enabled(),
|
||||||
|
status=ServiceStatusEnum(service.get_status().value),
|
||||||
|
url=service.get_url(),
|
||||||
|
dns_records=[
|
||||||
|
DnsRecord(
|
||||||
|
record_type=record.type,
|
||||||
|
name=record.name,
|
||||||
|
content=record.content,
|
||||||
|
ttl=record.ttl,
|
||||||
|
priority=record.priority,
|
||||||
|
)
|
||||||
|
for record in service.get_dns_records()
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def get_volume_by_id(volume_id: str) -> typing.Optional[StorageVolume]:
|
||||||
|
"""Get volume by id"""
|
||||||
|
volume = BlockDevices().get_block_device(volume_id)
|
||||||
|
if volume is None:
|
||||||
|
return None
|
||||||
|
return StorageVolume(
|
||||||
|
total_space=str(volume.fssize)
|
||||||
|
if volume.fssize is not None
|
||||||
|
else str(volume.size),
|
||||||
|
free_space=str(volume.fsavail),
|
||||||
|
used_space=str(volume.fsused),
|
||||||
|
root=volume.name == "sda1",
|
||||||
|
name=volume.name,
|
||||||
|
model=volume.model,
|
||||||
|
serial=volume.serial,
|
||||||
|
type=volume.type,
|
||||||
|
usages=[
|
||||||
|
ServiceStorageUsage(
|
||||||
|
service=service_to_graphql_service(service),
|
||||||
|
title=service.get_display_name(),
|
||||||
|
used_space=str(service.get_storage_usage()),
|
||||||
|
volume=get_volume_by_id(service.get_location()),
|
||||||
|
)
|
||||||
|
for service in get_services_by_location(volume.name)
|
||||||
|
],
|
||||||
|
)
|
|
@ -1,6 +1,8 @@
|
||||||
import typing
|
import typing
|
||||||
import strawberry
|
import strawberry
|
||||||
|
|
||||||
|
from selfprivacy_api.graphql.common_types.service import Service
|
||||||
|
|
||||||
|
|
||||||
@strawberry.type
|
@strawberry.type
|
||||||
class StorageVolume:
|
class StorageVolume:
|
||||||
|
@ -22,3 +24,10 @@ class StorageUsageInterface:
|
||||||
used_space: str
|
used_space: str
|
||||||
volume: typing.Optional[StorageVolume]
|
volume: typing.Optional[StorageVolume]
|
||||||
title: str
|
title: str
|
||||||
|
|
||||||
|
|
||||||
|
@strawberry.type
|
||||||
|
class ServiceStorageUsage(StorageUsageInterface):
|
||||||
|
"""Storage usage for a service"""
|
||||||
|
|
||||||
|
service: typing.Optional["Service"]
|
||||||
|
|
|
@ -1,112 +1,13 @@
|
||||||
"""Services status"""
|
"""Services status"""
|
||||||
# pylint: disable=too-few-public-methods
|
# pylint: disable=too-few-public-methods
|
||||||
from enum import Enum
|
|
||||||
import typing
|
import typing
|
||||||
import strawberry
|
import strawberry
|
||||||
import datetime
|
|
||||||
|
|
||||||
from selfprivacy_api.graphql.common_types.dns import DnsRecord
|
from selfprivacy_api.graphql.common_types.service import (
|
||||||
from selfprivacy_api.graphql.common_types.storage_usage import (
|
Service,
|
||||||
StorageUsageInterface,
|
service_to_graphql_service,
|
||||||
StorageVolume,
|
|
||||||
)
|
)
|
||||||
from selfprivacy_api.services import get_all_services, get_service_by_id
|
from selfprivacy_api.services import get_all_services
|
||||||
from selfprivacy_api.services import Service as ServiceInterface
|
|
||||||
from selfprivacy_api.utils.block_devices import BlockDevices
|
|
||||||
|
|
||||||
|
|
||||||
@strawberry.enum
|
|
||||||
class ServiceStatusEnum(Enum):
|
|
||||||
RUNNING = "RUNNING"
|
|
||||||
DEGRADED = "DEGRADED"
|
|
||||||
ERROR = "ERROR"
|
|
||||||
STOPPED = "STOPPED"
|
|
||||||
OFF = "OFF"
|
|
||||||
|
|
||||||
|
|
||||||
@strawberry.type
|
|
||||||
class ServiceStorageUsage(StorageUsageInterface):
|
|
||||||
"""Storage usage for a service"""
|
|
||||||
|
|
||||||
service: typing.Optional["Service"]
|
|
||||||
|
|
||||||
|
|
||||||
def get_storage_usage(root: "Service") -> ServiceStorageUsage:
|
|
||||||
"""Get storage usage for a service"""
|
|
||||||
service = get_service_by_id(root.id)
|
|
||||||
if service is None:
|
|
||||||
return ServiceStorageUsage(
|
|
||||||
service=service,
|
|
||||||
title="Not found",
|
|
||||||
used_space="0",
|
|
||||||
volume=get_volume_by_id("sda1"),
|
|
||||||
)
|
|
||||||
return ServiceStorageUsage(
|
|
||||||
service=service_to_graphql_service(service),
|
|
||||||
title=service.get_display_name(),
|
|
||||||
used_space=str(service.get_storage_usage()),
|
|
||||||
volume=get_volume_by_id(service.get_location()),
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@strawberry.type
|
|
||||||
class Service:
|
|
||||||
storage_usage: ServiceStorageUsage = strawberry.field(resolver=get_storage_usage)
|
|
||||||
id: str
|
|
||||||
display_name: str
|
|
||||||
description: str
|
|
||||||
svg_icon: str
|
|
||||||
is_movable: bool
|
|
||||||
is_required: bool
|
|
||||||
is_enabled: bool
|
|
||||||
status: ServiceStatusEnum
|
|
||||||
url: typing.Optional[str]
|
|
||||||
dns_records: typing.Optional[typing.List[DnsRecord]]
|
|
||||||
|
|
||||||
|
|
||||||
def service_to_graphql_service(service: ServiceInterface) -> Service:
|
|
||||||
"""Convert service to graphql service"""
|
|
||||||
return Service(
|
|
||||||
id=service.get_id(),
|
|
||||||
display_name=service.get_display_name(),
|
|
||||||
description=service.get_description(),
|
|
||||||
svg_icon=service.get_svg_icon(),
|
|
||||||
is_movable=service.is_movable(),
|
|
||||||
is_required=service.is_required(),
|
|
||||||
is_enabled=service.is_enabled(),
|
|
||||||
status=ServiceStatusEnum(service.get_status().value),
|
|
||||||
url=service.get_url(),
|
|
||||||
dns_records=[
|
|
||||||
DnsRecord(
|
|
||||||
record_type=record.type,
|
|
||||||
name=record.name,
|
|
||||||
content=record.content,
|
|
||||||
ttl=record.ttl,
|
|
||||||
priority=record.priority,
|
|
||||||
)
|
|
||||||
for record in service.get_dns_records()
|
|
||||||
],
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def get_volume_by_id(volume_id: str) -> typing.Optional[StorageVolume]:
|
|
||||||
"""Get volume by id"""
|
|
||||||
volume = BlockDevices().get_block_device(volume_id)
|
|
||||||
if volume is None:
|
|
||||||
return None
|
|
||||||
return StorageVolume(
|
|
||||||
total_space=str(volume.fssize)
|
|
||||||
if volume.fssize is not None
|
|
||||||
else str(volume.size),
|
|
||||||
free_space=str(volume.fsavail),
|
|
||||||
used_space=str(volume.fsused),
|
|
||||||
root=volume.name == "sda1",
|
|
||||||
name=volume.name,
|
|
||||||
model=volume.model,
|
|
||||||
serial=volume.serial,
|
|
||||||
type=volume.type,
|
|
||||||
usages=[],
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@strawberry.type
|
@strawberry.type
|
||||||
|
|
|
@ -2,7 +2,15 @@
|
||||||
# pylint: disable=too-few-public-methods
|
# pylint: disable=too-few-public-methods
|
||||||
import typing
|
import typing
|
||||||
import strawberry
|
import strawberry
|
||||||
from selfprivacy_api.graphql.common_types.storage_usage import StorageVolume
|
from selfprivacy_api.graphql.common_types.service import (
|
||||||
|
service_to_graphql_service,
|
||||||
|
get_volume_by_id,
|
||||||
|
)
|
||||||
|
from selfprivacy_api.graphql.common_types.storage_usage import (
|
||||||
|
ServiceStorageUsage,
|
||||||
|
StorageVolume,
|
||||||
|
)
|
||||||
|
from selfprivacy_api.services import get_services_by_location
|
||||||
from selfprivacy_api.utils.block_devices import BlockDevices
|
from selfprivacy_api.utils.block_devices import BlockDevices
|
||||||
|
|
||||||
|
|
||||||
|
@ -25,7 +33,15 @@ class Storage:
|
||||||
model=volume.model,
|
model=volume.model,
|
||||||
serial=volume.serial,
|
serial=volume.serial,
|
||||||
type=volume.type,
|
type=volume.type,
|
||||||
usages=[],
|
usages=[
|
||||||
|
ServiceStorageUsage(
|
||||||
|
service=service_to_graphql_service(service),
|
||||||
|
title=service.get_display_name(),
|
||||||
|
used_space=str(service.get_storage_usage()),
|
||||||
|
volume=get_volume_by_id(service.get_location()),
|
||||||
|
)
|
||||||
|
for service in get_services_by_location(volume.name)
|
||||||
|
],
|
||||||
)
|
)
|
||||||
for volume in BlockDevices().get_block_devices()
|
for volume in BlockDevices().get_block_devices()
|
||||||
]
|
]
|
||||||
|
|
|
@ -136,6 +136,7 @@ class System:
|
||||||
info: SystemInfo = SystemInfo()
|
info: SystemInfo = SystemInfo()
|
||||||
provider: SystemProviderInfo = strawberry.field(resolver=get_system_provider_info)
|
provider: SystemProviderInfo = strawberry.field(resolver=get_system_provider_info)
|
||||||
busy: bool = False
|
busy: bool = False
|
||||||
|
|
||||||
@strawberry.field
|
@strawberry.field
|
||||||
def working_directory(self) -> str:
|
def working_directory(self) -> str:
|
||||||
"""Get working directory"""
|
"""Get working directory"""
|
||||||
|
|
|
@ -10,7 +10,7 @@ from selfprivacy_api.services.ocserv import Ocserv
|
||||||
from selfprivacy_api.services.service import Service
|
from selfprivacy_api.services.service import Service
|
||||||
|
|
||||||
|
|
||||||
services = [
|
services: list[Service] = [
|
||||||
Bitwarden(),
|
Bitwarden(),
|
||||||
Gitea(),
|
Gitea(),
|
||||||
MailServer(),
|
MailServer(),
|
||||||
|
@ -37,3 +37,7 @@ def get_enabled_services() -> typing.List[Service]:
|
||||||
|
|
||||||
def get_disabled_services() -> typing.List[Service]:
|
def get_disabled_services() -> typing.List[Service]:
|
||||||
return [service for service in services if not service.is_enabled()]
|
return [service for service in services if not service.is_enabled()]
|
||||||
|
|
||||||
|
|
||||||
|
def get_services_by_location(location: str) -> typing.List[Service]:
|
||||||
|
return [service for service in services if service.get_location() == location]
|
||||||
|
|
Loading…
Reference in a new issue