summaryrefslogtreecommitdiff
path: root/lib/net/ssh/multi/session.rb
blob: 0a5c73ed67bd982394f4c8210d1859c24fc2c1f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
require 'thread'
require 'net/ssh'
require 'net/ssh/gateway'
require 'net/ssh/multi/channel'

module Net; module SSH; module Multi
  class Session
    attr_reader :connections
    attr_reader :gateway

    class Collector
      attr_reader :specifications

      def initialize
        @specifications = []
      end

      def to(host, user, options={})
        @specifications << [host, user, options]
        self
      end
    end

    def initialize
      @connections = []
      @gateway = nil
      @mutex = Mutex.new
    end

    def via(*args)
      if connection_specification?(args)
        @gateway = Net::SSH::Gateway.new(*args)
      elsif args.length == 1
        @gateway = args.first
      else
        raise ArgumentError, "expected either a connection specification or a Net::SSH::Gateway instance"
      end
      self
    end

    def use(*list)
      @connections += list.each { |c| c[:host] = c.host }
      self
    end

    def connect(*args)
      if connection_specification?(args)
        establish_connection(*args)
      elsif args.any?
        raise ArgumentError, "expected either a connection specification or a block"
      end

      if block_given?
        collector = Collector.new
        yield collector

        mutex = Mutex.new
        threads = collector.specifications.map do |host, user, options|
          Thread.new { establish_connection(host, user, options) }
        end

        threads.each { |t| t.join }
      end

      self
    end

    def close
      connections.each { |connection| connection.channels.each { |id, channel| channel.close } }
      loop(0) { busy?(true) }
      connections.each { |connection| connection.transport.close }
      gateway.shutdown! if gateway
    end

    def busy?(include_invisible=false)
      connections.any? { |connection| connection.busy?(include_invisible) }
    end

    alias :loop_forever :loop

    def loop(wait=nil, &block)
      running = block || Proc.new { |c| busy? }
      loop_forever { break unless process(wait, &running) }
    end

    def process(wait=nil, &block)
      connections.each { |c| return false unless c.preprocess(&block) }

      writers_by_connection, readers_by_connection = {}, {}

      writers = connections.map do |c|
        c.listeners.keys.select do |w|
          writers_by_connection[c] ||= []
          writers_by_connection[c] << w
          w.respond_to?(:pending_write?) && w.pending_write?
        end
      end.flatten

      readers = connections.map { |c| readers_by_connection[c] = c.listeners.keys }.flatten

      readers, writers = IO.select(readers, writers, nil, wait)

      connections.each do |c|
        readers_for_this = readers_by_connection[c] & (readers || [])
        writers_for_this = writers_by_connection[c] & (writers || [])
        return false unless c.postprocess(readers_for_this, writers_for_this)
      end

      return true
    end

    def send_global_request(type, *extra, &callback)
      connections.each { |connection| connection.send_global_request(type, *extra, &callback) }
      self
    end

    def open_channel(type="session", *extra, &on_confirm)
      channels = connections.map do |connection|
        channel = connection.open_channel(type, *extra, &on_confirm)
        channel[:host] = connection[:host]
        channel
      end
      Multi::Channel.new(self, channels)
    end

    def exec(command, &block)
      open_channel do |channel|
        channel.exec(command) do |ch, success|
          raise "could not execute command: #{command.inspect} (#{ch[:host]})" unless success

          channel.on_data do |ch, data|
            if block
              block.call(ch, :stdout, data)
            else
              data.chomp.each_line do |line|
                $stdout.puts("[#{ch[:host]}] #{line}")
              end
            end
          end

          channel.on_extended_data do |ch, type, data|
            if block
              block.call(ch, :stderr, data)
            else
              data.chomp.each_line do |line|
                $stderr.puts("[#{ch[:host]}] #{line}")
              end
            end
          end

          channel.on_request("exit-status") do |ch, data|
            ch[:exit_status] = data.read_long
          end
        end
      end
    end

    def exec!(command, &block)
      block ||= Proc.new do |ch, type, data|
        ch[:result] ||= {}
        ch[:result][ch.connection[:host]] ||= ""
        ch[:result][ch.connection[:host]] << data
      end

      channel = exec(command, &block)
      channel.wait

      return channel[:result]
    end

    def send_message(message)
      connections.each { |connection| connection.send_message(message) }
      self
    end

    private

      def connection_specification?(args)
        args.length == 2 || (args.length == 3 && args.last.is_a?(Hash))
      end

      def establish_connection(host, user, options={})
        connection = gateway ? gateway.ssh(host, user, options) :
          Net::SSH.start(host, user, options)
        connection[:host] = host
        @mutex.synchronize { @connections.push(connection) }
        return connection
      end
  end
end; end; end