diff --git a/lib/tfw/components/directory_snapshotting_event_handler.py b/lib/tfw/components/directory_snapshotting_event_handler.py index 5a02f1a..3d50315 100644 --- a/lib/tfw/components/directory_snapshotting_event_handler.py +++ b/lib/tfw/components/directory_snapshotting_event_handler.py @@ -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 diff --git a/lib/tfw/components/snapshot_provider.py b/lib/tfw/components/snapshot_provider.py index 2c8dcee..11c39c6 100644 --- a/lib/tfw/components/snapshot_provider.py +++ b/lib/tfw/components/snapshot_provider.py @@ -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