net-simple 0.1.0
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/README.textile +57 -0
- data/Rakefile +50 -0
- data/lib/net/simple.rb +45 -0
- data/lib/net/simple/version.rb +13 -0
- data/test/unit/net_simple_test.rb +60 -0
- metadata +78 -0
data/README.textile
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
h1. net-simple
|
2
|
+
|
3
|
+
h2. Description
|
4
|
+
|
5
|
+
A simple wrapper for net-ssh and net-scp which allows running commands and
|
6
|
+
transferring files. Inspired by some functionality found
|
7
|
+
|
8
|
+
h2. Installation
|
9
|
+
|
10
|
+
sudo gem install tpitale-net-simple
|
11
|
+
|
12
|
+
h2. Usage
|
13
|
+
|
14
|
+
require 'net/simple'
|
15
|
+
|
16
|
+
Make a connection:
|
17
|
+
|
18
|
+
host = Net::Simple.new('ip or host', 'username', 'password')
|
19
|
+
|
20
|
+
Three ways to run a command:
|
21
|
+
|
22
|
+
host.connect do
|
23
|
+
run('ls')
|
24
|
+
end
|
25
|
+
|
26
|
+
host.with_connection do |conn|
|
27
|
+
conn.run('ls')
|
28
|
+
end
|
29
|
+
|
30
|
+
host.connect
|
31
|
+
host.run('ls')
|
32
|
+
host.close
|
33
|
+
|
34
|
+
h2. License
|
35
|
+
|
36
|
+
Copyright (c) 2009 Tony Pitale
|
37
|
+
|
38
|
+
Permission is hereby granted, free of charge, to any person
|
39
|
+
obtaining a copy of this software and associated documentation
|
40
|
+
files (the "Software"), to deal in the Software without
|
41
|
+
restriction, including without limitation the rights to use,
|
42
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
43
|
+
copies of the Software, and to permit persons to whom the
|
44
|
+
Software is furnished to do so, subject to the following
|
45
|
+
conditions:
|
46
|
+
|
47
|
+
The above copyright notice and this permission notice shall be
|
48
|
+
included in all copies or substantial portions of the Software.
|
49
|
+
|
50
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
51
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
52
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
53
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
54
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
55
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
56
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
57
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake/gempackagetask'
|
3
|
+
require 'rake/testtask'
|
4
|
+
|
5
|
+
require 'lib/net/simple/version'
|
6
|
+
|
7
|
+
task :default => :test
|
8
|
+
|
9
|
+
spec = Gem::Specification.new do |s|
|
10
|
+
s.name = 'net-simple'
|
11
|
+
s.version = NetSimple::Version.to_s
|
12
|
+
s.summary = "Simple wrapper for net/ssh and net/scp"
|
13
|
+
s.author = 'Tony Pitale'
|
14
|
+
s.email = 'tpitale@gmail.com'
|
15
|
+
s.homepage = 'http://t.pitale.com'
|
16
|
+
s.files = %w(README.textile Rakefile) + Dir.glob("lib/**/*")
|
17
|
+
s.test_files = Dir.glob("test/**/*_test.rb")
|
18
|
+
|
19
|
+
s.add_dependency('net-ssh', '~> 2.0.10')
|
20
|
+
s.add_dependency('net-scp', '~> 1.0.1')
|
21
|
+
end
|
22
|
+
|
23
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
24
|
+
pkg.gem_spec = spec
|
25
|
+
end
|
26
|
+
|
27
|
+
Rake::TestTask.new do |t|
|
28
|
+
t.libs << 'test'
|
29
|
+
t.test_files = FileList["test/**/*_test.rb"]
|
30
|
+
t.verbose = true
|
31
|
+
end
|
32
|
+
|
33
|
+
desc 'Generate the gemspec to serve this Gem from Github'
|
34
|
+
task :github do
|
35
|
+
file = File.dirname(__FILE__) + "/#{spec.name}.gemspec"
|
36
|
+
File.open(file, 'w') {|f| f << spec.to_ruby }
|
37
|
+
puts "Created gemspec: #{file}"
|
38
|
+
end
|
39
|
+
|
40
|
+
begin
|
41
|
+
require 'rcov/rcovtask'
|
42
|
+
|
43
|
+
desc "Generate RCov coverage report"
|
44
|
+
Rcov::RcovTask.new(:rcov) do |t|
|
45
|
+
t.test_files = FileList['test/**/*_test.rb']
|
46
|
+
t.rcov_opts << "-x lib/net/simple/version.rb"
|
47
|
+
end
|
48
|
+
rescue LoadError
|
49
|
+
nil
|
50
|
+
end
|
data/lib/net/simple.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
$:.unshift File.dirname(__FILE__)
|
2
|
+
|
3
|
+
require 'net/ssh'
|
4
|
+
require 'net/scp'
|
5
|
+
|
6
|
+
module Net
|
7
|
+
class Simple
|
8
|
+
def initialize(host, username, password="", verbose = :fatal)
|
9
|
+
@host = host
|
10
|
+
@username = username
|
11
|
+
@ssh_options = {:password => password, :verbose => verbose}
|
12
|
+
end
|
13
|
+
|
14
|
+
def connect(&block)
|
15
|
+
@connection ||= Net::SSH.start(@host, @username, @ssh_options)
|
16
|
+
block_given? ? (instance_eval(&block) and close) : @connection
|
17
|
+
end
|
18
|
+
|
19
|
+
def with_connection(&block)
|
20
|
+
connect
|
21
|
+
yield self if block_given?
|
22
|
+
close
|
23
|
+
end
|
24
|
+
|
25
|
+
def run(command)
|
26
|
+
@connection.exec!(command)
|
27
|
+
end
|
28
|
+
|
29
|
+
def close
|
30
|
+
@connection.close
|
31
|
+
end
|
32
|
+
|
33
|
+
# def sudo(command)
|
34
|
+
# @connection.exec!("sudo #{command}")
|
35
|
+
# end
|
36
|
+
|
37
|
+
def download(remote, local)
|
38
|
+
Net::SCP.download!(@host, @username, remote, local, :ssh => @ssh_options)
|
39
|
+
end
|
40
|
+
|
41
|
+
def upload(local, remote)
|
42
|
+
Net::SCP.upload!(@host, @username, local, remote, :ssh => @ssh_options)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "..", "test_helper")
|
2
|
+
|
3
|
+
class SimpleTest < Test::Unit::TestCase
|
4
|
+
context "The Net/Simple class" do
|
5
|
+
setup do
|
6
|
+
@simple = Net::Simple.new('host', 'username', 'password')
|
7
|
+
@options = {:password => 'password', :verbose => :fatal}
|
8
|
+
end
|
9
|
+
|
10
|
+
should "make a connection" do
|
11
|
+
Net::SSH.expects(:start).with('host', 'username', @options).returns('connection')
|
12
|
+
assert_equal 'connection', @simple.connect
|
13
|
+
end
|
14
|
+
|
15
|
+
should "make a connection and eval a block" do
|
16
|
+
Net::SSH.expects(:start).with('host', 'username', @options).returns('connection')
|
17
|
+
@simple.expects(:run).with('hello').returns(true)
|
18
|
+
@simple.expects(:close)
|
19
|
+
|
20
|
+
@simple.connect do
|
21
|
+
run('hello')
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
should "run with a connection" do
|
26
|
+
@simple.expects(:connect)
|
27
|
+
@simple.expects(:close)
|
28
|
+
@simple.expects(:run).with('hello')
|
29
|
+
@simple.with_connection do |conn|
|
30
|
+
conn.run('hello')
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
should "run commands given to it" do
|
35
|
+
connection = mock
|
36
|
+
connection.expects(:exec!).with('hello').returns('hi to you')
|
37
|
+
Net::SSH.expects(:start).with('host', 'username', @options).returns(connection)
|
38
|
+
@simple.connect
|
39
|
+
assert_equal 'hi to you', @simple.run('hello')
|
40
|
+
end
|
41
|
+
|
42
|
+
should "download a file from a remote host" do
|
43
|
+
Net::SCP.expects(:download!).with('host', 'username', 'here', 'there', :ssh => @options).returns('download')
|
44
|
+
assert_equal 'download', @simple.download('here','there')
|
45
|
+
end
|
46
|
+
|
47
|
+
should "upload a file from a remote host" do
|
48
|
+
Net::SCP.expects(:upload!).with('host', 'username', 'here', 'there', :ssh => @options).returns('upload')
|
49
|
+
assert_equal 'upload', @simple.upload('here','there')
|
50
|
+
end
|
51
|
+
|
52
|
+
should "close an open connection" do
|
53
|
+
connection = mock
|
54
|
+
connection.expects(:close)
|
55
|
+
Net::SSH.expects(:start).with('host', 'username', @options).returns(connection)
|
56
|
+
@simple.connect
|
57
|
+
@simple.close
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: net-simple
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tony Pitale
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-05-02 00:00:00 -04:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: net-ssh
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ~>
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 2.0.10
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: net-scp
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.0.1
|
34
|
+
version:
|
35
|
+
description:
|
36
|
+
email: tpitale@gmail.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files: []
|
42
|
+
|
43
|
+
files:
|
44
|
+
- README.textile
|
45
|
+
- Rakefile
|
46
|
+
- lib/net/simple/version.rb
|
47
|
+
- lib/net/simple.rb
|
48
|
+
- test/unit/net_simple_test.rb
|
49
|
+
has_rdoc: true
|
50
|
+
homepage: http://t.pitale.com
|
51
|
+
licenses: []
|
52
|
+
|
53
|
+
post_install_message:
|
54
|
+
rdoc_options: []
|
55
|
+
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: "0"
|
63
|
+
version:
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: "0"
|
69
|
+
version:
|
70
|
+
requirements: []
|
71
|
+
|
72
|
+
rubyforge_project:
|
73
|
+
rubygems_version: 1.3.5
|
74
|
+
signing_key:
|
75
|
+
specification_version: 2
|
76
|
+
summary: Simple wrapper for net/ssh and net/scp
|
77
|
+
test_files:
|
78
|
+
- test/unit/net_simple_test.rb
|