Class: Divvy::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/divvy/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options = {})
  @host = host
  @options = options
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



11
12
13
# File 'lib/divvy/server.rb', line 11

def host
  @host
end

#optionsObject (readonly)

Returns the value of attribute options.



11
12
13
# File 'lib/divvy/server.rb', line 11

def options
  @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, options = {})
  options = {
    :verbose => Divvy.verbose, 
    :raise_on_exit_code => true,
    :raise_errors => true
  }.merge(options)
  puts command if options[:verbose]
  response_data = ''
  begin
    key_path = File.expand_path(self.options[:key]) if self.options[:key]
    Net::SSH.start(host, self.options[:user], :password => self.options[: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 options[: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 && options[: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 options[: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, options = {})
  key_path = File.expand_path(self.options[:key]) if self.options[:key]
  Net::SCP.start(host, self.options[:user], :password => self.options[:password], :keys => [key_path]) do |scp|
    scp.upload! source, target do |ch, name, sent, total|
      puts "#{name}: #{sent}/#{total}"
    end
  end
end