Class: Cucumber::Chef::SSH

Inherits:
Object
  • Object
show all
Defined in:
lib/cucumber/chef/ssh.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stdout = STDOUT, stderr = STDERR, stdin = STDIN) ⇒ SSH

Returns a new instance of SSH.



32
33
34
35
36
37
# File 'lib/cucumber/chef/ssh.rb', line 32

def initialize(stdout=STDOUT, stderr=STDERR, stdin=STDIN)
  @stdout, @stderr, @stdin = stdout, stderr, stdin
  @stdout.sync = true if @stdout.respond_to?(:sync=)

  @config = Hash.new(nil)
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



28
29
30
# File 'lib/cucumber/chef/ssh.rb', line 28

def config
  @config
end

#stderrObject

Returns the value of attribute stderr.



28
29
30
# File 'lib/cucumber/chef/ssh.rb', line 28

def stderr
  @stderr
end

#stdinObject

Returns the value of attribute stdin.



28
29
30
# File 'lib/cucumber/chef/ssh.rb', line 28

def stdin
  @stdin
end

#stdoutObject

Returns the value of attribute stdout.



28
29
30
# File 'lib/cucumber/chef/ssh.rb', line 28

def stdout
  @stdout
end

Instance Method Details

#consoleObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cucumber/chef/ssh.rb', line 41

def console
  $logger.debug { "config(#{@config.inspect})" }

  command = [ "ssh" ]
  command << [ "-q" ]
  command << [ "-o", "UserKnownHostsFile=/dev/null" ]
  command << [ "-o", "StrictHostKeyChecking=no" ]
  command << [ "-o", "KeepAlive=yes" ]
  command << [ "-o", "ServerAliveInterval=60" ]
  command << [ "-i", @config[:identity_file] ] if @config[:identity_file]
  command << [ "-o", "ProxyCommand=\"#{proxy_command}\"" ] if @config[:proxy]
  command << "#{@config[:ssh_user]}@#{@config[:host]}"
  command = command.flatten.compact.join(" ")
  $logger.info { "command(#{command})" }
  Kernel.exec(command)
end

#download(remote, local) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/cucumber/chef/ssh.rb', line 120

def download(remote, local)
  @sftp ||= Net::SFTP.start(@config[:host], @config[:ssh_user], ssh_options)

  $logger.debug { "config(#{@config.inspect})" }
  $logger.info { "parameters(#{remote},#{local})" }
  @sftp.download!(remote.to_s, local.to_s) do |event, downloader, *args|
    case event
    when :open
      $logger.info { "download(#{args[0].remote} -> #{args[0].local})" }
    when :close
      $logger.debug { "close(#{args[0].local})" }
    when :mkdir
      $logger.debug { "mkdir(#{args[0]})" }
    when :get
      $logger.debug { "get(#{args[0].remote}, size #{args[2].size} bytes, offset #{args[1]})" }
    when :finish
      $logger.info { "finish" }
    end
  end
end

#exec(command, options = {}) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/cucumber/chef/ssh.rb', line 60

def exec(command, options={})
  @ssh ||= Net::SSH.start(@config[:host], @config[:ssh_user], ssh_options)

  options = { :silence => false }.merge(options)
  silence = options[:silence]
  output = ""

  $logger.debug { "config(#{@config.inspect})" }
  $logger.debug { "options(#{options.inspect})" }
  $logger.info { "command(#{command})" }
  channel = @ssh.open_channel do |chan|
    $logger.debug { "channel opened" }
    chan.exec(command) do |ch, success|
      raise SSHError, "Could not execute '#{command}'." unless success

      ch.on_data do |c, data|
        output += data
        $logger.debug { data.chomp.strip }
        @stdout.print(data) if !silence
      end

      ch.on_extended_data do |c, type, data|
        output += data
        $logger.debug { data.chomp.strip }
        @stderr.print(data) if !silence
      end

    end
  end
  channel.wait
  $logger.debug { "channel closed" }

  output
end

#upload(local, remote) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/cucumber/chef/ssh.rb', line 97

def upload(local, remote)
  @sftp ||= Net::SFTP.start(@config[:host], @config[:ssh_user], ssh_options)

  $logger.debug { "config(#{@config.inspect})" }
  $logger.info { "parameters(#{local},#{remote})" }
  @sftp.upload!(local.to_s, remote.to_s) do |event, uploader, *args|
    case event
    when :open
      $logger.info { "upload(#{args[0].local} -> #{args[0].remote})" }
    when :close
      $logger.debug { "close(#{args[0].remote})" }
    when :mkdir
      $logger.debug { "mkdir(#{args[0]})" }
    when :put
      $logger.debug { "put(#{args[0].remote}, size #{args[2].size} bytes, offset #{args[1]})" }
    when :finish
      $logger.info { "finish" }
    end
  end
end