Search code examples
capistrano

Capistrano get list servers


I need write a task which executes one line command on each server only one time. For example:

task :start_service do
  on servers do
    execute 'sudo svc -t /etc/my_service'
  end
end

I searched it in docs http://www.rubydoc.info/gems/capistrano/toplevel in 3.10.1 version gem. But servers method is undefined in Capistrano. And look what I concocted)):

task :docker_start do
  servers = server('') # dirty hack to get all servers
  on servers do |h|
    next if h == ''
    execute 'sudo svc -t /etc/my_service'
  end
end

Is there a way to do it without this dirty hack? Thanks.

EDIT: My my_stage.rb look like this:

server 'serv1', roles: %w(app role1 role2)
server 'serv2', roles: %w(app role2 db role3), primary: true

I think I could add a special role for each server, ex: my_service:

server 'serv1', roles: %w(app role1 role2 my_service)
server 'serv2', roles: %w(app role2 db role3 my_service), primary: true

And write task as:

task :start_service do
  on roles :my_service do
    execute 'sudo svc -t /etc/my_service'
  end
end

But I think it is not the true way to add new role only for list servers.


Solution

  • There is a special role :all that matches all servers. In addition, you can specify in: :sequence to ensure that the execution takes place on one server at a time (as opposed to in parallel, the default).

    task :start_service do
      on roles(:all), in: :sequence do
        execute 'sudo svc -t /etc/my_service'
      end
    end