Fix initialization issues with DirectorySnapshottingEH

This commit is contained in:
Kristóf Tóth 2018-08-03 11:57:22 +02:00
parent e383be0149
commit 088a1cefc5
2 changed files with 20 additions and 6 deletions

View File

@ -5,6 +5,7 @@ from os.path import join as joinpath
from os.path import basename
from os import makedirs
from uuid import uuid4
from glob import glob
from dateutil import parser as dateparser
@ -29,13 +30,21 @@ class DirectorySnapshottingEventHandler(EventHandlerBase):
def init_snapshot_providers(self, directories):
for directory in directories:
git_dir = joinpath(
TFWENV.SNAPSHOTS_DIR,
f'{basename(directory)}-{str(uuid4())}'
)
makedirs(git_dir, exist_ok=True)
git_dir = self.init_git_dir(directory)
self.snapshot_providers[directory] = SnapshotProvider(directory, git_dir)
@staticmethod
def init_git_dir(directory):
git_dir_prefix = joinpath(
TFWENV.SNAPSHOTS_DIR,
f'{basename(directory)}-'
)
potential_dirs = glob(f'{git_dir_prefix}*')
git_dir = potential_dirs[0] if potential_dirs else f'{git_dir_prefix}{str(uuid4())}'
makedirs(git_dir, exist_ok=True)
return git_dir
def handle_event(self, message):
try:
data = message['data']
@ -45,12 +54,18 @@ class DirectorySnapshottingEventHandler(EventHandlerBase):
LOG.error('IGNORING MESSAGE: Invalid message received: %s', message)
def handle_take_snapshot(self, data):
LOG.debug('Taking snapshots of directories %s', self.snapshot_providers.keys())
for provider in self.snapshot_providers.values():
provider.take_snapshot()
return data
def handle_restore_snapshot(self, data):
date = dateparser.parse(data['value'])
LOG.debug(
'Restoring snapshots (@ %s) of directories %s',
date,
self.snapshot_providers.keys()
)
for provider in self.snapshot_providers.values():
provider.restore_snapshot(date)
return data

View File

@ -5,7 +5,6 @@ import re
from subprocess import run, CalledProcessError, PIPE
from getpass import getuser
from os.path import isdir
from datetime import datetime
from uuid import uuid4
from dateutil import parser as dateparser