Class: Pmux::MultiSession

Inherits:
Object
  • Object
show all
Defined in:
lib/pmux/multi_session.rb

Direct Known Subclasses

MRSession

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(addrs, options = {}, loop = nil) ⇒ MultiSession

Returns a new instance of MultiSession.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/pmux/multi_session.rb', line 29

def initialize addrs, options={}, loop=nil
  @addrs = addrs
  @options = options
  @user = options[:user]
  @loop = loop || Coolio::Loop.default
  Net::SSH::Compat.coolio_loop = @loop
  @timeout = 3600 #FIXME

  @err_addrs = []
  @sessions = {}
  @channels = {}

  @scptable = {}
  @scpid = 0
  @scp_queue = {}
  @buffers = {}

  @on_error = nil
  @session_lim = ($test ? 99 : 2)
end

Instance Attribute Details

#loopObject (readonly)

Returns the value of attribute loop.



26
27
28
# File 'lib/pmux/multi_session.rb', line 26

def loop
  @loop
end

#timeoutObject

Returns the value of attribute timeout.



27
28
29
# File 'lib/pmux/multi_session.rb', line 27

def timeout
  @timeout
end

Instance Method Details

#close_channel(addr = nil) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/pmux/multi_session.rb', line 109

def close_channel addr=nil
  if addr
    if (ch = @channels[addr])
      ch.close
      ch.wait
    else
    end
  else
    @channels.each {|addr, ch| ch.close}
  end
end

#connect_to_addr(addr, cmd = nil) ⇒ Object



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
# File 'lib/pmux/multi_session.rb', line 50

def connect_to_addr addr, cmd=nil
  return if @sessions[addr]
  @sessions[addr] = SessionWrapper.new addr
  @loop.start_ssh(addr, @user) {|ssh|
    if ssh.respond_to? :open_channel
      host = ssh.host
      @sessions[host].ssh = ssh
      if cmd
        channel = ssh.open_channel {|ch|
          ch.exec(cmd) {|ch, success|
            raise RuntimeError unless success
            @channels[host] = ch

            if (ary = @buffers[host]) and !ary.empty?
              ch.send_data ary.join('')
              ary.clear
            end

            setup_channel ch
          }
        }
      end

      if (queue = @scp_queue[host]) and !queue.empty?
        queue2 = queue.dup
        queue.clear
        scp = @sessions[host].scp
        queue2.each {|updown, qf, qaddr, qremote, qlocal, qoptions|
            scp_download_sub scp, qaddr, qf, qremote, qlocal, qoptions}
      end

      ssh.floop 0.1
    elsif ssh.kind_of? Exception
      e = ssh
      error_on_addr addr, e.inspect
    else
      error_on_addr addr, 'failed'
    end
  }
end

#error_on_addr(addr, err = nil) ⇒ Object



99
100
101
102
103
104
105
106
107
# File 'lib/pmux/multi_session.rb', line 99

def error_on_addr addr, err=nil
  session = @sessions[addr]
  session.scp_channels.each_value {|ch|
    ch.on_open_failed.call ch, 0xfe000001, err.to_s
  }
  @err_addrs.push addr
  @addrs.delete addr
  @on_error.call addr, err if @on_error
end

#on_error(&block) ⇒ Object



121
122
123
# File 'lib/pmux/multi_session.rb', line 121

def on_error &block
  @on_error = block
end

#process_scp_queue_once(addr) ⇒ Object



231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/pmux/multi_session.rb', line 231

def process_scp_queue_once addr
  scp = @sessions[addr].scp
  queue = (@scp_queue[addr] ||= [])
  if scp and !queue.empty?
    updown, future, addr, remote, local, options = queue.shift
    case updown
    when :down
      scp_download_sub scp, addr, future, remote, local, options
    when :up
      scp_upload_sub scp, addr, future, local, remote, options
    end
  end
end

#scp_download(addr, remote, local, options = {}) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
# File 'lib/pmux/multi_session.rb', line 187

