• Download Gist
cap_tasks nginx unicorn redis memcached resque rails_logs cmd_utility
1 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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
#Tasks have been added to fully maintain nginx, unicorn, redis, memcached, start resque workers and run any command on any server in the farm.
#<pre>
#cap nginx:restart # Restart Nginx.
#cap nginx:start # Start Nginx.
#cap nginx:status # Status of Nginx.
#cap nginx:stop # Stop Nginx.
#cap nginx:tail_error # Tail the Nginx error logs.
#cap unicorn:reload # reload your unicorn servers.
#cap unicorn:restart # restart your unicorn servers.
#cap unicorn:start # start your unicorn servers.
#cap unicorn:status # Status of Unicorn.
#cap unicorn:stop # stop your unicorn servers.
#cap memcached:flush_all # flush_all your memcached servers.
#cap memcached:restart # restart your memcached servers.
#cap memcached:start # start your memcached servers.
#cap memcached:status # status your memcached servers.
#cap memcached:stop # stop your memcached servers.
#cap redis:restart # restart your redis servers.
#cap redis:start # start your redis servers.
#cap redis:status # status your redis servers.
#cap redis:stop # stop your redis servers.
#cap resque:web # start the resque ui.
#cap resque:work # start a resque worker.
#cap resque:quit_worker # Gracefully kill a worker. If the worker is working, it will finish before shutting down.
#cap utility:run_cmd # execute basic command line functions.
#cap utility:run_sudo # execute sudo command line functions.
#</pre>
#
#There is also a task to get a quick look at the status of everything.
#<pre>
#cap server_farm:status # Get a quick full system status.
#</pre>
#
#You can also use cap to tail log files:
#<pre>
#cap nginx:tail_error # Tail the Nginx error logs.
#cap rails:log # Tail the rails log.
#</pre>
#
#Every task listed here has an optional hosts argument you can pass to specify the server you want to run the task on. By default, the task is run on all the servers in that environment for the given role. So if you ran:
#<pre>
#cap production unicorn:status
#</pre>
#and see that unicorn is up on 5 of the servers, but it's down on 192.168.228.11, then you can start unicorn back up on just that server with:
#<pre>
#cap production unicorn:start hosts=192.168.228.11
#</pre>
#hosts takes a comma delimited list of ip address, so if you wanted to start nginx on 192.168.228.11 and 192.168.228.16:
#<pre>
#cap production nginx:start hosts=192.168.228.11,192.168.228.16
#</pre>
#
#EXCEPTION: cap memcached:flush_all does not accept the optional hosts argument.
#
#EXCEPTION: The resque tasks take a host argument instead of a hosts argument and only accepts 1 ip address. While not require, you'll probably want to specify the host, otherwise you'll create a worker on every server in the farm.
#
#You'll probably want to use the hosts argument if you're using the log cap tasks.
 
 
namespace :rails do
desc "Tail the rails log. optional arg: hosts=ip1,ip2"
task :log, :roles => :app do
hosts = ENV['hosts'].to_s.split(',') || find_servers_for_task(current_task).collect{|s| s.host}
run("tail -f /var/www/rails/claritybase/current/log/#{stage}.log", :hosts => hosts) do |channel, stream, data|
puts "#{channel[:server]}: #{data}" unless data =~ /^10\.[01]\.0/ # skips lb pull pages
break if stream == :err
end
end
end
 
namespace :server_farm do
desc "Get a quick full system status. optional arg: hosts=ip1,ip2"
task :status do
nginx.status
unicorn.status
memcached.status
redis.status
end
end
 
# ====================================
# Redis Server TASKS
# ====================================
namespace :redis do
%w(start stop restart status).each do |cmd|
desc "#{cmd} your redis servers. optional arg: hosts=ip1,ip2"
task "#{cmd}".to_sym, :roles => :redis do
hosts = ENV['hosts'].to_s.split(',') || find_servers_for_task(current_task).collect{|s| s.host}
if cmd == 'status'
status = nil
run("ps -eaf | grep redis", :hosts => hosts) do |channel, stream, data|
status = (data =~ /redis-server/) ? 'up' : 'down'
puts " ** [#{stream} :: #{channel[:host]}] redis is #{status}"
end
else
sudo("/etc/init.d/redis #{cmd}", :hosts => hosts)
end
end
end
end
 
# ====================================
# Memcached Server TASKS
# ====================================
namespace :memcached do
%w(start stop restart status flush_all).each do |cmd|
desc "#{cmd} your memcached servers. optional arg: hosts=ip1,ip2"
task "#{cmd}".to_sym, :roles => :memcached do
hosts = ENV['hosts'].to_s.split(',') || find_servers_for_task(current_task).collect{|s| s.host}
if cmd == 'flush_all'
run("echo 'flush_all' | nc -q 5 localhost 11211", :hosts => hosts)
elsif cmd == 'status'
status = nil
run("ps -eaf | grep memcache", :hosts => hosts) do |channel, stream, data|
status = (data =~ /usr\/bin\/memcached/) ? 'up' : 'down'
puts " ** [#{stream} :: #{channel[:host]}] memcached is #{status}"
end
else
sudo("/etc/init.d/memcached #{cmd}", :hosts => hosts)
end
end
end
end
 
