# Path: cyclosible/playbook/plugins/storage/base.py
# class StorageBase(object):
#     """Base class for Cyclosible plugin
#     """
# 
#     def __init__(self, task_id):
#         self.redis_connection = Redis(connection_pool=ConnectionPool(**settings.WS4REDIS_CONNECTION))
#         self.tmpfile = tempfile.NamedTemporaryFile(mode='a+')
#         for log in self.redis_connection.lrange(':'.join(['tasks', task_id]), 0, -1):
#             self.tmpfile.write(log)
#         # Rewind the log file
#         self.tmpfile.seek(0)
#         self.log_url = None
# 
#     @abc.abstractmethod
#     def write_log(self):
#         """Write the log somewhere.
#         :returns: Boolean. True if success, False if failure
#         """
# 
#     def get_url_log(self):
#         """Get the url of the log.
#         :returns: Url of the log. Can be empty
#         """
#         return self.log_url


from .base import StorageBase
from django.conf import settings
from boto.s3.key import Key
import boto


class S3Plugin(StorageBase):
    def __init__(self, task_id):
        super(S3Plugin, self).__init__(task_id)
        self.s3_connection = boto.connect_s3(aws_access_key_id=settings.S3_ACCESS_KEY,
                                             aws_secret_access_key=settings.S3_SECRET_KEY)
        self.s3_filename = 'cyclosible-{task_id}.log'.format(task_id=task_id)
        self.bucket = self.s3_connection.get_bucket(settings.S3_BUCKET)

    def write_log(self):
        k = Key(self.bucket)
        k.key = self.s3_filename

# Path: cyclosible/playbook/plugins/storage/base.py
# class StorageBase(object):
#     """Base class for Cyclosible plugin
#     """
# 
#     def __init__(self, task_id):
#         self.redis_connection = Redis(connection_pool=ConnectionPool(**settings.WS4REDIS_CONNECTION))
#         self.tmpfile = tempfile.NamedTemporaryFile(mode='a+')
#         for log in self.redis_connection.lrange(':'.join(['tasks', task_id]), 0, -1):
#             self.tmpfile.write(log)
#         # Rewind the log file
#         self.tmpfile.seek(0)
#         self.log_url = None
# 
#     @abc.abstractmethod
#     def write_log(self):
#         """Write the log somewhere.
#         :returns: Boolean. True if success, False if failure
#         """
# 
#     def get_url_log(self):
#         """Get the url of the log.
#         :returns: Url of the log. Can be empty
#         """
#         return self.log_url


# Path: cyclosible/playbook/plugins/storage/s3.py
from .base import StorageBase
from django.conf import settings
from boto.s3.key import Key
import boto



class S3Plugin(StorageBase):
    def __init__(self, task_id):
        super(S3Plugin, self).__init__(task_id)
        self.s3_connection = boto.connect_s3(aws_access_key_id=settings.S3_ACCESS_KEY,
                                             aws_secret_access_key=settings.S3_SECRET_KEY)
        self.s3_filename = 'cyclosible-{task_id}.log'.format(task_id=task_id)
        self.bucket = self.s3_connection.get_bucket(settings.S3_BUCKET)

    def write_log(self):
        k = Key(self.bucket)
        k.key = self.s3_filename

        self.log_url = k.generate_url(expires_in=600, query_auth=False)