예제 #1
0
    def test_it_converts_FailedBackup_errors_to_CommandError(
            self, fake_backup, fake_validate_options, FakeBackupStorage):
        backup_dir = mock.Mock(name="backup_dir")
        recipients = mock.Mock(name="recipients")
        default_db = mock.Mock(name="default_db")
        backup_storage = mock.MagicMock(spec=BackupStorage)

        error_message = str(uuid.uuid1())
        error = FailedBackup(error_message)
        fake_backup.side_effect = error

        error_message2 = str(uuid.uuid1())
        error2 = FailedBackup(error_message2)
        FakeBackupStorage.return_value = backup_storage

        with override_settings(BACKUP_DIR=backup_dir,
                               BACKUP_RECIPIENTS=recipients,
                               DATABASES={'default': default_db}):
            # call_command results in the CommandError being caught and propagated as SystemExit

            with self.assertRaisesRegexp(CommandError, error_message):
                self.command.handle()

            # And make sure it catches exceptions from the storage context manager
            backup_storage.__enter__.side_effect = error2
            with self.assertRaisesRegexp(CommandError, error_message2):
                self.command.handle()
예제 #2
0
 def __enter__(self):
     """Make sure our storage is fine and yield self as the storage helper"""
     try:
         self.validate_destination()
     except (boto.exception.BotoClientError, boto.exception.BotoServerError,
             boto.exception.NoAuthHandlerFound), error:
         raise FailedBackup("Failed to interact with s3: {0}".format(error))
예제 #3
0
 def get_from_s3(self, key_name, source_bucket, destination):
     """Get contents from some key into the provided destination"""
     key = source_bucket.get_key(key_name)
     if not key:
         raise FailedBackup("Cannot find key {0} in bucket {1}".format(
             key_name, source_bucket.name))
     key.get_contents_to_filename(destination)
예제 #4
0
    def test_converts_FailedBackup_errors_into_CommandError(
            self, fake_restore):
        default_db = mock.Mock(name="default_db")
        restore_from = mock.Mock(name="restore_from")
        restore_from.startswith.return_value = False

        error_message = str(uuid.uuid1())
        error = FailedBackup(error_message)
        fake_restore.side_effect = error

        with override_settings(DATABASES={'default': default_db}):
            with self.assertRaisesRegexp(CommandError, error_message):
                # call_command results in the CommandError being caught and propagated as SystemExit
                self.command.handle(restore_from=restore_from)
            restore_from.startswith.assert_called_once_with("s3://")
예제 #5
0
    def from_address(cls, s3_address):
        info = urlparse.urlparse(s3_address)
        if info.scheme != "s3":
            raise FailedBackup(
                "Trying to restore from a non s3 address ({0})".format(
                    s3_address))

        key_name = info.path
        bucket_location = info.netloc.split(":")[0]

        tmp_file = None
        try:
            tmp_file = tempfile.NamedTemporaryFile(delete=False).name
            with BackupStorage(bucket_location) as storage:
                storage.get_from_s3(key_name, storage.bucket, tmp_file)
                yield tmp_file
        finally:
            if tmp_file and os.path.exists(tmp_file):
                os.remove(tmp_file)
예제 #6
0
    def validate_destination(self):
        """Make sure our s3 bucket exists"""
        if not self.bucket_location:
            self.bucket_location = settings.BACKUP_S3_BUCKET

        if self.bucket_location:
            log.info("Connecting to aws")
            conn = S3Connection()

            log.info("Seeing if your bucket exists ({0})".format(
                self.bucket_location))
            try:
                self.bucket = conn.get_bucket(self.bucket_location)
                self.has_storage = True
            except boto.exception.S3ResponseError, error:
                if error.status == 404:
                    raise FailedBackup(
                        "Please first create the s3 bucket where you want your backups to go ({0})"
                        .format(self.bucket_location))
                raise