weave 1.0.1 → 1.0.2.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/weave.rb +51 -26
  3. data/lib/weave/version.rb +1 -1
  4. metadata +4 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 10111ae444944741cc9e41736156eb73073e68af
4
- data.tar.gz: bfe973e6737f3a1be9504ba87943d6b925df5422
3
+ metadata.gz: 4c11f2751ec38a419142cd0f20bfab5a12f42393
4
+ data.tar.gz: e078add354738f1f22b85c12c01817e83349ca02
5
5
  SHA512:
6
- metadata.gz: 13a82c664f62f0eda334080ca5e74788f2ab209ccd64ee3e2b4111df7442cd8693dc09c6792c3567642ab774310afffef3ad9ba9238208c2e8865bf189bd3ebf
7
- data.tar.gz: 4cfe4a410fcb8af2533490d4c6aac398876c2e7129a900daa5237e8ef4a467bc2eb4000e108e5a22b291849846b52387c1b055256c637b2d61954277b162c25e
6
+ metadata.gz: d86352f8c23e2b51dd8f64ffbe91b5f5e8da46452442656b24dd07cec4e0ee5600958179f70685ef2244184424b5425b7ec064b7be7d0b69b0cc07eb7f2ea561
7
+ data.tar.gz: 824967f7cd66565084c8376899abd1379ef10a222e309603435f224f3edbfb0e692fb054ee5ae2c03d46e3e952ed2997947b67dad285c49469a0bfaba0857253
@@ -1,5 +1,6 @@
1
1
  require "net/ssh"
2
2
  require "thread"
3
+ require "timeout"
3
4
 
4
5
  module Weave
5
6
  DEFAULT_THREAD_POOL_SIZE = 10
@@ -143,47 +144,71 @@ module Weave
143
144
  # command terminated via a signal or with a non-zero exit status. Otherwise (the default), these will
144
145
  # cause a `Weave::Error` to be raised.
145
146
  #
147
+ # The option `:timeout`, if provided, specifies the number of seconds to allow the command to execute
148
+ # before timing it out. If this happens, the output is unchanged (but in most cases, this will be an error
149
+ # as your process was killed by a signal from the pseudo-tty Weave allocates for it on the remote host).
150
+ # If the command is timed out, the result hash also has `:timed_out => true` (is is `false` otherwise).
151
+ #
146
152
  # @param [Hash] options the output options
147
153
  # @option options [Symbol] :output the output format
154
+ # @option options [Symbol] :continue_on_failure whether to raise an exception if the command exits with a
155
+ # non-zero exit status or because of a signal
156
+ # @option options[Symbol] :timeout if given, how many seconds to give the command before timing it out
148
157
  def run(command, options = {})
149
158
  options[:output] ||= :pretty
150
159
  @connection ||= Net::SSH.start(@host, @user)
151
160
  result = options[:output] == :capture ? { :stdout => "", :stderr => "" } : {}
161
+ result[:timed_out] = false
152
162
  @connection.open_channel do |channel|
153
- channel.exec(command) do |_, success|
154
- unless success
155
- raise Error, "Could not run ssh command: #{command}"
156
- end
163
+ channel.request_pty do |ch, success|
164
+ raise Error, "Error requesting pty" unless success
165
+ ch.exec(command) do |_, succ|
166
+ unless succ
167
+ raise Error, "Could not run ssh command: #{command}"
168
+ end
157
169
 
158
- channel.on_data do |_, data|
159
- append_or_print_output(result, data, :stdout, options)
160
- end
170
+ ch.on_data do |_, data|
171
+ append_or_print_output(result, data, :stdout, options)
172
+ end
161
173
 
162
- channel.on_extended_data do |_, type, data|
163
- next unless type == 1
164
- append_or_print_output(result, data, :stderr, options)
165
- end
174
+ ch.on_extended_data do |_, type, data|
175
+ next unless type == 1
176
+ append_or_print_output(result, data, :stderr, options)
177
+ end
166
178
 
167
- channel.on_request("exit-status") do |_, data|
168
- code = data.read_long
169
- unless code.zero? || options[:continue_on_failure]
170
- raise Error, "[#{@host}] command finished with exit status #{code}: #{command}"
179
+ ch.on_request("exit-status") do |_, data|
180
+ code = data.read_long
181
+ unless code.zero? || options[:continue_on_failure]
182
+ raise Error, "[#{@host}] command finished with exit status #{code}: #{command}"
183
+ end
184
+ result[:exit_code] = code
171
185
  end
172
- result[:exit_code] = code
173
- end
174
186
 
175
- channel.on_request("exit-signal") do |_, data|
176
- signal = data.read_long
177
- unless options[:continue_on_failure]
178
- signal_name = Signal.list.invert[signal]
179
- signal_message = signal_name ? "#{signal} (#{signal_name})" : "#{signal}"
180
- raise Error, "[#{@host}] command received signal #{signal_message}: #{command}"
187
+ ch.on_request("exit-signal") do |_, data|
188
+ signal = data.read_long
189
+ unless options[:continue_on_failure]
190
+ signal_name = Signal.list.invert[signal]
191
+ signal_message = signal_name ? "#{signal} (#{signal_name})" : "#{signal}"
192
+ raise Error, "[#{@host}] command received signal #{signal_message}: #{command}"
193
+ end
194
+ result[:exit_signal] = signal
181
195
  end
182
- result[:exit_signal] = signal
183
196
  end
184
197
  end
185
198
  end
186
- @connection.loop(0.05)
199
+ if options[:timeout].nil?
200
+ @connection.loop(0.05)
201
+ else
202
+ begin
203
+ Timeout.timeout(options[:timeout]) do
204
+ @connection.loop(0.05)
205
+ end
206
+ rescue Timeout::Error
207
+ @connection.close
208
+ @connection = nil
209
+ result[:timed_out] = true
210
+ end
211
+ end
187
212
  result
188
213
  end
189
214
 
@@ -207,7 +232,7 @@ module Weave
207
232
 
208
233
  # @private
209
234
  def self.user_and_host(host_string)
210
- user, at, host = host_string.rpartition("@")
235
+ user, _, host = host_string.rpartition("@")
211
236
  if [user, host].any? { |part| part.nil? || part.empty? }
212
237
  raise "Bad hostname (needs to be of the form user@host): #{host_string}"
213
238
  end
@@ -1,3 +1,3 @@
1
1
  module Weave
2
- VERSION = "1.0.1"
2
+ VERSION = "1.0.2.beta1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: weave
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2.beta1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Caleb Spare
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-16 00:00:00.000000000 Z
11
+ date: 2014-01-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: net-ssh
@@ -116,9 +116,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
116
116
  version: '0'
117
117
  required_rubygems_version: !ruby/object:Gem::Requirement
118
118
  requirements:
119
- - - '>='
119
+ - - '>'
120
120
  - !ruby/object:Gem::Version
121
- version: '0'
121
+ version: 1.3.1
122
122
  requirements: []
123
123
  rubyforge_project:
124
124
  rubygems_version: 2.0.3