Add registry app updates
This commit is contained in:
@@ -4,9 +4,11 @@ import os
|
||||
from django.core.files.base import ContentFile
|
||||
from django.core.files.storage import FileSystemStorage
|
||||
from django.core.management.commands.dumpdata import Command
|
||||
from django.conf import settings
|
||||
from django.db import router, DEFAULT_DB_ALIAS
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.utils.translation import ugettext
|
||||
from django.utils.importlib import import_module
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -156,145 +158,4 @@ class AppBackup(object):
|
||||
results.append(u'%s - %s' % (manager, manager.info() or _(u'Nothing')))
|
||||
return u', '.join(results)
|
||||
|
||||
def backup(self, storage_module, dry_run=False):
|
||||
logger.debug('starting')
|
||||
|
||||
self.state = self.__class__.STATE_BACKING_UP
|
||||
for manager in self.backup_managers:
|
||||
result = manager.backup()
|
||||
storage_module.backup(result, dry_run=dry_run)
|
||||
self.state = self.__class__.STATE_IDLE
|
||||
|
||||
def restore(self, storage_module=None):
|
||||
logger.debug('starting')
|
||||
self.state = self.__class__.STATE_RESTORING
|
||||
for manager in self.backup_managers:
|
||||
manager.restore(storage_module.restore())
|
||||
self.state = self.__class__.STATE_IDLE
|
||||
|
||||
def __unicode__(self):
|
||||
return unicode(self.app)
|
||||
|
||||
|
||||
#Storage
|
||||
class StorageModuleBase(object):
|
||||
_registry = {}
|
||||
|
||||
# Local modules depend on hardware on a node and execute in the Scheduler
|
||||
# of a particular node
|
||||
REALM_LOCAL = 'local'
|
||||
|
||||
# Remote modules can be execute by any node in a cluster and are placed
|
||||
# in the JobQueue
|
||||
REALM_REMOTE = 'remote'
|
||||
|
||||
REALM_CHOICES = (
|
||||
(REALM_LOCAL, _(u'local')),
|
||||
(REALM_REMOTE, _(u'remote')),
|
||||
)
|
||||
|
||||
class UnknownStorageModule(Exception):
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
def register(cls, klass):
|
||||
"""
|
||||
Register a subclass of StorageModuleBase to make it available to the
|
||||
UI
|
||||
"""
|
||||
cls._registry[klass.name] = klass
|
||||
|
||||
@classmethod
|
||||
def get_all(cls):
|
||||
return cls._registry.values()
|
||||
|
||||
@classmethod
|
||||
def get(cls, name):
|
||||
try:
|
||||
return cls._registry[name]
|
||||
except KeyError:
|
||||
raise cls.UnknownStorageModule
|
||||
|
||||
@classmethod
|
||||
def get_as_choices(cls):
|
||||
return [(name, unicode(klass.label)) for name, klass in cls._registry.items()]
|
||||
|
||||
def get_arguments(self):
|
||||
return []
|
||||
|
||||
def is_local_realm(self):
|
||||
return self.realm == REALM_LOCAL
|
||||
|
||||
def is_remote_realm(self):
|
||||
return self.realm == REALM_REMOTE
|
||||
|
||||
def backup(self, data, dry_run):
|
||||
raise NotImplemented
|
||||
|
||||
def restore(self):
|
||||
"""
|
||||
Must return data or a file like object
|
||||
"""
|
||||
raise NotImplemented
|
||||
|
||||
def __unicode__(self):
|
||||
return unicode(self.label)
|
||||
|
||||
|
||||
class TestStorage(StorageModuleBase):
|
||||
name = 'test_storage'
|
||||
label = _(u'Test storage module')
|
||||
realm = StorageModuleBase.REALM_LOCAL
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.backup_path = kwargs.pop('backup_path', None)
|
||||
self.restore_path = kwargs.pop('restore_path', None)
|
||||
|
||||
def get_arguments(self):
|
||||
return ['backup_path', 'restore_path']
|
||||
|
||||
def backup(self, elements, dry_run):
|
||||
logger.debug('self.backup_path: %s' % self.backup_path)
|
||||
|
||||
for element in elements:
|
||||
content_file = element.save()
|
||||
logger.debug('element.filename: %s' % element.filename)
|
||||
logger.debug('element.content: %s' % element.content)
|
||||
|
||||
def restore(self):
|
||||
print 'restore from path: %s' % self.restore_path
|
||||
return 'sample_data'
|
||||
|
||||
|
||||
class LocalFileSystemStorage(FileSystemStorage):
|
||||
"""
|
||||
Simple wrapper for the stock Django FileSystemStorage class
|
||||
"""
|
||||
name = 'local_filesystem_storage'
|
||||
label = _(u'Local filesystem')
|
||||
realm = StorageModuleBase.REALM_LOCAL
|
||||
|
||||
separator = os.path.sep
|
||||
|
||||
def get_arguments(self):
|
||||
return ['backup_path', 'restore_path']
|
||||
|
||||
def backup(self, elements, dry_run):
|
||||
logger.debug('self.backup_path: %s' % self.backup_path)
|
||||
for element in elements:
|
||||
content_file = element.save()
|
||||
path = self.storage.save(content_file.name, content_file)
|
||||
logger.debug('element.filename: %s' % element.filename)
|
||||
logger.debug('element.content: %s' % element.content)
|
||||
|
||||
def restore(self):
|
||||
print 'restore from path: %s' % self.restore_path
|
||||
return 'sample_data'
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.backup_path = kwargs.pop('backup_path', None)
|
||||
self.storage = FileSystemStorage(location=self.backup_path)
|
||||
|
||||
|
||||
StorageModuleBase.register(LocalFileSystemStorage)
|
||||
StorageModuleBase.register(TestStorage)
|
||||
|
||||
Reference in New Issue
Block a user