Module: ZTK::SSH::Core

Included in:
ZTK::SSH
Defined in:
lib/ztk/ssh/core.rb

Overview

SSH Core Functionality

Instance Method Summary (collapse)

Instance Method Details

- (Object) close

Close our session gracefully.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/ztk/ssh/core.rb', line 88

def close
  config.ui.logger.debug { "close" }

  close_ssh
  close_gateway

  true

ensure
  @ssh     = nil
  @gateway = nil
  @sftp    = nil
  @scp     = nil
end

- (Object) close_gateway

Attempts to close the gateway session if it is valid.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ztk/ssh/core.rb', line 69

def close_gateway
  if (!@gateway.nil?)
    config.ui.logger.debug { "gateway object is valid" }

    begin
      config.ui.logger.debug { "attempting to shutdown" }
      @gateway.shutdown!
      config.ui.logger.debug { "shutdown" }

    rescue Exception => e
      config.ui.logger.fatal { "EXCEPTION: #{e.inspect}" }
    end

  else
    config.ui.logger.debug { "gateway object is NIL!" }
  end
end

- (Object) close_ssh

Attempts to close the SSH session if it is valid.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ztk/ssh/core.rb', line 50

def close_ssh
  if (!@ssh.nil? && !@ssh.closed?)
    config.ui.logger.debug { "SSH object is valid and not closed" }

    begin
      config.ui.logger.debug { "attempting to close" }
      @ssh.close
      config.ui.logger.debug { "closed" }

    rescue Exception => e
      config.ui.logger.fatal { "EXCEPTION: #{e.inspect}" }
    end

  else
    config.ui.logger.debug { "SSH object is NIL!" }
  end
end

- (Boolean) do_proxy?

Should we run a proxy?

Returns:

  • (Boolean)


45
46
47
# File 'lib/ztk/ssh/core.rb', line 45

def do_proxy?
  ((!config.proxy_host_name.nil? && !config.proxy_host_name.empty?) && (!config.proxy_user.nil? && !config.proxy_user.empty?))
end

- (Object) gateway

Starts an SSH gateway session. Can also be used to get the Net::SSH::Gateway object.

Primarily used internally.



39
40
41
42
# File 'lib/ztk/ssh/core.rb', line 39

def gateway
  @gateway ||= Net::SSH::Gateway.new(config.proxy_host_name, config.proxy_user, gateway_options)
  @gateway
end

- (Object) on_retry(exception)

The on_retry method we'll use with the RescueRetry class.



104
105
106
107
108
# File 'lib/ztk/ssh/core.rb', line 104

def on_retry(exception)
  config.ui.logger.warn { "ZTK::SSH on_retry triggered!" }

  (close rescue false)
end

- (Object) scp

Starts an SCP session. Can also be used to get the Net::SCP object.

Primarily used internally.



30
31
32
33
# File 'lib/ztk/ssh/core.rb', line 30

def scp
  @scp ||= self.ssh.scp
  @scp
end

- (Object) sftp

Starts an SFTP session. Can also be used to get the Net::SFTP object.

Primarily used internally.



22
23
24
25
# File 'lib/ztk/ssh/core.rb', line 22

def sftp
  @sftp ||= self.ssh.sftp
  @sftp
end

- (Object) ssh

Starts an SSH session. Can also be used to get the Net::SSH object.

Primarily used internally.



10
11
12
13
14
15
16
17
# File 'lib/ztk/ssh/core.rb', line 10

def ssh
  if do_proxy?
    @ssh ||= self.gateway.ssh(config.host_name, config.user, ssh_options)
  else
    @ssh ||= Net::SSH.start(config.host_name, config.user, ssh_options)
  end
  @ssh
end