コード例 #1
0
def prepare_s3_default_test_bucket():
    # Check credentials are present: this procedure should not be
    # called otherwise.
    if no_real_s3_credentials():
        assert False

    bucket_name = bucket_name_mangle('waletdefwuy')

    creds = s3.Credentials(os.getenv('AWS_ACCESS_KEY_ID'),
                           os.getenv('AWS_SECRET_ACCESS_KEY'),
                           os.getenv('AWS_SECURITY_TOKEN'))

    cinfo = calling_format.from_store_name(bucket_name, region='us-west-1')
    conn = cinfo.connect(creds)

    def _clean():
        bucket = conn.get_bucket(bucket_name)
        bucket.delete_keys(key.name for key in bucket.list())

    try:
        conn.create_bucket(bucket_name, location=Location.USWest)
    except boto.exception.S3CreateError as e:
        if e.status == 409:
            # Conflict: bucket already present.  Re-use it, but
            # clean it out first.
            _clean()
        else:
            raise
    else:
        # Success
        _clean()

    return bucket_name
コード例 #2
0
ファイル: cmd.py プロジェクト: zurikus/wal-e
def s3_explicit_creds(args):
    access_key = args.aws_access_key_id or os.getenv('AWS_ACCESS_KEY_ID')
    if access_key is None:
        raise UserException(
            msg='AWS Access Key credential is required but not provided',
            hint=(_config_hint_generate('aws-access-key-id', True)))

    secret_key = os.getenv('AWS_SECRET_ACCESS_KEY')
    if secret_key is None:
        raise UserException(
            msg='AWS Secret Key credential is required but not provided',
            hint=_config_hint_generate('aws-secret-access-key', False))

    security_token = os.getenv('AWS_SECURITY_TOKEN')

    from wal_e.blobstore import s3

    return s3.Credentials(access_key, secret_key, security_token)
コード例 #3
0
def configure_backup_cxt(args):
    # Try to find some WAL-E prefix to store data in.
    prefix = (args.s3_prefix or args.wabs_prefix
              or os.getenv('WALE_S3_PREFIX') or os.getenv('WALE_WABS_PREFIX')
              or os.getenv('WALE_SWIFT_PREFIX'))

    if prefix is None:
        raise UserException(
            msg='no storage prefix defined',
            hint=(
                'Either set one of the --wabs-prefix or --s3-prefix options or'
                ' define one of the WALE_WABS_PREFIX, WALE_S3_PREFIX, or '
                'WALE_SWIFT_PREFIX environment variables.'
            )
        )

    store = storage.StorageLayout(prefix)

    # GPG can be optionally layered atop of every backend, so a common
    # code path suffices.
    gpg_key_id = args.gpg_key_id or os.getenv('WALE_GPG_KEY_ID')
    if gpg_key_id is not None:
        external_program_check([GPG_BIN])

    # Define some hint-text generator to help the user with consistent
    # language between storage backends when possible.
    def _opt_env_hint(optname):
        option = '--' + optname.lower()
        env = optname.replace('-', '_').upper()

        return ('Pass "{0}" or set the environment variable "{1}".'
                .format(option, env))

    def _env_hint(optname):
        env = optname.replace('-', '_').upper()
        return 'Set the environment variable {0}.'.format(env)

    # Enumeration of reading in configuration for all supported
    # backend data stores, yielding value adhering to the
    # 'operator.Backup' protocol.
    if store.is_s3:
        access_key = args.aws_access_key_id or os.getenv('AWS_ACCESS_KEY_ID')
        if access_key is None:
            raise UserException(
                msg='AWS Access Key credential is required but not provided',
                hint=(_opt_env_hint('aws-access-key-id')))

        secret_key = os.getenv('AWS_SECRET_ACCESS_KEY')
        if secret_key is None:
            raise UserException(
                msg='AWS Secret Key credential is required but not provided',
                hint=_env_hint('aws-secret-access-key'))

        security_token = os.getenv('AWS_SECURITY_TOKEN')

        from wal_e.blobstore import s3
        from wal_e.operator.s3_operator import S3Backup

        creds = s3.Credentials(access_key, secret_key, security_token)

        return S3Backup(store, creds, gpg_key_id)
    elif store.is_wabs:
        account_name = args.wabs_account_name or os.getenv('WABS_ACCOUNT_NAME')
        if account_name is None:
            raise UserException(
                msg='WABS account name is undefined',
                hint=_opt_env_hint('wabs-account-name'))

        access_key = os.getenv('WABS_ACCESS_KEY')
        if access_key is None:
            raise UserException(
                msg='WABS access key credential is required but not provided',
                hint=_env_hint('wabs-access-key'))

        from wal_e.blobstore import wabs
        from wal_e.operator.wabs_operator import WABSBackup

        creds = wabs.Credentials(account_name, access_key)

        return WABSBackup(store, creds, gpg_key_id)
    elif store.is_swift:
        from wal_e.blobstore import swift
        from wal_e.operator.swift_operator import SwiftBackup

        creds = swift.Credentials(
            os.getenv('SWIFT_AUTHURL'),
            os.getenv('SWIFT_USER'),
            os.getenv('SWIFT_PASSWORD'),
            os.getenv('SWIFT_TENANT'),
            os.getenv('SWIFT_REGION'),
            os.getenv('SWIFT_ENDPOINT_TYPE', 'publicURL'),
        )
        return SwiftBackup(store, creds, gpg_key_id)
    else:
        raise UserCritical(
            msg='no unsupported blob stores should get here',
            hint='Report a bug.')