gofer 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .*.swp
3
+ .bundle
4
+ Gemfile.lock
5
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'rspec'
4
+ # Specify your gem's dependencies in gofer.gemspec
5
+ gemspec
data/README.md ADDED
@@ -0,0 +1,109 @@
1
+ # Gofer!
2
+
3
+ **Gofer** is a set of wrappers around the Net::SSH suite of tools to enable consistent access to remote systems.
4
+
5
+ **Gofer** has been written to support the needs of system automation scripts. As such, **gofer** will:
6
+
7
+ * automatically raise an error if a command returns a non-zero exit status
8
+ * print and capture STDOUT automatically
9
+ * print STDERR but don't capture it
10
+ * override the above: return non-zero exit status instead of raising an error, capture STDERR, suppress output
11
+
12
+ ## Examples
13
+
14
+ # in a block
15
+ Gofer::Host.new('ubuntu', 'my.host.com', :identity_file => 'key.pem').within do
16
+ # Basic usage
17
+ run "sudo stop mysqld"
18
+
19
+ # Copying files
20
+ upload 'file' 'remote_file'
21
+ download 'remote_dir', 'dir'
22
+
23
+ # Filesystem inspection
24
+ if exists?('remote_directory')
25
+ run "rm -rf 'remote_directory'"
26
+ end
27
+
28
+ # read/ls
29
+ puts read('a_remote_file')
30
+ puts ls('a_remote_dir').join(", ")
31
+
32
+ # error handling - default to critical failure if a command fails
33
+ run "false" # this will fail
34
+ run "false", :capture_exit_status => true # this won't ...
35
+ puts last_exit_status # and will make the exit status available
36
+
37
+ # stderr/stdout
38
+ hello = run "echo hello" # will print 'hello'
39
+ puts hello # will print "hello\n"
40
+
41
+ goodbye = run "echo goodbye 1>&2"
42
+ # goodbye will be empty, as we don't capture stderr by default
43
+
44
+ goodbye = run "echo goodbye 1>&2", :capture_stderr => true # unless you ask for it
45
+
46
+ # output suppression
47
+ run "echo noisy", :quiet => true # don't output from our command
48
+ run "echo noisier 1>&2", :quiet_stderr => true # don't even output stderr!
49
+
50
+ end
51
+
52
+ # using the instance directly
53
+ h = Gofer::Host.new('ubuntu', 'my.host.com')
54
+ h.run('sudo mysqld stop')
55
+ h.upload('file', 'remote_file')
56
+ # etc..
57
+
58
+ ## Planned Features
59
+
60
+ write("a string buffer", 'a_remote_file')
61
+ # constant connection (no reconnect for each action)
62
+ h = Gofer::Host.new(..., :keep_open => true)
63
+ h.run( ... )
64
+ h.close
65
+
66
+ # overriding defaults
67
+ set :quiet => true
68
+ set :capture_exit_status => false
69
+
70
+ # Separate the command from the arguments, system() style
71
+ run "echo" "Some" "arguments" "with" "'quotes'" "in" "them"
72
+
73
+ # Local system usage, too:
74
+ run "hostname" # > my.macbook.com
75
+
76
+ ## Testing
77
+
78
+ * Ensure that your user can ssh as itself to localhost using the key in `~/.ssh/id_rsa`.
79
+ * Run `rspec spec` or `bundle install && rake spec`
80
+
81
+ ## TODO
82
+
83
+ * ls, exists?, directory? should use sftp if available rather than shell commands
84
+ * wrap STDOUT with host prefix for easy identification of system output
85
+
86
+ ## License
87
+
88
+ (The MIT License)
89
+
90
+ Copyright (c) 2011 Michael Pearson
91
+
92
+ Permission is hereby granted, free of charge, to any person obtaining
93
+ a copy of this software and associated documentation files (the
94
+ 'Software'), to deal in the Software without restriction, including
95
+ without limitation the rights to use, copy, modify, merge, publish,
96
+ distribute, sublicense, and/or sell copies of the Software, and to
97
+ permit persons to whom the Software is furnished to do so, subject to
98
+ the following conditions:
99
+
100
+ The above copyright notice and this permission notice shall be
101
+ included in all copies or substantial portions of the Software.
102
+
103
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
104
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
105
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
106
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
107
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
108
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
109
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task :default => :spec
data/gofer.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "gofer/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "gofer"
7
+ s.version = Gofer::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Michael Pearson"]
10
+ s.email = ["mipearson@gmail.com"]
11
+ s.homepage = "https://github.com/mipearson/gofer"
12
+ s.summary = %q{run commands on remote servers using SSH}
13
+ s.description = %q{
14
+ Gofer provides a flexible and reliable model for performing tasks on remote
15
+ server using Net::SSH
16
+ }
17
+
18
+ s.files = `git ls-files`.split("\n")
19
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_dependency('net-ssh', '>= 2.0.23')
23
+ s.add_dependency('net-scp', '>= 1.0.4')
24
+ end
data/lib/gofer.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'gofer/ssh_wrapper'
2
+ require 'gofer/host'
3
+ require 'gofer/version'
4
+
data/lib/gofer/host.rb ADDED
@@ -0,0 +1,62 @@
1
+ module Gofer
2
+ class HostError < Exception
3
+ def initialize host, message
4
+ super "#{host.hostname}: #{message}"
5
+ end
6
+ end
7
+
8
+ class Host
9
+
10
+ attr_reader :last_exit_status, :hostname
11
+
12
+ def initialize username, _hostname, identity_file=nil
13
+ @hostname = _hostname
14
+ @ssh = SshWrapper.new(username, hostname, identity_file)
15
+ end
16
+
17
+ def run command, opts={}
18
+ @ssh.run command, opts
19
+ if opts[:capture_exit_status]
20
+ @last_exit_status = @ssh.last_exit_status
21
+ elsif @ssh.last_exit_status != 0
22
+ raise HostError.new(self, "Command #{command} failed with exit status #{@ssh.last_exit_status}")
23
+ end
24
+ @ssh.last_output
25
+ end
26
+
27
+ def exists? path
28
+ @ssh.run "sh -c '[ -e #{path} ]'"
29
+ @ssh.last_exit_status == 0
30
+ end
31
+
32
+ def read path
33
+ @ssh.read_file path
34
+ end
35
+
36
+ def directory? path
37
+ @ssh.run "sh -c '[ -d #{path} ]'"
38
+ @ssh.last_exit_status == 0
39
+ end
40
+
41
+ def ls path
42
+ @ssh.run "ls -1 #{path}", :quiet => true
43
+ if @ssh.last_exit_status == 0
44
+ @ssh.last_output.strip.split("\n")
45
+ else
46
+ raise HostError.new(self, "Could not list #{path}, exit status #{@ssh.last_exit_status}")
47
+ end
48
+ end
49
+
50
+ def upload from, to
51
+ @ssh.upload from, to, :recursive => File.directory?(from)
52
+ end
53
+
54
+ def download from, to
55
+ @ssh.download from, to, :recursive => directory?(from)
56
+ end
57
+
58
+ def within &block
59
+ instance_eval &block
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,37 @@
1
+ # Unused, keeping for later use.
2
+ module Gofer
3
+ class Options
4
+ VALID_OPTIONS => %w{identity_file}
5
+
6
+ def valid_options
7
+ VALID_OPTIONS
8
+ end
9
+
10
+ def initialize
11
+ @options = {}
12
+ end
13
+
14
+ def merge_in opts={}
15
+ opts.each |k,v|
16
+ set k, v
17
+ end
18
+ end
19
+
20
+ def set k, v
21
+ k = option_valid_check(k)
22
+ @options[k] = v
23
+ end
24
+
25
+ def get k
26
+ k = option_valid_check(k)
27
+ @options[k]
28
+ end
29
+
30
+ private
31
+
32
+ def option_valid_check(k)
33
+ k = k.to_s
34
+ raise "Invalid option #{k}" unless valid_options.include?(k)
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,91 @@
1
+ require 'net/ssh'
2
+ require 'net/scp'
3
+
4
+ module Gofer
5
+ class SshWrapper
6
+
7
+ attr_reader :last_output, :last_exit_status
8
+
9
+ def initialize username, hostname, identity_file = nil
10
+ @username = username
11
+ @hostname = hostname
12
+ @identity_file = identity_file
13
+ @last_exit_status = nil
14
+ @last_output = nil
15
+ end
16
+
17
+ def run command, opts={}
18
+ Net::SSH.start(*net_ssh_credentials) do |ssh|
19
+ ssh_execute(ssh, command, opts)
20
+ end
21
+ end
22
+
23
+ def read_file path
24
+ a = nil
25
+ with_scp do |scp|
26
+ a = scp.download! path
27
+ end
28
+ a
29
+ end
30
+
31
+ def download from, to, opts={}
32
+ with_scp do |scp|
33
+ scp.download! from, to, opts
34
+ end
35
+ end
36
+
37
+ def upload from, to, opts={}
38
+ with_scp do |scp|
39
+ scp.upload! from, to, opts
40
+ end
41
+ end
42
+
43
+ private
44
+
45
+ def with_scp
46
+ Net::SCP.start(*net_ssh_credentials) do |scp|
47
+ yield scp
48
+ end
49
+ end
50
+
51
+ def net_ssh_credentials
52
+ creds = [@hostname, @username]
53
+ creds << {:keys => [@identity_file] } if @identity_file
54
+ creds
55
+ end
56
+
57
+ def ssh_execute(ssh, command, opts={})
58
+ output = ''
59
+ exit_code = 0
60
+ ssh.open_channel do |channel|
61
+ channel.exec(command) do |ch, success|
62
+ unless success
63
+ raise "Couldn't execute command #{command} (ssh channel failure)"
64
+ end
65
+
66
+ channel.on_data do |ch, data| # stdout
67
+ output += data
68
+ $stdout.print data unless opts[:quiet]
69
+ end
70
+
71
+ channel.on_extended_data do |ch, type, data|
72
+ next unless type == 1 # only handle stderr
73
+ output += data if opts[:capture_stderr]
74
+ $stderr.print data unless opts[:quiet_stderr]
75
+ end
76
+
77
+ channel.on_request("exit-status") do |ch, data|
78
+ exit_code = data.read_long
79
+ channel.close # Necessary or backgrounded processes will 'hang' the channel
80
+ end
81
+
82
+ end
83
+ end
84
+
85
+ ssh.loop
86
+
87
+ @last_exit_status = exit_code
88
+ @last_output = output
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,3 @@
1
+ module Gofer
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,165 @@
1
+ require 'spec_helper'
2
+ require 'tempfile'
3
+
4
+ describe Gofer do
5
+
6
+ HOSTNAME = ENV['TEST_HOST'] || 'localhost'
7
+ USERNAME = ENV['TEST_USER'] || ENV['USER']
8
+ IDENTITY_FILE = ENV['TEST_IDENTITY_FILE'] || '~/.ssh/id_rsa'
9
+
10
+ def raw_ssh command
11
+ out = `ssh -o PasswordAuthentication=no -ni #{IDENTITY_FILE} #{USERNAME}@#{HOSTNAME} #{command}`
12
+ raise "Command #{command} failed" unless $? == 0
13
+ out
14
+ end
15
+
16
+ def in_tmpdir path
17
+ File.join(@tmpdir, path)
18
+ end
19
+
20
+ def with_local_tmpdir template
21
+ f = Tempfile.new template
22
+ path = f.path
23
+ f.unlink
24
+ FileUtils.mkdir path
25
+ begin
26
+ yield path
27
+ ensure
28
+ FileUtils.rm_rf path unless ENV['KEEPTMPDIR']
29
+ end
30
+ end
31
+
32
+ before :all do
33
+ @host = Gofer::Host.new(USERNAME, HOSTNAME, IDENTITY_FILE)
34
+ @tmpdir = raw_ssh("mktemp -d /tmp/gofertest.XXXXX").chomp
35
+ end
36
+
37
+ after :all do
38
+ if ENV['KEEPTMPDIR']
39
+ puts "TMPDIR is #{@tmpdir}"
40
+ else
41
+ raw_ssh "rm -rf #{@tmpdir}" if @tmpdir && @tmpdir =~ %r{gofertest}
42
+ end
43
+ end
44
+
45
+ describe :hostname do
46
+ it "should be the hostname of the host we're connecting to" do
47
+ @host.hostname.should == HOSTNAME
48
+ end
49
+ end
50
+
51
+ describe :run do
52
+ it "should run a command and capture its output" do
53
+ output = @host.run "echo hello", :quiet => true
54
+ output.should == "hello\n"
55
+ end
56
+
57
+ it "should run a command not capture its stderr by default" do
58
+ output = @host.run "echo hello 1>&2", :quiet_stderr => true
59
+ output.should == ""
60
+ end
61
+
62
+ it "should run a command capture its stderr if asked" do
63
+ output = @host.run "echo hello 1>&2", :quiet_stderr => true, :capture_stderr => true
64
+ output.should == "hello\n"
65
+ end
66
+
67
+ it "should error if a command returns a non-zero response" do
68
+ lambda {@host.run "false"}.should raise_error /failed with exit status/
69
+ end
70
+
71
+ it "should capture a non-zero exit status if asked" do
72
+ @host.run "false", :capture_exit_status => true
73
+ @host.last_exit_status.should == 1
74
+ end
75
+ end
76
+
77
+ describe :exists? do
78
+ it "should return true if a path or file exists" do
79
+ raw_ssh "touch #{in_tmpdir 'exists'}"
80
+ @host.exists?(in_tmpdir 'exists').should be true
81
+ end
82
+
83
+ it "should return false if a path does not exist" do
84
+ @host.exists?(in_tmpdir 'doesnotexist').should be false
85
+ end
86
+ end
87
+
88
+ describe :directory? do
89
+ it "should return true if a path is a directory" do
90
+ @host.directory?(@tmpdir).should be true
91
+ end
92
+
93
+ it "should return false if a path is not a directory" do
94
+ raw_ssh "touch #{in_tmpdir 'a_file'}"
95
+ @host.directory?(in_tmpdir('a_file')).should be false
96
+ end
97
+ end
98
+
99
+ describe :read do
100
+ it "should read in the contents of a file" do
101
+ raw_ssh "echo 'hello' > #{@tmpdir}/hello.txt"
102
+ @host.read(@tmpdir + '/hello.txt').should == "hello\n"
103
+ end
104
+ end
105
+
106
+ describe :ls do
107
+ it "should list the contents of a directory" do
108
+ raw_ssh "mkdir #{@tmpdir}/lstmp && touch #{@tmpdir}/lstmp/f"
109
+ @host.ls(@tmpdir + '/lstmp').should == ['f']
110
+ end
111
+ end
112
+
113
+ describe :upload do
114
+ it "should upload a file to the remote server" do
115
+ f = Tempfile.new('upload_tmp')
116
+ begin
117
+ f.write('uploadtmp')
118
+ f.close
119
+ @host.upload(f.path, in_tmpdir('uploaded'))
120
+ raw_ssh("cat #{in_tmpdir 'uploaded'}").should == 'uploadtmp'
121
+ ensure
122
+ f.unlink
123
+ end
124
+ end
125
+ it "should upload a directory to the remote server" do
126
+ f = with_local_tmpdir('upload_dir_tmp') do |path|
127
+ system "echo 'hey' >> #{File.join(path, 'temp')}"
128
+ @host.upload(path, in_tmpdir('uploaded_dir'))
129
+ raw_ssh("cat #{in_tmpdir 'uploaded_dir/temp'}").should == "hey\n"
130
+ end
131
+ end
132
+ end
133
+
134
+ describe :download do
135
+ it "should download a file from the remove server" do
136
+ f = Tempfile.new('download_dir')
137
+ begin
138
+ f.close
139
+ raw_ssh "echo 'download' > #{in_tmpdir 'download'}"
140
+ @host.download(in_tmpdir('download'), f.path)
141
+ File.open(f.path).read.should == "download\n"
142
+ ensure
143
+ f.unlink
144
+ end
145
+ end
146
+
147
+ it "should download a directory from the remote server" do
148
+ with_local_tmpdir 'download_dir' do |path|
149
+ download_dir = in_tmpdir 'download_dir'
150
+ raw_ssh "mkdir #{download_dir} && echo 'sup' > #{download_dir}/hey"
151
+
152
+ @host.download(download_dir, path)
153
+ File.open(path + '/download_dir/hey').read.should == "sup\n"
154
+ end
155
+ end
156
+ end
157
+
158
+ describe :within do
159
+ it "should execute commands in the context of the host instance" do
160
+ @host.within do
161
+ run("echo sup", :quiet => true).should == "sup\n"
162
+ end
163
+ end
164
+ end
165
+ end
@@ -0,0 +1,2 @@
1
+ require "rspec"
2
+ require "gofer"
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gofer
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Michael Pearson
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-04-03 01:00:00 +11:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: net-ssh
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 33
30
+ segments:
31
+ - 2
32
+ - 0
33
+ - 23
34
+ version: 2.0.23
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: net-scp
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 31
46
+ segments:
47
+ - 1
48
+ - 0
49
+ - 4
50
+ version: 1.0.4
51
+ type: :runtime
52
+ version_requirements: *id002
53
+ description: |
54
+
55
+ Gofer provides a flexible and reliable model for performing tasks on remote
56
+ server using Net::SSH
57
+
58
+ email:
59
+ - mipearson@gmail.com
60
+ executables: []
61
+
62
+ extensions: []
63
+
64
+ extra_rdoc_files: []
65
+
66
+ files:
67
+ - .gitignore
68
+ - Gemfile
69
+ - README.md
70
+ - Rakefile
71
+ - gofer.gemspec
72
+ - lib/gofer.rb
73
+ - lib/gofer/host.rb
74
+ - lib/gofer/options.rb
75
+ - lib/gofer/ssh_wrapper.rb
76
+ - lib/gofer/version.rb
77
+ - spec/gofer/integration_spec.rb
78
+ - spec/spec_helper.rb
79
+ has_rdoc: true
80
+ homepage: https://github.com/mipearson/gofer
81
+ licenses: []
82
+
83
+ post_install_message:
84
+ rdoc_options: []
85
+
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ hash: 3
94
+ segments:
95
+ - 0
96
+ version: "0"
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ hash: 3
103
+ segments:
104
+ - 0
105
+ version: "0"
106
+ requirements: []
107
+
108
+ rubyforge_project:
109
+ rubygems_version: 1.3.7
110
+ signing_key:
111
+ specification_version: 3
112
+ summary: run commands on remote servers using SSH
113
+ test_files:
114
+ - spec/gofer/integration_spec.rb
115
+ - spec/spec_helper.rb