Skip to content

Commit

Permalink
Gemspec, upload, download, read
Browse files Browse the repository at this point in the history
  • Loading branch information
mipearson committed Apr 1, 2011
1 parent 67ebf4f commit 2f9a864
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 15 deletions.
23 changes: 18 additions & 5 deletions README.md
Expand Up @@ -16,6 +16,7 @@
run "rm -rf 'remote_directory'"
end

# read/ls
puts read('a_remote_file')
puts ls('a_remote_dir').join(", ")

Expand All @@ -25,9 +26,17 @@
puts last_exit_status # and will make the exit status available

# stderr/stdout
hello = run "echo hello" # will print 'Host my.host.com> hello'
hello = run "echo hello" # will print 'hello'
puts hello # will print "hello\n"
# stdout/stderr will be interpolated for simplicity

goodbye = run "echo goodbye 1>&2"
# goodbye will be empty, as we don't capture stderr by default

goodbye = run "echo goodbye 1>&2", :capture_stderr => true # unless you ask for it

# output suppression
run "echo noisy", :quiet => true # don't output from our command
run "echo noisier 1>&2", :quiet_stderr => true # don't even output stderr!

end

Expand All @@ -39,9 +48,12 @@

### Later:

# output capture
run "echo goodbye", :quiet => true # won't print anything

write("a string buffer", 'a_remote_file')
# constant connection (no reconnect for each action)
h = Gofer::Host.new(..., :keep_open => true)
h.run( ... )
h.close

# overriding defaults
set :quiet => true
set :capture_exit_status => false
Expand All @@ -52,3 +64,4 @@
# Local system usage, too:
run "hostname" # > my.macbook.com


1 change: 1 addition & 0 deletions gofer.gemspec
Expand Up @@ -20,4 +20,5 @@ server using Net::SSH
s.require_paths = ["lib"]

s.add_dependency('net-ssh', '>= 2.0.23')
s.add_dependency('net-scp', '>= 1.0.4')
end
16 changes: 8 additions & 8 deletions lib/gofer/host.rb
Expand Up @@ -30,12 +30,12 @@ def exists? path
end

def read path
@ssh.run "cat #{path}", :quiet => true
if @ssh.last_exit_status == 0
@ssh.last_output
else
raise HostError.new(self, "Could not read #{path}, exit status #{@ssh.last_exit_status}")
end
@ssh.read_file path
end

def directory? path
@ssh.run "sh -c '[ -d #{path} ]'"
@ssh.last_exit_status == 0
end

def ls path
Expand All @@ -48,11 +48,11 @@ def ls path
end

def upload from, to
@ssh.scp_to_host from, to
@ssh.upload from, to, :recursive => File.directory?(from)
end

def download from, to
@ssh.scp_from_host from, to
@ssh.download from, to, :recursive => directory?(from)
end

def within &block
Expand Down
27 changes: 27 additions & 0 deletions lib/gofer/ssh_wrapper.rb
@@ -1,4 +1,5 @@
require 'net/ssh'
require 'net/scp'

module Gofer
class SshWrapper
Expand All @@ -18,9 +19,35 @@ def run command, opts={}
ssh_execute(ssh, command, opts)
end
end

def read_file path
a = nil
with_scp do |scp|
a = scp.download! path
end
a
end

def download from, to, opts={}
with_scp do |scp|
scp.download! from, to, opts
end
end

def upload from, to, opts={}
with_scp do |scp|
scp.upload! from, to, opts
end
end

private

def with_scp
Net::SCP.start(*net_ssh_credentials) do |scp|
yield scp
end
end

def net_ssh_credentials
creds = [@hostname, @username]
creds << {:keys => [@identity_file] } if @identity_file
Expand Down
71 changes: 69 additions & 2 deletions spec/gofer/integration_spec.rb
@@ -1,4 +1,5 @@
require 'spec_helper'
require 'tempfile'

describe Gofer do

Expand All @@ -16,6 +17,18 @@ def in_tmpdir path
File.join(@tmpdir, path)
end

def with_local_tmpdir template
f = Tempfile.new template
path = f.path
f.unlink
FileUtils.mkdir path
begin
yield path
ensure
FileUtils.rm_rf path unless ENV['KEEPTMPDIR']
end
end

before :all do
@host = Gofer::Host.new(USERNAME, HOSTNAME, IDENTITY_FILE)
@tmpdir = raw_ssh("mktemp -d /tmp/gofertest.XXXXX").chomp
Expand Down Expand Up @@ -66,6 +79,17 @@ def in_tmpdir path
end
end

describe :directory? do
it "should return true if a path is a directory" do
@host.directory?(@tmpdir).should be true
end

it "should return false if a path is not a directory" do
raw_ssh "touch #{in_tmpdir 'a_file'}"
@host.directory?(in_tmpdir('a_file')).should be false
end
end

describe :read do
it "should read in the contents of a file" do
raw_ssh "echo 'hello' > #{@tmpdir}/hello.txt"
Expand All @@ -80,7 +104,50 @@ def in_tmpdir path
end
end

describe :upload
describe :download
describe :upload do
it "should upload a file to the remote server" do
f = Tempfile.new('upload_tmp')
begin
f.write('uploadtmp')
f.close
@host.upload(f.path, in_tmpdir('uploaded'))
raw_ssh("cat #{in_tmpdir 'uploaded'}").should == 'uploadtmp'
ensure
f.unlink
end
end
it "should upload a directory to the remote server" do
f = with_local_tmpdir('upload_dir_tmp') do |path|
system "echo 'hey' >> #{File.join(path, 'temp')}"
@host.upload(path, in_tmpdir('uploaded_dir'))
raw_ssh("cat #{in_tmpdir 'uploaded_dir/temp'}").should == "hey\n"
end
end
end

describe :download do
it "should download a file from the remove server" do
f = Tempfile.new('download_dir')
begin
f.close
raw_ssh "echo 'download' > #{in_tmpdir 'download'}"
@host.download(in_tmpdir('download'), f.path)
File.open(f.path).read.should == "download\n"
ensure
f.unlink
end
end

it "should download a directory from the remote server" do
with_local_tmpdir 'download_dir' do |path|
download_dir = in_tmpdir 'download_dir'
raw_ssh "mkdir #{download_dir} && echo 'sup' > #{download_dir}/hey"

@host.download(download_dir, path)
File.open(path + '/download_dir/hey').read.should == "sup\n"
end
end
end

describe :within
end

0 comments on commit 2f9a864

Please sign in to comment.