ztk 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/lib/ztk.rb +2 -0
- data/lib/ztk/base.rb +36 -16
- data/lib/ztk/command.rb +103 -0
- data/lib/ztk/parallel.rb +35 -29
- data/lib/ztk/spinner.rb +71 -0
- data/lib/ztk/ssh.rb +223 -91
- data/lib/ztk/tcp_socket_check.rb +12 -12
- data/lib/ztk/template.rb +2 -2
- data/lib/ztk/version.rb +1 -1
- data/spec/spec_helper.rb +0 -8
- data/spec/ztk/command_spec.rb +78 -0
- data/spec/ztk/parallel_spec.rb +29 -14
- data/spec/ztk/ssh_spec.rb +29 -12
- data/spec/ztk/tcp_socket_check_spec.rb +90 -25
- data/spec/ztk/template_spec.rb +7 -0
- data/ztk.gemspec +2 -0
- metadata +41 -5
- data/WIKI.md +0 -439
data/lib/ztk/tcp_socket_check.rb
CHANGED
@@ -30,7 +30,7 @@ module ZTK
|
|
30
30
|
|
31
31
|
################################################################################
|
32
32
|
|
33
|
-
class TCPSocketCheck < Base
|
33
|
+
class TCPSocketCheck < ZTK::Base
|
34
34
|
|
35
35
|
################################################################################
|
36
36
|
|
@@ -49,28 +49,28 @@ module ZTK
|
|
49
49
|
def ready?
|
50
50
|
if @config.host.nil?
|
51
51
|
message = "You must supply a host!"
|
52
|
-
|
52
|
+
log(:fatal) { message }
|
53
53
|
raise TCPSocketCheckError, message
|
54
54
|
end
|
55
55
|
|
56
56
|
if @config.port.nil?
|
57
57
|
message = "You must supply a port!"
|
58
|
-
|
58
|
+
log(:fatal) { message }
|
59
59
|
raise TCPSocketCheckError, message
|
60
60
|
end
|
61
61
|
|
62
|
-
socket =
|
62
|
+
socket = TCPSocket.new(@config.host, @config.port)
|
63
63
|
|
64
64
|
if @config.data.nil?
|
65
|
-
|
66
|
-
((
|
65
|
+
log(:debug) { "read(#{@config.host}:#{@config.port})" }
|
66
|
+
((IO.select([socket], nil, nil, @config.timeout) && socket.gets) ? true : false)
|
67
67
|
else
|
68
|
-
|
69
|
-
((
|
68
|
+
log(:debug) { "write(#{@config.host}:#{@config.port}, '#{@config.data}')" }
|
69
|
+
((IO.select(nil, [socket], nil, @config.timeout) && socket.write(@config.data)) ? true : false)
|
70
70
|
end
|
71
71
|
|
72
72
|
rescue Errno::ETIMEDOUT, Errno::ECONNREFUSED, Errno::EHOSTUNREACH => e
|
73
|
-
|
73
|
+
log(:debug) { "#{@config.host}:#{@config.port} - #{e.message}" }
|
74
74
|
false
|
75
75
|
ensure
|
76
76
|
(socket && socket.close)
|
@@ -79,16 +79,16 @@ module ZTK
|
|
79
79
|
################################################################################
|
80
80
|
|
81
81
|
def wait
|
82
|
-
|
82
|
+
log(:debug) { "waiting for socket to become available; timeout after #{@config.wait} seconds" }
|
83
83
|
Timeout.timeout(@config.wait) do
|
84
84
|
until ready?
|
85
|
-
|
85
|
+
log(:debug) { "sleeping 1 second" }
|
86
86
|
sleep(1)
|
87
87
|
end
|
88
88
|
end
|
89
89
|
true
|
90
90
|
rescue Timeout::Error => e
|
91
|
-
|
91
|
+
log(:warn) { "socket(#{@config.host}:#{@config.port}) timeout!" }
|
92
92
|
false
|
93
93
|
end
|
94
94
|
|
data/lib/ztk/template.rb
CHANGED
@@ -47,13 +47,13 @@ module ZTK
|
|
47
47
|
################################################################################
|
48
48
|
|
49
49
|
def load_template(template)
|
50
|
-
|
50
|
+
IO.read(template).chomp
|
51
51
|
end
|
52
52
|
|
53
53
|
################################################################################
|
54
54
|
|
55
55
|
def render_template(template, context={})
|
56
|
-
|
56
|
+
Erubis::Eruby.new(template).evaluate(context)
|
57
57
|
end
|
58
58
|
|
59
59
|
################################################################################
|
data/lib/ztk/version.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -22,14 +22,6 @@ require "ztk"
|
|
22
22
|
|
23
23
|
################################################################################
|
24
24
|
|
25
|
-
$logger = ZTK::Logger.new("/dev/null")
|
26
|
-
|
27
|
-
dev_null = File.open("/dev/null", "w")
|
28
|
-
$stdout = dev_null
|
29
|
-
$stderr = dev_null
|
30
|
-
|
31
|
-
################################################################################
|
32
|
-
|
33
25
|
require 'simplecov'
|
34
26
|
SimpleCov.start do
|
35
27
|
add_filter '/spec/'
|
@@ -0,0 +1,78 @@
|
|
1
|
+
################################################################################
|
2
|
+
#
|
3
|
+
# Author: Zachary Patten <zachary@jovelabs.com>
|
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::Command do
|
24
|
+
|
25
|
+
subject { ZTK::Command.new }
|
26
|
+
|
27
|
+
before(:all) do
|
28
|
+
$logger = ZTK::Logger.new("/dev/null")
|
29
|
+
$stdout = File.open("/dev/null", "w")
|
30
|
+
$stderr = File.open("/dev/null", "w")
|
31
|
+
$stdin = File.open("/dev/null", "r")
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "class" do
|
35
|
+
|
36
|
+
it "should be an instance of ZTK::Command" do
|
37
|
+
subject.should be_an_instance_of ZTK::Command
|
38
|
+
end
|
39
|
+
|
40
|
+
describe "default config" do
|
41
|
+
|
42
|
+
it "should use $stdout as the default" do
|
43
|
+
subject.config.stdout.should be_a_kind_of $stdout.class
|
44
|
+
subject.config.stdout.should == $stdout
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should use $stderr as the default" do
|
48
|
+
subject.config.stderr.should be_a_kind_of $stderr.class
|
49
|
+
subject.config.stderr.should == $stderr
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should use $stdin as the default" do
|
53
|
+
subject.config.stdin.should be_a_kind_of $stdin.class
|
54
|
+
subject.config.stdin.should == $stdin
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should use $logger as the default" do
|
58
|
+
subject.config.logger.should be_a_kind_of ZTK::Logger
|
59
|
+
subject.config.logger.should == $logger
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should be able to execute the command \"hostname -f\"" do
|
67
|
+
stdout = StringIO.new
|
68
|
+
subject.config do |config|
|
69
|
+
config.stdout = stdout
|
70
|
+
end
|
71
|
+
hostname = %x( hostname -f ).chomp
|
72
|
+
status = subject.exec("hostname -f")
|
73
|
+
status.exitstatus.should == 0
|
74
|
+
stdout.rewind
|
75
|
+
stdout.read.chomp.should == hostname
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
data/spec/ztk/parallel_spec.rb
CHANGED
@@ -24,6 +24,13 @@ describe ZTK::Parallel do
|
|
24
24
|
|
25
25
|
subject { ZTK::Parallel.new }
|
26
26
|
|
27
|
+
before(:all) do
|
28
|
+
$logger = ZTK::Logger.new("/dev/null")
|
29
|
+
$stdout = File.open("/dev/null", "w")
|
30
|
+
$stderr = File.open("/dev/null", "w")
|
31
|
+
$stdin = File.open("/dev/null", "r")
|
32
|
+
end
|
33
|
+
|
27
34
|
describe "class" do
|
28
35
|
|
29
36
|
it "should be an instance of ZTK::Parallel" do
|
@@ -32,22 +39,22 @@ describe ZTK::Parallel do
|
|
32
39
|
|
33
40
|
describe "default config" do
|
34
41
|
|
35
|
-
it "should use $stdout as the default
|
42
|
+
it "should use $stdout as the default" do
|
36
43
|
subject.config.stdout.should be_a_kind_of $stdout.class
|
37
44
|
subject.config.stdout.should == $stdout
|
38
45
|
end
|
39
46
|
|
40
|
-
it "should use $stderr as the default
|
47
|
+
it "should use $stderr as the default" do
|
41
48
|
subject.config.stderr.should be_a_kind_of $stderr.class
|
42
49
|
subject.config.stderr.should == $stderr
|
43
50
|
end
|
44
51
|
|
45
|
-
it "should use $stdin as the default
|
52
|
+
it "should use $stdin as the default" do
|
46
53
|
subject.config.stdin.should be_a_kind_of $stdin.class
|
47
54
|
subject.config.stdin.should == $stdin
|
48
55
|
end
|
49
56
|
|
50
|
-
it "should use $logger as the default
|
57
|
+
it "should use $logger as the default" do
|
51
58
|
subject.config.logger.should be_a_kind_of ZTK::Logger
|
52
59
|
subject.config.logger.should == $logger
|
53
60
|
end
|
@@ -56,18 +63,26 @@ describe ZTK::Parallel do
|
|
56
63
|
|
57
64
|
end
|
58
65
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
66
|
+
describe "behaviour" do
|
67
|
+
|
68
|
+
it "should throw an exception if the process method is called without a block" do
|
69
|
+
lambda{ subject.process }.should raise_error ZTK::ParallelError, "You must supply a block to the process method!"
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should spawn multiple processes to handle each iteration" do
|
73
|
+
3.times do |x|
|
74
|
+
subject.process do
|
75
|
+
Process.pid
|
76
|
+
end
|
63
77
|
end
|
78
|
+
subject.waitall
|
79
|
+
puts subject.results.inspect
|
80
|
+
subject.results.all?{ |r| r.should be_kind_of Integer }
|
81
|
+
subject.results.all?{ |r| r.should > 0 }
|
82
|
+
subject.results.uniq.count.should == 3
|
83
|
+
subject.results.include?(Process.pid).should be false
|
64
84
|
end
|
65
|
-
|
66
|
-
puts subject.results.inspect
|
67
|
-
subject.results.all?{ |r| r.should be_kind_of Integer }
|
68
|
-
subject.results.all?{ |r| r.should > 0 }
|
69
|
-
subject.results.uniq.count.should == 3
|
70
|
-
subject.results.include?(Process.pid).should be false
|
85
|
+
|
71
86
|
end
|
72
87
|
|
73
88
|
end
|
data/spec/ztk/ssh_spec.rb
CHANGED
@@ -24,6 +24,13 @@ describe ZTK::SSH do
|
|
24
24
|
|
25
25
|
subject { ZTK::SSH.new }
|
26
26
|
|
27
|
+
before(:all) do
|
28
|
+
$logger = ZTK::Logger.new("test.log")
|
29
|
+
$stdout = File.open("/dev/null", "w")
|
30
|
+
$stderr = File.open("/dev/null", "w")
|
31
|
+
$stdin = File.open("/dev/null", "r")
|
32
|
+
end
|
33
|
+
|
27
34
|
describe "class" do
|
28
35
|
|
29
36
|
it "should be an instance of ZTK::SSH" do
|
@@ -32,22 +39,22 @@ describe ZTK::SSH do
|
|
32
39
|
|
33
40
|
describe "default config" do
|
34
41
|
|
35
|
-
it "should use $stdout as the default
|
42
|
+
it "should use $stdout as the default" do
|
36
43
|
subject.config.stdout.should be_a_kind_of $stdout.class
|
37
44
|
subject.config.stdout.should == $stdout
|
38
45
|
end
|
39
46
|
|
40
|
-
it "should use $stderr as the default
|
47
|
+
it "should use $stderr as the default" do
|
41
48
|
subject.config.stderr.should be_a_kind_of $stderr.class
|
42
49
|
subject.config.stderr.should == $stderr
|
43
50
|
end
|
44
51
|
|
45
|
-
it "should use $stdin as the default
|
52
|
+
it "should use $stdin as the default" do
|
46
53
|
subject.config.stdin.should be_a_kind_of $stdin.class
|
47
54
|
subject.config.stdin.should == $stdin
|
48
55
|
end
|
49
56
|
|
50
|
-
it "should use $logger as the default
|
57
|
+
it "should use $logger as the default" do
|
51
58
|
subject.config.logger.should be_a_kind_of ZTK::Logger
|
52
59
|
subject.config.logger.should == $logger
|
53
60
|
end
|
@@ -60,23 +67,33 @@ describe ZTK::SSH do
|
|
60
67
|
if !ENV['CI'] && !ENV['TRAVIS']
|
61
68
|
|
62
69
|
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
|
70
|
+
stdout = StringIO.new
|
63
71
|
subject.config do |config|
|
64
|
-
config.
|
65
|
-
config.
|
72
|
+
config.stdout = stdout
|
73
|
+
config.user = ENV["USER"]
|
74
|
+
config.host_name = "127.0.0.1"
|
66
75
|
end
|
67
76
|
hostname = %x( hostname -f ).chomp
|
68
|
-
subject.exec("hostname -f")
|
77
|
+
status = subject.exec("hostname -f")
|
78
|
+
status.exit.exitstatus.should == 0
|
79
|
+
stdout.rewind
|
80
|
+
stdout.read.chomp.should == hostname
|
69
81
|
end
|
70
82
|
|
71
83
|
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
|
84
|
+
stdout = StringIO.new
|
72
85
|
subject.config do |config|
|
73
|
-
config.
|
74
|
-
config.
|
75
|
-
config.
|
76
|
-
config.
|
86
|
+
config.stdout = stdout
|
87
|
+
config.user = ENV["USER"]
|
88
|
+
config.host_name = "127.0.0.1"
|
89
|
+
config.proxy_user = ENV["USER"]
|
90
|
+
config.proxy_host_name = "127.0.0.1"
|
77
91
|
end
|
78
92
|
hostname = %x( hostname -f ).chomp
|
79
|
-
subject.exec("hostname -f")
|
93
|
+
status = subject.exec("hostname -f")
|
94
|
+
status.exit.exitstatus.should == 0
|
95
|
+
stdout.rewind
|
96
|
+
stdout.read.chomp.should == hostname
|
80
97
|
end
|
81
98
|
|
82
99
|
end
|
@@ -22,22 +22,55 @@ require "spec_helper"
|
|
22
22
|
|
23
23
|
describe ZTK::TCPSocketCheck do
|
24
24
|
|
25
|
-
subject { ZTK::TCPSocketCheck }
|
25
|
+
subject { ZTK::TCPSocketCheck.new }
|
26
|
+
|
27
|
+
before(:all) do
|
28
|
+
$logger = ZTK::Logger.new("/dev/null")
|
29
|
+
$stdout = File.open("/dev/null", "w")
|
30
|
+
$stderr = File.open("/dev/null", "w")
|
31
|
+
$stdin = File.open("/dev/null", "r")
|
32
|
+
end
|
26
33
|
|
27
34
|
describe "class" do
|
28
35
|
|
29
|
-
it "should be ZTK::TCPSocketCheck" do
|
30
|
-
subject.should
|
36
|
+
it "should be an instance of ZTK::TCPSocketCheck" do
|
37
|
+
subject.should be_an_instance_of ZTK::TCPSocketCheck
|
38
|
+
end
|
39
|
+
|
40
|
+
describe "default config" do
|
41
|
+
|
42
|
+
it "should use $stdout as the default" do
|
43
|
+
subject.config.stdout.should be_a_kind_of $stdout.class
|
44
|
+
subject.config.stdout.should == $stdout
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should use $stderr as the default" do
|
48
|
+
subject.config.stderr.should be_a_kind_of $stderr.class
|
49
|
+
subject.config.stderr.should == $stderr
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should use $stdin as the default" do
|
53
|
+
subject.config.stdin.should be_a_kind_of $stdin.class
|
54
|
+
subject.config.stdin.should == $stdin
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should use $logger as the default" do
|
58
|
+
subject.config.logger.should be_a_kind_of ZTK::Logger
|
59
|
+
subject.config.logger.should == $logger
|
60
|
+
end
|
61
|
+
|
31
62
|
end
|
32
63
|
|
33
64
|
describe "config" do
|
34
65
|
|
35
66
|
it "should throw an exception if the host is not specified" do
|
36
|
-
|
67
|
+
subject.config.port = 22
|
68
|
+
lambda{ subject.ready? }.should raise_error ZTK::TCPSocketCheckError, "You must supply a host!"
|
37
69
|
end
|
38
70
|
|
39
71
|
it "should throw an exception if the port is not specified" do
|
40
|
-
|
72
|
+
subject.config.host = "127.0.0.1"
|
73
|
+
lambda{ subject.ready? }.should raise_error ZTK::TCPSocketCheckError, "You must supply a port!"
|
41
74
|
end
|
42
75
|
|
43
76
|
end
|
@@ -51,13 +84,19 @@ describe ZTK::TCPSocketCheck do
|
|
51
84
|
describe "read check" do
|
52
85
|
|
53
86
|
it "should return true on a remote read check to github.com:22" do
|
54
|
-
|
55
|
-
|
87
|
+
subject.config do |config|
|
88
|
+
config.host = "github.com"
|
89
|
+
config.port = 22
|
90
|
+
end
|
91
|
+
subject.ready?.should == true
|
56
92
|
end
|
57
93
|
|
58
94
|
it "should return false on a remote read check to 127.0.0.1:1" do
|
59
|
-
|
60
|
-
|
95
|
+
subject.config do |config|
|
96
|
+
config.host = "127.0.0.1"
|
97
|
+
config.port = 1
|
98
|
+
end
|
99
|
+
subject.ready?.should == false
|
61
100
|
end
|
62
101
|
|
63
102
|
end
|
@@ -65,13 +104,21 @@ describe ZTK::TCPSocketCheck do
|
|
65
104
|
describe "write check" do
|
66
105
|
|
67
106
|
it "should return true on a remote write check to www.google.com:80" do
|
68
|
-
|
69
|
-
|
107
|
+
subject.config do |config|
|
108
|
+
config.host = "www.google.com"
|
109
|
+
config.port = 80
|
110
|
+
config.data = "GET"
|
111
|
+
end
|
112
|
+
subject.ready?.should == true
|
70
113
|
end
|
71
114
|
|
72
115
|
it "should return false on a remote write check to 127.0.0.1:1" do
|
73
|
-
|
74
|
-
|
116
|
+
subject.config do |config|
|
117
|
+
config.host = "127.0.0.1"
|
118
|
+
config.port = 1
|
119
|
+
config.data = "GET"
|
120
|
+
end
|
121
|
+
subject.ready?.should == false
|
75
122
|
end
|
76
123
|
|
77
124
|
end
|
@@ -82,28 +129,46 @@ describe ZTK::TCPSocketCheck do
|
|
82
129
|
|
83
130
|
describe "read check" do
|
84
131
|
|
85
|
-
it "should
|
86
|
-
|
87
|
-
|
132
|
+
it "should return false on a read check to 127.0.0.1:1" do
|
133
|
+
subject.config do |config|
|
134
|
+
config.host = "127.0.0.1"
|
135
|
+
config.port = 1
|
136
|
+
config.wait = 5
|
137
|
+
end
|
138
|
+
subject.wait.should == false
|
88
139
|
end
|
89
140
|
|
90
|
-
it "should
|
91
|
-
|
92
|
-
|
141
|
+
it "should return true on a read check to github.com:22" do
|
142
|
+
subject.config do |config|
|
143
|
+
config.host = "github.com"
|
144
|
+
config.port = 22
|
145
|
+
config.wait = 5
|
146
|
+
end
|
147
|
+
subject.wait.should == true
|
93
148
|
end
|
94
149
|
|
95
150
|
end
|
96
151
|
|
97
152
|
describe "write check" do
|
98
153
|
|
99
|
-
it "should
|
100
|
-
|
101
|
-
|
154
|
+
it "should return false on a write check to 127.0.0.1:1" do
|
155
|
+
subject.config do |config|
|
156
|
+
config.host = "127.0.0.1"
|
157
|
+
config.port = 1
|
158
|
+
config.data = "GET"
|
159
|
+
config.wait = 5
|
160
|
+
end
|
161
|
+
subject.wait.should == false
|
102
162
|
end
|
103
163
|
|
104
|
-
it "should
|
105
|
-
|
106
|
-
|
164
|
+
it "should return true on a write check to www.google.com:80" do
|
165
|
+
subject.config do |config|
|
166
|
+
config.host = "www.google.com"
|
167
|
+
config.port = 80
|
168
|
+
config.data = "GET"
|
169
|
+
config.wait = 5
|
170
|
+
end
|
171
|
+
subject.wait.should == true
|
107
172
|
end
|
108
173
|
|
109
174
|
end
|