ztk 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/ztk.rb +10 -9
- data/lib/ztk/base.rb +9 -9
- data/lib/ztk/command.rb +1 -1
- data/lib/ztk/config.rb +130 -0
- data/lib/ztk/parallel.rb +11 -2
- data/lib/ztk/spinner.rb +2 -1
- data/lib/ztk/ssh.rb +1 -1
- data/lib/ztk/tcp_socket_check.rb +1 -1
- data/lib/ztk/template.rb +1 -1
- data/lib/ztk/version.rb +1 -1
- data/spec/support/test-config.rb +2 -0
- data/spec/ztk/config_spec.rb +72 -0
- data/spec/ztk/ssh_spec.rb +34 -30
- metadata +9 -4
data/lib/ztk.rb
CHANGED
@@ -18,7 +18,7 @@
|
|
18
18
|
#
|
19
19
|
################################################################################
|
20
20
|
|
21
|
-
require
|
21
|
+
require 'ztk/version'
|
22
22
|
|
23
23
|
# Main ZTK module
|
24
24
|
#
|
@@ -30,13 +30,14 @@ module ZTK
|
|
30
30
|
# @author Zachary Patten <zachary@jovelabs.net>
|
31
31
|
class Error < StandardError; end
|
32
32
|
|
33
|
-
autoload :Base,
|
34
|
-
autoload :Command,
|
35
|
-
autoload :
|
36
|
-
autoload :
|
37
|
-
autoload :
|
38
|
-
autoload :
|
39
|
-
autoload :
|
40
|
-
autoload :
|
33
|
+
autoload :Base, 'ztk/base'
|
34
|
+
autoload :Command, 'ztk/command'
|
35
|
+
autoload :Config, 'ztk/config'
|
36
|
+
autoload :Logger, 'ztk/logger'
|
37
|
+
autoload :Parallel, 'ztk/parallel'
|
38
|
+
autoload :Spinner, 'ztk/spinner'
|
39
|
+
autoload :SSH, 'ztk/ssh'
|
40
|
+
autoload :TCPSocketCheck, 'ztk/tcp_socket_check'
|
41
|
+
autoload :Template, 'ztk/template'
|
41
42
|
|
42
43
|
end
|
data/lib/ztk/base.rb
CHANGED
@@ -22,7 +22,7 @@ require "ostruct"
|
|
22
22
|
|
23
23
|
module ZTK
|
24
24
|
|
25
|
-
# ZTK::Base
|
25
|
+
# ZTK::Base Error Class
|
26
26
|
class BaseError < Error; end
|
27
27
|
|
28
28
|
# ZTK Base Class
|
@@ -35,18 +35,18 @@ module ZTK
|
|
35
35
|
# and extend functionality as appropriate.
|
36
36
|
class Base
|
37
37
|
|
38
|
-
# @param [Hash] config
|
39
|
-
# @option config [IO] :stdout
|
40
|
-
# @option config [IO] :stderr
|
41
|
-
# @option config [IO] :stdin
|
42
|
-
# @option config [Logger] :logger
|
38
|
+
# @param [Hash] config Configuration options hash.
|
39
|
+
# @option config [IO] :stdout Instance of IO to be used for STDOUT.
|
40
|
+
# @option config [IO] :stderr Instance of IO to be used for STDERR.
|
41
|
+
# @option config [IO] :stdin Instance of IO to be used for STDIN.
|
42
|
+
# @option config [Logger] :logger Instance of Logger to be used for logging.
|
43
43
|
def initialize(config={})
|
44
|
-
defined?(Rails) and rails_logger = Rails.logger
|
44
|
+
# defined?(Rails) and rails_logger = Rails.logger
|
45
45
|
@config = OpenStruct.new({
|
46
46
|
:stdout => $stdout,
|
47
47
|
:stderr => $stderr,
|
48
48
|
:stdin => $stdin,
|
49
|
-
:logger =>
|
49
|
+
:logger => $logger
|
50
50
|
}.merge(config))
|
51
51
|
|
52
52
|
@config.stdout.respond_to?(:sync=) and @config.stdout.sync = true
|
@@ -85,7 +85,7 @@ module ZTK
|
|
85
85
|
if block_given?
|
86
86
|
@config.logger and @config.logger.method(method_name.to_sym).call { yield }
|
87
87
|
else
|
88
|
-
raise
|
88
|
+
raise BaseError, "You must supply a block to the log method!"
|
89
89
|
end
|
90
90
|
end
|
91
91
|
|
data/lib/ztk/command.rb
CHANGED
data/lib/ztk/config.rb
ADDED
@@ -0,0 +1,130 @@
|
|
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 'ostruct'
|
22
|
+
|
23
|
+
module ZTK
|
24
|
+
|
25
|
+
# ZTK::Config Error Class
|
26
|
+
#
|
27
|
+
# @author Zachary Patten <zachary@jovelabs.net>
|
28
|
+
class ConfigError < Error; end
|
29
|
+
|
30
|
+
# Configuration Module
|
31
|
+
#
|
32
|
+
# @author Zachary Patten <zachary@jovelabs.net>
|
33
|
+
module Config
|
34
|
+
|
35
|
+
# Extend base class with this module.
|
36
|
+
#
|
37
|
+
# This will add the *configuration* attribute to the base class and create
|
38
|
+
# a new *OpenStruct* object assigning it to the *configuration* attribute.
|
39
|
+
def self.extended(base)
|
40
|
+
class << base
|
41
|
+
attr_accessor :configuration
|
42
|
+
end
|
43
|
+
|
44
|
+
base.configuration = OpenStruct.new
|
45
|
+
end
|
46
|
+
|
47
|
+
# Loads a configuration from a file.
|
48
|
+
def from_file(filename)
|
49
|
+
self.instance_eval(IO.read(filename), filename, 1)
|
50
|
+
end
|
51
|
+
|
52
|
+
# Yields the configuration OpenStruct object to a block.
|
53
|
+
#
|
54
|
+
# @yield [configuration] Pass the configuration OpenStruct object to the
|
55
|
+
# specified block.
|
56
|
+
def config(&block)
|
57
|
+
block and block.call(self.configuration)
|
58
|
+
end
|
59
|
+
|
60
|
+
# Get the value of a configuration key.
|
61
|
+
#
|
62
|
+
# @param [Symbol, String] key A symbol or string of the configuration
|
63
|
+
# key to return.
|
64
|
+
#
|
65
|
+
# @return The value currently assigned to the configuration key.
|
66
|
+
def [](key)
|
67
|
+
_get(key)
|
68
|
+
end
|
69
|
+
|
70
|
+
# Set the value of a configuration key.
|
71
|
+
#
|
72
|
+
# @param [Symbol, String] key A symbol or string of the configuration
|
73
|
+
# key to set.
|
74
|
+
# @param value The value which you want to assign to the configuration
|
75
|
+
# key.
|
76
|
+
#
|
77
|
+
# @return The value assigned to the configuration key.
|
78
|
+
def []=(key, value)
|
79
|
+
_set(key, value)
|
80
|
+
end
|
81
|
+
|
82
|
+
# @see Hash#keys
|
83
|
+
def keys
|
84
|
+
self.configuration.send(:table).keys
|
85
|
+
end
|
86
|
+
|
87
|
+
# @see Hash#has_key?
|
88
|
+
def has_key?(key)
|
89
|
+
self.configuration.send(:table).has_key?(key)
|
90
|
+
end
|
91
|
+
|
92
|
+
# @see Hash#merge
|
93
|
+
def merge(hash)
|
94
|
+
self.configuration.send(:table).merge(hash)
|
95
|
+
end
|
96
|
+
|
97
|
+
# @see Hash#merge!
|
98
|
+
def merge!(hash)
|
99
|
+
self.configuration.send(:table).merge!(hash)
|
100
|
+
end
|
101
|
+
|
102
|
+
# Handles method calls for our configuration keys.
|
103
|
+
def method_missing(method_symbol, *method_args)
|
104
|
+
if method_args.length > 0
|
105
|
+
_set(method_symbol, method_args.first)
|
106
|
+
end
|
107
|
+
|
108
|
+
_get(method_symbol)
|
109
|
+
end
|
110
|
+
|
111
|
+
|
112
|
+
private
|
113
|
+
|
114
|
+
def _set(key, value)
|
115
|
+
key = key.to_s
|
116
|
+
(key =~ /=/) or key += '='
|
117
|
+
|
118
|
+
self.configuration.send(key.to_sym, value)
|
119
|
+
end
|
120
|
+
|
121
|
+
def _get(key)
|
122
|
+
key = key.to_s
|
123
|
+
(key !~ /=/) or key = key[0..-2]
|
124
|
+
|
125
|
+
self.configuration.send(key.to_sym)
|
126
|
+
end
|
127
|
+
|
128
|
+
end
|
129
|
+
|
130
|
+
end
|
data/lib/ztk/parallel.rb
CHANGED
@@ -22,7 +22,7 @@ require "base64"
|
|
22
22
|
|
23
23
|
module ZTK
|
24
24
|
|
25
|
-
# ZTK::Parallel
|
25
|
+
# ZTK::Parallel Error Class
|
26
26
|
#
|
27
27
|
# @author Zachary Patten <zachary@jovelabs.net>
|
28
28
|
class ParallelError < Error; end
|
@@ -62,6 +62,13 @@ module ZTK
|
|
62
62
|
# @author Zachary Patten <zachary@jovelabs.net>
|
63
63
|
class Parallel < ZTK::Base
|
64
64
|
|
65
|
+
MAX_FORKS = case RUBY_PLATFORM
|
66
|
+
when /darwin/ then
|
67
|
+
%x( sysctl hw.ncpu ).chomp.split(':').last.strip.to_i
|
68
|
+
when /linux/ then
|
69
|
+
%x( grep -c processor /proc/cpuinfo ).chomp.strip.to_i
|
70
|
+
end
|
71
|
+
|
65
72
|
# Result Set
|
66
73
|
attr_accessor :results
|
67
74
|
|
@@ -71,9 +78,11 @@ module ZTK
|
|
71
78
|
# @option config [Proc] :after_fork (nil) Proc to call after forking.
|
72
79
|
def initialize(config={})
|
73
80
|
super({
|
74
|
-
:max_forks =>
|
81
|
+
:max_forks => MAX_FORKS
|
75
82
|
}.merge(config))
|
76
83
|
|
84
|
+
raise ParallelError, "max_forks must be equal to or greater than one!" if @config.max_forks < 1
|
85
|
+
|
77
86
|
@forks = Array.new
|
78
87
|
@results = Array.new
|
79
88
|
GC.respond_to?(:copy_on_write_friendly=) and GC.copy_on_write_friendly = true
|
data/lib/ztk/spinner.rb
CHANGED
@@ -22,13 +22,14 @@ require "ostruct"
|
|
22
22
|
|
23
23
|
module ZTK
|
24
24
|
|
25
|
-
# ZTK::Spinner
|
25
|
+
# ZTK::Spinner Error Class
|
26
26
|
#
|
27
27
|
# @author Zachary Patten <zachary@jovelabs.net>
|
28
28
|
# @author Stephen Nelson-Smith <stephen@atalanta-systems.com>
|
29
29
|
class SpinnerError < Error; end
|
30
30
|
|
31
31
|
# Spinner Class
|
32
|
+
#
|
32
33
|
# @author Zachary Patten <zachary@jovelabs.net>
|
33
34
|
# @author Stephen Nelson-Smith <stephen@atalanta-systems.com>
|
34
35
|
class Spinner
|
data/lib/ztk/ssh.rb
CHANGED
data/lib/ztk/tcp_socket_check.rb
CHANGED
data/lib/ztk/template.rb
CHANGED
data/lib/ztk/version.rb
CHANGED
@@ -0,0 +1,72 @@
|
|
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::Config do
|
24
|
+
|
25
|
+
subject { class C; extend(ZTK::Config); end; C }
|
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 a kind of ZTK::Config" do
|
36
|
+
subject.should be_a_kind_of ZTK::Config
|
37
|
+
end
|
38
|
+
|
39
|
+
describe "default config" do
|
40
|
+
|
41
|
+
it "should have an OpenStruct object for holding the configuration" do
|
42
|
+
subject.configuration.should be_an_instance_of OpenStruct
|
43
|
+
subject.keys.length.should == 0
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
describe "behaviour" do
|
49
|
+
|
50
|
+
it "should allow setting of arbratary configuration keys" do
|
51
|
+
subject.thing = "something"
|
52
|
+
subject.thing.should == "something"
|
53
|
+
subject[:thing].should == "something"
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should allow hash bracket style access to configuration keys" do
|
57
|
+
subject[:thing] = "nothing"
|
58
|
+
subject[:thing].should == "nothing"
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should allow loading of configurations from disk" do
|
62
|
+
config_file = File.expand_path(File.join(File.dirname(__FILE__), "..", "support", "test-config.rb"))
|
63
|
+
subject.from_file(config_file)
|
64
|
+
subject.message.should == "Hello World"
|
65
|
+
subject.thing.should == 2
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
data/spec/ztk/ssh_spec.rb
CHANGED
@@ -60,39 +60,43 @@ describe ZTK::SSH do
|
|
60
60
|
|
61
61
|
end
|
62
62
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
63
|
+
# this stuff doesn't work as is under travis-ci
|
64
|
+
if !ENV['CI'] && !ENV['TRAVIS']
|
65
|
+
|
66
|
+
describe "behaviour" do
|
67
|
+
|
68
|
+
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
|
69
|
+
stdout = StringIO.new
|
70
|
+
subject.config do |config|
|
71
|
+
config.stdout = stdout
|
72
|
+
config.user = ENV["USER"]
|
73
|
+
config.host_name = "127.0.0.1"
|
74
|
+
end
|
75
|
+
hostname = %x( hostname -f ).chomp
|
76
|
+
status = subject.exec("hostname -f")
|
77
|
+
status.exit.exitstatus.should == 0
|
78
|
+
stdout.rewind
|
79
|
+
stdout.read.chomp.should == hostname
|
80
|
+
end
|
81
|
+
|
82
|
+
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
|
83
|
+
stdout = StringIO.new
|
84
|
+
subject.config do |config|
|
85
|
+
config.stdout = stdout
|
86
|
+
config.user = ENV["USER"]
|
87
|
+
config.host_name = "127.0.0.1"
|
88
|
+
config.proxy_user = ENV["USER"]
|
89
|
+
config.proxy_host_name = "127.0.0.1"
|
90
|
+
end
|
91
|
+
hostname = %x( hostname -f ).chomp
|
92
|
+
status = subject.exec("hostname -f")
|
93
|
+
status.exit.exitstatus.should == 0
|
94
|
+
stdout.rewind
|
95
|
+
stdout.read.chomp.should == hostname
|
96
|
+
end
|
67
97
|
|
68
|
-
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
|
69
|
-
stdout = StringIO.new
|
70
|
-
subject.config do |config|
|
71
|
-
config.stdout = stdout
|
72
|
-
config.user = ENV["USER"]
|
73
|
-
config.host_name = "127.0.0.1"
|
74
98
|
end
|
75
|
-
hostname = %x( hostname -f ).chomp
|
76
|
-
status = subject.exec("hostname -f")
|
77
|
-
status.exit.exitstatus.should == 0
|
78
|
-
stdout.rewind
|
79
|
-
stdout.read.chomp.should == hostname
|
80
|
-
end
|
81
99
|
|
82
|
-
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
|
83
|
-
stdout = StringIO.new
|
84
|
-
subject.config do |config|
|
85
|
-
config.stdout = stdout
|
86
|
-
config.user = ENV["USER"]
|
87
|
-
config.host_name = "127.0.0.1"
|
88
|
-
config.proxy_user = ENV["USER"]
|
89
|
-
config.proxy_host_name = "127.0.0.1"
|
90
|
-
end
|
91
|
-
hostname = %x( hostname -f ).chomp
|
92
|
-
status = subject.exec("hostname -f")
|
93
|
-
status.exit.exitstatus.should == 0
|
94
|
-
stdout.rewind
|
95
|
-
stdout.read.chomp.should == hostname
|
96
100
|
end
|
97
101
|
|
98
102
|
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.0.
|
4
|
+
version: 0.0.9
|
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: 2012-09-
|
12
|
+
date: 2012-09-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: erubis
|
@@ -175,6 +175,7 @@ files:
|
|
175
175
|
- lib/ztk.rb
|
176
176
|
- lib/ztk/base.rb
|
177
177
|
- lib/ztk/command.rb
|
178
|
+
- lib/ztk/config.rb
|
178
179
|
- lib/ztk/logger.rb
|
179
180
|
- lib/ztk/parallel.rb
|
180
181
|
- lib/ztk/spinner.rb
|
@@ -183,8 +184,10 @@ files:
|
|
183
184
|
- lib/ztk/template.rb
|
184
185
|
- lib/ztk/version.rb
|
185
186
|
- spec/spec_helper.rb
|
187
|
+
- spec/support/test-config.rb
|
186
188
|
- spec/support/test-template.txt.erb
|
187
189
|
- spec/ztk/command_spec.rb
|
190
|
+
- spec/ztk/config_spec.rb
|
188
191
|
- spec/ztk/logger_spec.rb
|
189
192
|
- spec/ztk/parallel_spec.rb
|
190
193
|
- spec/ztk/ssh_spec.rb
|
@@ -205,7 +208,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
205
208
|
version: '0'
|
206
209
|
segments:
|
207
210
|
- 0
|
208
|
-
hash: -
|
211
|
+
hash: -1649300180131473211
|
209
212
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
210
213
|
none: false
|
211
214
|
requirements:
|
@@ -214,7 +217,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
214
217
|
version: '0'
|
215
218
|
segments:
|
216
219
|
- 0
|
217
|
-
hash: -
|
220
|
+
hash: -1649300180131473211
|
218
221
|
requirements: []
|
219
222
|
rubyforge_project:
|
220
223
|
rubygems_version: 1.8.24
|
@@ -223,8 +226,10 @@ specification_version: 3
|
|
223
226
|
summary: Contains various classes and utilities I find I regularly need.
|
224
227
|
test_files:
|
225
228
|
- spec/spec_helper.rb
|
229
|
+
- spec/support/test-config.rb
|
226
230
|
- spec/support/test-template.txt.erb
|
227
231
|
- spec/ztk/command_spec.rb
|
232
|
+
- spec/ztk/config_spec.rb
|
228
233
|
- spec/ztk/logger_spec.rb
|
229
234
|
- spec/ztk/parallel_spec.rb
|
230
235
|
- spec/ztk/ssh_spec.rb
|