def scp_download addr, remote, local, options={}
  future = MR::Future.new self, @loop
  queue = (@scp_queue[addr] ||= [])

  if (scp = @sessions[addr].scp)
    scp_download_sub scp, addr, future, remote, local, options
  else
    queue.push [:down, future, addr, remote, local, options]
  end
  future
end

#scp_download_sub(scp, addr, future, remote, local, options) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/pmux/multi_session.rb', line 199

def scp_download_sub scp, addr, future, remote, local, options
  session = @sessions[addr]
  if !session or session.scp_session_count > @session_lim
    queue = (@scp_queue[addr] ||= [])
    queue.push [:down, future, addr, remote, local, options]
    return
  end
  session.scp_session_count += 1

  scpid = @scpid
  @scpid += 1
  @scptable[scpid] = future
  channel = scp.download(remote, local, options)
  channel.on_eof {|ch|
    session.scp_session_count -= 1
    @loop.set_timer(0) {process_scp_queue_once addr}

    future.set_result(nil, options[:set_result])
    @scptable.delete scpid
  }
  channel.on_open_failed {|ch, code, desc|
    Log.error "#{addr}: scp error: #{desc}"
    err = RuntimeError.new "scp error: #{desc}"
    @on_error.call addr, err
    session.scp_session_count -= 1
    @loop.set_timer(0) {process_scp_queue_once addr}

    future.set_result(nil, options[:set_result])
    @scptable.delete scpid
  }
end

#scp_upload(addr, local, remote, options = {}) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
# File 'lib/pmux/multi_session.rb', line 134

def scp_upload addr, local, remote, options={}
  future = MR::Future.new self, @loop
  queue = (@scp_queue[addr] ||= [])

  if (scp = @sessions[addr].scp)
    scp_upload_sub scp, addr, future, local, remote, options
  else
    queue.push [:up, future, addr, remote, local, options]
  end
  future
end

#scp_upload_files(addr, files, remote, options = {}) ⇒ Object



125
126
127
128
129
130
131
132
# File 'lib/pmux/multi_session.rb', line 125

def scp_upload_files addr, files, remote, options={}
  mf = MR::MultiFuture.new
  for file in files
    future = scp_upload addr, file, remote, options
    mf.add future
  end
  mf
end

#scp_upload_sub(scp, addr, future, local, remote, options) ⇒ Object



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
# File 'lib/pmux/multi_session.rb', line 146

def scp_upload_sub scp, addr, future, local, remote, options
  session = @sessions[addr]
  if !session or session.scp_session_count > @session_lim
    queue = (@scp_queue[addr] ||= [])
    queue.push [:up, future, addr, remote, local, options]
    return
  end
  session.scp_session_count += 1

  scpid = @scpid
  @scpid += 1
  @scptable[scpid] = future

  channel = scp.upload(local, remote, options)
  session.scp_channels[scpid] = channel
  channel.on_eof {|ch|
    session.scp_session_count -= 1
    session.scp_channels.delete scpid
    @loop.set_timer(0) {process_scp_queue_once addr}

    future.set_result(nil, options[:set_result])
    @scptable.delete scpid
  }
  channel.on_open_failed {|ch, code, desc|
    Log.error "#{addr}: scp error: #{desc}"
    session.scp_session_count -= 1
    session.scp_channels.delete scpid
    @loop.set_timer(0) {process_scp_queue_once addr}

    if code == 0xfe000001
      err = desc
    else
      err = RuntimeError.new "scp error: #{desc}"
      @on_error.call addr, err
      err = nil
    end
    future.set_result(err, options[:set_result])
    @scptable.delete scpid
  }
end

#setup_channel(ch) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/pmux/multi_session.rb', line 91

def setup_channel ch
  ch.on_data {|c, data|}
  ch.on_extended_data {|c, type, data|
    #STDERR.puts c.connection.host+': '+data
  }
  #ch.on_close {|c| error_on_addr ch.connection.host}
end