Sha256: 40ffff6924c5f924105612ec3beabbfe916430d94fd120d8cb8d1b3f7bb85f69

Contents?: true

Size: 1.94 KB

Versions: 1

Compression:

Stored size: 1.94 KB

Contents

require 'fog'

module DeployS3
  class Main
    def initialize(options)
      config_path = options[:config_file] || "./.deploy"
      @config = options[:config] || YAML::load_file(config_path)

      raise "Missing s3: option in .deploy file" unless @config['s3']

      @path = @config['s3'].split("/")
      @bucket = @path.shift
      @options = options
    end

    def environment
      @options[:environment]
    end

    def save_sha
      directory = connection.directories.get(@bucket)
      file = directory.files.create(:key => key,
                                    :body => local_sha)
      @_remote_sha = nil
    end

    def run!
      if @config['before_hook']
        `#{@config['before_hook']}`
      end

      save_sha

      if @config['after_hook']
        `#{@config['after_hook']}`
      end
    end

    def connection
      @_connection ||= Fog::Storage.new(:provider => 'AWS')
    end

    def remote_sha
      return @_remote_sha if @_remote_sha

      directory = connection.directories.get(@bucket)
      file = directory.files.get(key)
      @_remote_sha = file.body if file
    end

    def filename
      [environment, 'sha'].join('.')
    end

    def key
      [@path, filename].join("/")
    end

    def up_to_date
      local_sha == remote_sha
    end

    def local_sha
      rev = @options[:rev] || @config[:branch] || 'head'
      execute("git rev-parse --verify --short #{rev}").chomp
    end

    def diff
      execute "git log --pretty=format:'    %h %<(20)%an %ar\t   %s' -10 #{remote_sha}..#{local_sha}"
    end

    def reverse_diff
      execute "git log --pretty=format:'    %h %<(20)%an %ar\t   %s' -10 #{local_sha}..#{remote_sha}"
    end

    def older_local_sha
      return false unless remote_sha
      execute("git merge-base --is-ancestor #{local_sha} #{remote_sha}") && $?.exitstatus == 0
    end

    def commit_count
      diff.split("\n").size
    end

  protected

    def execute(cmd)
      `#{cmd}`
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
deploy_s3-0.0.1 lib/deploy_s3/main.rb