ztk 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -24,6 +24,13 @@ describe ZTK::Template do
24
24
 
25
25
  subject { ZTK::Template }
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 ZTK::Template" do
@@ -23,4 +23,6 @@ Gem::Specification.new do |gem|
23
23
  gem.add_development_dependency("rake")
24
24
  gem.add_development_dependency("rspec")
25
25
  gem.add_development_dependency("simplecov")
26
+ gem.add_development_dependency("yard")
27
+ gem.add_development_dependency("redcarpet")
26
28
  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.5
4
+ version: 0.0.6
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-01 00:00:00.000000000 Z
12
+ date: 2012-09-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: erubis
@@ -123,6 +123,38 @@ dependencies:
123
123
  - - ! '>='
124
124
  - !ruby/object:Gem::Version
125
125
  version: '0'
126
+ - !ruby/object:Gem::Dependency
127
+ name: yard
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ - !ruby/object:Gem::Dependency
143
+ name: redcarpet
144
+ requirement: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ! '>='
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ type: :development
151
+ prerelease: false
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ! '>='
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
126
158
  description: Zachary's Tool Kit
127
159
  email:
128
160
  - zachary@jovelabs.com
@@ -139,18 +171,20 @@ files:
139
171
  - LICENSE
140
172
  - README.md
141
173
  - Rakefile
142
- - WIKI.md
143
174
  - bin/ztk
144
175
  - lib/ztk.rb
145
176
  - lib/ztk/base.rb
177
+ - lib/ztk/command.rb
146
178
  - lib/ztk/logger.rb
147
179
  - lib/ztk/parallel.rb
180
+ - lib/ztk/spinner.rb
148
181
  - lib/ztk/ssh.rb
149
182
  - lib/ztk/tcp_socket_check.rb
150
183
  - lib/ztk/template.rb
151
184
  - lib/ztk/version.rb
152
185
  - spec/spec_helper.rb
153
186
  - spec/support/test-template.txt.erb
187
+ - spec/ztk/command_spec.rb
154
188
  - spec/ztk/logger_spec.rb
155
189
  - spec/ztk/parallel_spec.rb
156
190
  - spec/ztk/ssh_spec.rb
@@ -171,7 +205,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
171
205
  version: '0'
172
206
  segments:
173
207
  - 0
174
- hash: -3563770214735056009
208
+ hash: 3329543031620665477
175
209
  required_rubygems_version: !ruby/object:Gem::Requirement
176
210
  none: false
177
211
  requirements:
@@ -180,7 +214,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
180
214
  version: '0'
181
215
  segments:
182
216
  - 0
183
- hash: -3563770214735056009
217
+ hash: 3329543031620665477
184
218
  requirements: []
185
219
  rubyforge_project:
186
220
  rubygems_version: 1.8.24
@@ -190,8 +224,10 @@ summary: Contains various classes and utilities I find I regularly need.
190
224
  test_files:
191
225
  - spec/spec_helper.rb
192
226
  - spec/support/test-template.txt.erb
227
+ - spec/ztk/command_spec.rb
193
228
  - spec/ztk/logger_spec.rb
194
229
  - spec/ztk/parallel_spec.rb
195
230
  - spec/ztk/ssh_spec.rb
196
231
  - spec/ztk/tcp_socket_check_spec.rb
197
232
  - spec/ztk/template_spec.rb
