Class: ChefMetal::Transport::SSH

Inherits:
ChefMetal::Transport show all
Defined in:
lib/chef_metal/transport/ssh.rb

Defined Under Namespace

Classes: SSHResult

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, username, ssh_options, options) ⇒ SSH

Returns a new instance of SSH.



6
7
8
9
10
11
12
13
# File 'lib/chef_metal/transport/ssh.rb', line 6

def initialize(host, username, ssh_options, options)
  require 'net/ssh'
  require 'net/scp'
  @host = host
  @username = username
  @ssh_options = ssh_options
  @options = options
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



15
16
17
# File 'lib/chef_metal/transport/ssh.rb', line 15

def host
  @host
end

#optionsObject (readonly)

Returns the value of attribute options.



18
19
20
# File 'lib/chef_metal/transport/ssh.rb', line 18

def options
  @options
end

#ssh_optionsObject (readonly)

Returns the value of attribute ssh_options.



17
18
19
# File 'lib/chef_metal/transport/ssh.rb', line 17

def ssh_options
  @ssh_options
end

#usernameObject (readonly)

Returns the value of attribute username.



16
17
18
# File 'lib/chef_metal/transport/ssh.rb', line 16

def username
  @username
end

Instance Method Details

#available?Boolean

Returns:

  • (Boolean)


114
115
116
117
118
119
120
# File 'lib/chef_metal/transport/ssh.rb', line 114

def available?
  execute('pwd')
  true
rescue Errno::ETIMEDOUT, Errno::ECONNREFUSED, Errno::ECONNRESET, Net::SSH::AuthenticationFailed, Net::SSH::Disconnect, Net::SSH::HostKeyMismatch
  Chef::Log.debug("#{username}@#{host} unavailable: could not execute 'pwd' on #{host}: #{$!.inspect}")
  false
end

#disconnectObject



103
104
105
106
107
108
109
110
111
112
# File 'lib/chef_metal/transport/ssh.rb', line 103

def disconnect
  if @session
    begin
      Chef::Log.debug("Closing SSH session on #{username}@#{host}")
      @session.close
    rescue
    end
    @session = nil
  end
end

#download_file(path, local_path) ⇒ Object



66
67
68
69
# File 'lib/chef_metal/transport/ssh.rb', line 66

def download_file(path, local_path)
  Chef::Log.debug("Downloading file #{path} from #{username}@#{host} to local #{local_path}")
  download(path, local_path)
end

#execute(command) ⇒ Object



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
57
# File 'lib/chef_metal/transport/ssh.rb', line 20

def execute(command)
  Chef::Log.info("Executing #{command} on #{username}@#{host}")
  stdout = ''
  stderr = ''
  exitstatus = nil
  channel = session.open_channel do |channel|
    # Enable PTY unless otherwise specified, some instances require this
    unless options[:ssh_pty_enable] == false
      channel.request_pty do |chan, success|
         raise "could not get pty" if !success && options[:ssh_pty_enable]
      end
    end


    channel.exec("#{options[:prefix]}#{command}") do |ch, success|
      raise "could not execute command: #{command.inspect}" unless success

      channel.on_data do |ch2, data|
        stdout << data
      end

      channel.on_extended_data do |ch2, type, data|
        stderr << data
      end

      channel.on_request "exit-status" do |ch, data|
        exitstatus = data.read_long
      end
    end
  end

  channel.wait

  Chef::Log.info("Completed #{command} on #{username}@#{host}: exit status #{exitstatus}")
  Chef::Log.debug("Stdout was:\n#{stdout}") if stdout != ''
  Chef::Log.info("Stderr was:\n#{stderr}") if stderr != ''
  SSHResult.new(stdout, stderr, exitstatus)
end

#forward_remote_port_to_local(remote_port, local_port) ⇒ Object



97
98
99
100
101
# File 'lib/chef_metal/transport/ssh.rb', line 97

def forward_remote_port_to_local(remote_port, local_port)
  # TODO IPv6
  Chef::Log.debug("Forwarding local server 127.0.0.1:#{local_port} to port #{remote_port} on #{username}@#{host}")
  session.forward.remote(local_port, "127.0.0.1", remote_port)
end

#read_file(path) ⇒ Object



59
60
61
62
63
64
# File 'lib/chef_metal/transport/ssh.rb', line 59

def read_file(path)
  Chef::Log.debug("Reading file #{path} from #{username}@#{host}")
  result = StringIO.new
  download(path, result)
  result.string
end

#upload_file(local_path, path) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/chef_metal/transport/ssh.rb', line 84

def upload_file(local_path, path)
  if options[:prefix]
    # Make a tempfile on the other side, upload to that, and sudo mv / chown / etc.
    remote_tempfile = "/tmp/#{File.basename(path)}.#{Random.rand(2**32)}"
    Chef::Log.debug("Uploading #{local_path} to #{remote_tempfile} on #{username}@#{host}")
    Net::SCP.new(session).upload!(local_path, remote_tempfile)
    execute("mv #{remote_tempfile} #{path}")
  else
    Chef::Log.debug("Uploading #{local_path} to #{path} on #{username}@#{host}")
    Net::SCP.new(session).upload!(local_path, path)
  end
end

#write_file(path, content) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/chef_metal/transport/ssh.rb', line 71

def write_file(path, content)
  if options[:prefix]
    # Make a tempfile on the other side, upload to that, and sudo mv / chown / etc.
    remote_tempfile = "/tmp/#{File.basename(path)}.#{Random.rand(2**32)}"
    Chef::Log.debug("Writing #{content.length} bytes to #{remote_tempfile} on #{username}@#{host}")
    Net::SCP.new(session).upload!(StringIO.new(content), remote_tempfile)
    execute("mv #{remote_tempfile} #{path}")
  else
    Chef::Log.debug("Writing #{content.length} bytes to #{path} on #{username}@#{host}")
    Net::SCP.new(session).upload!(StringIO.new(content), path)
  end
end