Module: FastGitDeploy::Rollback

Defined in:
lib/fast_git_deploy/recipes/fast_git_deploy/rollback.rb

Class Method Summary collapse

Class Method Details

.load_into(configuration) ⇒ Object



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
# File 'lib/fast_git_deploy/recipes/fast_git_deploy/rollback.rb', line 3

def self.load_into(configuration)
  configuration.load do
    namespace :deploy do
      namespace :rollback do
        desc "Rolls the app back one revision"
        task :code, :except => { :no_release => true } do
          current_revision  = capture("cat #{version_file}").gsub(/\r?\n?/, "")
          previous_revision = nil

          revision_log_data = capture("cat #{revision_log}").split(/\r?\n/)
          revisions = revision_log_data.map do |entry|
            entry.split(" ").last
          end

          revisions.reverse!

          revisions.each_with_index do |revision, index|
            if current_revision == revision
              # we have found the currently deployed revision
              # so scan the file backwards until a different revision
              # is found (removing duplicates - i.e.):
              #
              # 2010-04-21 15:22:59 deploy 2a285b0f600c7ed31b307390ad91c
              # 2010-04-22 09:58:28 deploy 53cff5db28116ecd5ad32d11ee6e1
              # 2010-04-22 10:02:41 deploy 53cff5db28116ecd5ad32d11ee6e1
              # 2010-04-26 08:18:39 deploy 5494dc2a00beb5350eff6be151987
              # 2010-04-27 09:10:06 deploy 5494dc2a00beb5350eff6be151987
              # 2010-04-27 11:49:29 deploy 5494dc2a00beb5350eff6be151987
              #
              # Rolling back from 5494dc should yield 53cff5db
              previous_revision = revisions[index..revision_log_data.length-1].detect do |rev|
                rev != current_revision
              end
            end
          end

          if previous_revision
            run [
              "cd #{current_path}",
              "#{scm_command} reset --hard #{previous_revision}"
            ].join(" && ")
          else
            raise(Capistrano::Error, "Couldn't find a revision previous to #{current_revision}")
          end
        end

        desc "Rolls back the app one revision, restarts mongrel, and writes the revision to the VERSION file (but not revisions.log)"
        task :default do
          transaction do
            rollback.code
            deploy.restart
            deploy.set_version_file
          end
        end
      end
    end
  end
end