2014-04-03 21:31:46 +00:00
|
|
|
from storage.local import LocalStorage
|
2014-09-09 19:54:03 +00:00
|
|
|
from storage.cloud import S3Storage, GoogleCloudStorage, RadosGWStorage
|
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
|
|
|
|
2016-08-24 16:55:33 +00:00
|
|
|
from urlparse import urlparse, parse_qs
|
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,
|
2014-08-12 06:06:44 +00:00
|
|
|
}
|
|
|
|
|
2016-01-15 16:15:40 +00:00
|
|
|
def get_storage_driver(metric_queue, 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)
|
2016-01-15 16:15:40 +00:00
|
|
|
return driver_class(metric_queue, **parameters)
|
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):
|
2016-08-24 16:55:33 +00:00
|
|
|
def __init__(self, app=None, metric_queue=None, instance_keys=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:
|
|
|
|
self.state = self.init_app(app, metric_queue, instance_keys)
|
2014-04-03 21:31:46 +00:00
|
|
|
else:
|
|
|
|
self.state = None
|
|
|
|
|
2016-08-24 16:55:33 +00:00
|
|
|
def init_app(self, app, metric_queue, instance_keys):
|
2014-06-17 20:03:43 +00:00
|
|
|
storages = {}
|
|
|
|
for location, storage_params in app.config.get('DISTRIBUTED_STORAGE_CONFIG').items():
|
2016-01-15 16:15:40 +00:00
|
|
|
storages[location] = get_storage_driver(metric_queue, 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)
|