scutil 0.4.3 → 0.4.4

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/CHANGELOG.rdoc CHANGED
@@ -1,20 +1,26 @@
1
1
 
2
2
  ==Changelog
3
3
 
4
- ===0.4.3 | 2011-03-14
4
+ ===0.4.4 | 2012-05-24
5
+
6
+ * Added Net::SCP wrapper support to the module functions.
7
+
8
+ * DRY'd things up a bit.
9
+
10
+ ===0.4.3 | 2012-03-14
5
11
 
6
12
  * Added preliminary support for a Net::SCP wrapper.
7
13
 
8
- ===0.4.2 | 2011-02-23
14
+ ===0.4.2 | 2012-02-23
9
15
 
10
16
  * Fixed bug when using Ruby 1.8.
11
17
 
12
- ===0.4.1 | 2011-02-22
18
+ ===0.4.1 | 2012-02-22
13
19
 
14
20
  * Fixed uninitialized constant error introduced in 0.4.0.
15
21
  * Yet more doc clean up...
16
22
 
17
- ===0.4.0 | 2011-02-21
23
+ ===0.4.0 | 2012-02-21
18
24
 
19
25
  * Added built-in sudo password support.
20
26
  * Some new tests and documentation fixes.
data/lib/scutil.rb CHANGED
@@ -29,7 +29,7 @@ require 'scutil/connection_cache'
29
29
  require 'scutil/system_connection'
30
30
 
31
31
  module Scutil
32
- SCUTIL_VERSION = '0.4.3'
32
+ SCUTIL_VERSION = '0.4.4'
33
33
 
34
34
  # By default, buffer 10M of data before writing.
35
35
  DEFAULT_OUTPUT_BUFFER_SIZE = 0xA00000
@@ -57,7 +57,6 @@ module Scutil
57
57
 
58
58
  # Should we request a PTY? Uses custom regex if defined in
59
59
  # +:scutil_pty_regex+.
60
- #
61
60
  def check_pty_needed?(cmd, options, hostname)
62
61
  if options[:scutil_force_pty]
63
62
  return true
@@ -124,26 +123,7 @@ module Scutil
124
123
  # Do we need a PTY?
125
124
  pty_needed = check_pty_needed? cmd, options, hostname
126
125
 
127
- # Check for an existing connection in the cache based on the hostname. If
128
- # the hostname exists find a suitable connection.
129
- conn = nil
130
- begin
131
- if (Scutil.connection_cache.exists?(hostname))
132
- sys_conn = Scutil.connection_cache.fetch(hostname)
133
- print "[#{hostname}] Using existing connection\n" if options[:scutil_verbose]
134
- conn = sys_conn.get_connection(hostname, username, pty_needed, options)
135
- else
136
- sys_conn = SystemConnection.new(hostname, options)
137
- # Call get_connection first. Don't add to cache unless established.
138
- conn = sys_conn.get_connection(hostname, username, pty_needed, options)
139
- print "[#{hostname}] Adding new connection to cache\n" if options[:scutil_verbose]
140
- Scutil.connection_cache << sys_conn
141
- end
142
- rescue Net::SSH::AuthenticationFailed => err
143
- raise Scutil::Error.new("Authenication failed for user: #{username}", hostname)
144
- rescue SocketError => err
145
- raise Scutil::Error.new(err.message, hostname)
146
- end
126
+ conn = find_connection(hostname, username, pty_needed, options)
147
127
 
148
128
  fh = $stdout
149
129
  if (output.nil?)
@@ -269,6 +249,70 @@ Define in :scutil_sudo_passwd or check :scutil_sudo_failed_passwd for the correc
269
249
  return exit_status
270
250
  end
271
251
 
