summaryrefslogtreecommitdiff
path: root/lib/capistrano/s3.rb
blob: 218a13df0662e5d65609bed2d1a1a60beb046caf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
load File.expand_path("../../tasks/s3.rake", __FILE__)

require "capistrano/scm"

class Capistrano::S3 < Capistrano::SCM
  def s3(*args)
    args.unshift "--profile default"
    args.unshift :s3
    args.unshift :aws
    context.execute(*args)
  end

  module DefaultStrategy
    def test
      test! " [ -f #{repo_path}/HEAD ] "
    end

    def check
      s3 "ls #{bucket_name}"
    end

    def clone
      context.execute("mkdir -p #{repo_path}")
    end

    def update
      source = "s3://#{bucket_name}/#{rails_env}/#{build_revision}"
      destination = "#{repo_path}/#{build_revision}"
      s3 "cp #{source} #{destination}"
    end

    def release
      context.execute("mkdir -p #{release_path}")
      path = "#{repo_path}/#{build_revision}"
      strip = "--strip-components=1"
      context.execute("tar -xvzf #{path} #{strip} -C #{release_path}")
    end

    def bucket_name
      fetch(:bucket_name)
    end

    def rails_env
      fetch(:rails_env)
    end

    def build_revision
      command = [
        "s3 ls #{bucket_name}/#{rails_env}/",
        "grep tar.gz",
        "sort",
        "tail -n1",
        "awk '{ print $4 }'"
      ]
      context.capture(:aws, command.join(" | ")).strip
    end
  end
end