Class: Divvy::Server
- Inherits:
-
Object
- Object
- Divvy::Server
- Defined in:
- lib/divvy/server.rb
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#initialize(host, options = {}) ⇒ Server
constructor
A new instance of Server.
- #remote_command(command, options = {}) ⇒ Object
- #scp(source, target, options = {}) ⇒ Object
Constructor Details
#initialize(host, options = {}) ⇒ Server
Returns a new instance of Server.
6 7 8 9 |
# File 'lib/divvy/server.rb', line 6 def initialize(host, = {}) @host = host @options = end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
11 12 13 |
# File 'lib/divvy/server.rb', line 11 def host @host end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
11 12 13 |
# File 'lib/divvy/server.rb', line 11 def @options end |
Instance Method Details
#remote_command(command, options = {}) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/divvy/server.rb', line 13 def remote_command(command, = {}) = { :verbose => Divvy.verbose, :raise_on_exit_code => true, :raise_errors => true }.merge() puts command if [:verbose] response_data = '' begin key_path = File.(self.[:key]) if self.[:key] Net::SSH.start(host, self.[:user], :password => self.[:password], :keys => [key_path]) do |ssh| ssh.open_channel do |channel| channel.exec(command) do |ch, success| raise "FAILED: couldn't execute command (ssh.channel.exec failure)" unless success channel.on_data do |ch, data| # stdout print data if [:verbose] STDOUT.flush response_data << data end channel.on_extended_data do |ch, type, data| next unless type == 1 # only handle stderr $stderr.print data end channel.on_request("exit-status") do |ch, data| exit_code = data.read_long raise NonZeroExitCode.new(command, exit_code) if exit_code > 0 && [:raise_on_exit_code] end channel.on_request("exit-signal") do |ch, data| puts "SIGNAL: #{data.read_long}" end end end ssh.loop end rescue Exception => err return false unless [:raise_errors] raise end response_data end |
#scp(source, target, options = {}) ⇒ Object
58 59 60 61 62 63 64 65 |
# File 'lib/divvy/server.rb', line 58 def scp(source, target, = {}) key_path = File.(self.[:key]) if self.[:key] Net::SCP.start(host, self.[:user], :password => self.[:password], :keys => [key_path]) do |scp| scp.upload! source, target do |ch, name, sent, total| puts "#{name}: #{sent}/#{total}" end end end |