mirror of
				https://github.com/avatao-content/baseimage-tutorial-framework
				synced 2025-11-04 01:52:55 +00:00 
			
		
		
		
	Implement first version of DirectorySnapshottingEventHandler
This commit is contained in:
		@@ -10,3 +10,4 @@ from .terminal_commands import TerminalCommands
 | 
			
		||||
from .log_monitoring_event_handler import LogMonitoringEventHandler
 | 
			
		||||
from .fsm_managing_event_handler import FSMManagingEventHandler
 | 
			
		||||
from .snapshot_provider import SnapshotProvider
 | 
			
		||||
from .directory_snapshotting_event_handler import DirectorySnapshottingEventHandler
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										56
									
								
								lib/tfw/components/directory_snapshotting_event_handler.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										56
									
								
								lib/tfw/components/directory_snapshotting_event_handler.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,56 @@
 | 
			
		||||
# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
 | 
			
		||||
# All Rights Reserved. See LICENSE file for details.
 | 
			
		||||
 | 
			
		||||
from os.path import join as joinpath
 | 
			
		||||
from os.path import basename
 | 
			
		||||
from os import makedirs
 | 
			
		||||
from uuid import uuid4
 | 
			
		||||
 | 
			
		||||
from dateutil import parser as dateparser
 | 
			
		||||
 | 
			
		||||
from tfw.event_handler_base import EventHandlerBase
 | 
			
		||||
from tfw.components.snapshot_provider import SnapshotProvider
 | 
			
		||||
from tfw.config import TFWENV
 | 
			
		||||
from tfw.config.logs import logging
 | 
			
		||||
 | 
			
		||||
LOG = logging.getLogger(__name__)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class DirectorySnapshottingEventHandler(EventHandlerBase):
 | 
			
		||||
    def __init__(self, key, directories):
 | 
			
		||||
        super().__init__(key)
 | 
			
		||||
        self.snapshot_providers = {}
 | 
			
		||||
        self.init_snapshot_providers(directories)
 | 
			
		||||
 | 
			
		||||
        self.command_handlers = {
 | 
			
		||||
            'take_snapshot': self.handle_take_snapshot,
 | 
			
		||||
            'restore_snapshot': self.handle_restore_snapshot
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    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)
 | 
			
		||||
            self.snapshot_providers[directory] = SnapshotProvider(directory, git_dir)
 | 
			
		||||
 | 
			
		||||
    def handle_event(self, message):
 | 
			
		||||
        try:
 | 
			
		||||
            data = message['data']
 | 
			
		||||
            message['data'] = self.command_handlers[data['command']](data)
 | 
			
		||||
            return message
 | 
			
		||||
        except KeyError:
 | 
			
		||||
            LOG.error('IGNORING MESSAGE: Invalid message received: %s', message)
 | 
			
		||||
 | 
			
		||||
    def handle_take_snapshot(self, data):
 | 
			
		||||
        for provider in self.snapshot_providers.values():
 | 
			
		||||
            provider.take_snapshot()
 | 
			
		||||
        return data
 | 
			
		||||
 | 
			
		||||
    def handle_restore_snapshot(self, data):
 | 
			
		||||
        date = dateparser.parse(data['value'])
 | 
			
		||||
        for provider in self.snapshot_providers.values():
 | 
			
		||||
            provider.restore_snapshot(date)
 | 
			
		||||
        return data
 | 
			
		||||
		Reference in New Issue
	
	Block a user