class RHC::SSHHelpers::MultipleGearTask

Public Class Methods

new(command, over, opts={}) click to toggle source
# File lib/rhc/ssh_helpers.rb, line 25
def initialize(command, over, opts={})
  requires_ssh_multi!

  @command = command
  @over = over
  @opts = opts
end

Public Instance Methods

run() { |gear, gear, item| ... } click to toggle source
# File lib/rhc/ssh_helpers.rb, line 33
def run(&block)
  out = nil

  Net::SSH::Multi.start(
    :concurrent_connections => @opts[:limit], 
    :on_error => lambda{ |server| $stderr.puts "Unable to connect to gear #{server}" }
  ) do |session|
    
    @over.each do |item| 
      case item
      when RHC::Rest::GearGroup
        item.gears.each do |gear|
          session.use ssh_host_for(gear), :properties => {:gear => gear, :group => item}
        end
      #when RHC::Rest::Gear
      #  session.use ssh_host_for(item), :properties => {:gear => item}
      #end
      else 
        raise "Cannot establish an SSH session to this type"
      end
    end
    session.exec @command, &(
      case
      when @opts[:raw]
        lambda { |ch, dest, data|
          (dest == :stdout ? $stdout : $stderr).puts data
        }
      when @opts[:as] == :table
        out = []
        lambda { |ch, dest, data| 
          label = label_for(ch)
          data.chomp.each_line do |line|
            row = out.find{ |row| row[0] == label } || (out << [label, []])[-1]
            row[1] << line
          end
        }
      when @opts[:as] == :gear
        lambda { |ch, dest, data| (ch.connection.properties[:gear]['data'] ||= "") << data }              
      else
        width = 0
        lambda { |ch, dest, data|
          label = label_for(ch)
          io = dest == :stdout ? $stdout : $stderr
          data.chomp!

          if data.each_line.to_a.count < 2
            io.puts "[#{label}] #{data}"
          elsif @opts[:always_prefix]
            data.each_line do |line|
              io.puts "[#{label}] #{line}"
            end
          else
            io.puts "=== #{label}"
            io.puts data
          end
        }
      end)
    session.loop
  end

  if block_given? && !@opts[:raw]
    case
    when @opts[:as] == :gear
      out = []
      @over.each do |item|
        case item
        when RHC::Rest::GearGroup then item.gears.each{ |gear| out << yield(gear, gear['data'], item) }
        #when RHC::Rest::Gear      then out << yield(gear, gear['data'], nil)
        end
      end
    end
  end

  out
end

Protected Instance Methods

key_for(channel) click to toggle source
# File lib/rhc/ssh_helpers.rb, line 121
def key_for(channel)
  channel.connection.properties[:gear]['id']
end
label_for(channel) click to toggle source
# File lib/rhc/ssh_helpers.rb, line 113
def label_for(channel)
  channel.properties[:label] ||= 
    begin
      group = channel.connection.properties[:group]
      "#{key_for(channel)} #{group.cartridges.map{ |c| c['name'] }.join('+')}"
    end
end
requires_ssh_multi!() click to toggle source
# File lib/rhc/ssh_helpers.rb, line 125
def requires_ssh_multi!
  begin 
    require 'net/ssh/multi' 
  rescue LoadError
    raise RHC::OperationNotSupportedException, "You must install Net::SSH::Multi to use the --gears option.  Most systems: 'gem install net-ssh-multi'"
  end
end
ssh_host_for(gear) click to toggle source
# File lib/rhc/ssh_helpers.rb, line 109
def ssh_host_for(gear)
  RHC::Helpers.ssh_string(gear['ssh_url']) or raise NoPerGearOperations
end