2019-06-10 13:32:45 +00:00
|
|
|
import logging
|
2018-08-01 15:24:39 +00:00
|
|
|
from os.path import join as joinpath
|
|
|
|
from os.path import basename
|
|
|
|
from os import makedirs
|
2018-08-06 12:55:58 +00:00
|
|
|
from datetime import datetime
|
2018-08-01 15:24:39 +00:00
|
|
|
|
|
|
|
from dateutil import parser as dateparser
|
|
|
|
|
2019-07-24 13:17:16 +00:00
|
|
|
from .snapshot_provider import SnapshotProvider
|
2019-06-28 13:11:02 +00:00
|
|
|
|
2018-08-01 15:24:39 +00:00
|
|
|
LOG = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2019-07-24 13:17:16 +00:00
|
|
|
class SnapshotHandler:
|
2019-07-12 21:25:16 +00:00
|
|
|
keys = ['snapshot']
|
|
|
|
|
2019-07-23 13:32:50 +00:00
|
|
|
def __init__(self, *, directories, snapshots_dir, exclude_unix_patterns=None):
|
|
|
|
self._snapshots_dir = snapshots_dir
|
2018-08-01 15:24:39 +00:00
|
|
|
self.snapshot_providers = {}
|
2018-08-06 13:47:14 +00:00
|
|
|
self._exclude_unix_patterns = exclude_unix_patterns
|
2018-08-01 15:24:39 +00:00
|
|
|
self.init_snapshot_providers(directories)
|
|
|
|
|
|
|
|
self.command_handlers = {
|
2019-08-08 13:05:37 +00:00
|
|
|
'snapshot.take': self.handle_take_snapshot,
|
|
|
|
'snapshot.restore': self.handle_restore_snapshot,
|
|
|
|
'snapshot.exclude': self.handle_exclude
|
2018-08-01 15:24:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def init_snapshot_providers(self, directories):
|
2018-08-06 11:40:16 +00:00
|
|
|
for index, directory in enumerate(directories):
|
|
|
|
git_dir = self.init_git_dir(index, directory)
|
2018-08-06 13:47:14 +00:00
|
|
|
self.snapshot_providers[directory] = SnapshotProvider(
|
|
|
|
directory,
|
|
|
|
git_dir,
|
|
|
|
self._exclude_unix_patterns
|
|
|
|
)
|
2018-08-01 15:24:39 +00:00
|
|
|
|
2019-07-23 13:32:50 +00:00
|
|
|
def init_git_dir(self, index, directory):
|
2018-08-06 11:40:16 +00:00
|
|
|
git_dir = joinpath(
|
2019-07-23 13:32:50 +00:00
|
|
|
self._snapshots_dir,
|
2018-08-06 11:40:16 +00:00
|
|
|
f'{basename(directory)}-{index}'
|
2018-08-03 09:57:22 +00:00
|
|
|
)
|
|
|
|
makedirs(git_dir, exist_ok=True)
|
|
|
|
return git_dir
|
|
|
|
|
2019-08-08 13:05:37 +00:00
|
|
|
def handle_event(self, message, _):
|
2018-08-01 15:24:39 +00:00
|
|
|
try:
|
2019-08-08 13:05:37 +00:00
|
|
|
self.command_handlers[message['key']](message)
|
2018-08-01 15:24:39 +00:00
|
|
|
except KeyError:
|
|
|
|
LOG.error('IGNORING MESSAGE: Invalid message received: %s', message)
|
|
|
|
|
2019-08-08 13:05:37 +00:00
|
|
|
def handle_take_snapshot(self, _):
|
2018-08-03 09:57:22 +00:00
|
|
|
LOG.debug('Taking snapshots of directories %s', self.snapshot_providers.keys())
|
2018-08-01 15:24:39 +00:00
|
|
|
for provider in self.snapshot_providers.values():
|
|
|
|
provider.take_snapshot()
|
|
|
|
|
2019-08-08 13:05:37 +00:00
|
|
|
def handle_restore_snapshot(self, message):
|
2018-08-06 12:55:58 +00:00
|
|
|
date = dateparser.parse(
|
2019-08-08 13:05:37 +00:00
|
|
|
message.get(
|
2018-08-06 12:55:58 +00:00
|
|
|
'value',
|
|
|
|
datetime.now().isoformat()
|
|
|
|
)
|
|
|
|
)
|
2018-08-03 09:57:22 +00:00
|
|
|
LOG.debug(
|
|
|
|
'Restoring snapshots (@ %s) of directories %s',
|
|
|
|
date,
|
|
|
|
self.snapshot_providers.keys()
|
|
|
|
)
|
2018-08-01 15:24:39 +00:00
|
|
|
for provider in self.snapshot_providers.values():
|
|
|
|
provider.restore_snapshot(date)
|
2018-08-06 13:47:14 +00:00
|
|
|
|
2019-08-08 13:05:37 +00:00
|
|
|
def handle_exclude(self, message):
|
|
|
|
exclude_unix_patterns = message['value']
|
2018-08-06 13:47:14 +00:00
|
|
|
if not isinstance(exclude_unix_patterns, list):
|
|
|
|
raise KeyError
|
|
|
|
|
|
|
|
for provider in self.snapshot_providers.values():
|
|
|
|
provider.exclude = exclude_unix_patterns
|