2019-01-06 17:34:02 +01:00
|
|
|
import os
|
|
|
|
import pwd
|
2020-03-18 21:09:17 +01:00
|
|
|
import json
|
2021-05-20 17:39:47 +02:00
|
|
|
import yaml as YAML
|
2019-01-06 17:34:02 +01:00
|
|
|
import logging
|
2019-02-22 21:26:01 +01:00
|
|
|
import jsonschema
|
2019-01-06 17:34:02 +01:00
|
|
|
|
2020-03-19 21:16:38 +01:00
|
|
|
class RegularUserException(Exception):
|
|
|
|
pass
|
2019-01-06 17:34:02 +01:00
|
|
|
|
|
|
|
class Config:
|
2020-01-06 13:26:52 +01:00
|
|
|
__shared_state = dict()
|
|
|
|
_config = dict()
|
2019-01-06 17:34:02 +01:00
|
|
|
|
|
|
|
def __init__(self):
|
2020-01-05 13:17:46 +01:00
|
|
|
self.__dict__ = self.__shared_state
|
2020-01-06 13:26:52 +01:00
|
|
|
if not self._config:
|
|
|
|
logging.info("Loading configuration...")
|
|
|
|
self._schema = dict()
|
|
|
|
self._backups = list()
|
2020-03-19 21:16:38 +01:00
|
|
|
self._devices = dict()
|
2020-01-06 13:26:52 +01:00
|
|
|
file_path = os.path.realpath(__file__)
|
2021-05-20 17:39:47 +02:00
|
|
|
logging.debug("config file_path: " + file_path)
|
2020-01-06 13:26:52 +01:00
|
|
|
schema_path = os.path.join(
|
|
|
|
os.path.dirname(
|
|
|
|
file_path
|
|
|
|
),
|
|
|
|
"schema.yml"
|
|
|
|
)
|
2021-05-20 17:39:47 +02:00
|
|
|
logging.debug("schema path: " + schema_path)
|
2020-01-06 13:26:52 +01:00
|
|
|
with open(schema_path, "r") as stream:
|
2021-05-20 17:39:47 +02:00
|
|
|
self._schema = YAML.safe_load(stream)
|
2020-03-18 21:09:17 +01:00
|
|
|
self.find_config()
|
2019-01-06 17:34:02 +01:00
|
|
|
|
|
|
|
def find_config(self):
|
|
|
|
# who are we?
|
|
|
|
uid = os.getuid()
|
|
|
|
# name?
|
|
|
|
user = pwd.getpwuid(uid).pw_name
|
2020-03-18 21:09:17 +01:00
|
|
|
logging.debug("Trying to find the configuration")
|
2019-01-06 17:34:02 +01:00
|
|
|
try:
|
|
|
|
if uid == 0:
|
2019-01-06 21:29:21 +01:00
|
|
|
config_file = "/etc/backive.yml"
|
2019-01-06 17:34:02 +01:00
|
|
|
else:
|
2020-03-19 21:16:38 +01:00
|
|
|
raise RegularUserException(
|
|
|
|
"""
|
|
|
|
It is planned to add functionality to use this service
|
|
|
|
as a regular user, but for the time being it is advised to
|
|
|
|
execute this service as root, because this feature is still
|
|
|
|
planned and needs more development time.
|
|
|
|
""")
|
2019-05-03 21:36:44 +02:00
|
|
|
config_file = os.path.join(
|
|
|
|
os.path.expanduser("~"),
|
|
|
|
".config",
|
|
|
|
"backive",
|
|
|
|
"backive.yml"
|
|
|
|
)
|
2019-01-06 17:34:02 +01:00
|
|
|
|
|
|
|
with open(config_file, "r") as cfg:
|
2021-05-20 20:24:46 +02:00
|
|
|
self._config = YAML.safe_load(cfg)
|
2020-03-19 21:16:38 +01:00
|
|
|
logging.debug(
|
|
|
|
"Found config: %s\n%s",
|
|
|
|
config_file,
|
|
|
|
json.dumps(self._config, indent=4)
|
|
|
|
)
|
2019-02-22 21:26:01 +01:00
|
|
|
jsonschema.validate(self._config, self._schema)
|
2020-03-19 21:16:38 +01:00
|
|
|
except RegularUserException as e:
|
|
|
|
raise e
|
2019-01-06 17:34:02 +01:00
|
|
|
except Exception as e:
|
|
|
|
logging.error(e)
|
2019-01-06 21:29:21 +01:00
|
|
|
|
|
|
|
def get_devices(self):
|
2020-03-19 21:16:38 +01:00
|
|
|
from backive.core.device import Device
|
2020-01-05 13:17:46 +01:00
|
|
|
if self._config.get("devices", None) and not self._devices:
|
2019-01-06 21:29:21 +01:00
|
|
|
data = self._config.get("devices")
|
2020-03-19 21:16:38 +01:00
|
|
|
for device, values in data.items():
|
|
|
|
self._devices.update({
|
|
|
|
device:
|
2019-01-06 21:29:21 +01:00
|
|
|
Device.instance(
|
|
|
|
device,
|
2020-03-19 21:16:38 +01:00
|
|
|
values
|
2019-01-06 21:29:21 +01:00
|
|
|
)
|
2020-03-19 21:16:38 +01:00
|
|
|
})
|
2020-01-05 13:17:46 +01:00
|
|
|
return self._devices
|
2019-01-06 21:29:21 +01:00
|
|
|
|
2020-03-19 21:16:38 +01:00
|
|
|
async def get_device(self, name):
|
|
|
|
for device, value in self.get_devices().items():
|
|
|
|
if device == name:
|
|
|
|
return value
|
|
|
|
return None
|
|
|
|
|
|
|
|
def get_backups(self) -> list:
|
|
|
|
from backive.core.backup import Backup
|
2020-01-05 13:17:46 +01:00
|
|
|
if self._config.get("backups", None) and not self._backups:
|
2019-01-06 21:29:21 +01:00
|
|
|
data = self._config.get("backups")
|
|
|
|
for name in data:
|
2020-01-05 13:17:46 +01:00
|
|
|
self._backups.append(
|
2019-01-06 21:29:21 +01:00
|
|
|
Backup.instance(
|
|
|
|
name,
|
|
|
|
data.get(name)
|
|
|
|
)
|
|
|
|
)
|
2020-01-05 13:17:46 +01:00
|
|
|
return self._backups
|
|
|
|
|
2020-03-19 21:16:38 +01:00
|
|
|
async def get_uuid_device(self, uuid):
|
|
|
|
logging.debug("get device %s", uuid)
|
|
|
|
for device, value in self.get_devices().items():
|
|
|
|
logging.debug(
|
|
|
|
"device %s, config %s", device, json.dumps(value.config)
|
|
|
|
)
|
|
|
|
if value.config.get("uuid") == uuid:
|
|
|
|
return value
|
|
|
|
return None
|
|
|
|
|
2020-03-18 21:09:17 +01:00
|
|
|
async def get_backups_by_device(self, uuid):
|
2020-01-05 13:17:46 +01:00
|
|
|
name = None
|
2020-03-18 21:09:17 +01:00
|
|
|
if not self._config.get("devices"):
|
|
|
|
return None
|
2020-01-05 13:17:46 +01:00
|
|
|
for k, v in self._config.get("devices").items():
|
|
|
|
if v.get("uuid") == uuid:
|
|
|
|
name = k
|
|
|
|
if name:
|
|
|
|
return self.get_device_backups(name)
|
|
|
|
return None
|
2019-01-06 21:29:21 +01:00
|
|
|
|
2020-01-05 13:17:46 +01:00
|
|
|
def get_device_backups(self, name):
|
|
|
|
backups = list()
|
2019-02-22 21:26:01 +01:00
|
|
|
for backup in self.get_backups():
|
2020-03-18 21:09:17 +01:00
|
|
|
if backup.config.get("target_device") == name:
|
2019-02-22 21:26:01 +01:00
|
|
|
backups.append(backup)
|
|
|
|
return backups
|
|
|
|
|
2020-03-19 21:16:38 +01:00
|
|
|
async def get_preferences(self):
|
2019-02-22 21:26:01 +01:00
|
|
|
if self._config.get("preferences", None):
|
|
|
|
return self._config.get("preferences")
|
2019-01-06 21:29:21 +01:00
|
|
|
return {}
|