def tarbell_publish(command, args):
    """
    Publish to s3.
    """
    with ensure_settings(command, args) as settings, ensure_project(command, args) as site:
        bucket_name = list_get(args, 0, "staging")

        try:
            bucket_url = S3Url(site.project.S3_BUCKETS[bucket_name])
        except KeyError:
            show_error(
                "\nThere's no bucket configuration called '{0}' in "
                "tarbell_config.py.".format(colored.yellow(bucket_name)))
            sys.exit(1)

        extra_context = {
            "ROOT_URL": bucket_url,
            "S3_BUCKET": bucket_url.root,
            "BUCKET_NAME": bucket_name,
        }

        tempdir = "{0}/".format(tarbell_generate(command,
            args, extra_context=extra_context, skip_args=True, quiet=True))
        try:
            title = site.project.DEFAULT_CONTEXT.get("title", "")
            puts("\nDeploying {0} to {1} ({2})\n".format(
                colored.yellow(title),
                colored.red(bucket_name),
                colored.green(bucket_url)
            ))

            # Get creds
            if settings.config:
                # If settings has a config section, use it
                kwargs = settings.config['s3_credentials'].get(bucket_url.root)
                if not kwargs:
                    kwargs = {
                        'access_key_id': settings.config.get('default_s3_access_key_id'),
                        'secret_access_key': settings.config.get('default_s3_secret_access_key'),
                    }
                    puts("Using default bucket credentials")
                else:
                    puts("Using custom bucket configuration for {0}".format(bucket_url.root))
            else:
                # If no configuration exists, read from environment variables if possible
                puts("Attemping to use AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY")
                kwargs = {
                    'access_key_id': os.environ["AWS_ACCESS_KEY_ID"],
                    'secret_access_key': os.environ["AWS_SECRET_ACCESS_KEY"],
                }

            if not kwargs.get('access_key_id') and not kwargs.get('secret_access_key'):
                show_error('S3 access is not configured. Set up S3 with {0} to publish.'
                            .format(colored.green('tarbell configure')))
                sys.exit()

            s3 = S3Sync(tempdir, bucket_url, **kwargs)
            s3.deploy_to_s3()
            site.call_hook("publish", site, s3)

            puts("\nIf you have website hosting enabled, you can see your project at:")
            puts(colored.green("http://{0}\n".format(bucket_url)))
        except KeyboardInterrupt:
            show_error("ctrl-c pressed, bailing out!")
        finally:
            _delete_dir(tempdir)
def tarbell_publish ( command , args ) : with ensure_settings ( command , args ) as settings , ensure_project ( command , args ) as site : bucket_name = list_get ( args , 0 , "staging" ) try : bucket_url = S3Url ( site . project . S3_BUCKETS [ bucket_name ] ) except KeyError : show_error ( "\nThere's no bucket configuration called '{0}' in " "tarbell_config.py." . format ( colored . yellow ( bucket_name ) ) ) sys . exit ( 1 ) extra_context = { "ROOT_URL" : bucket_url , "S3_BUCKET" : bucket_url . root , "BUCKET_NAME" : bucket_name , } tempdir = "{0}/" . format ( tarbell_generate ( command , args , extra_context = extra_context , skip_args = True , quiet = True ) ) try : title = site . project . DEFAULT_CONTEXT . get ( "title" , "" ) puts ( "\nDeploying {0} to {1} ({2})\n" . format ( colored . yellow ( title ) , colored . red ( bucket_name ) , colored . green ( bucket_url ) ) ) if settings . config : kwargs = settings . config [ 's3_credentials' ] . get ( bucket_url . root ) if not kwargs : kwargs = { 'access_key_id' : settings . config . get ( 'default_s3_access_key_id' ) , 'secret_access_key' : settings . config . get ( 'default_s3_secret_access_key' ) , } puts ( "Using default bucket credentials" ) else : puts ( "Using custom bucket configuration for {0}" . format ( bucket_url . root ) ) else : puts ( "Attemping to use AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY" ) kwargs = { 'access_key_id' : os . environ [ "AWS_ACCESS_KEY_ID" ] , 'secret_access_key' : os . environ [ "AWS_SECRET_ACCESS_KEY" ] , } if not kwargs . get ( 'access_key_id' ) and not kwargs . get ( 'secret_access_key' ) : show_error ( 'S3 access is not configured. Set up S3 with {0} to publish.' . format ( colored . green ( 'tarbell configure' ) ) ) sys . exit ( ) s3 = S3Sync ( tempdir , bucket_url , ** kwargs ) s3 . deploy_to_s3 ( ) site . call_hook ( "publish" , site , s3 ) puts ( "\nIf you have website hosting enabled, you can see your project at:" ) puts ( colored . green ( "http://{0}\n" . format ( bucket_url ) ) ) except KeyboardInterrupt : show_error ( "ctrl-c pressed, bailing out!" ) finally : _delete_dir ( tempdir )
Publish to s3.