summaryrefslogtreecommitdiff
path: root/lib/net/ssh/multi/session.rb
blob: 9d44e71b6570d62e5c5f7a44d92418de2b893e86 (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
191
192
193
require 'thread'
require 'net/ssh/gateway'
require 'net/ssh/multi/server'
require 'net/ssh/multi/channel'

module Net; module SSH; module Multi
  class Session
    attr_reader :servers
    attr_reader :default_gateway
    attr_reader :groups

    def initialize
      @servers = []
      @groups = {}
      @gateway = nil
      @connections_mutex = Mutex.new
      @groups_mutex = Mutex.new
      @active_groups = {}
      @open_groups = []
    end

    def group(*args)
      mapping = args.last.is_a?(Hash) ? args.pop : {}

      if mapping.any? && block_given?
        raise ArgumentError, "must provide group mapping OR block, not both"
      elsif block_given?
        begin
          saved_groups = @open_groups.dup
          @open_groups.concat(args.map { |a| a.to_sym }).uniq!
          yield self if block_given?
        ensure
          @open_groups.replace(saved_groups)
        end
      else
        mapping.each do |key, value|
          (@open_groups + Array(key)).uniq.each do |grp|
            (groups[grp.to_sym] ||= []).concat(Array(value))
          end
        end
      end
    end

    def via(host, user, options={})
      @default_gateway = Net::SSH::Gateway.new(host, user, options)
      self
    end

    def use(host, user, options={})
      server = Server.new(host, user, {:via => default_gateway}.merge(options))
      unless servers.include?(server)
        servers << server
        group [] => server
      end
      server
    end

    def with(*groups)
      saved_groups = @active_groups.dup

      new_map = groups.inject({}) do |map, group|
        if group.is_a?(Hash)
          group.each do |gr, value|
            raise ArgumentError, "the value for any group must be a Hash" unless value.is_a?(Hash)
            map[gr] = (@active_groups[gr] || {}).merge(value)
          end
        else
          map[group] = @active_groups[group] || {}
        end
        map
      end

      @active_groups.update(new_map)
      yield self
    ensure
      @active_groups.replace(saved_groups)
    end

    def active_sessions
      list = if @active_groups.empty?
        servers
      else
        @active_groups.inject([]) do |list, (group, properties)|
          list.concat(groups[group].select { |server| properties.all? { |prop, value| server[prop] == value } })
        end
      end

      sessions_for(list.uniq)
    end

    def connect!
      active_sessions
      self
    end

    def close
      servers.each { |server| server.close_channels }
      loop(0) { busy?(true) }
      servers.each { |server| server.close }
      default_gateway.shutdown! if default_gateway
    end

    def busy?(include_invisible=false)
      servers.any? { |server| server.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)
      return false if servers.any? { |server| !server.preprocess(&block) }

      readers = servers.map { |s| s.readers }.flatten
      writers = servers.map { |s| s.writers }.flatten

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

      return servers.all? { |server| server.postprocess(readers, writers) }
    end

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

    def open_channel(type="session", *extra, &on_confirm)
      channels = active_sessions.map do |ssh|
        channel = ssh.open_channel(type, *extra, &on_confirm)
        channel[:server] = ssh[:server]
        channel[:host] = ssh[:server].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[:server]] ||= ""
        ch[:result][ch[:server]] << data
      end

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

      return channel[:result]
    end

    private

      def sessions_for(servers)
        threads = servers.map { |server| Thread.new { server.session(true) } }
        threads.each { |thread| thread.join }
        servers.map { |server| server.session }
      end
  end
end; end; end