From 959e06a0fc1dd3b85aeb5d0d005eff2a02eb547e Mon Sep 17 00:00:00 2001
From: Jamis Buck <jamis@37signals.com>
Date: Sat, 27 Aug 2005 14:42:58 +0000
Subject: SwitchTower: log checkouts to a "revisions.log" file

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2060 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
---
 switchtower/CHANGELOG                         |  2 ++
 switchtower/lib/switchtower/scm/base.rb       | 21 +++++++++++++++++++--
 switchtower/lib/switchtower/scm/cvs.rb        | 11 +++++------
 switchtower/lib/switchtower/scm/darcs.rb      |  8 +-------
 switchtower/lib/switchtower/scm/subversion.rb | 13 ++++---------
 switchtower/test/utils.rb                     |  5 +++++
 6 files changed, 36 insertions(+), 24 deletions(-)

(limited to 'switchtower')

diff --git a/switchtower/CHANGELOG b/switchtower/CHANGELOG
index 27f69cef2b..94f368d674 100644
--- a/switchtower/CHANGELOG
+++ b/switchtower/CHANGELOG
@@ -1,5 +1,7 @@
 *SVN*
 
+* Log checkouts to a "revisions.log" file
+
 * Changed behavior of checkout to use the timestamp as the release name, instead of the revision number
 
 * Added CVS module (very very experimental!)
diff --git a/switchtower/lib/switchtower/scm/base.rb b/switchtower/lib/switchtower/scm/base.rb
index 4e079d2758..4e325aa155 100644
--- a/switchtower/lib/switchtower/scm/base.rb
+++ b/switchtower/lib/switchtower/scm/base.rb
@@ -9,9 +9,26 @@ module SwitchTower
         @configuration = configuration
       end
 
-      def checkout(actor)
-        raise NotImplementedError, "subclasses must implement checkout"
+      def latest_revision
+        nil
       end
+
+      private
+
+        def run_checkout(actor, guts, &block)
+          log = "#{configuration.deploy_to}/revisions.log"
+          directory = File.basename(configuration.release_path)
+
+          command = <<-STR
+            if [[ ! -d #{configuration.release_path} ]]; then
+              #{guts}
+              echo `date +"%Y-%m-%d %H:%M:%S"` $USER #{latest_revision} #{directory} >> #{log};
+              chmod 666 #{log};
+            fi
+          STR
+
+          actor.run(command, &block)
+        end
     end
 
   end
diff --git a/switchtower/lib/switchtower/scm/cvs.rb b/switchtower/lib/switchtower/scm/cvs.rb
index db3eae9261..794a3b5212 100644
--- a/switchtower/lib/switchtower/scm/cvs.rb
+++ b/switchtower/lib/switchtower/scm/cvs.rb
@@ -44,12 +44,11 @@ module SwitchTower
         cvs_rsh = configuration[:cvs_rsh] || ENV['CVS_RSH'] || "ssh"
 
         command = <<-CMD
-          if [[ ! -d #{actor.release_path} ]]; then
-            cd #{configuration.releases_path};
-            CVS_RSH="#{cvs_rsh}" #{cvs} -d #{configuration.repository} -Q co -D "#{latest_revision}" -d #{File.basename(actor.release_path)} #{actor.application};
-          fi
+          cd #{configuration.releases_path};
+          CVS_RSH="#{cvs_rsh}" #{cvs} -d #{configuration.repository} -Q co -D "#{latest_revision}" -d #{File.basename(actor.release_path)} #{actor.application};
         CMD
-        actor.run(command) do |ch, stream, out|
+
+        run_checkout(actor, command) do |ch, stream, out|
           prefix = "#{stream} :: #{ch[:host]}"
           actor.logger.info out, prefix
           if out =~ %r{password:}
@@ -64,7 +63,7 @@ module SwitchTower
       end
 
       private
-      
+
         def cvs_log(path)
           `cd #{path || "."} && cvs -q log -N -rHEAD`
         end
diff --git a/switchtower/lib/switchtower/scm/darcs.rb b/switchtower/lib/switchtower/scm/darcs.rb
index acaa006951..ebc2f02549 100644
--- a/switchtower/lib/switchtower/scm/darcs.rb
+++ b/switchtower/lib/switchtower/scm/darcs.rb
@@ -18,13 +18,7 @@ module SwitchTower
       # revision. Uses the given actor instance to execute the command.
       def checkout(actor)
         darcs = configuration[:darcs] ? configuration[:darcs] : "darcs"
-
-        command = <<-CMD
-          if [[ ! -d #{actor.release_path} ]]; then
-            #{darcs} get -q --set-scripts-executable #{configuration.repository} #{actor.release_path};
-          fi
-        CMD
-        actor.run(command)
+        run_checkout(actor, "#{darcs} get -q --set-scripts-executable #{configuration.repository} #{actor.release_path};")
       end
     end
 
diff --git a/switchtower/lib/switchtower/scm/subversion.rb b/switchtower/lib/switchtower/scm/subversion.rb
index 368c965dec..7656cfe3f8 100644
--- a/switchtower/lib/switchtower/scm/subversion.rb
+++ b/switchtower/lib/switchtower/scm/subversion.rb
@@ -42,14 +42,9 @@ module SwitchTower
       def checkout(actor)
         svn = configuration[:svn] ? configuration[:svn] : "svn"
 
-        command = <<-CMD
-          if [[ -d #{actor.release_path} ]]; then
-            #{svn} up -q -r#{latest_revision} #{actor.release_path};
-          else
-            #{svn} co -q -r#{latest_revision} #{configuration.repository} #{actor.release_path};
-          fi
-        CMD
-        actor.run(command) do |ch, stream, out|
+        command = "#{svn} co -q -r#{latest_revision} #{configuration.repository} #{actor.release_path};"
+
+        run_checkout(actor, command) do |ch, stream, out|
           prefix = "#{stream} :: #{ch[:host]}"
           actor.logger.info out, prefix
           if out =~ /^Password.*:/
@@ -72,7 +67,7 @@ module SwitchTower
       end
 
       private
-      
+
         def svn_log(path)
           `svn log -q -rhead #{path}`
         end
diff --git a/switchtower/test/utils.rb b/switchtower/test/utils.rb
index b483f0c005..4ddfbdbad1 100644
--- a/switchtower/test/utils.rb
+++ b/switchtower/test/utils.rb
@@ -22,6 +22,11 @@ class MockLogger
 end
 
 class MockConfiguration < Hash
+  def initialize(*args)
+    super
+    self[:release_path] = "/path/to/releases/version"
+  end
+
   def logger
     @logger ||= MockLogger.new
   end
-- 
cgit v1.2.3