Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capistrano deploy to all servers but run some tasks on only one

I was deploying my Symfony2 application on one server only for a few month and it was perfect, but as I've added more servers, I'm stuck when I want to update all my servers at the same time, but run some commands only once (like database migration, and assets generation and push to S3).

Here are my servers:

server '192.168.0.100', user: 'ubuntu', roles: %w{app db web toto}
server '192.168.0.101', user: 'ubuntu', roles: %w{app db web}

And here is one custom task I have in my deploy.rb:

namespace :specific do
    task :command do
        on roles(:all) do |host|
            if host.roles.include?(:toto)
                invoke 'symfony:console', 'one:specific:command'
            end
        end
    end
end

With cap --roles=toto staging deploy but then only one of my two servers were updated.

I've tried the filter method like that:

namespace :assets do
    task :increment do
        set :filter, :roles => %w{toto}
        invoke 'symfony:console', 'one:specific:command'
    end
end

And other methods, copy pasting from stack overflow answers, but didn't found any matching answer šŸ˜•

like image 819
LaurentG Avatar asked Mar 11 '23 06:03

LaurentG


1 Answers

I might have hesitated longer, as this answer is not working. And I'm still unable to find a solution, in any other post, SO question, or Capistrano documentation... šŸ˜“


I hesitated to post this question and finally found my answer. But as it took me a long search, here is the answer:

Just add primary: true to one website :

server '192.168.0.100', roles: %w{app db web}, primary: true
server '192.168.0.101', roles: %w{app db web}

And in the deploy.rb:

namespace :specific do
    task :command do
        on primary(:web) do
            invoke 'symfony:console', 'one:specific:command'
        end
    end
end

My command output is still specifying the two servers, but hopefully only one did the specific command:

  03 php app/console one:specific:command
āœ” 03 [email protected] 1.153s
āœ” 03 [email protected] 6.551s

Finally!

like image 52
LaurentG Avatar answered May 01 '23 17:05

LaurentG