Push your public key to a server
1 2 cat .ssh/id_rsa.pub | ssh deploy@myserver.com "cat >> .ssh/authorized_keys2"
13480 users tagging and storing useful source code snippets
Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
1 2 cat .ssh/id_rsa.pub | ssh deploy@myserver.com "cat >> .ssh/authorized_keys2"
1 2 namespace :mod_rails do 3 desc <<-DESC 4 Restart the application altering tmp/restart.txt for mod_rails. 5 DESC 6 task :restart, :roles => :app do 7 run "touch #{release_path}/tmp/restart.txt" 8 end 9 end 10 11 namespace :deploy do 12 %w(start restart).each { |name| task name, :roles => :app do mod_rails.restart end } 13 end
1 2 # Example needed variables at the top of your deploy.rb 3 4 set :default_env, 'beta' 5 set :rails_env, ENV['rails_env'] || ENV['RAILS_ENV'] || default_env 6 set :extra_deploys, 'config/deployments/' 7 8 ... 9 10 # Now add this to the bottom of your deploy.rb, last thing to load. 11 12 if extra_deploys && File.exists?(extra_deploys+rails_env+".rb") 13 puts "Loaded #{extra_deploys+rails_env}.rb" if load extra_deploys+rails_env 14 else 15 puts "Could not find #{extra_deploys+rails_env}.rb" 16 end 17
1 2 desc "Updates RubyGems version" 3 task :gem_update_system do 4 sudo "gem update --system" 5 end 6 7 8 desc "Install a RubyGem from remote source" 9 task :gem_install do 10 puts "USAGE: GEM=gemname cap gems_install" and next unless ENV['GEM'] 11 sudo "gem install #{ENV['GEM']} --no-ri --no-rdoc" 12 end
1 2 cap gem_update_system 3 GEM=rails cap gem_install
1 2 namespace :deploy do 3 namespace :mongrel do 4 [ :stop, :start, :restart ].each do |t| 5 desc "#{t.to_s.capitalize} the mongrel appserver" 6 task t, :roles => :app do 7 run "mongrel_rails cluster::#{t.to_s} --clean -C #{mongrel_conf}" 8 end 9 end 10 end 11 12 namespace :apache do 13 desc "Start Apache" 14 task :start, :roles => :web do 15 sudo "/etc/init.d/httpd start > /dev/null" 16 end 17 18 desc "Stop Apache" 19 task :stop, :roles => :web do 20 sudo "/etc/init.d/httpd stop > /dev/null" 21 end 22 23 desc "Restart Apache" 24 task :restart, :roles => :web do 25 sudo "/etc/init.d/httpd restart > /dev/null" 26 end 27 end 28 29 desc "Custom restart task for mongrel cluster" 30 task :restart do 31 deploy.mongrel.restart 32 deploy.apache.restart 33 end 34 35 desc "Custom start task for mongrel cluster" 36 task :start, :roles => :app do 37 deploy.mongrel.start 38 deploy.apache.start 39 end 40 41 desc "Custom stop task for mongrel cluster" 42 task :stop, :roles => :app do 43 deploy.apache.stop 44 deploy.mongrel.stop 45 end 46 47 end
1 2 desc "Dumps target database into development db" 3 task :sync_db do 4 env = ENV['RAILS_ENV'] || ENV['DB'] || 'production' 5 file = "#{application}.sql.bz2" 6 remote_file = "#{shared}/log/#{file}" 7 run "pg_dump --clean --no-owner --no-privileges -U#{db_user} -h#{db_host} #{db_name}_#{env} | bzip2 > #{file}" do |ch, stream, out| 8 ch.send_data "#{db_password}\n" if out =~ /^Password:/ 9 puts out 10 end 11 puts rsync = "rsync #{user}@#{domain}:#{file} tmp" 12 `#{rsync}` 13 puts depackage = "bzcat tmp/#{file} | psql #{local_db_dev}" 14 `#{depackage}` 15 end
1 2 desc "Returns last lines of log file. Usage: cap log [-s lines=100] [-s rails_env=production]" 3 task :log do 4 lines = configuration.variables[:lines] || 100 5 rails_env = configuration.variables[:rails_env] || 'production' 6 run "tail -n #{lines} #{app_dir}/log/#{rails_env}.log" do |ch, stream, out| 7 puts out 8 end 9 end 10
patches/*.diffin your local directory, and apply them before restarting your app.
1 2 task :after_setup do 3 patches_setup 4 end 5 6 task :after_update_code do 7 send_and_apply_patches 8 end 9 10 task :patches_setup do 11 run "mkdir -p #{deploy_to}/#{shared_dir}/patches" 12 end 13 14 task :send_and_apply_patches do 15 Dir[File.join(File.dirname(__FILE__), '../patches/*.diff')].sort.each do |patch| 16 puts "sending #{File.basename(patch)}" 17 put(File.read(patch), 18 "#{deploy_to}/#{shared_dir}/patches/#{File.basename(patch)}", 19 :mode => 0777) 20 puts "applying #{File.basename(patch)}" 21 run "cd #{release_path}; patch -p0 < #{deploy_to}/#{shared_dir}/patches/#{File.basename(patch)}" 22 end 23 end
1 2 if ENV["SERVER"] && ENV["SERVER"] == "production" 3 set :primary_server, "production.com" 4 set :user, "ben" 5 elsif ENV["SERVER"] && ENV["SERVER"] == "staging" 6 set :primary_server, "staging.local" 7 set :user, "ben" 8 else 9 ... 10 end 11 12 role :web, primary_server 13 role :app, primary_server 14 role :db, primary_server, :primary => true 15
1 2 Capistrano.configuration(:must_exist).load do 3 4 set :svn_tag_dir, 'tags' # new config var to denote the tag sub-directory in the svn repository 5 6 desc <<-DESC 7 Update all servers with the provided tag of the source code. All this does 8 is do a checkout of the provided svn tag (as defined by the svn module). 9 DESC 10 task :update_tag, :roles => [:app, :db, :web] do 11 12 puts " * deploying tag #{release}" 13 14 on_rollback { delete release_path, :recursive => true } 15 source.checkout_tag(self) 16 run <<-CMD 17 rm -rf #{release_path}/log #{release_path}/public/system && 18 ln -nfs #{shared_path}/log #{release_path}/log && 19 ln -nfs #{shared_path}/system #{release_path}/public/system 20 CMD 21 22 end 23 24 desc <<-DESC 25 Deploy svn tag, reset symlink and restart server. 26 DESC 27 task :deploy_tag, :roles => [:app, :db, :web] do 28 set_release_tag 29 30 transaction do 31 update_tag 32 symlink 33 compile_mo 34 end 35 end 36 37 desc <<-DESC 38 Deploy svn tag, migrate, compile .po files to .mo and reset symlink and restart server. 39 Note that everything is done in a transaction 40 DESC 41 task :deploy_tag_with_migrations, :roles => [:app, :db, :web] do 42 set_release_tag 43 44 transaction do 45 update_tag 46 symlink 47 migrate 48 compile_mo 49 end 50 end 51 52 def set_release_tag 53 set :release, ENV['TAG'] 54 unless release 55 puts " * no tag specified, assuming latest" 56 set(:release) {source.latest_tag} 57 end 58 end 59 end 60 61 module Capistrano 62 63 #override a configuration object method to return the correct release_path for the tag. 64 class Configuration 65 66 # Return the full path to the named release. If a release is not specified, 67 # the provided tag name (passed as a cli parameter) is used. 68 def tag_path(tag_name = release) 69 File.join(releases_path, tag_name) 70 end 71 alias_method :release_path, :tag_path 72 73 end 74 75 # add a method to the subversion object to checkout tags 76 module SCM 77 class Subversion 78 79 def latest_tag 80 configuration.logger.debug "querying latest tag ..." unless @latest_revision 81 tags_dir = configuration.repository + '/' + configuration.svn_tag_dir + '/' 82 @latest_revision = svn_ls(tags_dir).split("\n").last.chomp('/') 83 end 84 alias_method :latest_revision, :latest_tag 85 86 # checkout a tag from a pre-configured svn tag dir. 87 def checkout_tag(actor) 88 op = configuration[:checkout] || "co" 89 username = configuration[:svn_username] ? "--username #{configuration[:svn_username]}" : "" 90 command = "#{svn} #{op} #{username} -q #{configuration.repository}/#{configuration.svn_tag_dir}/#{configuration.release} #{actor.release_path} &&" 91 run_checkout(actor, command, &svn_stream_handler(actor)) 92 end 93 94 private 95 def svn_ls(path) 96 `svn ls #{path}` 97 end 98 end 99 end 100 end 101