selfprivacy-rest-api/selfprivacy_api/services/nextcloud/__init__.py

172 lines
5.2 KiB
Python
Raw Normal View History

2022-07-25 17:08:31 +03:00
"""Class representing Nextcloud service."""
import base64
import subprocess
2022-08-02 22:50:16 +03:00
import typing
2022-08-17 23:58:56 +03:00
from selfprivacy_api.jobs import Job, Jobs
2022-08-11 02:36:36 +03:00
from selfprivacy_api.services.generic_service_mover import FolderMoveNames, move_service
from selfprivacy_api.services.generic_size_counter import get_storage_usage
from selfprivacy_api.services.generic_status_getter import get_service_status
2022-08-02 22:50:16 +03:00
from selfprivacy_api.services.service import Service, ServiceDnsRecord, ServiceStatus
2022-08-13 00:29:18 +03:00
from selfprivacy_api.utils import ReadUserData, WriteUserData, get_domain
2022-08-02 22:50:16 +03:00
from selfprivacy_api.utils.block_devices import BlockDevice
2022-08-20 21:49:51 +03:00
import selfprivacy_api.utils.network as network_utils
2022-08-13 00:42:11 +03:00
from selfprivacy_api.services.nextcloud.icon import NEXTCLOUD_ICON
2022-08-11 02:36:36 +03:00
2022-07-25 17:08:31 +03:00
class Nextcloud(Service):
"""Class representing Nextcloud service."""
2022-08-11 02:36:36 +03:00
@staticmethod
2022-08-13 00:29:18 +03:00
def get_id() -> str:
2022-07-25 17:08:31 +03:00
"""Return service id."""
return "nextcloud"
2022-08-11 02:36:36 +03:00
@staticmethod
def get_display_name() -> str:
2022-07-25 17:08:31 +03:00
"""Return service display name."""
return "Nextcloud"
2022-08-11 02:36:36 +03:00
@staticmethod
def get_description() -> str:
2022-07-25 17:08:31 +03:00
"""Return service description."""
return "Nextcloud is a cloud storage service that offers a web interface and a desktop client."
2022-08-11 02:36:36 +03:00
@staticmethod
def get_svg_icon() -> str:
2022-07-25 17:08:31 +03:00
"""Read SVG icon from file and return it as base64 encoded string."""
2022-08-13 00:42:11 +03:00
return base64.b64encode(NEXTCLOUD_ICON.encode("utf-8")).decode("utf-8")
2022-07-25 17:08:31 +03:00
2022-08-13 00:29:18 +03:00
@staticmethod
def get_url() -> typing.Optional[str]:
"""Return service url."""
domain = get_domain()
return f"https://cloud.{domain}"
2022-08-11 02:36:36 +03:00
@staticmethod
def is_movable() -> bool:
return True
@staticmethod
def is_required() -> bool:
return False
@staticmethod
def is_enabled() -> bool:
2022-07-25 17:08:31 +03:00
with ReadUserData() as user_data:
return user_data.get("nextcloud", {}).get("enable", False)
2022-08-11 02:36:36 +03:00
@staticmethod
def get_status() -> ServiceStatus:
2022-07-25 17:08:31 +03:00
"""
Return Nextcloud status from systemd.
Use command return code to determine status.
Return code 0 means service is running.
Return code 1 or 2 means service is in error stat.
Return code 3 means service is stopped.
Return code 4 means service is off.
"""
2022-08-11 02:36:36 +03:00
return get_service_status("phpfpm-nextcloud.service")
2022-07-25 17:08:31 +03:00
2022-08-11 02:36:36 +03:00
@staticmethod
def enable():
2022-07-25 17:08:31 +03:00
"""Enable Nextcloud service."""
with WriteUserData() as user_data:
if "nextcloud" not in user_data:
user_data["nextcloud"] = {}
user_data["nextcloud"]["enable"] = True
2022-08-11 02:36:36 +03:00
@staticmethod
def disable():
2022-07-25 17:08:31 +03:00
"""Disable Nextcloud service."""
with WriteUserData() as user_data:
if "nextcloud" not in user_data:
user_data["nextcloud"] = {}
user_data["nextcloud"]["enable"] = False
2022-08-11 02:36:36 +03:00
@staticmethod
def stop():
2022-07-25 17:08:31 +03:00
"""Stop Nextcloud service."""
subprocess.Popen(["systemctl", "stop", "phpfpm-nextcloud.service"])
2022-08-11 02:36:36 +03:00
@staticmethod
def start():
2022-07-25 17:08:31 +03:00
"""Start Nextcloud service."""
subprocess.Popen(["systemctl", "start", "phpfpm-nextcloud.service"])
2022-08-11 02:36:36 +03:00
@staticmethod
def restart():
2022-07-25 17:08:31 +03:00
"""Restart Nextcloud service."""
subprocess.Popen(["systemctl", "restart", "phpfpm-nextcloud.service"])
2022-08-11 02:36:36 +03:00
@staticmethod
def get_configuration() -> dict:
2022-07-25 17:08:31 +03:00
"""Return Nextcloud configuration."""
return {}
2022-08-11 02:36:36 +03:00
@staticmethod
def set_configuration(config_items):
2022-07-25 17:08:31 +03:00
return super().set_configuration(config_items)
2022-08-11 02:36:36 +03:00
@staticmethod
def get_logs():
2022-07-25 17:08:31 +03:00
"""Return Nextcloud logs."""
return ""
2022-08-11 02:36:36 +03:00
@staticmethod
def get_storage_usage() -> int:
2022-08-02 22:50:16 +03:00
"""
Calculate the real storage usage of /var/lib/nextcloud and all subdirectories.
Calculate using pathlib.
Do not follow symlinks.
"""
2022-08-11 02:36:36 +03:00
return get_storage_usage("/var/lib/nextcloud")
2022-08-02 22:50:16 +03:00
2022-08-11 02:36:36 +03:00
@staticmethod
def get_location() -> str:
2022-08-02 22:50:16 +03:00
"""Get the name of disk where Nextcloud is installed."""
with ReadUserData() as user_data:
if user_data.get("useBinds", False):
return user_data.get("nextcloud", {}).get("location", "sda1")
else:
return "sda1"
2022-08-11 02:36:36 +03:00
@staticmethod
def get_dns_records() -> typing.List[ServiceDnsRecord]:
return [
ServiceDnsRecord(
type="A",
name="cloud",
2022-08-20 21:49:51 +03:00
content=network_utils.get_ip4(),
ttl=3600,
),
ServiceDnsRecord(
type="AAAA",
name="cloud",
content=network_utils.get_ip6(),
2022-08-11 02:36:36 +03:00
ttl=3600,
),
]
2022-08-02 22:50:16 +03:00
2022-08-17 23:58:56 +03:00
def move_to_volume(self, volume: BlockDevice) -> Job:
2022-08-02 23:30:03 +03:00
job = Jobs.get_instance().add(
2022-08-17 23:58:56 +03:00
type_id="services.nextcloud.move",
name="Move Nextcloud",
2022-08-02 22:50:16 +03:00
description=f"Moving Nextcloud to volume {volume.name}",
)
2022-08-11 02:36:36 +03:00
move_service(
self,
volume,
job,
2022-08-02 22:50:16 +03:00
[
2022-08-11 02:36:36 +03:00
FolderMoveNames(
name="nextcloud",
bind_location="/var/lib/nextcloud",
owner="nextcloud",
group="nextcloud",
),
2022-08-02 22:50:16 +03:00
],
2022-08-11 02:36:36 +03:00
"nextcloud",
2022-08-02 22:50:16 +03:00
)
2022-08-11 02:36:36 +03:00
return job