# ====================================
# nginx Server TASKS
# ====================================
namespace :nginx do
desc "Start Nginx. optional arg: hosts=ip1,ip2"
task :start, :roles => :app do
hosts = ENV['hosts'].to_s.split(',') || find_servers_for_task(current_task).collect{|s| s.host}
sudo("/opt/nginx/sbin/nginx", :hosts => hosts)
end
 
desc "Stop Nginx. optional arg: hosts=ip1,ip2"
task :stop, :roles => :app do
hosts = ENV['hosts'].to_s.split(',') || find_servers_for_task(current_task).collect{|s| s.host}
sudo("kill `cat /opt/nginx/logs/nginx.pid`", :hosts => hosts)
end
 
desc "Restart Nginx. optional arg: hosts=ip1,ip2"
task :restart, :roles => :app do
hosts = ENV['hosts'].to_s.split(',') || find_servers_for_task(current_task).collect{|s| s.host}
sudo("kill -HUP `cat /opt/nginx/logs/nginx.pid`", :hosts => hosts)
end
 
desc "Status of Nginx. optional arg: hosts=ip1,ip2"
task :status, :roles => :app do
hosts = ENV['hosts'].to_s.split(',') || find_servers_for_task(current_task).collect{|s| s.host}
status = nil
run("ps -eaf | grep nginx", :hosts => hosts) do |channel, stream, data|
status = (data =~ /nginx: master process/) ? 'up' : 'down'
puts " ** [#{stream} :: #{channel[:host]}] nginx is #{status}"
end
end
 
desc "Tail the Nginx error logs. optional arg: hosts=ip1,ip2"
task :tail_error, :roles => :app do
hosts = ENV['hosts'].to_s.split(',') || find_servers_for_task(current_task).collect{|s| s.host}
run("tail -f /opt/nginx/logs/error.log", :hosts => hosts) do |channel, stream, data|
puts "#{channel[:server]}: #{data}" unless data =~ /^10\.[01]\.0/ # skips lb pull pages
break if stream == :err
end
end
end
 
# ====================================
# Unicorn Server TASKS
# ====================================
namespace :unicorn do
%w(start stop restart reload).each do |cmd|
desc "#{cmd} your unicorn servers. optional arg: hosts=ip1,ip2"
task "#{cmd}".to_sym, :roles => :app do
hosts = ENV['hosts'].to_s.split(',') || find_servers_for_task(current_task).collect{|s| s.host}
run("/etc/init.d/unicorn #{cmd}", :hosts => hosts)
end
end
 
desc "Status of Unicorn. optional arg: hosts=ip1,ip2"
task :status, :roles => :app do
hosts = ENV['hosts'].to_s.split(',') || find_servers_for_task(current_task).collect{|s| s.host}
status = nil
run("ps -eaf | grep unicorn", :hosts => hosts) do |channel, stream, data|
status = (data =~ /unicorn_rails master/) ? 'up' : 'down'
puts " ** [#{stream} :: #{channel[:host]}] unicorn is #{status}"
end
end
end
 
 
# ====================================
# Resque TASKS
# ====================================
namespace :resque do
desc "start a resque worker. optional arg: host=ip queue=name"
task :work, :roles => :app do
hosts = ENV['host'] || find_servers_for_task(current_task).collect{|s| s.host}
queue = ENV['queue'] || '*'
rake = fetch(:rake, "rake")
rails_env = fetch(:rails_env, "staging")
run("cd #{current_path}; #{rake} RAILS_ENV=#{rails_env} QUEUE=#{queue} resque:work", :hosts => hosts)
end
 
desc "Gracefully kill a worker. If the worker is working, it will finish before shutting down. arg: host=ip pid=pid"
task :quit_worker, :roles => :app do
if ENV['host'].nil? || ENV['host'].empty? || ENV['pid'].nil? || ENV['pid'].empty?
puts 'You must enter the host and pid to kill..cap resque:quit host=ip pid=pid'
else
hosts = ENV['host'] || find_servers_for_task(current_task).collect{|s| s.host}
run("kill -QUIT #{ENV['pid']}", :hosts => hosts)
end
end
desc "start the resque ui. optional arg: host=ip"
task :web, :roles => :app do
hosts = ENV['host'] || find_servers_for_task(current_task).collect{|s| s.host}
rails_env = fetch(:rails_env, "staging")
run("cd #{current_path}; RAILS_ENV=#{rails_env} resque-web #{current_path}/config/initializers/resque.rb", :hosts => hosts)
end
end
 
# ====================================
# Utility TASKS
# ====================================
namespace :utility do
desc "execute basic command line functions. ie. cap utility:run_cmd 'ps -eaf | grep redis' optional arg: hosts=ip1,ip2"
task :run_cmd, :roles => :app do
hosts = ENV['hosts'].to_s.split(',') || find_servers_for_task(current_task).collect{|s| s.host}
run(ARGV[2], :hosts => hosts)
end
desc "execute sudo command line functions. ie. cap utility:run_sudo 'gem install sinatra' optional arg: hosts=ip1,ip2"
task :run_sudo, :roles => :app do
hosts = ENV['hosts'].to_s.split(',') || find_servers_for_task(current_task).collect{|s| s.host}
sudo(ARGV[2], :hosts => hosts)
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.