252
+ begin
253
+ require 'net/scp'
254
+ rescue LoadError
255
+ end
256
+
257
+ if defined? Net::SCP
258
+ # Convenience method for uploading files. Only available if you have
259
+ # Net::SCP. This function simply calls Net::SCP#upload! but reuses the
260
+ # SSH connection if it's available. All options and semantics are
261
+ # identical to Scutil.exec_command and Net::SCP.
262
+ #
263
+ # <em>*NB:* This function currently calls the *blocking* Net::SCP#upload!
264
+ # function rather than the *non-blocking* #upload function. This is by
265
+ # design and will most likely be changed in the near future.</em>
266
+ def upload(hostname, username, local, remote, new_options={}, &progress)
267
+ options = get_default_options
268
+ options.merge! new_options
269
+ conn = find_connection(hostname, username, false, options)
270
+ conn.scp.upload!(local, remote, options, &progress)
271
+ end
272
+
273
+ # Convenience method for downloading files. Only available if you have
274
+ # Net::SCP. This function simply calls Net::SCP#download! but reuses the
275
+ # SSH connection if it's available. All options and semantics are
276
+ # identical to Scutil.exec_command and Net::SCP. If _local_ is nil the
277
+ # downloaded file will be stored in a string in memory returned by
278
+ # +download+.
279
+ #
280
+ # <em>*NB:* This function currently calls the *blocking* Net::SCP#download!
281
+ # function rather than the *non-blocking* #download function. This is by
282
+ # design and will most likely be changed in the near future.</em>
283
+ def download(hostname, username, remote, local=nil, new_options={}, &progress)
284
+ options = get_default_options
285
+ options.merge! new_options
286
+ conn = find_connection(hostname, username, false, options)
287
+ conn.scp.download!(remote, local, options, &progress)
288
+ end
289
+ end
290
+
291
+ # Check for an existing connection in the cache based on _hostname_. If the
292
+ # _hostname_ exists find a suitable connection. Otherwise establish a
293
+ # connection and add it to the pool.
294
+ def find_connection(hostname, username, pty_needed=false, options)
295
+ conn = nil
296
+ begin
297
+ if (Scutil.connection_cache.exists?(hostname))
298
+ sys_conn = Scutil.connection_cache.fetch(hostname)
299
+ print "[#{hostname}] Using existing connection\n" if options[:scutil_verbose]
300
+ conn = sys_conn.get_connection(hostname, username, pty_needed, options)
301
+ else
302
+ sys_conn = SystemConnection.new(hostname, options)
303
+ # Call get_connection first. Don't add to cache unless established.
304
+ conn = sys_conn.get_connection(hostname, username, pty_needed, options)
305
+ print "[#{hostname}] Adding new connection to cache\n" if options[:scutil_verbose]
306
+ Scutil.connection_cache << sys_conn
307
+ end
308
+ rescue Net::SSH::AuthenticationFailed => err
309
+ raise Scutil::Error.new("Authenication failed for user: #{username}", hostname)
310
+ rescue SocketError => err
311
+ raise Scutil::Error.new(err.message, hostname)
312
+ end
313
+ return conn
314
+ end
315
+
272
316
  private
273
317
  # Set the default options for connection.
274
318
  def get_default_options
@@ -282,4 +326,9 @@ Define in :scutil_sudo_passwd or check :scutil_sudo_failed_passwd for the correc
282
326
  }
283
327
  end
284
328
  end
329
+
330
+ def method_missing(method, *args, &block)
331
+ return if ((method == :download) || (method == :upload))
332
+ super
333
+ end
285
334
  end
data/lib/scutil/exec.rb CHANGED
@@ -20,8 +20,8 @@ module Scutil
20
20
  # specified.
21
21
  def initialize(hostname, username=nil, options={})
22
22
  @hostname = hostname
23
- @username = username.nil? ? ENV['USER'] : username
24
- @options = options
23
+ @username = username.nil? ? ENV['USER'] : username
24
+ @options = options
25
25
  end
26
26
 
27
27
  # See Scutil.exec_command. Takes _cmd_ and optionally _output_, and
@@ -38,22 +38,12 @@ module Scutil
38
38
  # Exposes the raw Net::SSH::Connection::Session object associated
39
39
  # with +:hostname+.
40
40
  def conn(pty_needed=false)
