Sha256: b0f5d7cfd4b6f7ab0c8b343a31d331fcd334f74597f89aee408abf6da554bd12

Contents?: true

Size: 1.22 KB

Versions: 4

Compression:

Stored size: 1.22 KB

Contents

require 'net/ssh/multi'

module Astrovan
  module Exec
    # Executes a command on the remote servers.
    #
    # Options:
    # * +on+ - specifies a subset of servers to run the command on.
    def exec(command, options = {})
      session = session_for(options.delete(:on))
      begin
        command = command.gsub(/[\$\\"`]/) { |m| "\\#{m}" }
        command = %Q(sh -c "#{command}")
        if env = options[:env] || self.env
          env = env.collect { |var,value| %Q(#{var}=#{value.gsub(/[ "]/) { |m| "\\#{m}" }}) }.join(' ') unless env.is_a?(String)
          command = "env #{env} #{command}"
        end
        channel = session.exec command do |ch, stream, data|
          STDERR.puts "[#{ch[:host]}] #{stream}: #{data}"
        end
        channel.wait
        errors = 0
        channel.each do |c|
          if c[:exit_status] != 0
            STDERR.puts "[#{c[:host]}] ERROR: terminated with non-zero exit status (#{c[:exit_status]})"
            errors += 1
          else
            STDERR.puts "[#{c[:host]}] terminated with exit status of zero"
          end
        end
        raise "#{errors} remote command(s) failed" if errors > 0
      ensure
        session.close if session != self.session
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sbfaulkner-astrovan-0.5.0 lib/astrovan/exec.rb
sbfaulkner-astrovan-0.5.2 lib/astrovan/exec.rb
sbfaulkner-astrovan-0.5.3 lib/astrovan/exec.rb
sbfaulkner-astrovan-0.5.4 lib/astrovan/exec.rb