80 lines
2.7 KiB
Python
80 lines
2.7 KiB
Python
"""Archive client command-line entry point."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import asyncio
|
|
import json
|
|
import logging
|
|
from pathlib import Path
|
|
from typing import Sequence
|
|
|
|
from archive_clients.config import ClientConfig
|
|
from archive_clients.daemon import ArchiveClientDaemon
|
|
from archive_clients.logging_config import configure_logging
|
|
from archive_clients.probes import probe_root, probe_writable_directory
|
|
from archive_clients.qbittorrent import QBittorrentReader
|
|
from archive_clients.services import probe_qbittorrent, probe_syncthing
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def main(argv: Sequence[str] | None = None) -> int:
|
|
parser = argparse.ArgumentParser(prog="archive-client")
|
|
parser.add_argument("--config", type=Path, required=True)
|
|
parser.add_argument("--mode", choices=("archive", "cache"))
|
|
parser.add_argument("--check-config", action="store_true")
|
|
arguments = parser.parse_args(argv)
|
|
config = ClientConfig.load(arguments.config, arguments.mode)
|
|
probes = [
|
|
probe_root(config.qbittorrent.local_root),
|
|
probe_root(config.syncthing.local_root),
|
|
]
|
|
probe_writable_directory(config.state_db.parent)
|
|
probe_writable_directory(config.backup_dir)
|
|
shared_token = config.read_shared_token()
|
|
qb_password = config.qbittorrent.read_password()
|
|
syncthing_key = config.syncthing.read_api_key()
|
|
if arguments.check_config:
|
|
print(json.dumps({
|
|
"client_id": config.client_id,
|
|
"role": config.role,
|
|
"filesystems": [probe.__dict__ | {"root": str(probe.root)} for probe in probes],
|
|
}, sort_keys=True))
|
|
return 0
|
|
configure_logging(
|
|
secret for secret in (shared_token, qb_password, syncthing_key) if secret
|
|
)
|
|
logger.info(
|
|
"client_starting",
|
|
extra={"client_id": config.client_id, "role": config.role},
|
|
)
|
|
try:
|
|
service_probes = [
|
|
probe_qbittorrent(config.qbittorrent),
|
|
probe_syncthing(
|
|
config.syncthing, sparse_supported=probes[1].sparse_files
|
|
),
|
|
]
|
|
for service in service_probes:
|
|
logger.info(
|
|
"service_probe_completed",
|
|
extra={
|
|
"client_id": config.client_id,
|
|
"role": config.role,
|
|
"service": service.service,
|
|
"health": service.state,
|
|
},
|
|
)
|
|
asyncio.run(ArchiveClientDaemon(
|
|
config, probes, service_probes,
|
|
resource_reader=QBittorrentReader(config.qbittorrent),
|
|
).run())
|
|
except KeyboardInterrupt:
|
|
logger.info(
|
|
"client_stopped",
|
|
extra={"client_id": config.client_id, "role": config.role},
|
|
)
|
|
return 0
|