<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: capistrano code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 24 Jul 2008 04:22:18 GMT</pubDate>
    <description>DZone Snippets: capistrano code</description>
    <item>
      <title>Push your public key to a server</title>
      <link>http://snippets.dzone.com/posts/show/5730</link>
      <description>If you've already created your ssh keys locally do this to push the public key to a server so you won't have to login to the server everytime you ssh or cap deploy. &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt; cat .ssh/id_rsa.pub | ssh deploy@myserver.com "cat &gt;&gt; .ssh/authorized_keys2"&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 07 Jul 2008 11:42:26 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5730</guid>
      <author>humblefrog (Steve)</author>
    </item>
    <item>
      <title>Deploying on a passenger / mod_rails host with capistrano</title>
      <link>http://snippets.dzone.com/posts/show/5466</link>
      <description>// add this to deploy.rb&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;namespace :mod_rails do&lt;br /&gt;  desc &lt;&lt;-DESC&lt;br /&gt;  Restart the application altering tmp/restart.txt for mod_rails.&lt;br /&gt;  DESC&lt;br /&gt;  task :restart, :roles =&gt; :app do&lt;br /&gt;    run "touch  #{release_path}/tmp/restart.txt"&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;namespace :deploy do&lt;br /&gt;  %w(start restart).each { |name| task name, :roles =&gt; :app do mod_rails.restart end }&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 06 May 2008 05:25:13 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5466</guid>
      <author>jerome ()</author>
    </item>
    <item>
      <title>Rails Environment Specific Capistrano Includes/Overrides</title>
      <link>http://snippets.dzone.com/posts/show/4950</link>
      <description>This cap technique allows you to include environment specific tasks and namespaces as well as override anything in the master deploy.rb. Its rather simplistic.&lt;br /&gt;&lt;br /&gt;Add your environment specific cap scripts into RAILS_ROOT/config/deployments/.&lt;br /&gt;&lt;br /&gt;Example command-line usage:&lt;br /&gt;&lt;br /&gt;# RAILS_ENV=production cap deploy&lt;br /&gt;&lt;br /&gt;...or...&lt;br /&gt;&lt;br /&gt;# cap deploy rails_env=beta&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# Example needed variables at the top of your deploy.rb&lt;br /&gt;&lt;br /&gt;set :default_env,  'beta'&lt;br /&gt;set :rails_env,     ENV['rails_env'] || ENV['RAILS_ENV'] || default_env&lt;br /&gt;set :extra_deploys, 'config/deployments/'&lt;br /&gt;&lt;br /&gt;...&lt;br /&gt;&lt;br /&gt;# Now add this to the bottom of your deploy.rb, last thing to load.&lt;br /&gt;&lt;br /&gt;if extra_deploys &amp;&amp; File.exists?(extra_deploys+rails_env+".rb")&lt;br /&gt;  puts "Loaded #{extra_deploys+rails_env}.rb" if load extra_deploys+rails_env&lt;br /&gt;else&lt;br /&gt;  puts "Could not find #{extra_deploys+rails_env}.rb"&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;You may change your default_env or any other piece to fit into your deployments. The secret is "load" method call burried in that latter code portion above. The reason you load at the end of your deploy.rb is so you can override anything defined above that call in your environment specific scripts.</description>
      <pubDate>Sat, 05 Jan 2008 19:21:40 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4950</guid>
      <author>justinwr (Justin R)</author>
    </item>
    <item>
      <title>Upgrade rubygems and/or specific gems themselves via capistrano</title>
      <link>http://snippets.dzone.com/posts/show/4905</link>
      <description>When a latest RubyGems is released (e.g. 0.9.5 recently) or Rails (e.g. 2.0.2) you might want to push those upgrades to all your production machines.&lt;br /&gt;&lt;br /&gt;Add these two tasks to your deploy.rb capistrano file (add namespaces for cap2.0 if you like)&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  desc "Updates RubyGems version"&lt;br /&gt;  task :gem_update_system do&lt;br /&gt;    sudo "gem update --system"&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;    &lt;br /&gt;  desc "Install a RubyGem from remote source"&lt;br /&gt;  task :gem_install do&lt;br /&gt;    puts "USAGE: GEM=gemname cap gems_install" and next unless ENV['GEM']&lt;br /&gt;    sudo "gem install #{ENV['GEM']} --no-ri --no-rdoc"&lt;br /&gt;  end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;To upgrade rubygems and rails, for example:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;cap gem_update_system&lt;br /&gt;GEM=rails cap gem_install&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Wed, 19 Dec 2007 22:10:32 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4905</guid>
      <author>nicwilliams (Dr Nic Williams)</author>
    </item>
    <item>
      <title>Mongrel and Apache fun with Capistrano 2.0</title>
      <link>http://snippets.dzone.com/posts/show/4510</link>
      <description>I got the Mongrel recipes from somewhere else -- I sadly don't remember where -- and modified them a bit.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;namespace :deploy do&lt;br /&gt;  namespace :mongrel do&lt;br /&gt;    [ :stop, :start, :restart ].each do |t|&lt;br /&gt;      desc "#{t.to_s.capitalize} the mongrel appserver"&lt;br /&gt;      task t, :roles =&gt; :app do&lt;br /&gt;        run "mongrel_rails cluster::#{t.to_s} --clean -C #{mongrel_conf}"&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  namespace :apache do&lt;br /&gt;    desc "Start Apache"&lt;br /&gt;    task :start, :roles =&gt; :web do&lt;br /&gt;      sudo "/etc/init.d/httpd start &gt; /dev/null"&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;    desc "Stop Apache"&lt;br /&gt;    task :stop, :roles =&gt; :web do&lt;br /&gt;      sudo "/etc/init.d/httpd stop &gt; /dev/null"&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;    desc "Restart Apache"&lt;br /&gt;    task :restart, :roles =&gt; :web do&lt;br /&gt;      sudo "/etc/init.d/httpd restart &gt; /dev/null"&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  desc "Custom restart task for mongrel cluster"&lt;br /&gt;  task :restart do&lt;br /&gt;    deploy.mongrel.restart&lt;br /&gt;    deploy.apache.restart&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  desc "Custom start task for mongrel cluster"&lt;br /&gt;  task :start, :roles =&gt; :app do&lt;br /&gt;    deploy.mongrel.start&lt;br /&gt;    deploy.apache.start&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  desc "Custom stop task for mongrel cluster"&lt;br /&gt;  task :stop, :roles =&gt; :app do&lt;br /&gt;    deploy.apache.stop&lt;br /&gt;    deploy.mongrel.stop&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;end&lt;/code&gt;</description>
      <pubDate>Fri, 07 Sep 2007 16:31:34 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4510</guid>
      <author>hmans (Hendrik Mans)</author>
    </item>
    <item>
      <title>Dump postgres production data into development database</title>
      <link>http://snippets.dzone.com/posts/show/3958</link>
      <description>When bug requests come in, its great to grab a copy of the current production (or system test env) data and sync it into your development database.&lt;br /&gt;&lt;br /&gt;Here's a capistrano recipe for postgresql:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;desc "Dumps target database into development db"&lt;br /&gt;task :sync_db do&lt;br /&gt;  env   = ENV['RAILS_ENV'] || ENV['DB'] || 'production'&lt;br /&gt;  file  = "#{application}.sql.bz2"&lt;br /&gt;  remote_file = "#{shared}/log/#{file}"&lt;br /&gt;  run "pg_dump --clean --no-owner --no-privileges -U#{db_user} -h#{db_host} #{db_name}_#{env} | bzip2 &gt; #{file}" do |ch, stream, out|&lt;br /&gt;    ch.send_data "#{db_password}\n" if out =~ /^Password:/&lt;br /&gt;    puts out&lt;br /&gt;  end&lt;br /&gt;  puts rsync = "rsync #{user}@#{domain}:#{file} tmp"&lt;br /&gt;  `#{rsync}`&lt;br /&gt;  puts depackage = "bzcat tmp/#{file} | psql #{local_db_dev}"&lt;br /&gt;  `#{depackage}`&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 06 May 2007 09:00:17 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3958</guid>
      <author>nicwilliams (Dr Nic Williams)</author>
    </item>
    <item>
      <title>Capistrano logs</title>
      <link>http://snippets.dzone.com/posts/show/3794</link>
      <description>To get the last n lines from a remote log file, use this task&lt;br /&gt;&lt;br /&gt;Usage: cap log [-s lines=100] [-s rails_env=production]&lt;br /&gt;So, "cap log" returns 100 lines from production.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;desc "Returns last lines of log file. Usage: cap log [-s lines=100] [-s rails_env=production]"&lt;br /&gt;task :log do&lt;br /&gt;  lines     = configuration.variables[:lines] || 100&lt;br /&gt;  rails_env = configuration.variables[:rails_env] || 'production'&lt;br /&gt;  run "tail -n #{lines} #{app_dir}/log/#{rails_env}.log" do |ch, stream, out|&lt;br /&gt;    puts out&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 10 Apr 2007 20:09:05 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3794</guid>
      <author>nicwilliams (Dr Nic Williams)</author>
    </item>
    <item>
      <title>Capistrano : apply local patches when deploying from an external source code repository</title>
      <link>http://snippets.dzone.com/posts/show/3147</link>
      <description>I've submitted patches to a couple rails apps, and want to run off of their SCM's trunk code, but with my local patches applied. These Capistrano tasks will take any files matching &lt;pre&gt;patches/*.diff&lt;/pre&gt; in your local directory, and apply them before restarting your app.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;task :after_setup do&lt;br /&gt;  patches_setup&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;task :after_update_code do&lt;br /&gt;  send_and_apply_patches&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;task :patches_setup do&lt;br /&gt;  run "mkdir -p #{deploy_to}/#{shared_dir}/patches" &lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;task :send_and_apply_patches do&lt;br /&gt;  Dir[File.join(File.dirname(__FILE__), '../patches/*.diff')].sort.each do |patch|&lt;br /&gt;    puts "sending #{File.basename(patch)}"&lt;br /&gt;    put(File.read(patch),&lt;br /&gt;       "#{deploy_to}/#{shared_dir}/patches/#{File.basename(patch)}",&lt;br /&gt;       :mode =&gt; 0777)&lt;br /&gt;    puts "applying #{File.basename(patch)}"&lt;br /&gt;    run "cd #{release_path}; patch -p0 &lt; #{deploy_to}/#{shared_dir}/patches/#{File.basename(patch)}"&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 13 Dec 2006 20:51:35 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3147</guid>
      <author>jnewland (Jesse Newland)</author>
    </item>
    <item>
      <title>Deploying to different locations with Capistrano</title>
      <link>http://snippets.dzone.com/posts/show/3094</link>
      <description>// description of your code here&lt;br /&gt;In your deploy.rb file:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;if ENV["SERVER"] &amp;&amp; ENV["SERVER"] == "production"&lt;br /&gt;  set :primary_server, "production.com"&lt;br /&gt;  set :user, "ben"&lt;br /&gt;elsif ENV["SERVER"] &amp;&amp; ENV["SERVER"] == "staging"&lt;br /&gt;  set :primary_server, "staging.local"&lt;br /&gt;  set :user, "ben"&lt;br /&gt;else&lt;br /&gt; ...&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;role :web, primary_server&lt;br /&gt;role :app, primary_server&lt;br /&gt;role :db,  primary_server, :primary =&gt; true&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;The you say:&lt;br /&gt;rake deploy SERVER=staging</description>
      <pubDate>Wed, 06 Dec 2006 06:34:08 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3094</guid>
      <author>bmarini (Ben Marini)</author>
    </item>
    <item>
      <title>Capistrano Task Library for Deploying Subversion Tags</title>
      <link>http://snippets.dzone.com/posts/show/2876</link>
      <description>=begin&lt;br /&gt; &lt;br /&gt;  Capistrano Task Library &gt; capistrano_svn_tags.rb&lt;br /&gt;  Created by Anil Bawa &lt;anil@quotesque.net&gt;&lt;br /&gt;  Enhanced by Sava Chankov &lt;sava.chankov@gmail.com&gt;&lt;br /&gt;  Distributed Under the BSD license&lt;br /&gt;  &lt;br /&gt;  + Task library to provide subversion tag support to Capistrano.&lt;br /&gt;  &lt;br /&gt;  - place this file somewhere in your rails app (i suggest lib/tasks)&lt;br /&gt;  - add a 'require' statement in config/deploy.rb to load this file in&lt;br /&gt;  - run 'rake remote:deploy_tag [tagname]' to deploy a tag (you can provide your own custom implementation of the task - edit it below)&lt;br /&gt;  - tag rollbacks will work fine.&lt;br /&gt;  - this library overrides a capistrano core method, so if you wish to use the default release type (latest revision, deployed as timestamp) then comment out the require.&lt;br /&gt;  &lt;br /&gt;=end&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;Capistrano.configuration(:must_exist).load do&lt;br /&gt;&lt;br /&gt;  set :svn_tag_dir, 'tags' # new config var to denote the tag sub-directory in the svn repository  &lt;br /&gt;  &lt;br /&gt;  desc &lt;&lt;-DESC&lt;br /&gt;  Update all servers with the provided tag of the source code. All this does&lt;br /&gt;  is do a checkout of the provided svn tag (as defined by the svn module).&lt;br /&gt;  DESC&lt;br /&gt;  task :update_tag, :roles =&gt; [:app, :db, :web] do&lt;br /&gt;  &lt;br /&gt;    puts "  * deploying tag #{release}"&lt;br /&gt;&lt;br /&gt;    on_rollback { delete release_path, :recursive =&gt; true }&lt;br /&gt;    source.checkout_tag(self)&lt;br /&gt;    run &lt;&lt;-CMD&lt;br /&gt;      rm -rf #{release_path}/log #{release_path}/public/system &amp;&amp;&lt;br /&gt;      ln -nfs #{shared_path}/log #{release_path}/log &amp;&amp;&lt;br /&gt;      ln -nfs #{shared_path}/system #{release_path}/public/system&lt;br /&gt;    CMD&lt;br /&gt;&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  desc &lt;&lt;-DESC&lt;br /&gt;  Deploy svn tag, reset symlink and restart server.&lt;br /&gt;  DESC&lt;br /&gt;  task :deploy_tag, :roles =&gt; [:app, :db, :web] do&lt;br /&gt;    set_release_tag&lt;br /&gt;&lt;br /&gt;    transaction do&lt;br /&gt;      update_tag    &lt;br /&gt;      symlink&lt;br /&gt;      compile_mo&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  desc &lt;&lt;-DESC&lt;br /&gt;  Deploy svn tag, migrate, compile .po files to .mo and reset symlink and restart server.&lt;br /&gt;  Note that everything is done in a transaction&lt;br /&gt;  DESC&lt;br /&gt;  task :deploy_tag_with_migrations, :roles =&gt; [:app, :db, :web] do&lt;br /&gt;    set_release_tag&lt;br /&gt;&lt;br /&gt;    transaction do&lt;br /&gt;      update_tag&lt;br /&gt;      symlink&lt;br /&gt;      migrate&lt;br /&gt;      compile_mo    &lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def set_release_tag&lt;br /&gt;    set :release, ENV['TAG']&lt;br /&gt;    unless release&lt;br /&gt;      puts "  * no tag specified, assuming latest"&lt;br /&gt;      set(:release) {source.latest_tag}&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;module Capistrano&lt;br /&gt;&lt;br /&gt;  #override a configuration object method to return the correct release_path for the tag.&lt;br /&gt;  class Configuration&lt;br /&gt;  &lt;br /&gt;    # Return the full path to the named release. If a release is not specified,&lt;br /&gt;    # the provided tag name (passed as a cli parameter) is used.&lt;br /&gt;    def tag_path(tag_name = release)&lt;br /&gt;      File.join(releases_path, tag_name)&lt;br /&gt;    end&lt;br /&gt;    alias_method :release_path, :tag_path&lt;br /&gt;    &lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  # add a method to the subversion object to checkout tags&lt;br /&gt;  module SCM&lt;br /&gt;    class Subversion&lt;br /&gt;      &lt;br /&gt;      def latest_tag&lt;br /&gt;        configuration.logger.debug "querying latest tag ..." unless @latest_revision&lt;br /&gt;        tags_dir = configuration.repository + '/' + configuration.svn_tag_dir + '/'&lt;br /&gt;        @latest_revision = svn_ls(tags_dir).split("\n").last.chomp('/')&lt;br /&gt;      end&lt;br /&gt;      alias_method :latest_revision, :latest_tag&lt;br /&gt;      &lt;br /&gt;      # checkout a tag from a pre-configured svn tag dir.&lt;br /&gt;      def checkout_tag(actor)&lt;br /&gt;        op = configuration[:checkout] || "co"&lt;br /&gt;        username = configuration[:svn_username] ? "--username #{configuration[:svn_username]}" : ""&lt;br /&gt;        command = "#{svn} #{op} #{username} -q #{configuration.repository}/#{configuration.svn_tag_dir}/#{configuration.release} #{actor.release_path} &amp;&amp;"        &lt;br /&gt;        run_checkout(actor, command, &amp;svn_stream_handler(actor))         &lt;br /&gt;      end&lt;br /&gt;      &lt;br /&gt;    private&lt;br /&gt;      def svn_ls(path)&lt;br /&gt;        `svn ls #{path}`&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 23 Oct 2006 15:27:21 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2876</guid>
      <author>sava.chankov (Sava Chankov)</author>
    </item>
  </channel>
</rss>
