Sha256: 966c4575e020ec40c5b85a3ee3df064f1c957a1e788ce1a1a67257a590cea184

Contents?: true

Size: 1.93 KB

Versions: 1

Compression:

Stored size: 1.93 KB

Contents

require 'aws/s3'
require 'mime/types'

unless Capistrano::Configuration.respond_to?(:instance)
  abort "capistrano-s3 requires Capistrano >= 2."
end

Capistrano::Configuration.instance(true).load do
  def _cset(name, *args, &block)
    set(name, *args, &block) if !exists?(name)
  end
  
  _cset :deployment_path, Dir.pwd.gsub("\n", "") + "/public"
  
  def base_file_path(file)
    file.gsub(deployment_path, "")
  end
  
  def files
    Dir.glob("#{deployment_path}/**/*")
  end
  
  # Establishes the connection to Amazon S3
  def establish_connection!
    # Send logging to STDOUT
    AWS.config(:logger => Logger.new(STDOUT))

    AWS::S3.new(
      :access_key_id => access_key_id,
      :secret_access_key => secret_access_key
    )
  end
  
  # Deployment recipes
  namespace :deploy do
    namespace :s3 do      
      desc "Empties bucket of all files. Caution when using this command, as it cannot be undone!"
      task :empty do
        _s3 = establish_connection!
        _s3.buckets[bucket].clear!
      end

      desc "Upload files to the bucket in the current state"
      task :upload_files do
        _s3 = establish_connection!

        files.each do |file|
          if !File.directory?(file)
            path = base_file_path(file)
            path.gsub!(/^\//, "") # Remove preceding slash for S3

            contents = case File.extname(path)
            open(file)

            types = MIME::Types.type_for(File.basename(file))
            if types.empty?
              options = {
                :acl => :public_read
              }
            else
              options = {
                :acl => :public_read,
                :content_type => types[0]
              }
            end
            options.merge!(bucket_write_options)
            _s3.buckets[bucket].objects[path].write(contents, options)
          end
        end
      end
    end
    
    task :update do
      s3.upload_files
    end

    task :restart do; end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
capistrano-s3-0.2.1 lib/capistrano-s3.rb