Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save devdatta/343205 to your computer and use it in GitHub Desktop.
Save devdatta/343205 to your computer and use it in GitHub Desktop.
#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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment