capistrano sequential restarts

2019-04-09 00:44发布

问题:

I have capistrano configured to deploy across three physical servers. I would like to configure the restart task to sequentially go to each server and restart the app rather than the default way of going to all servers at once.

Here is the current deploy task:

namespace :deploy do

  task :start, :roles => :app, :except => { :no_release => true } do 
    run "cd #{current_path} && bundle exec unicorn_rails -c #{current_path}/config/unicorn.rb -E #{rails_env} -D"
  end

  task :stop, :roles => :app, :except => { :no_release => true } do 
    run "kill `cat #{current_path}/tmp/pids/unicorn.pid`"
  end

  task :restart, :roles => :app, :except => { :no_release => true } do
    stop
    sleep(10)
    start
  end

end

I am thinking something like this:

#this does not work 
task :sequential_restart do
   find_servers(:roles => :app).each
    restart
   end
 end

Any Ideas?

回答1:

I do something very similar using the HOSTFILTER environment variable, which effectively scopes everything to the hosts matching the filter.

Something like

find_servers(:roles => :app).each do |server|
  ENV['HOSTFILTER'] = server.host
  restart
end
ENV['HOSTFILTER'] = nil

should do the trick.