Class: Gofer::Host
- Inherits:
-
Object
- Object
- Gofer::Host
- Defined in:
- lib/gofer/host.rb
Instance Attribute Summary collapse
-
#hostname ⇒ Object
readonly
Returns the value of attribute hostname.
-
#output_prefix ⇒ Object
Returns the value of attribute output_prefix.
-
#quiet ⇒ Object
Returns the value of attribute quiet.
Instance Method Summary collapse
-
#directory?(path) ⇒ Boolean
Return
true
ifpath
is a directory. -
#download(from, to, opts = {}) ⇒ Object
Download the file or directory at
from
toto
. -
#exist?(path) ⇒ Boolean
Return
true
ifpath
exits. -
#initialize(_hostname, username, opts = {}) ⇒ Host
constructor
Create a new Host connection.
-
#ls(path) ⇒ Object
Return a list of files in the directory at
path
. -
#read(path) ⇒ Object
Return the contents of the file at
path
. -
#run(command, opts = {}) ⇒ Object
Run
command
. -
#run_multiple(commands, opts = {}) ⇒ Object
Run
commands
one by one in order. -
#upload(from, to, opts = {}) ⇒ Object
Upload the file or directory at
from
toto
. -
#write(data, to) ⇒ Object
Write
data
to a file atto
.
Constructor Details
#initialize(_hostname, username, opts = {}) ⇒ Host
Create a new Host connection
Options:
quiet
-
Don’t print stdout output from
run
commands output_prefix
-
Prefix each line of stdout to differentiate multiple host output
All other+opts+ is passed through directly to Net::SSH.start See net-ssh.github.com/ssh/v2/api/index.html for valid arguments.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/gofer/host.rb', line 26 def initialize _hostname, username, opts={} @hostname = _hostname # support legacy positional argument use if opts.is_a? String opts = { :keys => [opts]} end @quiet = opts.delete(:quiet) @output_prefix = opts.delete(:output_prefix) # support legacy identity_file argument if opts[:identity_file] opts[:keys] = [opts.delete(:identity_file)] end @ssh = SshWrapper.new(hostname, username, opts) end |
Instance Attribute Details
#hostname ⇒ Object (readonly)
Returns the value of attribute hostname.
15 16 17 |
# File 'lib/gofer/host.rb', line 15 def hostname @hostname end |
#output_prefix ⇒ Object
Returns the value of attribute output_prefix.
16 17 18 |
# File 'lib/gofer/host.rb', line 16 def output_prefix @output_prefix end |
#quiet ⇒ Object
Returns the value of attribute quiet.
16 17 18 |
# File 'lib/gofer/host.rb', line 16 def quiet @quiet end |
Instance Method Details
#directory?(path) ⇒ Boolean
Return true
if path
is a directory.
106 107 108 |
# File 'lib/gofer/host.rb', line 106 def directory? path @ssh.run("sh -c '[ -d #{path} ]'").exit_status == 0 end |
#download(from, to, opts = {}) ⇒ Object
Download the file or directory at from
to to
126 127 128 |
# File 'lib/gofer/host.rb', line 126 def download from, to, opts = {} @ssh.download from, to, {:recursive => directory?(from)}.merge(opts) end |
#exist?(path) ⇒ Boolean
Return true
if path
exits.
96 97 98 |
# File 'lib/gofer/host.rb', line 96 def exist? path @ssh.run("sh -c '[ -e #{path} ]'").exit_status == 0 end |
#ls(path) ⇒ Object
Return a list of files in the directory at path
.
111 112 113 114 115 116 117 118 |
# File 'lib/gofer/host.rb', line 111 def ls path response = @ssh.run "ls -1 #{path}", :quiet => true if response.exit_status == 0 response.stdout.strip.split("\n") else raise HostError.new(self, response, "Could not list #{path}, exit status #{response.exit_status}") end end |
#read(path) ⇒ Object
Return the contents of the file at path
.
101 102 103 |
# File 'lib/gofer/host.rb', line 101 def read path @ssh.read_file path end |
#run(command, opts = {}) ⇒ Object
Run command
.
Raise an error if command
exits with a non-zero status.
Print stdout
and stderr
as they’re received.
Return a Gofer::Response object.
Options:
quiet
-
Don’t print
stdout
, can also be set withquiet=
on the instance quiet_stderr
-
Don’t print
stderr
capture_exit_status
-
Don’t raise an error on a non-zero exit status
58 59 60 61 62 63 64 65 66 |
# File 'lib/gofer/host.rb', line 58 def run command, opts={} opts[:quiet] = quiet unless opts.include?(:quiet) opts[:output_prefix] = @output_prefix response = @ssh.run command, opts if !opts[:capture_exit_status] && response.exit_status != 0 raise HostError.new(self, response, "Command #{command} failed with exit status #{@ssh.last_exit_status}") end response end |
#run_multiple(commands, opts = {}) ⇒ Object
Run commands
one by one in order.
Raise an error if a command in commands
exits with a non-zero status.
Print stdout
and stderr
as they’re received.
Return a Gofer::Response object.
Options:
quiet
-
Don’t print
stdout
, can also be set withquiet=
on the instance quiet_stderr
-
Don’t print
stderr
The behaviour of passing capture_exit_status
here is undefined.
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/gofer/host.rb', line 82 def run_multiple commands, opts={} return if commands.empty? responses = commands.map do |command| run command, opts end first_response = responses.shift responses.reduce(first_response) do |cursor, response| Response.new(cursor.stdout + response.stdout, cursor.stderr + response.stderr, cursor.output + response.output, 0) end end |
#upload(from, to, opts = {}) ⇒ Object
Upload the file or directory at from
to to
.
121 122 123 |
# File 'lib/gofer/host.rb', line 121 def upload from, to, opts = {} @ssh.upload from, to, {:recursive => File.directory?(from)}.merge(opts) end |
#write(data, to) ⇒ Object
Write data
to a file at to
131 132 133 134 135 136 137 |
# File 'lib/gofer/host.rb', line 131 def write data, to Tempfile.open "gofer_write" do |file| file.write data file.close @ssh.upload(file.path, to, :recursive => false) end end |