Class: Bolt::Transport::SSH

Inherits:
Sudoable show all
Defined in:
lib/bolt/transport/ssh.rb,
lib/bolt/transport/ssh/connection.rb

Defined Under Namespace

Classes: Connection

Constant Summary

Constants inherited from Base

Base::ENVIRONMENT_METHODS, Base::STDIN_METHODS

Instance Attribute Summary

Attributes inherited from Base

#logger

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Sudoable

#make_wrapper_stringio, #run_command, #run_script, #run_task, sudo_prompt, #upload, validate_sudo_options

Methods inherited from Base

#assert_batch_size_one, #batch_command, #batch_connected?, #batch_script, #batch_task, #batch_upload, #batches, #default_input_method, #envify_params, filter_options, #run_command, #run_script, #run_task, #select_implementation, #select_interpreter, #unwrap_sensitive_args, #upload, #with_events

Constructor Details

#initializeSSH

Returns a new instance of SSH.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/bolt/transport/ssh.rb', line 59

def initialize
  super

  require 'net/ssh'
  require 'net/scp'
  begin
    require 'net/ssh/krb'
  rescue LoadError
    logger.debug("Authentication method 'gssapi-with-mic' (Kerberos) is not available.")
  end

  @transport_logger = Logging.logger[Net::SSH]
  @transport_logger.level = :warn
end

Class Method Details

.default_optionsObject



16
17
18
19
20
21
22
23
# File 'lib/bolt/transport/ssh.rb', line 16

def self.default_options
  {
    'connect-timeout' => 10,
    'tty' => false,
    'load-config' => true,
    'disconnect-timeout' => 5
  }
end

.optionsObject



11
12
13
14
# File 'lib/bolt/transport/ssh.rb', line 11

def self.options
  %w[host port user password sudo-password private-key host-key-check sudo-executable
     connect-timeout disconnect-timeout tmpdir script-dir run-as tty run-as-command proxyjump interpreters]
end

.validate(options) ⇒ Object



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/bolt/transport/ssh.rb', line 29

def self.validate(options)
  validate_sudo_options(options)

  host_key = options['host-key-check']
  unless host_key.nil? || !!host_key == host_key
    raise Bolt::ValidationError, 'host-key-check option must be a Boolean true or false'
  end

  if (key_opt = options['private-key'])
    unless key_opt.instance_of?(String) || (key_opt.instance_of?(Hash) && key_opt.include?('key-data'))
      raise Bolt::ValidationError,
            "private-key option must be the path to a private key file or a hash containing the 'key-data'"
    end
  end

  %w[connect-timeout disconnect-timeout].each do |timeout|
    timeout_value = options[timeout]
    unless timeout_value.is_a?(Integer) || timeout_value.nil?
      error_msg = "#{timeout} value must be an Integer, received #{timeout_value}:#{timeout_value.class}"
      raise Bolt::ValidationError, error_msg
    end
  end

  if (dir_opt = options['script-dir'])
    unless dir_opt.is_a?(String) && !dir_opt.empty?
      raise Bolt::ValidationError, "script-dir option must be a non-empty string"
    end
  end
end

Instance Method Details

#connected?(target) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
89
90
# File 'lib/bolt/transport/ssh.rb', line 86

def connected?(target)
  with_connection(target) { true }
rescue Bolt::Node::ConnectError
  false
end

#provided_featuresObject



25
26
27
# File 'lib/bolt/transport/ssh.rb', line 25

def provided_features
  ['shell']
end

#with_connection(target) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/bolt/transport/ssh.rb', line 74

def with_connection(target)
  conn = Connection.new(target, @transport_logger)
  conn.connect
  yield conn
ensure
  begin
    conn&.disconnect
  rescue StandardError => e
    logger.info("Failed to close connection to #{target.safe_name} : #{e.message}")
  end
end