ztk 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/ztk.rb +8 -0
- data/lib/ztk/command.rb +2 -4
- data/lib/ztk/logger.rb +18 -20
- data/lib/ztk/spinner.rb +4 -18
- data/lib/ztk/ssh.rb +28 -14
- data/lib/ztk/tcp_socket_check.rb +49 -22
- data/lib/ztk/template.rb +26 -22
- data/lib/ztk/version.rb +1 -1
- metadata +3 -3
data/lib/ztk.rb
CHANGED
@@ -20,7 +20,14 @@
|
|
20
20
|
|
21
21
|
require "ztk/version"
|
22
22
|
|
23
|
+
# Main ZTK module
|
24
|
+
#
|
25
|
+
# @author Zachary Patten <zachary@jovelabs.com>
|
23
26
|
module ZTK
|
27
|
+
|
28
|
+
# ZTK error class
|
29
|
+
#
|
30
|
+
# @author Zachary Patten <zachary@jovelabs.com>
|
24
31
|
class Error < StandardError; end
|
25
32
|
|
26
33
|
autoload :Base, "ztk/base"
|
@@ -31,4 +38,5 @@ module ZTK
|
|
31
38
|
autoload :SSH, "ztk/ssh"
|
32
39
|
autoload :TCPSocketCheck, "ztk/tcp_socket_check"
|
33
40
|
autoload :Template, "ztk/template"
|
41
|
+
|
34
42
|
end
|
data/lib/ztk/command.rb
CHANGED
@@ -20,12 +20,10 @@
|
|
20
20
|
|
21
21
|
require "ostruct"
|
22
22
|
|
23
|
-
################################################################################
|
24
|
-
|
25
23
|
module ZTK
|
26
24
|
|
27
|
-
|
28
|
-
|
25
|
+
# ZTK::Command error class
|
26
|
+
# @author Zachary Patten <zachary@jovelabs.com>
|
29
27
|
class CommandError < Error; end
|
30
28
|
|
31
29
|
################################################################################
|
data/lib/ztk/logger.rb
CHANGED
@@ -20,27 +20,24 @@
|
|
20
20
|
|
21
21
|
require "logger"
|
22
22
|
|
23
|
-
################################################################################
|
24
|
-
|
25
23
|
module ZTK
|
26
24
|
|
27
|
-
|
28
|
-
|
25
|
+
# @author Zachary Patten <zachary@jovelabs.com>
|
29
26
|
class Logger < ::Logger
|
30
27
|
|
31
|
-
################################################################################
|
32
|
-
|
33
28
|
SEVERITIES = Severity.constants.inject([]) {|arr,c| arr[Severity.const_get(c)] = c; arr}
|
34
29
|
|
35
|
-
################################################################################
|
36
|
-
|
37
30
|
def initialize(*args)
|
38
31
|
super(*args)
|
39
32
|
set_log_level
|
40
33
|
end
|
41
34
|
|
42
|
-
################################################################################
|
43
35
|
|
36
|
+
private
|
37
|
+
|
38
|
+
# Parses caller entries, extracting the file, line number and method.
|
39
|
+
#
|
40
|
+
# @param [String] at Entry from the caller Array.
|
44
41
|
def parse_caller(at)
|
45
42
|
if /^(.+?):(\d+)(?::in `(.*)')?/ =~ at
|
46
43
|
file = Regexp.last_match[1]
|
@@ -52,15 +49,21 @@ module ZTK
|
|
52
49
|
end
|
53
50
|
end
|
54
51
|
|
55
|
-
|
56
|
-
|
52
|
+
# Writes a log message if the current log level is at or below the supplied
|
53
|
+
# severity.
|
54
|
+
#
|
55
|
+
# @param [Constant] severity Log level severity.
|
56
|
+
# @param [String] message Optional message to prefix the log entry with.
|
57
|
+
# @param [String] progname Optional name of the program to prefix the log
|
58
|
+
# entry with.
|
59
|
+
# @yieldreturn [String] The block should return the desired log message.
|
57
60
|
def add(severity, message = nil, progname = nil, &block)
|
58
61
|
return if (@level > severity)
|
59
62
|
|
60
63
|
called_by = parse_caller(caller[1])
|
61
64
|
msg = (block && block.call)
|
62
65
|
(msg.nil? || msg.strip.empty?) and return
|
63
|
-
message = [message, progname, msg].
|
66
|
+
message = [message, progname, msg].flatten.compact.join(": ")
|
64
67
|
message = "%19s.%06d|%05d|%5s|%s%s\n" % [Time.now.utc.strftime("%Y-%m-%d|%H:%M:%S"), Time.now.utc.usec, Process.pid, SEVERITIES[severity], called_by, message]
|
65
68
|
|
66
69
|
@logdev.write(message)
|
@@ -68,20 +71,15 @@ module ZTK
|
|
68
71
|
true
|
69
72
|
end
|
70
73
|
|
71
|
-
|
72
|
-
|
74
|
+
# Sets the log level.
|
75
|
+
#
|
76
|
+
# @param [String] level Log level to use.
|
73
77
|
def set_log_level(level=nil)
|
74
78
|
defined?(Rails) and (default = (Rails.env.production? ? "INFO" : "DEBUG")) or (default = "INFO")
|
75
79
|
log_level = (ENV['LOG_LEVEL'] || level || default)
|
76
80
|
self.level = ZTK::Logger.const_get(log_level.to_s.upcase)
|
77
81
|
end
|
78
82
|
|
79
|
-
################################################################################
|
80
|
-
|
81
83
|
end
|
82
84
|
|
83
|
-
################################################################################
|
84
|
-
|
85
85
|
end
|
86
|
-
|
87
|
-
################################################################################
|
data/lib/ztk/spinner.rb
CHANGED
@@ -20,24 +20,18 @@
|
|
20
20
|
|
21
21
|
require "ostruct"
|
22
22
|
|
23
|
-
################################################################################
|
24
|
-
|
25
23
|
module ZTK
|
26
24
|
|
27
|
-
|
28
|
-
|
25
|
+
# ZTK::Spinner error class
|
26
|
+
#
|
27
|
+
# @author Zachary Patten <zachary@jovelabs.com>
|
29
28
|
class SpinnerError < Error; end
|
30
29
|
|
31
|
-
|
32
|
-
|
30
|
+
# @author Zachary Patten <zachary@jovelabs.com>
|
33
31
|
class Spinner
|
34
32
|
|
35
|
-
################################################################################
|
36
|
-
|
37
33
|
class << self
|
38
34
|
|
39
|
-
################################################################################
|
40
|
-
|
41
35
|
def spin(stdout=$stdout)
|
42
36
|
charset = %w( | / - \\ )
|
43
37
|
count = 0
|
@@ -56,16 +50,8 @@ module ZTK
|
|
56
50
|
end
|
57
51
|
end
|
58
52
|
|
59
|
-
################################################################################
|
60
|
-
|
61
53
|
end
|
62
54
|
|
63
|
-
################################################################################
|
64
|
-
|
65
55
|
end
|
66
56
|
|
67
|
-
################################################################################
|
68
|
-
|
69
57
|
end
|
70
|
-
|
71
|
-
################################################################################
|
data/lib/ztk/ssh.rb
CHANGED
@@ -26,6 +26,8 @@ require "net/sftp"
|
|
26
26
|
module ZTK
|
27
27
|
|
28
28
|
# ZTK::SSH error class
|
29
|
+
#
|
30
|
+
# @author Zachary Patten <zachary@jovelabs.com>
|
29
31
|
class SSHError < Error; end
|
30
32
|
|
31
33
|
# We can get a new instance of SSH like so:
|
@@ -69,24 +71,36 @@ module ZTK
|
|
69
71
|
# ssh.config do |config|
|
70
72
|
# config.host_key_verify = true
|
71
73
|
# end
|
74
|
+
#
|
75
|
+
# @author Zachary Patten <zachary@jovelabs.com>
|
72
76
|
class SSH < ZTK::Base
|
73
77
|
|
74
78
|
# @param [Hash] config Configuration options hash.
|
75
79
|
# @option config [String] :host_name Server hostname to connect to.
|
76
80
|
# @option config [String] :user Username to use for authentication.
|
77
|
-
# @option config [String, Array<String>] :keys A single or series of
|
81
|
+
# @option config [String, Array<String>] :keys A single or series of
|
82
|
+
# identity files to use for authentication.
|
78
83
|
# @option config [String] :password Password to use for authentication.
|
79
84
|
# @option config [Integer] :timeout SSH connection timeout to use.
|
80
|
-
# @option config [Boolean] :compression Weither or not to use compression
|
81
|
-
#
|
85
|
+
# @option config [Boolean] :compression Weither or not to use compression
|
86
|
+
# for this session.
|
87
|
+
# @option config [Integer] :compression_level What level of compression to
|
88
|
+
# use.
|
82
89
|
# @option config [String] :proxy_host_name Server hostname to proxy through.
|
83
|
-
# @option config [String] :proxy_user Username to use for proxy
|
84
|
-
#
|
90
|
+
# @option config [String] :proxy_user Username to use for proxy
|
91
|
+
# authentication.
|
92
|
+
# @option config [String, Array<String>] :proxy_keys A single or series of
|
93
|
+
# identity files to use for authentication with the proxy.
|
85
94
|
def initialize(config={})
|
86
|
-
super(
|
95
|
+
super({
|
96
|
+
:forward_agent => true,
|
97
|
+
:compression => false,
|
98
|
+
:user_known_hosts_file => '/dev/null'
|
99
|
+
}.merge(config))
|
87
100
|
end
|
88
101
|
|
89
|
-
# Launches an SSH console, replacing the current process with the console
|
102
|
+
# Launches an SSH console, replacing the current process with the console
|
103
|
+
# process.
|
90
104
|
#
|
91
105
|
# @example Launch a console:
|
92
106
|
# $logger = ZTK::Logger.new(STDOUT)
|
@@ -107,9 +121,13 @@ module ZTK
|
|
107
121
|
#
|
108
122
|
# @param [String] command The command to execute.
|
109
123
|
# @param [Hash] options The options hash for executing the command.
|
110
|
-
# @option options [Boolean] :silence Squelch output to STDOUT and STDERR.
|
124
|
+
# @option options [Boolean] :silence Squelch output to STDOUT and STDERR.
|
125
|
+
# If the log level is :debug, STDOUT and STDERR will go to the log file
|
126
|
+
# regardless of this setting. STDOUT and STDERR are always returned in
|
127
|
+
# the output return value regardless of this setting.
|
111
128
|
#
|
112
|
-
# @return [OpenStruct#output] The output of the command, both STDOUT and
|
129
|
+
# @return [OpenStruct#output] The output of the command, both STDOUT and
|
130
|
+
# STDERR.
|
113
131
|
# @return [OpenStruct#exit] The exit status (i.e. $?).
|
114
132
|
#
|
115
133
|
# @example Execute a command:
|
@@ -296,11 +314,7 @@ module ZTK
|
|
296
314
|
log(:debug) { "ssh_options" }
|
297
315
|
log(:debug) { "config(#{@config.inspect})" }
|
298
316
|
|
299
|
-
options = {
|
300
|
-
:forward_agent => true,
|
301
|
-
:compression => false,
|
302
|
-
:user_known_hosts_file => '/dev/null'
|
303
|
-
}
|
317
|
+
options = {}
|
304
318
|
|
305
319
|
# These are plainly documented on the Net::SSH config class.
|
306
320
|
options.merge!(:encryption => @config.encryption) if @config.encryption
|
data/lib/ztk/tcp_socket_check.rb
CHANGED
@@ -18,34 +18,63 @@
|
|
18
18
|
#
|
19
19
|
################################################################################
|
20
20
|
|
21
|
-
require
|
22
|
-
|
23
|
-
################################################################################
|
21
|
+
require 'socket'
|
22
|
+
require 'timeout'
|
24
23
|
|
25
24
|
module ZTK
|
26
25
|
|
27
|
-
|
28
|
-
|
26
|
+
# ZTK::TCPSocketCheck error class
|
27
|
+
#
|
28
|
+
# @author Zachary Patten <zachary@jovelabs.com>
|
29
29
|
class TCPSocketCheckError < Error; end
|
30
30
|
|
31
|
-
|
32
|
-
|
31
|
+
# TCP Socket Checking Class
|
32
|
+
#
|
33
|
+
# Given a host and port we want to check, we can do something like this:
|
34
|
+
# sc = ZTK::TCPSocketCheck.new(:host => "www.github.com", :port => 22)
|
35
|
+
#
|
36
|
+
# Then if we want to check if this host is responding on the specified port:
|
37
|
+
# sc.ready? and puts("They are there!")
|
38
|
+
#
|
39
|
+
# This works well for protocols that spew forth some data right away for use
|
40
|
+
# to read. However, with certain protocols, such as HTTP, we need to send
|
41
|
+
# some data first before we get a response.
|
42
|
+
#
|
43
|
+
# Given we want to check a host and port that requires some giving before we
|
44
|
+
# can take:
|
45
|
+
# sc = ZTK::TCPSocketCheck.new(:host => "www.google.com", :port => 80, :data => "GET")
|
46
|
+
#
|
47
|
+
# Then if we want to check if this host is responding on the specified port:
|
48
|
+
# sc.ready? and puts("They are there!")
|
49
|
+
# The ready? methods timeout is bound to the configuration option *timeout*.
|
50
|
+
#
|
51
|
+
# If we are waiting for a service to come online, we can do this:
|
52
|
+
# sc.wait and puts("They are there!")
|
53
|
+
# The wait methods timeout is bound to the configuration option *wait*.
|
54
|
+
#
|
55
|
+
# @author Zachary Patten <zachary@jovelabs.com>
|
33
56
|
class TCPSocketCheck < ZTK::Base
|
34
57
|
|
35
|
-
|
36
|
-
|
58
|
+
# @param [Hash] config Configuration options hash.
|
59
|
+
# @option config [String] :host Host to connect to.
|
60
|
+
# @option config [Integer, String] :port Port to connect to.
|
61
|
+
# @option config [String] :data Data to send to host to provoke a response.
|
62
|
+
# @option config [Integer] :timeout (5) Set the IO select timeout.
|
63
|
+
# @option config [Integer] :wait (60) Set the amount of time before the wait
|
64
|
+
# method call will timeout.
|
37
65
|
def initialize(config={})
|
38
66
|
super({
|
39
|
-
:host => nil,
|
40
|
-
:port => nil,
|
41
|
-
:data => nil,
|
42
67
|
:timeout => 5,
|
43
68
|
:wait => 60
|
44
69
|
}.merge(config))
|
45
70
|
end
|
46
71
|
|
47
|
-
|
48
|
-
|
72
|
+
# Check to see if socket on the host and port specified is ready. This
|
73
|
+
# method will timeout and return false after the amount of seconds specified
|
74
|
+
# in *config.timeout* has passed if the socket has not become ready.
|
75
|
+
#
|
76
|
+
# @return [Boolean] Returns true or false depending on weither the socket
|
77
|
+
# is ready or not.
|
49
78
|
def ready?
|
50
79
|
if @config.host.nil?
|
51
80
|
message = "You must supply a host!"
|
@@ -76,8 +105,12 @@ module ZTK
|
|
76
105
|
(socket && socket.close)
|
77
106
|
end
|
78
107
|
|
79
|
-
|
80
|
-
|
108
|
+
# Wait for the socket on the host and port specified to become ready. This
|
109
|
+
# method will timeout and return false after the amount of seconds specified
|
110
|
+
# in *config.wait* has passed if the socket has not become ready.
|
111
|
+
#
|
112
|
+
# @return [Boolean] Returns true or false depending on weither the socket
|
113
|
+
# became ready or not.
|
81
114
|
def wait
|
82
115
|
log(:debug) { "waiting for socket to become available; timeout after #{@config.wait} seconds" }
|
83
116
|
Timeout.timeout(@config.wait) do
|
@@ -92,12 +125,6 @@ module ZTK
|
|
92
125
|
false
|
93
126
|
end
|
94
127
|
|
95
|
-
################################################################################
|
96
|
-
|
97
128
|
end
|
98
129
|
|
99
|
-
################################################################################
|
100
|
-
|
101
130
|
end
|
102
|
-
|
103
|
-
################################################################################
|
data/lib/ztk/template.rb
CHANGED
@@ -20,52 +20,56 @@
|
|
20
20
|
|
21
21
|
require "erubis"
|
22
22
|
|
23
|
-
################################################################################
|
24
|
-
|
25
23
|
module ZTK
|
26
24
|
|
27
|
-
|
28
|
-
|
25
|
+
# ZTK::Template error class
|
26
|
+
#
|
27
|
+
# @author Zachary Patten <zachary@jovelabs.com>
|
29
28
|
class TemplateError < Error; end
|
30
29
|
|
31
|
-
|
32
|
-
|
30
|
+
# Erubis Templating Class
|
31
|
+
#
|
32
|
+
# Given a template like this (i.e. "template.erb"):
|
33
|
+
# This is a test template!
|
34
|
+
# <%= @variable %>
|
35
|
+
#
|
36
|
+
# We can do this:
|
37
|
+
# ZTK::Template.render("template.erb", { :variable => "Hello World" })
|
38
|
+
#
|
39
|
+
# And get:
|
40
|
+
# This is a test template!
|
41
|
+
# Hello World
|
42
|
+
#
|
43
|
+
# @author Zachary Patten <zachary@jovelabs.com>
|
33
44
|
class Template
|
34
45
|
|
35
|
-
################################################################################
|
36
|
-
|
37
46
|
class << self
|
38
47
|
|
39
|
-
|
40
|
-
|
48
|
+
# Renders a template to a string.
|
49
|
+
#
|
50
|
+
# @param [String] template The ERB template to process.
|
51
|
+
# @param [Hash] context A hash containing key-pairs, for which the keys are turned into global variables with their respective values.
|
52
|
+
#
|
53
|
+
# @return [String] The evaulated template content.
|
41
54
|
def render(template, context=nil)
|
42
55
|
render_template(load_template(template), context)
|
43
56
|
end
|
44
57
|
|
45
|
-
|
58
|
+
|
46
59
|
private
|
47
|
-
################################################################################
|
48
60
|
|
61
|
+
# Loads the template files contents.
|
49
62
|
def load_template(template)
|
50
63
|
IO.read(template).chomp
|
51
64
|
end
|
52
65
|
|
53
|
-
|
54
|
-
|
66
|
+
# Renders the template through Erubis.
|
55
67
|
def render_template(template, context={})
|
56
68
|
Erubis::Eruby.new(template).evaluate(context)
|
57
69
|
end
|
58
70
|
|
59
|
-
################################################################################
|
60
|
-
|
61
71
|
end
|
62
72
|
|
63
|
-
################################################################################
|
64
|
-
|
65
73
|
end
|
66
74
|
|
67
|
-
################################################################################
|
68
|
-
|
69
75
|
end
|
70
|
-
|
71
|
-
################################################################################
|
data/lib/ztk/version.rb
CHANGED
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.7
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -205,7 +205,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
205
205
|
version: '0'
|
206
206
|
segments:
|
207
207
|
- 0
|
208
|
-
hash:
|
208
|
+
hash: -971283166384197742
|
209
209
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
210
210
|
none: false
|
211
211
|
requirements:
|
@@ -214,7 +214,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
214
214
|
version: '0'
|
215
215
|
segments:
|
216
216
|
- 0
|
217
|
-
hash:
|
217
|
+
hash: -971283166384197742
|
218
218
|
requirements: []
|
219
219
|
rubyforge_project:
|
220
220
|
rubygems_version: 1.8.24
|