Sha256: 9f0f8c0129f4f835d1fef9e3f066b4fc8be97527aee98e606357826af0450bbd

Contents?: true

Size: 1.28 KB

Versions: 5

Compression:

Stored size: 1.28 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.
    # * +env+ - the initial environment for the remote server.
    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

5 entries across 5 versions & 1 rubygems

Version Path
sbfaulkner-astrovan-0.5.6 lib/astrovan/exec.rb
sbfaulkner-astrovan-0.5.7 lib/astrovan/exec.rb
sbfaulkner-astrovan-0.5.8 lib/astrovan/exec.rb
sbfaulkner-astrovan-0.5.9 lib/astrovan/exec.rb
sbfaulkner-astrovan-0.6.0 lib/astrovan/exec.rb