233
+ has_rdoc:
data/WIKI.md DELETED
@@ -1,439 +0,0 @@
1
- [![Build Status](https://secure.travis-ci.org/jovelabs/ztk.png)](http://travis-ci.org/jovelabs/ztk)
2
-
3
- # ZTK
4
-
5
- Zachary's Tool Kit is a general purpose utility gem, featuring a collection of classes meant to simplify development of complex systems in Ruby.
6
-
7
- ## Installation
8
-
9
- Add this line to your application's Gemfile:
10
-
11
- gem "ztk"
12
-
13
- And then execute:
14
-
15
- $ bundle
16
-
17
- Or install it yourself as:
18
-
19
- $ gem install ztk
20
-
21
- ## Usage
22
-
23
- ## Console
24
-
25
- bundle install
26
- bundle exec ztk
27
-
28
- ## ZTK::Parallel
29
-
30
- Parallel Processing Class
31
-
32
- This class can be used to easily run iterative and linear processes in a parallel manner.
33
-
34
- ### ZTK::Parallel Example Ruby Code
35
-
36
- $logger = ZTK::Logger.new(STDOUT)
37
- a_callback = Proc.new do |pid|
38
- puts "Hello from After Callback - PID #{pid}"
39
- end
40
- b_callback = Proc.new do |pid|
41
- puts "Hello from Before Callback - PID #{pid}"
42
- end
43
- parallel = ZTK::Parallel.new
44
- parallel.config do |config|
45
- config.before_fork = b_callback
46
- config.after_fork = a_callback
47
- end
48
- 3.times do |x|
49
- parallel.process do
50
- x
51
- end
52
- end
53
- parallel.waitall
54
- parallel.results
55
-
56
- ### ZTK::Parallel Example Code Pry Run
57
-
58
- [1] pry(main)> $logger = ZTK::Logger.new(STDOUT)
59
- => #<ZTK::Logger:0x0000000204d498
60
- @default_formatter=#<Logger::Formatter:0x0000000204d290 @datetime_format=nil>,
61
- @formatter=nil,
62
- @level=1,
63
- @logdev=
64
- #<Logger::LogDevice:0x0000000204cfe8
65
- @dev=#<IO:<STDOUT>>,
66
- @filename=nil,
67
- @mutex=
68
- #<Logger::LogDevice::LogDeviceMutex:0x0000000204cf20
69
- @mon_count=0,
70
- @mon_mutex=#<Mutex:0x0000000204ce58>,
71
- @mon_owner=nil>,
72
- @shift_age=nil,
73
- @shift_size=nil>,
74
- @progname=nil>
75
- [2] pry(main)> a_callback = Proc.new do |pid|
76
- [2] pry(main)* puts "Hello from After Callback - PID #{pid}"
77
- [2] pry(main)* end
78
- => #<Proc:0x0000000253e1a0@(pry):2>
79
- [3] pry(main)> b_callback = Proc.new do |pid|
80
- [3] pry(main)* puts "Hello from Before Callback - PID #{pid}"
81
- [3] pry(main)* end
82
- => #<Proc:0x0000000274bda8@(pry):5>
83
- [4] pry(main)> parallel = ZTK::Parallel.new
84
- => #<ZTK::Parallel:0x000000026310d0
85
- @config=
86
- #<OpenStruct stdout=#<IO:<STDOUT>>, stderr=#<IO:<STDERR>>, stdin=#<IO:<STDIN>>, logger=#<ZTK::Logger:0x0000000204d498 @progname=nil, @level=1, @default_formatter=#<Logger::Formatter:0x0000000204d290 @datetime_format=nil>, @formatter=nil, @logdev=#<Logger::LogDevice:0x0000000204cfe8 @shift_size=nil, @shift_age=nil, @filename=nil, @dev=#<IO:<STDOUT>>, @mutex=#<Logger::LogDevice::LogDeviceMutex:0x0000000204cf20 @mon_owner=nil, @mon_count=0, @mon_mutex=#<Mutex:0x0000000204ce58>>>>, max_forks=12, one_shot=false, before_fork=nil, after_fork=nil>,
87
- @forks=[],
88
- @results=[]>
89
- [5] pry(main)> parallel.config do |config|
90
- [5] pry(main)* config.before_fork = b_callback
91
- [5] pry(main)* config.after_fork = a_callback
92
- [5] pry(main)* end
93
- => #<Proc:0x0000000253e1a0@(pry):2>
94
- [6] pry(main)> 3.times do |x|
95
- [6] pry(main)* parallel.process do
96
- [6] pry(main)* x
97
- [6] pry(main)* end
98
- [6] pry(main)* end
99
- Hello from Before Callback - PID 31579
100
- Hello from After Callback - PID 31579
101
- Hello from Before Callback - PID 31579
102
- Hello from After Callback - PID 31614
103
- Hello from After Callback - PID 31579
104
- Hello from Before Callback - PID 31579
105
- Hello from After Callback - PID 31617
106
- Hello from After Callback - PID 31579
107
- Hello from After Callback - PID 31620
108
- => 3
109
- [7] pry(main)> parallel.waitall
110
- => [[31614, #<Process::Status: pid 31614 exit 0>, 0],
111
- [31617, #<Process::Status: pid 31617 exit 0>, 1],
112
- [31620, #<Process::Status: pid 31620 exit 0>, 2]]
113
- [8] pry(main)> parallel.results
114
- => [0, 1, 2]
115
-
116
- Config values can also be passed like:
117
-
118
- parallel = ZTK::Parallel.new(:before_fork => b_callback, :after_fork => a_callback)
119
-
120
- Or:
121
-
122
- parallel = ZTK::Parallel.new
123
- parallel.config.before_fork = b_callback
124
- parallel.config.after_fork = a_callback
125
-
126
- Default Config Values for ZTK::Parallel:
127
-
128
- :stdout => $stdout,
129
- :stderr => $stderr,
130
- :stdin => $stdin,
131
- :logger => $logger,
132
- :max_forks => `grep -c processor /proc/cpuinfo`.chomp.to_i,
133
- :one_shot => false,
134
- :before_fork => nil,
135
- :after_fork => nil
136
-
137
- ## ZTK::Logger
138
-
139
- Logging Class
140
-
141
- This is a logging class based off the ruby core Logger class; but with very verbose logging information, adding PID, micro second timing to log messages. It favors passing messages via blocks in order to speed up execution when log messages do not need to be yielded. New takes the same options as the ruby core logger class.
142
-
143
- Example:
144
-
145
- $logger = ZTK::Logger.new("/dev/null")
146
-
147
- $logger.debug { "This is a debug message!" }
148
- $logger.info { "This is a info message!" }
149
- $logger.warn { "This is a warn message!" }
150
- $logger.error { "This is a error message!" }
151
- $logger.fatal { "This is a fatal message!" }
152
-
153
- Example Output:
154
-
155
- 2012-09-01|21:34:45.216262|23603|FATAL|logger_spec.rb:122:block (5 levels) in <top (required)>|This is a test fatal message
156
-
157
- ## ZTK::SSH
158
-
159
- SSH Class
160
-
161
- Simplified SSH class; it provides for remote execute of commands and returning of command output. Additionally it allows for uploading and downloading of files.
162
-
163
- ### ZTK::SSH Example Ruby Code
164
-
165
- $logger = ZTK::Logger.new(STDOUT)
166
- ssh = ZTK::SSH.new
167
- ssh.config do |config|
168
- config.ssh.user = ENV["USER"]
169
- config.ssh.host = "127.0.0.1"
170
- end
171
- puts ssh.exec("hostname -f")
172
- local = File.expand_path(File.join("/tmp", "id_rsa.pub"))
173
- remote = File.expand_path(File.join(ENV["HOME"], ".ssh", "id_rsa.pub"))
174
- ssh.download(remote, local)
175
-
176
- ### ZTK::SSH Example Code Pry Run
177
-
178
- [1] pry(main)> $logger = ZTK::Logger.new(STDOUT)
179
- => #<ZTK::Logger:0x000000025f2c18
180
- @default_formatter=#<Logger::Formatter:0x000000025f2ab0 @datetime_format=nil>,
181
- @formatter=nil,
182
- @level=1,
183
- @logdev=
184
- #<Logger::LogDevice:0x000000025fcc18
185
- @dev=#<IO:<STDOUT>>,
186
- @filename=nil,
187
- @mutex=
188
- #<Logger::LogDevice::LogDeviceMutex:0x000000025fcbf0
189
- @mon_count=0,
190
- @mon_mutex=#<Mutex:0x000000025fc9e8>,
191
- @mon_owner=nil>,
192
- @shift_age=nil,
193
- @shift_size=nil>,
194
- @progname=nil>
195
- [2] pry(main)> ssh = ZTK::SSH.new
196
- => #<ZTK::SSH:0x00000002c317c8
197
- @config=
198
- #<OpenStruct stdout=#<IO:<STDOUT>>, stderr=#<IO:<STDERR>>, stdin=#<IO:<STDIN>>, logger=#<ZTK::Logger:0x000000025f2c18 @progname=nil, @level=1, @default_formatter=#<Logger::Formatter:0x000000025f2ab0 @datetime_format=nil>, @formatter=nil, @logdev=#<Logger::LogDevice:0x000000025fcc18 @shift_size=nil, @shift_age=nil, @filename=nil, @dev=#<IO:<STDOUT>>, @mutex=#<Logger::LogDevice::LogDeviceMutex:0x000000025fcbf0 @mon_owner=nil, @mon_count=0, @mon_mutex=#<Mutex:0x000000025fc9e8>>>>, ssh=#<OpenStruct>>>
199
- [3] pry(main)> ssh.config do |config|
200
- [3] pry(main)* config.ssh.user = ENV["USER"]
201
- [3] pry(main)* config.ssh.host = "127.0.0.1"
202
- [3] pry(main)* end
203
- => "127.0.0.1"
204
- [4] pry(main)> puts ssh.exec("hostname -f")
205
- 2012-09-01|04:49:47.723411|25709| INFO|ssh.rb:76:exec|command(hostname -f)
206
- staging.jovelabs.net
207
- staging.jovelabs.net
208
- => nil
209
- [5] pry(main)> local = File.expand_path(File.join("/tmp", "id_rsa.pub"))
210
- => "/tmp/id_rsa.pub"
211
- [6] pry(main)> remote = File.expand_path(File.join(ENV["HOME"], ".ssh", "id_rsa.pub"))
212
- => "/home/zpatten/.ssh/id_rsa.pub"
213
- [7] pry(main)> ssh.download(remote, local)
214
- 2012-09-01|04:49:48.153389|25709| INFO|ssh.rb:133:download|parameters(/home/zpatten/.ssh/id_rsa.pub,/tmp/id_rsa.pub)
215
- 2012-09-01|04:49:48.153536|25709| INFO|ssh.rb:137:block in download|download(/home/zpatten/.ssh/id_rsa.pub -> /tmp/id_rsa.pub)
216
- 2012-09-01|04:49:48.156243|25709| INFO|ssh.rb:145:block in download|finish
217
- => true
218
-
219
- ### ZTK::SSH Config
220
-
221
- To proxy through another host, for example SSH to 192.168.1.1 through 192.168.0.1:
222
-
223
- ssh.config do |config|
224
- config.ssh.user = ENV["USER"]
225
- config.ssh.host = "192.168.1.1"
226
- config.ssh.proxy_user = ENV["USER"]
227
- config.ssh.proxy_host = "192.168.0.1"
228
- end
229
-
230
- Specify an identity file:
231
-
232
- ssh.config do |config|
233
- config.ssh.identify_file = File.expand_path(File.join(ENV["HOME"], ".ssh", "id_rsa"))
234
- config.ssh.proxy_identify_file = File.expand_path(File.join(ENV["HOME"], ".ssh", "id_rsa"))
235
- end
236
-
237
- Specify a timeout:
238
-
239
- ssh.config do |config|
240
- config.ssh.timeout = 30
241
- end
242
-
243
- Specify a password:
244
-
245
- ssh.config do |config|
246
- config.ssh.password = "p@$$w0rd"
247
- end
248
-
249
- Check host keys, the default is false (off):
250
-
251
- ssh.config do |config|
252
- config.ssh.host_key_verify = true
253
- end
254
-
255
- ### ZTK::SSH Core Methods
256
-
257
- #### Execute A Command
258
-
259
- ssh.exec(command, options={})
260
-
261
- Where `command` is a command (i.e. `String`) to execute on the remote host (i.e. "hostname -f"). You can optionally pass `:silence => true` into `options` to suppress session output from going to STDOUT and STDERR.
262
-
263
- #### Upload A File
264
-
265
- ssh.upload(local, remote)
266
-
267
- Where `local` is the local file/path you wish to upload on the local host to the `remote` file/path on the remote host.
268
-
269
- #### Download A File
270
-
271
- ssh.download(remote, local)
272
-
273
- Where `remote` is the remote file/path you wish to download on the remote host to the `local` file/path on the local host.
274
-
275
- ## ZTK::Template
276
-
277
- Erubis Templating
278
-
279
- ### ZTK::Template Example Ruby Code
280
-
281
- template_file = File.expand_path(File.join(File.dirname(__FILE__), "spec", "support", "test-template.txt.erb"))
282
- IO.read(template_file)
283
- context = { :test_variable => "Hello World" }
284
- ZTK::Template.render(template_file, context)
285
-
286
- ### ZTK::Template Example Code Pry Run
287
-
288
- [1] pry(main)> template_file = File.expand_path(File.join(File.dirname(__FILE__), "spec", "support", "test-template.txt.erb"))
289
- => "/home/zpatten/Dropbox/code/ztk/spec/support/test-template.txt.erb"
290
- [2] pry(main)> IO.read(template_file)
291
- => "<%= @test_variable %>\n"
292
- [3] pry(main)> context = { :test_variable => "Hello World" }
293
- => {:test_variable=>"Hello World"}
294
- [4] pry(main)> ZTK::Template.render(template_file, context)
295
- => "Hello World"
296
-
297
- ## ZTK::TCPSocketCheck
298
-
299
- Check TCP Sockets
300
-
301
- This class has two basic modes of operation:
302
-
303
- * Read Test
304
-
305
- By default we will perform a read test against the host and port specified. In this mode we will attempt to connect to the host and port supplied and if we can read any amount of data, regardless of the content we view this as success.
306
-
307
- * Write Test
308
-
309
- If `data` is supplied via the configuration, this will change the mode of operation to a write test. Certain services, such as HTTP don't send any data unless you send something first. In this mode we will attempt to connect to the host and port supplied, once connected we will write the supplied `data` to the socket and then attempt to read from the socket. If we can read any amount of data, reagardless of the conent we view this as success.
310
-
311
- ### ZTK::TCPSocketCheck Example Ruby Code
312
-
313
- tcp_check = ZTK::TCPSocketCheck.new(:host => "github.com", :port => 22)
314
- tcp_check.wait
315
- tcp_check.ready?
316
-
317
- ### ZTK::TCPSocketCheck Example Code Pry Run
318
-
319
- [1] pry(main)> tcp_check = ZTK::TCPSocketCheck.new(:host => "github.com", :port => 22)
320
- => #<ZTK::TCPSocketCheck:0x00000001890d18
321
- @config=
322
- #<OpenStruct stdout=#<IO:<STDOUT>>, stderr=#<IO:<STDERR>>, stdin=#<IO:<STDIN>>, logger=nil, host="github.com", port=22, data=nil, timeout=5>>
323
- [2] pry(main)> tcp_check.wait
324
- => nil
325
- [3] pry(main)> tcp_check.ready?
326
- => true
327
-
328
- ### ZTK::TCPSocketCheck Config
329
-
330
- Here is an example TCPSocketCheck configuration. The `timeout` and `wait` values shown are the defaults if omitted.
331
-
332
- tcp_check = ZTK::TCPSocketCheck.new
333
- tcp_check.config do |config|
334
- config.host = "www.google.com"
335
- config.port = 80
336
- config.data = "GET"
337
- config.timeout = 5
338
- config.wait = 60
339
- end
340
-
341
- Specify the host and port (required):
342
-
343
- tcp_check = ZTK::TCPSocketCheck.new
344
- tcp_check.config do |config|
345
- config.host = "www.google.com"
346
- config.port = 80
347
- end
348
-
349
- Specify data to write (switches to a write mode test if this is supplied):
350
-
351
- tcp_check = ZTK::TCPSocketCheck.new
352
- tcp_check.config do |config|
353
- config.host = "www.google.com"
354
- config.port = 80
355
- config.data = "GET"
356
- end
357
-
358
- Override the `timeout` and `wait` values:
359
-
360
- tcp_check = ZTK::TCPSocketCheck.new
361
- tcp_check.config do |config|
362
- config.host = "www.google.com"
363
- config.port = 80
364
- config.data = "GET"
365
- config.timeout = 3
366
- config.wait = 5
367
- end
368
-
369
- ### ZTK::TCPSocketCheck Core Methods
370
-
371
- #### Socket Ready?
372
-
373
- We can use the `ready?` method to test if the socket is ready in a one off manner. This operations runtime is bound by the `timeout` configuration value.
374
-
375
- tcp_check = ZTK::TCPSocketCheck.new
376
- tcp_check.config do |config|
377
- config.host = "www.google.com"
378
- config.port = 80
379
- config.data = "GET"
380
- end
381
- if tcp_check.ready? == true
382
- puts "The Socket Is Ready!"
383
- else
384
- puts "Looks like no one is home!"
385
- end
386
-
387
- #### Socket Wait
388
-
389
- We can use the `wait` method to block on the socket's `ready?` state. The method will return only if the socket becomes ready or a timeout occurs. This operations runtime is bound by the `wait` configuration value.
390
-
391
- tcp_check = ZTK::TCPSocketCheck.new
392
- tcp_check.config do |config|
393
- config.host = "www.google.com"
394
- config.port = 80
395
- config.data = "GET"
396
- end
397
- if tcp_check.wait == true
398
- puts "The Socket Is Ready!"
399
- else
400
- puts "We timed out or got no answer!"
401
- end
402
-
403
- ## ZTK::Command
404
-
405
- A class to execute commands locally.
406
-
407
- # RESOURCES
408
-
409
- Source:
410
-
411
- * https://github.com/jovelabs/ztk
412
-
413
- Issues:
414
-
415
- * https://github.com/jovelabs/ztk/issues
416
-
417
- Wiki:
418
-
419
- * https://github.com/jovelabs/ztk/wiki
420
-
421
- # LICENSE
422
-
423
- ZTK - Zachary's Tool Kit
424
-
425
- * Author: Zachary Patten <zachary@jovelabs.com>
426
- * Copyright: Copyright (c) Jove Labs
427
- * License: Apache License, Version 2.0
428
-
429
- Licensed under the Apache License, Version 2.0 (the "License");
430
- you may not use this file except in compliance with the License.
431
- You may obtain a copy of the License at
432
-
433
- http://www.apache.org/licenses/LICENSE-2.0
434
-
435
- Unless required by applicable law or agreed to in writing, software
436
- distributed under the License is distributed on an "AS IS" BASIS,
437
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
438
- See the License for the specific language governing permissions and
439
- limitations under the License.