Search code examples
capistranocapistrano3

Capistrano: How can I trigger a task on ALL hosts if ANY of them is failing a check?


I am using capistrano to deploy a rails application.

I have a challenge of running a check across ALL hosts and then determining if a task should be triggered on ALL hosts, if ANY of them fails the check. The check could e.g. be looking for a file:

desc "Make sure file is on all servers"
  task :do_something do
    on roles(:app) do
      unless test("[ -f /some_dir/the_file.txt ]")
        # Run something on ALL servers if missing on just one of them...
      end
    end
  end
end

I have also considered if it could somehow be done by first running one task on all servers and combining up the result of those and then in a second task run it if the output of the first had the file missing on any of the servers.


Solution

  • I figured it:

    task :check_something do
      has_file = []
      on roles(:app) do
        has_file << test("[ -f /some_dir/the_file.txt ]")
      end
      set(:should_run_something, has_file.any?{|file| file == false})
    end
    
    task :run_something do
      on roles(:app) do
        if fetch(:should_run_something)
          # Run my command
        end
      end
    end
    after :check_something, :run_something