41
- sys_conn = nil
42
- conn = nil
43
- if Scutil.connection_cache.exists?(@hostname)
44
- sys_conn = Scutil.connection_cache.fetch(@hostname)
45
- conn = sys_conn.get_connection(@hostname, @username, pty_needed, @options)
46
- else
47
- sys_conn = SystemConnection.new(@hostname, @options)
48
- conn = sys_conn.get_connection(@hostname, @username, pty_needed, @options)
49
- end
50
- Scutil.connection_cache << sys_conn
51
- conn
41
+ Scutil.find_connection(@hostname, @username, pty_needed=false, @options)
52
42
  end
53
43
 
54
44
  # Alter the options set on this instance.
55
45
  def set_options(options={})
56
- @options.merge!(options)
46
+ @options.merge!(options)
57
47
  end
58
48
 
59
49
  begin
@@ -62,30 +52,18 @@ module Scutil
62
52
  end
63
53
 
64
54
  if defined? Net::SCP
65
- # Convenience method for uploading files. Only available if you have
66
- # Net::SCP. This function simply calls Net::SCP#upload! but reuses the
67
- # SSH connection if it's available. All options and semantics are
68
- # identical to Scutil.exec_command and Net::SCP.
69
- #
70
- # <em>*NB:* This function currently calls the *blocking* +upload!+
71
- # function rather than the *non-blocking* _upload_ function. This is by
72
- # design and will most likely be changed in the near future.</em>
55
+ # See Scutil.upload. The _options_ specified here will take precedence
56
+ # over those specified in the constructor.
73
57
  def upload(local, remote, options={}, &progress)
74
58
  set_options options
75
- conn.scp.upload!(local, remote, options={}, &progress)
59
+ Scutil.upload(@hostname, @username, local, remote, @options, &progress)
76
60
  end
77
61
 
78
- # Convenience method for downloading files. Only available if you have
79
- # Net::SCP. This function simply calls Net::SCP#download! but reuses the
80
- # SSH connection if it's available. All options and semantics are
81
- # identical to Scutil.exec_command and Net::SCP.
82
- #
83
- # <em>*NB:* This function currently calls the *blocking* +download!+
84
- # function rather than the *non-blocking* +download+ function. This is by
85
- # design and will most likely be changed in the near future.</em>
62
+ # See Scutil.download. The _options_ specified here will take precedence
63
+ # over those specified in the constructor.
86
64
  def download(remote, local=nil, options={}, &progress)
87
65
  set_options options
88
- conn.scp.download!(remote, local, options={}, &progress)
66
+ Scutil.download(@hostname, @username, remote, local, @options, &progress)
89
67
  end
90
68
  end
91
69
  end
data/scutil.gemspec CHANGED
@@ -1,8 +1,8 @@
1
1
 
2
2
  Gem::Specification.new do |s|
3
3
  s.name = 'scutil'
4
- s.version = '0.4.3'
5
- s.date = '2012-03-14'
4
+ s.version = '0.4.4'
5
+ s.date = '2012-05-24'
6
6
  s.summary = 'SSH Command UTILity'
7
7
  s.description = <<-EOF
8
8
  Scutil is a library for conveniently executing commands
data/test/test_scutil.rb CHANGED
@@ -279,7 +279,7 @@ class TestScutilAlt < Test::Unit::TestCase
279
279
  end
280
280
 
281
281
  if (ARGV[0].nil? || (ARGV[0] !~ /\w+:\d+/))
282
- puts "Usage: #{$0} host:port"
282
+ puts "Usage: #{$0} host:port [--name testname]"
283
283
  exit(1)
284
284
  end
285
285
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scutil
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 0.4.4
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-03-14 00:00:00.000000000 Z
12
+ date: 2012-05-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: net-ssh
@@ -72,7 +72,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
72
72
  version: '0'
73
73
  requirements: []
74
74
  rubyforge_project:
75
- rubygems_version: 1.8.18
75
+ rubygems_version: 1.8.24
76
76
  signing_key:
77
77
  specification_version: 3
78
78
  summary: SSH Command UTILity