Capistrano 3: Run task only on a single server from a pool of servers assigned a role

為{幸葍}努か 提交于 2019-12-07 06:14:07

问题


I have 20 servers that are in the "web" role. I have a task I need to perform on only one of them as the change affects shared storage. My current solution is a hack to get around this (below). Looking for a better way, I don't have a ton of ruby or cap experience.

task :checkout_project_properties do
    num_runs = 0
    on roles(:web), in: :sequence do
        if num_runs > 0
            abort('Only running on one server.  Exiting')
        end
        execute("checkout-project-properties #{uc_stage} #{repo} #{branch}")
        num_runs += 1
    end
end

回答1:


I assume that you are referring to your production configuration, with so many web servers. In this case, your config/deploy/production.rb probably contains many lines like this:

server 'web_1', roles: %w(web)
server 'web_2', roles: %w(web)
server 'web_3', roles: %w(web)
...

Simply make one of these servers primary, so it looks like:

server 'web_1', roles: %w(web), primary: true
server 'web_2', roles: %w(web)
server 'web_3', roles: %w(web)
...    

Then change your task so it looks like this:

task :checkout_project_properties do
    on primary(:web) do
        execute("checkout-project-properties #{uc_stage} #{repo} #{branch}")
    end
end


来源:https://stackoverflow.com/questions/23095865/capistrano-3-run-task-only-on-a-single-server-from-a-pool-of-servers-assigned-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!