Module: RobotArmy::GitDeployer

Defined in:
lib/robot-army-git-deploy/git_deployer.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/robot-army-git-deploy/git_deployer.rb', line 2

def self.included(base)
  base.const_set(:DEPLOY_COUNT, 5)

  base.class_eval do
    method_options :target_revision => :string

    desc "check", "Checks the deploy status"
    def check(opts={})
      update_server_refs

      run_pager

      say "Deployed Revisions"

      deployed_revisions.each do |host, revision|
        if revision
          commit = commit_from_revision_or_abort(revision)
          puts "%s: %s %s [%s]" % [
            host,
            color(commit.id_abbrev, :yellow),
            commit.message.to_a.first.chomp,
            commit.author.name]
        else
          puts "%s: %s %s" % [
            host,
            color('0000000', :yellow),
            "(no deployed revision)"]
        end
      end

      puts

      say "On Deck"

      if oldest_deployed_revision == target_revision
        puts "Deployed revision is up to date"
      elsif oldest_deployed_revision
        shortlog "#{oldest_deployed_revision}..#{target_revision}"
        diff "#{oldest_deployed_revision}..#{target_revision}"
      else
        shortlog target_revision, :root => true
        diff target_revision, :root => true
      end
    end

    desc "archive", "Write HEAD to a tgz file"
    def archive
      say "Archiving to #{archive_path}"
      %x{git archive --format=tar #{target_revision} | gzip >#{archive_path}}
    end

    desc "stage", "Stages the locally-generated archive on each host"
    def stage
      revision = repo.commits.first.id

      # create the destination directory
      sudo do
        FileUtils.mkdir_p(deploy_path)
        FileUtils.chown(user, group, deploy_path)
      end

      say "Staging #{app} into #{deploy_path}"
      cptemp(archive_path, :user => user) do |path|
        %x{tar -xvz -f #{path} -C #{deploy_path}}
        File.open(File.join(deploy_path, 'REVISION'), 'w') {|f| f << revision}
        path # just so that we don't try to return a File and generate a warning
      end
    end

  no_tasks do

    def install
      say "Installing #{app} into #{current_link}"
      sudo do
        FileUtils.rm_f(current_link)
        FileUtils.ln_sf(deploy_path, current_link)
      end
      update_server_refs(true)
    end

    def cleanup
      clean_temporary_files
      clean_old_revisions
    end

    def clean_temporary_files
      say "Cleaning up temporary files"
      FileUtils.rm_f("#{app}-archive.tar.gz")
    end

    def clean_old_revisions
      say "Cleaning up old revisions"
      deploy_count = self.class.const_get(:DEPLOY_COUNT)

      sudo do
        deploy_paths = Dir.glob(File.join(deploy_root, '*')).sort
        deploy_paths -= [current_link]
        FileUtils.rm_rf(deploy_paths.first(deploy_paths.size - deploy_count)) if deploy_paths.size > deploy_count
      end
    end

    desc "run", "Run a full deploy"
    def run
      archive
      stage
      install
      cleanup
    end

  end
  end
end