2014-04-03 21:31:46 +00:00
|
|
|
from storage.local import LocalStorage
|
2017-09-26 20:08:50 +00:00
|
|
|
from storage.cloud import S3Storage, GoogleCloudStorage, RadosGWStorage, CloudFrontedS3Storage
|
2014-04-03 21:31:46 +00:00
|
|
|
from storage.fakestorage import FakeStorage
|
2014-06-17 20:03:43 +00:00
|
|
|
from storage.distributedstorage import DistributedStorage
|
2015-05-21 19:22:59 +00:00
|
|
|
from storage.swift import SwiftStorage
|
2016-08-24 16:55:33 +00:00
|
|
|
from storage.downloadproxy import DownloadProxy
|
2014-04-03 21:31:46 +00:00
|
|
|
|
2014-08-12 06:06:44 +00:00
|
|
|
STORAGE_DRIVER_CLASSES = {
|
|
|
|
'LocalStorage': LocalStorage,
|
|
|
|
'S3Storage': S3Storage,
|
|
|
|
'GoogleCloudStorage': GoogleCloudStorage,
|
2014-09-09 19:54:03 +00:00
|
|
|
'RadosGWStorage': RadosGWStorage,
|
2015-05-21 19:22:59 +00:00
|
|
|
'SwiftStorage': SwiftStorage,
|
2017-09-26 20:08:50 +00:00
|
|
|
'CloudFrontedS3Storage': CloudFrontedS3Storage,
|
2014-08-12 06:06:44 +00:00
|
|
|
}
|
|
|
|
|
2017-01-24 21:31:33 +00:00
|
|
|
|
2017-09-26 20:08:50 +00:00
|
|
|
def get_storage_driver(location, metric_queue, chunk_cleanup_queue, config_provider, ip_resolver, storage_params):
|
2015-01-07 21:20:51 +00:00
|
|
|
""" Returns a storage driver class for the given storage configuration
|
|
|
|
(a pair of string name and a dict of parameters). """
|
|
|
|
driver = storage_params[0]
|
|
|
|
parameters = storage_params[1]
|
|
|
|
driver_class = STORAGE_DRIVER_CLASSES.get(driver, FakeStorage)
|
2017-09-26 20:08:50 +00:00
|
|
|
context = StorageContext(location, metric_queue, chunk_cleanup_queue, config_provider, ip_resolver)
|
2016-11-10 18:54:04 +00:00
|
|
|
return driver_class(context, **parameters)
|
|
|
|
|
|
|
|
|
|
|
|
class StorageContext(object):
|
2017-09-26 20:08:50 +00:00
|
|
|
def __init__(self, location, metric_queue, chunk_cleanup_queue, config_provider, ip_resolver):
|
2016-11-10 18:54:04 +00:00
|
|
|
self.location = location
|
|
|
|
self.metric_queue = metric_queue
|
|
|
|
self.chunk_cleanup_queue = chunk_cleanup_queue
|
2017-09-26 20:08:50 +00:00
|
|
|
self.config_provider = config_provider
|
|
|
|
self.ip_resolver = ip_resolver
|
2015-01-07 21:20:51 +00:00
|
|
|
|
2014-08-12 06:06:44 +00:00
|
|
|
|
2014-04-03 21:31:46 +00:00
|
|
|
class Storage(object):
|
2017-09-26 20:08:50 +00:00
|
|
|
def __init__(self, app=None, metric_queue=None, chunk_cleanup_queue=None, instance_keys=None,
|
|
|
|
config_provider=None, ip_resolver=None):
|
2014-04-03 21:31:46 +00:00
|
|
|
self.app = app
|
2016-08-24 16:55:33 +00:00
|
|
|
if app is not None:
|
2017-09-26 20:08:50 +00:00
|
|
|
self.state = self.init_app(app, metric_queue, chunk_cleanup_queue, instance_keys,
|
|
|
|
config_provider, ip_resolver)
|
2014-04-03 21:31:46 +00:00
|
|
|
else:
|
|
|
|
self.state = None
|
|
|
|
|
2017-09-26 20:08:50 +00:00
|
|
|
def init_app(self, app, metric_queue, chunk_cleanup_queue, instance_keys, config_provider, ip_resolver):
|
2014-06-17 20:03:43 +00:00
|
|
|
storages = {}
|
|
|
|
for location, storage_params in app.config.get('DISTRIBUTED_STORAGE_CONFIG').items():
|
2016-11-10 18:54:04 +00:00
|
|
|
storages[location] = get_storage_driver(location, metric_queue, chunk_cleanup_queue,
|
2017-09-26 20:08:50 +00:00
|
|
|
config_provider, ip_resolver, storage_params)
|
2014-06-17 20:03:43 +00:00
|
|
|
|
|
|
|
preference = app.config.get('DISTRIBUTED_STORAGE_PREFERENCE', None)
|
|
|
|
if not preference:
|
|
|
|
preference = storages.keys()
|
|
|
|
|
2015-06-28 10:29:22 +00:00
|
|
|
default_locations = app.config.get('DISTRIBUTED_STORAGE_DEFAULT_LOCATIONS') or []
|
2016-08-24 16:55:33 +00:00
|
|
|
|
|
|
|
download_proxy = None
|
|
|
|
if app.config.get('FEATURE_PROXY_STORAGE', False) and instance_keys is not None:
|
|
|
|
download_proxy = DownloadProxy(app, instance_keys)
|
|
|
|
|
|
|
|
d_storage = DistributedStorage(storages, preference, default_locations, download_proxy)
|
2014-04-03 21:31:46 +00:00
|
|
|
|
|
|
|
# register extension with app
|
|
|
|
app.extensions = getattr(app, 'extensions', {})
|
2014-06-17 20:03:43 +00:00
|
|
|
app.extensions['storage'] = d_storage
|
|
|
|
return d_storage
|
2014-04-03 21:31:46 +00:00
|
|
|
|
|
|
|
def __getattr__(self, name):
|
2014-06-17 20:03:43 +00:00
|
|
|
return getattr(self.state, name, None)
|