master
 1load File.expand_path("../../tasks/s3.rake", __FILE__)
 2
 3require "capistrano/scm"
 4
 5class Capistrano::S3 < Capistrano::SCM
 6  def s3(*args)
 7    args.unshift "--profile default"
 8    args.unshift :s3
 9    args.unshift :aws
10    context.execute(*args)
11  end
12
13  module DefaultStrategy
14    def test
15      test! " [ -f #{repo_path}/HEAD ] "
16    end
17
18    def check
19      s3 "ls #{bucket_name}"
20    end
21
22    def clone
23      context.execute("mkdir -p #{repo_path}")
24    end
25
26    def update
27      source = "s3://#{bucket_name}/#{rails_env}/#{build_revision}"
28      destination = "#{repo_path}/#{build_revision}"
29      s3 "cp #{source} #{destination}"
30    end
31
32    def release
33      context.execute("mkdir -p #{release_path}")
34      path = "#{repo_path}/#{build_revision}"
35      strip = "--strip-components=1"
36      context.execute("tar -xvzf #{path} #{strip} -C #{release_path}")
37    end
38
39    def bucket_name
40      fetch(:bucket_name)
41    end
42
43    def rails_env
44      fetch(:rails_env)
45    end
46
47    def build_revision
48      command = [
49        "s3 ls #{bucket_name}/#{rails_env}/",
50        "grep tar.gz",
51        "sort",
52        "tail -n1",
53        "awk '{ print $4 }'"
54      ]
55      context.capture(:aws, command.join(" | ")).strip
56    end
57  end
58end