ztk 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/ztk.rb +1 -0
- data/lib/ztk/benchmark.rb +18 -5
- data/lib/ztk/logger.rb +4 -0
- data/lib/ztk/rescue_retry.rb +63 -0
- data/lib/ztk/ssh.rb +74 -47
- data/lib/ztk/version.rb +1 -1
- data/spec/spec_helper.rb +3 -2
- data/spec/ztk/benchmark_spec.rb +90 -0
- data/spec/ztk/logger_spec.rb +19 -2
- data/spec/ztk/rescue_retry_spec.rb +84 -0
- data/spec/ztk/ssh_spec.rb +164 -26
- metadata +9 -4
data/lib/ztk.rb
CHANGED
@@ -36,6 +36,7 @@ module ZTK
|
|
36
36
|
autoload :Config, 'ztk/config'
|
37
37
|
autoload :Logger, 'ztk/logger'
|
38
38
|
autoload :Parallel, 'ztk/parallel'
|
39
|
+
autoload :RescueRetry, 'ztk/rescue_retry'
|
39
40
|
autoload :Spinner, 'ztk/spinner'
|
40
41
|
autoload :SSH, 'ztk/ssh'
|
41
42
|
autoload :TCPSocketCheck, 'ztk/tcp_socket_check'
|
data/lib/ztk/benchmark.rb
CHANGED
@@ -33,9 +33,20 @@ module ZTK
|
|
33
33
|
|
34
34
|
class << self
|
35
35
|
|
36
|
-
def bench(
|
37
|
-
!
|
38
|
-
|
36
|
+
def bench(options={}, &block)
|
37
|
+
!block_given? and raise BenchmarkError, "You must supply a block!"
|
38
|
+
|
39
|
+
options = { :stdout => STDOUT, :logger => $logger, :message => nil, :mark => nil }.merge(options)
|
40
|
+
|
41
|
+
stdout = options[:stdout]
|
42
|
+
logger = options[:logger]
|
43
|
+
message = options[:message]
|
44
|
+
mark = options[:mark]
|
45
|
+
|
46
|
+
logger and logger.debug { options.inspect }
|
47
|
+
|
48
|
+
(!message.nil? && !mark.nil?) and stdout.print("#{message} ")
|
49
|
+
benchmark = ::Benchmark.realtime do
|
39
50
|
if message.nil?
|
40
51
|
yield
|
41
52
|
else
|
@@ -44,9 +55,11 @@ module ZTK
|
|
44
55
|
end
|
45
56
|
end
|
46
57
|
end
|
47
|
-
!message.nil? and puts("completed in %0.4f seconds.\n" % mark)
|
48
58
|
|
49
|
-
mark
|
59
|
+
(!message.nil? && !mark.nil?) and stdout.print("#{mark}\n" % benchmark)
|
60
|
+
logger and logger.info { "#{message} #{mark}" }
|
61
|
+
|
62
|
+
benchmark
|
50
63
|
end
|
51
64
|
|
52
65
|
end
|
data/lib/ztk/logger.rb
CHANGED
@@ -0,0 +1,63 @@
|
|
1
|
+
################################################################################
|
2
|
+
#
|
3
|
+
# Author: Zachary Patten <zachary@jovelabs.net>
|
4
|
+
# Copyright: Copyright (c) Jove Labs
|
5
|
+
# License: Apache License, Version 2.0
|
6
|
+
#
|
7
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
8
|
+
# you may not use this file except in compliance with the License.
|
9
|
+
# You may obtain a copy of the License at
|
10
|
+
#
|
11
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
12
|
+
#
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
16
|
+
# See the License for the specific language governing permissions and
|
17
|
+
# limitations under the License.
|
18
|
+
#
|
19
|
+
################################################################################
|
20
|
+
module ZTK
|
21
|
+
|
22
|
+
# ZTK::RescueRetry Error Class
|
23
|
+
#
|
24
|
+
# @author Zachary Patten <zachary@jovelabs.net>
|
25
|
+
class RescueRetryError < Error; end
|
26
|
+
|
27
|
+
# RescueRetry Class
|
28
|
+
#
|
29
|
+
# @author Zachary Patten <zachary@jovelabs.net>
|
30
|
+
class RescueRetry
|
31
|
+
|
32
|
+
class << self
|
33
|
+
|
34
|
+
def try(options={}, &block)
|
35
|
+
!block_given? and raise RescueRetryError, "You must supply a block!"
|
36
|
+
|
37
|
+
options = { :logger => $logger, :tries => 1, :on => Exception }.merge(options)
|
38
|
+
|
39
|
+
logger = options[:logger]
|
40
|
+
tries = options[:tries]
|
41
|
+
on = options[:on]
|
42
|
+
|
43
|
+
logger and logger.debug { options.inspect }
|
44
|
+
|
45
|
+
begin
|
46
|
+
return block.call
|
47
|
+
rescue on => e
|
48
|
+
if ((tries -= 1) > 0)
|
49
|
+
logger and logger.warn { "Caught #{e.inspect}, we will give it #{tries} more tr#{tries > 1 ? 'ies' : 'y'}." }
|
50
|
+
retry
|
51
|
+
else
|
52
|
+
logger and logger.fatal { "Caught #{e.inspect}, sorry, we have to give up now." }
|
53
|
+
raise e
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
data/lib/ztk/ssh.rb
CHANGED
@@ -182,27 +182,54 @@ module ZTK
|
|
182
182
|
log(:debug) { "options(#{options.inspect})" }
|
183
183
|
|
184
184
|
output = ""
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
185
|
+
stdout_header = false
|
186
|
+
stderr_header = false
|
187
|
+
|
188
|
+
ZTK::RescueRetry.try(:tries => 3, :on => EOFError) do
|
189
|
+
@ssh = Net::SSH.start(@config.host_name, @config.user, ssh_options)
|
190
|
+
|
191
|
+
channel = ssh.open_channel do |chan|
|
192
|
+
log(:debug) { "Channel opened." }
|
193
|
+
direct_log(:debug) { "===[OPENED]===[OPENED]===[#{self.inspect}]===[OPENED]===[OPENED]===\n" }
|
194
|
+
|
195
|
+
chan.exec(command) do |ch, success|
|
196
|
+
raise SSHError, "Could not execute '#{command}'." unless success
|
197
|
+
|
198
|
+
ch.on_data do |c, data|
|
199
|
+
if !stdout_header
|
200
|
+
direct_log(:debug) { "===[STDOUT]===[STDOUT]===[#{self.inspect}]===[STDOUT]===[STDOUT]===\n" }
|
201
|
+
stdout_header = true
|
202
|
+
stderr_header = false
|
203
|
+
end
|
204
|
+
direct_log(:debug) { data }
|
205
|
+
|
206
|
+
@config.stdout.print(data) unless options.silence
|
207
|
+
output += data.chomp.strip
|
208
|
+
end
|
209
|
+
|
210
|
+
ch.on_extended_data do |c, type, data|
|
211
|
+
if !stderr_header
|
212
|
+
direct_log(:debug) { "===[STDERR]===[STDERR]===[#{self.inspect}]===[STDERR]===[STDERR]===\n" }
|
213
|
+
stderr_header = true
|
214
|
+
stdout_header = false
|
215
|
+
end
|
216
|
+
direct_log(:debug) { data }
|
217
|
+
|
218
|
+
@config.stderr.print(data) unless options.silence
|
219
|
+
output += data.chomp.strip
|
220
|
+
end
|
221
|
+
|
222
|
+
ch.on_open_failed do |c, code, desc|
|
223
|
+
log(:fatal) { "Open failed! (#{code.inspect} - #{desc.inspect})" }
|
224
|
+
end
|
195
225
|
|
196
|
-
ch.on_extended_data do |c, type, data|
|
197
|
-
direct_log(:debug) { "[#{self.inspect}] #{data}" }
|
198
|
-
@config.stderr.print(data) unless options.silence
|
199
|
-
output += data.chomp.strip
|
200
226
|
end
|
201
|
-
|
202
227
|
end
|
228
|
+
channel.wait
|
229
|
+
|
230
|
+
direct_log(:debug) { "===[CLOSED]===[CLOSED]===[#{self.inspect}]===[CLOSED]===[CLOSED]===\n" }
|
231
|
+
log(:debug) { "Channel closed." }
|
203
232
|
end
|
204
|
-
channel.wait
|
205
|
-
log(:debug) { "channel closed" }
|
206
233
|
|
207
234
|
OpenStruct.new(:output => output, :exit => $?)
|
208
235
|
end
|
@@ -226,18 +253,21 @@ module ZTK
|
|
226
253
|
log(:debug) { "config(#{@config.inspect})" }
|
227
254
|
log(:info) { "upload(#{local.inspect}, #{remote.inspect})" }
|
228
255
|
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
256
|
+
ZTK::RescueRetry.try(:tries => 3, :on => EOFError) do
|
257
|
+
@sftp = Net::SFTP.start(@config.host_name, @config.user, ssh_options)
|
258
|
+
sftp.upload!(local.to_s, remote.to_s) do |event, uploader, *args|
|
259
|
+
case event
|
260
|
+
when :open
|
261
|
+
log(:debug) { "upload(#{args[0].local} -> #{args[0].remote})" }
|
262
|
+
when :close
|
263
|
+
log(:debug) { "close(#{args[0].remote})" }
|
264
|
+
when :mkdir
|
265
|
+
log(:debug) { "mkdir(#{args[0]})" }
|
266
|
+
when :put
|
267
|
+
log(:debug) { "put(#{args[0].remote}, size #{args[2].size} bytes, offset #{args[1]})" }
|
268
|
+
when :finish
|
269
|
+
log(:debug) { "finish" }
|
270
|
+
end
|
241
271
|
end
|
242
272
|
end
|
243
273
|
|
@@ -263,18 +293,21 @@ module ZTK
|
|
263
293
|
log(:debug) { "config(#{@config.inspect})" }
|
264
294
|
log(:info) { "download(#{remote.inspect}, #{local.inspect})" }
|
265
295
|
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
296
|
+
ZTK::RescueRetry.try(:tries => 3, :on => EOFError) do
|
297
|
+
@sftp = Net::SFTP.start(@config.host_name, @config.user, ssh_options)
|
298
|
+
sftp.download!(remote.to_s, local.to_s) do |event, downloader, *args|
|
299
|
+
case event
|
300
|
+
when :open
|
301
|
+
log(:debug) { "download(#{args[0].remote} -> #{args[0].local})" }
|
302
|
+
when :close
|
303
|
+
log(:debug) { "close(#{args[0].local})" }
|
304
|
+
when :mkdir
|
305
|
+
log(:debug) { "mkdir(#{args[0]})" }
|
306
|
+
when :get
|
307
|
+
log(:debug) { "get(#{args[0].remote}, size #{args[2].size} bytes, offset #{args[1]})" }
|
308
|
+
when :finish
|
309
|
+
log(:debug) { "finish" }
|
310
|
+
end
|
278
311
|
end
|
279
312
|
end
|
280
313
|
|
@@ -286,8 +319,6 @@ module ZTK
|
|
286
319
|
|
287
320
|
# Builds our SSH console command.
|
288
321
|
def console_command
|
289
|
-
log(:debug) { "console_command" }
|
290
|
-
|
291
322
|
command = [ "ssh" ]
|
292
323
|
command << [ "-q" ]
|
293
324
|
command << [ "-A" ]
|
@@ -306,8 +337,6 @@ module ZTK
|
|
306
337
|
|
307
338
|
# Builds our SSH proxy command.
|
308
339
|
def proxy_command
|
309
|
-
log(:debug) { "proxy_command" }
|
310
|
-
|
311
340
|
if !@config.proxy_user
|
312
341
|
message = "You must specify an proxy user in order to SSH proxy."
|
313
342
|
log(:fatal) { message }
|
@@ -338,8 +367,6 @@ module ZTK
|
|
338
367
|
|
339
368
|
# Builds our SSH options hash.
|
340
369
|
def ssh_options
|
341
|
-
log(:debug) { "ssh_options" }
|
342
|
-
|
343
370
|
options = {}
|
344
371
|
|
345
372
|
# These are plainly documented on the Net::SSH config class.
|
data/lib/ztk/version.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -18,7 +18,8 @@
|
|
18
18
|
#
|
19
19
|
################################################################################
|
20
20
|
|
21
|
-
require
|
21
|
+
require 'ztk'
|
22
|
+
require 'tempfile'
|
22
23
|
|
23
24
|
################################################################################
|
24
25
|
|
@@ -27,6 +28,6 @@ SimpleCov.start do
|
|
27
28
|
add_filter '/spec/'
|
28
29
|
end if ENV["COVERAGE"]
|
29
30
|
|
30
|
-
$logger = ZTK::Logger.new("test.log")
|
31
|
+
$logger = ZTK::Logger.new(File.join("/tmp", "test.log"))
|
31
32
|
|
32
33
|
################################################################################
|
@@ -0,0 +1,90 @@
|
|
1
|
+
################################################################################
|
2
|
+
#
|
3
|
+
# Author: Zachary Patten <zachary@jovelabs.net>
|
4
|
+
# Copyright: Copyright (c) Jove Labs
|
5
|
+
# License: Apache License, Version 2.0
|
6
|
+
#
|
7
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
8
|
+
# you may not use this file except in compliance with the License.
|
9
|
+
# You may obtain a copy of the License at
|
10
|
+
#
|
11
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
12
|
+
#
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
16
|
+
# See the License for the specific language governing permissions and
|
17
|
+
# limitations under the License.
|
18
|
+
#
|
19
|
+
################################################################################
|
20
|
+
|
21
|
+
require "spec_helper"
|
22
|
+
|
23
|
+
describe ZTK::Benchmark do
|
24
|
+
|
25
|
+
subject { ZTK::Benchmark }
|
26
|
+
|
27
|
+
before(:all) do
|
28
|
+
$stdout = File.open("/dev/null", "w")
|
29
|
+
$stderr = File.open("/dev/null", "w")
|
30
|
+
$stdin = File.open("/dev/null", "r")
|
31
|
+
end
|
32
|
+
|
33
|
+
describe "class" do
|
34
|
+
|
35
|
+
it "should be ZTK::Benchmark" do
|
36
|
+
subject.should be ZTK::Benchmark
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
describe "behaviour" do
|
42
|
+
|
43
|
+
it "should throw an exception if executed without a block" do
|
44
|
+
lambda {
|
45
|
+
ZTK::Benchmark.bench
|
46
|
+
}.should raise_error ZTK::BenchmarkError, "You must supply a block!"
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should return the benchmark of the given block" do
|
50
|
+
mark = ZTK::Benchmark.bench do
|
51
|
+
sleep(0.1)
|
52
|
+
end
|
53
|
+
mark.should be_an_instance_of Float
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should not write to STDOUT if not given a message or mark" do
|
57
|
+
stdout = StringIO.new
|
58
|
+
ZTK::Benchmark.bench(:stdout => stdout) do
|
59
|
+
sleep(0.1)
|
60
|
+
end
|
61
|
+
stdout.size.should == 0
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should not write to STDOUT if not given a message" do
|
65
|
+
stdout = StringIO.new
|
66
|
+
ZTK::Benchmark.bench(:stdout => stdout, :mark => "%0.4f") do
|
67
|
+
sleep(0.1)
|
68
|
+
end
|
69
|
+
stdout.size.should == 0
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should not write to STDOUT if not given a mark" do
|
73
|
+
stdout = StringIO.new
|
74
|
+
ZTK::Benchmark.bench(:stdout => stdout, :message => "Hello World") do
|
75
|
+
sleep(0.1)
|
76
|
+
end
|
77
|
+
stdout.size.should == 0
|
78
|
+
end
|
79
|
+
|
80
|
+
it "should write to STDOUT if given a message and mark" do
|
81
|
+
stdout = StringIO.new
|
82
|
+
ZTK::Benchmark.bench(:stdout => stdout, :message => "Hello World", :mark => "%0.4f") do
|
83
|
+
sleep(0.1)
|
84
|
+
end
|
85
|
+
stdout.size.should > 0
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
data/spec/ztk/logger_spec.rb
CHANGED
@@ -31,7 +31,10 @@ describe ZTK::Logger do
|
|
31
31
|
:error => "This is a test error message",
|
32
32
|
:fatal => "This is a test fatal message"
|
33
33
|
}
|
34
|
-
@logfile = "/tmp
|
34
|
+
@logfile = File.join("/tmp", "logger.log")
|
35
|
+
end
|
36
|
+
|
37
|
+
before(:each) do
|
35
38
|
File.exists?(@logfile) && File.delete(@logfile)
|
36
39
|
end
|
37
40
|
|
@@ -43,6 +46,10 @@ describe ZTK::Logger do
|
|
43
46
|
subject.should be_an_instance_of ZTK::Logger
|
44
47
|
end
|
45
48
|
|
49
|
+
it "should provide access to the raw log file handle" do
|
50
|
+
subject.logdev.should be_an_instance_of File
|
51
|
+
end
|
52
|
+
|
46
53
|
end
|
47
54
|
|
48
55
|
describe "general logging functionality" do
|
@@ -74,9 +81,19 @@ describe ZTK::Logger do
|
|
74
81
|
|
75
82
|
end
|
76
83
|
|
84
|
+
describe "speciality logging functionality" do
|
85
|
+
|
86
|
+
it "should allow writing directly to the log device" do
|
87
|
+
data = "Hello World"
|
88
|
+
IO.write(subject.logdev, data)
|
89
|
+
IO.read(@logfile).match(data).should_not be nil
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
93
|
+
|
77
94
|
describe "log message" do
|
78
95
|
|
79
|
-
before(:
|
96
|
+
before(:each) do
|
80
97
|
subject.debug { @messages[:debug] }
|
81
98
|
end
|
82
99
|
|
@@ -0,0 +1,84 @@
|
|
1
|
+
################################################################################
|
2
|
+
#
|
3
|
+
# Author: Zachary Patten <zachary@jovelabs.net>
|
4
|
+
# Copyright: Copyright (c) Jove Labs
|
5
|
+
# License: Apache License, Version 2.0
|
6
|
+
#
|
7
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
8
|
+
# you may not use this file except in compliance with the License.
|
9
|
+
# You may obtain a copy of the License at
|
10
|
+
#
|
11
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
12
|
+
#
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
16
|
+
# See the License for the specific language governing permissions and
|
17
|
+
# limitations under the License.
|
18
|
+
#
|
19
|
+
################################################################################
|
20
|
+
|
21
|
+
require "spec_helper"
|
22
|
+
|
23
|
+
describe ZTK::RescueRetry do
|
24
|
+
|
25
|
+
subject { ZTK::RescueRetry }
|
26
|
+
|
27
|
+
before(:all) do
|
28
|
+
$stdout = File.open("/dev/null", "w")
|
29
|
+
$stderr = File.open("/dev/null", "w")
|
30
|
+
$stdin = File.open("/dev/null", "r")
|
31
|
+
end
|
32
|
+
|
33
|
+
describe "class" do
|
34
|
+
|
35
|
+
it "should be ZTK::RescueRetry" do
|
36
|
+
subject.should be ZTK::RescueRetry
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
describe "behaviour" do
|
42
|
+
|
43
|
+
it "should throw an exception if executed without a block" do
|
44
|
+
lambda {
|
45
|
+
ZTK::RescueRetry.try(:tries => 5)
|
46
|
+
}.should raise_error ZTK::RescueRetryError, "You must supply a block!"
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should retry on all exceptions" do
|
50
|
+
$counter = 0
|
51
|
+
lambda {
|
52
|
+
ZTK::RescueRetry.try(:tries => 3) do
|
53
|
+
$counter += 1
|
54
|
+
raise "TestException"
|
55
|
+
end
|
56
|
+
}.should raise_error "TestException"
|
57
|
+
$counter.should == 3
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should retry on specific exceptions" do
|
61
|
+
$counter = 0
|
62
|
+
lambda {
|
63
|
+
ZTK::RescueRetry.try(:tries => 3, :on => EOFError) do
|
64
|
+
$counter += 1
|
65
|
+
raise EOFError
|
66
|
+
end
|
67
|
+
}.should raise_error EOFError
|
68
|
+
$counter.should == 3
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should not retry on specific exceptions if exceptions do not match" do
|
72
|
+
$counter = 0
|
73
|
+
lambda {
|
74
|
+
ZTK::RescueRetry.try(:tries => 3, :on => EOFError) do
|
75
|
+
$counter += 1
|
76
|
+
raise "TestException"
|
77
|
+
end
|
78
|
+
}.should raise_error "TestException"
|
79
|
+
$counter.should == 1
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
data/spec/ztk/ssh_spec.rb
CHANGED
@@ -63,36 +63,174 @@ describe ZTK::SSH do
|
|
63
63
|
# this stuff doesn't work as is under travis-ci
|
64
64
|
if !ENV['CI'] && !ENV['TRAVIS']
|
65
65
|
|
66
|
-
describe "behaviour" do
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
config
|
73
|
-
|
66
|
+
describe "direct behaviour" do
|
67
|
+
|
68
|
+
describe "execute" do
|
69
|
+
|
70
|
+
it "should be able to connect to 127.0.0.1 as the current user and execute a command (your key must be in ssh-agent)" do
|
71
|
+
stdout, stderr = StringIO.new, StringIO.new
|
72
|
+
subject.config do |config|
|
73
|
+
config.stdout = stdout
|
74
|
+
config.stderr = stderr
|
75
|
+
config.user = ENV["USER"]
|
76
|
+
config.host_name = "127.0.0.1"
|
77
|
+
end
|
78
|
+
|
79
|
+
data = %x(hostname -f).chomp
|
80
|
+
|
81
|
+
status = subject.exec("hostname -f")
|
82
|
+
status.exit.exitstatus.should == 0
|
83
|
+
stdout.rewind
|
84
|
+
stdout.read.chomp.should == data
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
88
|
+
|
89
|
+
describe "upload" do
|
90
|
+
|
91
|
+
it "should be able to upload a file to 127.0.0.1 as the current user and execute a command (your key must be in ssh-agent)" do
|
92
|
+
stdout, stderr = StringIO.new, StringIO.new
|
93
|
+
subject.config do |config|
|
94
|
+
config.stdout = stdout
|
95
|
+
config.stderr = stderr
|
96
|
+
config.user = ENV["USER"]
|
97
|
+
config.host_name = "127.0.0.1"
|
98
|
+
end
|
99
|
+
|
100
|
+
data = "Hello World"
|
101
|
+
|
102
|
+
remote_file = File.join("/tmp", "ssh-upload-remote")
|
103
|
+
File.exists?(remote_file) && File.delete(remote_file)
|
104
|
+
|
105
|
+
local_file = File.join("/tmp", "ssh-upload-local")
|
106
|
+
IO.write(local_file, data)
|
107
|
+
|
108
|
+
File.exists?(remote_file).should == false
|
109
|
+
subject.upload(local_file, remote_file)
|
110
|
+
File.exists?(remote_file).should == true
|
111
|
+
|
112
|
+
File.exists?(remote_file) && File.delete(remote_file)
|
113
|
+
File.exists?(local_file) && File.delete(local_file)
|
114
|
+
end
|
115
|
+
|
116
|
+
end
|
117
|
+
|
118
|
+
describe "download" do
|
119
|
+
|
120
|
+
it "should be able to download a file from 127.0.0.1 as the current user and execute a command (your key must be in ssh-agent)" do
|
121
|
+
stdout, stderr = StringIO.new, StringIO.new
|
122
|
+
subject.config do |config|
|
123
|
+
config.stdout = stdout
|
124
|
+
config.stderr = stderr
|
125
|
+
config.user = ENV["USER"]
|
126
|
+
config.host_name = "127.0.0.1"
|
127
|
+
end
|
128
|
+
|
129
|
+
data = "Hello World"
|
130
|
+
|
131
|
+
local_file = File.join("/tmp", "ssh-download-local")
|
132
|
+
File.exists?(local_file) && File.delete(local_file)
|
133
|
+
|
134
|
+
remote_file = File.join("/tmp", "ssh-download-remote")
|
135
|
+
IO.write(remote_file, data)
|
136
|
+
|
137
|
+
File.exists?(local_file).should == false
|
138
|
+
subject.download(remote_file, local_file)
|
139
|
+
File.exists?(local_file).should == true
|
140
|
+
|
141
|
+
File.exists?(local_file) && File.delete(local_file)
|
142
|
+
File.exists?(remote_file) && File.delete(remote_file)
|
143
|
+
end
|
144
|
+
|
145
|
+
end
|
146
|
+
|
147
|
+
end
|
148
|
+
|
149
|
+
describe "proxy behaviour" do
|
150
|
+
|
151
|
+
describe "execute" do
|
152
|
+
|
153
|
+
it "should be able to proxy through 127.0.0.1, connecting to 127.0.0.1 as the current user and execute a command (your key must be in ssh-agent)" do
|
154
|
+
stdout, stderr = StringIO.new, StringIO.new
|
155
|
+
subject.config do |config|
|
156
|
+
config.stdout = stdout
|
157
|
+
config.stderr = stderr
|
158
|
+
config.user = ENV["USER"]
|
159
|
+
config.host_name = "127.0.0.1"
|
160
|
+
config.proxy_user = ENV["USER"]
|
161
|
+
config.proxy_host_name = "127.0.0.1"
|
162
|
+
end
|
163
|
+
|
164
|
+
data = %x( hostname -f ).chomp
|
165
|
+
|
166
|
+
status = subject.exec("hostname -f")
|
167
|
+
status.exit.exitstatus.should == 0
|
168
|
+
stdout.rewind
|
169
|
+
stdout.read.chomp.should == data
|
170
|
+
end
|
171
|
+
|
172
|
+
end
|
173
|
+
|
174
|
+
describe "upload" do
|
175
|
+
|
176
|
+
it "should be able to upload a file to 127.0.0.1 as the current user and execute a command (your key must be in ssh-agent)" do
|
177
|
+
stdout, stderr = StringIO.new, StringIO.new
|
178
|
+
subject.config do |config|
|
179
|
+
config.stdout = stdout
|
180
|
+
config.stderr = stderr
|
181
|
+
config.user = ENV["USER"]
|
182
|
+
config.host_name = "127.0.0.1"
|
183
|
+
config.proxy_user = ENV["USER"]
|
184
|
+
config.proxy_host_name = "127.0.0.1"
|
185
|
+
end
|
186
|
+
|
187
|
+
data = "Hello World"
|
188
|
+
|
189
|
+
remote_file = File.join("/tmp", "ssh-upload-remote")
|
190
|
+
File.exists?(remote_file) && File.delete(remote_file)
|
191
|
+
|
192
|
+
local_file = File.join("/tmp", "ssh-upload-local")
|
193
|
+
IO.write(local_file, data)
|
194
|
+
|
195
|
+
File.exists?(remote_file).should == false
|
196
|
+
subject.upload(local_file, remote_file)
|
197
|
+
File.exists?(remote_file).should == true
|
198
|
+
|
199
|
+
File.exists?(remote_file) && File.delete(remote_file)
|
200
|
+
File.exists?(local_file) && File.delete(local_file)
|
74
201
|
end
|
75
|
-
|
76
|
-
status = subject.exec("hostname -f")
|
77
|
-
status.exit.exitstatus.should == 0
|
78
|
-
stdout.rewind
|
79
|
-
stdout.read.chomp.should == hostname
|
202
|
+
|
80
203
|
end
|
81
204
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
config
|
87
|
-
|
88
|
-
|
89
|
-
|
205
|
+
describe "download" do
|
206
|
+
|
207
|
+
it "should be able to download a file from 127.0.0.1 as the current user and execute a command (your key must be in ssh-agent)" do
|
208
|
+
stdout, stderr = StringIO.new, StringIO.new
|
209
|
+
subject.config do |config|
|
210
|
+
config.stdout = stdout
|
211
|
+
config.stderr = stderr
|
212
|
+
config.user = ENV["USER"]
|
213
|
+
config.host_name = "127.0.0.1"
|
214
|
+
config.proxy_user = ENV["USER"]
|
215
|
+
config.proxy_host_name = "127.0.0.1"
|
216
|
+
end
|
217
|
+
|
218
|
+
data = "Hello World"
|
219
|
+
|
220
|
+
local_file = File.join("/tmp", "ssh-download-local")
|
221
|
+
File.exists?(local_file) && File.delete(local_file)
|
222
|
+
|
223
|
+
remote_file = File.join("/tmp", "ssh-download-remote")
|
224
|
+
IO.write(remote_file, data)
|
225
|
+
|
226
|
+
File.exists?(local_file).should == false
|
227
|
+
subject.download(remote_file, local_file)
|
228
|
+
File.exists?(local_file).should == true
|
229
|
+
|
230
|
+
File.exists?(local_file) && File.delete(local_file)
|
231
|
+
File.exists?(remote_file) && File.delete(remote_file)
|
90
232
|
end
|
91
|
-
|
92
|
-
status = subject.exec("hostname -f")
|
93
|
-
status.exit.exitstatus.should == 0
|
94
|
-
stdout.rewind
|
95
|
-
stdout.read.chomp.should == hostname
|
233
|
+
|
96
234
|
end
|
97
235
|
|
98
236
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ztk
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-01-
|
12
|
+
date: 2013-01-24 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: erubis
|
@@ -179,6 +179,7 @@ files:
|
|
179
179
|
- lib/ztk/config.rb
|
180
180
|
- lib/ztk/logger.rb
|
181
181
|
- lib/ztk/parallel.rb
|
182
|
+
- lib/ztk/rescue_retry.rb
|
182
183
|
- lib/ztk/spinner.rb
|
183
184
|
- lib/ztk/ssh.rb
|
184
185
|
- lib/ztk/tcp_socket_check.rb
|
@@ -187,10 +188,12 @@ files:
|
|
187
188
|
- spec/spec_helper.rb
|
188
189
|
- spec/support/test-config.rb
|
189
190
|
- spec/support/test-template.txt.erb
|
191
|
+
- spec/ztk/benchmark_spec.rb
|
190
192
|
- spec/ztk/command_spec.rb
|
191
193
|
- spec/ztk/config_spec.rb
|
192
194
|
- spec/ztk/logger_spec.rb
|
193
195
|
- spec/ztk/parallel_spec.rb
|
196
|
+
- spec/ztk/rescue_retry_spec.rb
|
194
197
|
- spec/ztk/ssh_spec.rb
|
195
198
|
- spec/ztk/tcp_socket_check_spec.rb
|
196
199
|
- spec/ztk/template_spec.rb
|
@@ -209,7 +212,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
209
212
|
version: '0'
|
210
213
|
segments:
|
211
214
|
- 0
|
212
|
-
hash: -
|
215
|
+
hash: -981934524806514468
|
213
216
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
214
217
|
none: false
|
215
218
|
requirements:
|
@@ -218,7 +221,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
218
221
|
version: '0'
|
219
222
|
segments:
|
220
223
|
- 0
|
221
|
-
hash: -
|
224
|
+
hash: -981934524806514468
|
222
225
|
requirements: []
|
223
226
|
rubyforge_project:
|
224
227
|
rubygems_version: 1.8.24
|
@@ -229,10 +232,12 @@ test_files:
|
|
229
232
|
- spec/spec_helper.rb
|
230
233
|
- spec/support/test-config.rb
|
231
234
|
- spec/support/test-template.txt.erb
|
235
|
+
- spec/ztk/benchmark_spec.rb
|
232
236
|
- spec/ztk/command_spec.rb
|
233
237
|
- spec/ztk/config_spec.rb
|
234
238
|
- spec/ztk/logger_spec.rb
|
235
239
|
- spec/ztk/parallel_spec.rb
|
240
|
+
- spec/ztk/rescue_retry_spec.rb
|
236
241
|
- spec/ztk/ssh_spec.rb
|
237
242
|
- spec/ztk/tcp_socket_check_spec.rb
|
238
243
|
- spec/ztk/template_spec.rb
|