Class: Nucleus::SSHHandler

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/nucleus/core/common/ssh_handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

configure_logger_for, #log, logger_for

Constructor Details

#initialize(custom_ssh_key_file = nil) ⇒ Nucleus::SSHHandler

Setup the SSHHandler.

Parameters:

  • custom_ssh_key_file (String) (defaults to: nil)

    path to the key file



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/nucleus/core/common/ssh_handler.rb', line 10

def initialize(custom_ssh_key_file = nil)
  @custom_ssh_key_file = custom_ssh_key_file
  @key_file = @custom_ssh_key_file
  @key_file_history = []
  @agent_file_history = []

  if custom_ssh_key_file
    # file must not be accessible by others, otherwise usage will be forbidden by git.
    FileUtils.chmod(0600, custom_ssh_key_file) if OS.unix?
    cache_public_key
  end

  # create the initial agent / key combination
  create_agent_key_combo
end

Instance Attribute Details

#key_fileObject (readonly)

Returns the value of attribute key_file.



5
6
7
# File 'lib/nucleus/core/common/ssh_handler.rb', line 5

def key_file
  @key_file
end

Instance Method Details

#cleanupObject

Cleanup all created tmp files. Usually to be invoked before shutdown.



37
38
39
40
# File 'lib/nucleus/core/common/ssh_handler.rb', line 37

def cleanup
  @agent_file_history.each { |af| FileUtils.rm_f(af) }
  @key_file_history.each { |kf| FileUtils.rm_f(kf) }
end

#public_keyString

Get the public key that shall be used for authentication. Furthermore, the method assures that the agent, which was registered in the GIT_SSH variable, is available.

Returns:

  • (String)

    ssh public key in full format (type, value, comment)



29
30
31
32
33
34
# File 'lib/nucleus/core/common/ssh_handler.rb', line 29

def public_key
  return @public_key if File.exist?(@agent_file) && File.exist?(@key_file)
  # create the agent and key if at least one file does not exist (anymore)
  create_agent_key_combo
  @public_key
end