2020-04-16 01:18:05 +00:00
|
|
|
import asyncio
|
|
|
|
import os
|
2020-04-16 20:21:29 +00:00
|
|
|
from . import backups, config, remotes
|
2020-04-16 01:18:05 +00:00
|
|
|
from .. import defaults, utils
|
|
|
|
|
|
|
|
|
|
|
|
_own_uid = str(os.getuid())
|
|
|
|
|
|
|
|
|
|
|
|
class BackupManager():
|
|
|
|
__con:utils.Connection
|
|
|
|
|
|
|
|
def __init__(self, con:utils.Connection):
|
|
|
|
self.__con = con
|
|
|
|
|
|
|
|
|
|
|
|
def gen_create_backup_func(master:BackupManager):
|
|
|
|
async def callback_func(backups:list):
|
|
|
|
raise NotImplementedError()
|
|
|
|
return callback_func
|
|
|
|
|
|
|
|
|
|
|
|
def gen_callback_func(master:BackupManager):
|
|
|
|
@utils.rpc_callback
|
|
|
|
async def callback_func(data, uid):
|
|
|
|
# Check uid
|
|
|
|
if _own_uid != uid:
|
|
|
|
raise PermissionError("%s user try to connect." % uid)
|
|
|
|
|
|
|
|
# Get operation
|
|
|
|
if not isinstance(data, dict):
|
|
|
|
raise ValueError("data have to be a object.")
|
|
|
|
if "operation" not in data:
|
|
|
|
raise ValueError("'operation' isn't set.")
|
|
|
|
operation = data["operation"]
|
2020-04-16 14:02:31 +00:00
|
|
|
del data["operation"]
|
2020-04-16 01:18:05 +00:00
|
|
|
|
|
|
|
# Run operation
|
2020-04-16 20:21:29 +00:00
|
|
|
if operation == "backup-add":
|
|
|
|
return await backups.add_backup(data)
|
|
|
|
elif operation == "remote-add":
|
2020-04-16 14:02:31 +00:00
|
|
|
return await remotes.add_remote(data)
|
2020-04-16 16:27:41 +00:00
|
|
|
elif operation == "remote-list":
|
|
|
|
return await remotes.remote_list(data)
|
2020-04-16 19:19:21 +00:00
|
|
|
elif operation == "remote-delete":
|
|
|
|
return await remotes.remote_delete(data)
|
2020-04-16 14:02:31 +00:00
|
|
|
else:
|
|
|
|
raise NotImplementedError("%s isn't a supported operation." % repr(operation))
|
2020-04-16 01:18:05 +00:00
|
|
|
return callback_func
|
|
|
|
|
|
|
|
|
2020-04-16 12:13:13 +00:00
|
|
|
async def run_deamon(user_path:str=None, fork:bool=False, sys_path:str=defaults.DEFAULT_PATH):
|
2020-04-16 01:18:05 +00:00
|
|
|
# Find path
|
|
|
|
if user_path is None:
|
2020-04-16 11:35:11 +00:00
|
|
|
user_path = defaults.USER_PATH
|
|
|
|
if user_path is None:
|
2020-04-16 01:18:05 +00:00
|
|
|
raise RuntimeError("Can't find environemt variable 'XDG_RUNTIME_DIR'.")
|
|
|
|
|
|
|
|
# Connect to sys service
|
|
|
|
con = utils.Connection()
|
|
|
|
await con.init(sys_path)
|
|
|
|
|
|
|
|
# Timer
|
|
|
|
backup_manager = BackupManager(con)
|
|
|
|
timer_task = asyncio.create_task(config.Timer().run(gen_create_backup_func(backup_manager)))
|
|
|
|
|
|
|
|
# Start serving
|
2020-04-16 12:13:13 +00:00
|
|
|
await utils.run_access_socket(user_path, gen_callback_func(backup_manager), fork=fork)
|