2014-07-10 23:57:38 +02:00
|
|
|
import os
|
|
|
|
import logging
|
|
|
|
import ConfigParser
|
|
|
|
|
2014-07-11 22:16:30 +02:00
|
|
|
from libcloud.storage.types import Provider, ObjectDoesNotExistError
|
2014-07-10 23:57:38 +02:00
|
|
|
from libcloud.storage.providers import get_driver
|
|
|
|
|
|
|
|
CONFIG_PATH = '/etc/airtime/airtime.conf'
|
|
|
|
|
|
|
|
class CloudStorageDownloader:
|
|
|
|
def __init__(self):
|
|
|
|
config = self.read_config_file(CONFIG_PATH)
|
|
|
|
|
2014-07-11 22:16:30 +02:00
|
|
|
CLOUD_STORAGE_CONFIG_SECTION = "cloud_storage"
|
|
|
|
self._provider = config.get(CLOUD_STORAGE_CONFIG_SECTION, 'provider')
|
|
|
|
self._bucket = config.get(CLOUD_STORAGE_CONFIG_SECTION, 'bucket')
|
|
|
|
self._api_key = config.get(CLOUD_STORAGE_CONFIG_SECTION, 'api_key')
|
|
|
|
self._api_key_secret = config.get(CLOUD_STORAGE_CONFIG_SECTION, 'api_key_secret')
|
2014-07-10 23:57:38 +02:00
|
|
|
|
|
|
|
def download_obj(self, dst, obj_name):
|
2014-07-11 22:16:30 +02:00
|
|
|
cls = get_driver(getattr(Provider, self._provider))
|
|
|
|
driver = cls(self._api_key, self._api_key_secret)
|
|
|
|
|
2014-07-10 23:57:38 +02:00
|
|
|
try:
|
2014-07-11 22:16:30 +02:00
|
|
|
cloud_obj = driver.get_object(container_name=self._bucket,
|
2014-07-10 23:57:38 +02:00
|
|
|
object_name=obj_name)
|
|
|
|
except ObjectDoesNotExistError:
|
|
|
|
logging.info("Could not find object: %s" % obj_name)
|
|
|
|
exit(-1)
|
|
|
|
logging.info('Downloading: %s to %s' % (cloud_obj.name, dst))
|
|
|
|
cloud_obj.download(destination_path=dst)
|
|
|
|
|
|
|
|
def read_config_file(self, config_path):
|
|
|
|
"""Parse the application's config file located at config_path."""
|
|
|
|
config = ConfigParser.SafeConfigParser()
|
|
|
|
try:
|
|
|
|
config.readfp(open(config_path))
|
|
|
|
except IOError as e:
|
|
|
|
print "Failed to open config file at " + config_path + ": " + e.strerror
|
|
|
|
exit(-1)
|
|
|
|
except Exception:
|
|
|
|
print e.strerror
|
|
|
|
exit(-1)
|
|
|
|
|
2014-07-10 23:58:00 +02:00
|
|
|
return config
|