Class: Kraaken::Ssh

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

Defined Under Namespace

Classes: Connection

Instance Method Summary collapse

Constructor Details

#initialize(config:) ⇒ Ssh

Returns a new instance of Ssh.



6
7
8
# File 'lib/kraaken/ssh.rb', line 6

def initialize(config:)
  @config = config
end

Instance Method Details

#connect(name) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/kraaken/ssh.rb', line 36

def connect(name)
  result = nil
  Net::SSH.start(name) do |ssh|
    result = yield Kraaken::Ssh::Connection.new(ssh, logger: config.logger) if block_given?
  end
  result
end

#regenerate_configObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/kraaken/ssh.rb', line 10

def regenerate_config
  servers = config.cloud.servers
  jump = servers.find(&:jump?)
  servers.reject!(&:jump?)
  config = <<~SSH
    Host jump
    User root
    HostName #{jump.public_ip}
    ForwardAgent yes
  SSH
  servers.each do |server|
    config += <<~SSH

      Host #{server.name}
      User nerd
      HostName #{server.ip}
      ProxyJump jump
    SSH
  end
  File.write(File.join(Dir.home, ".ssh", "cloud_config"), config)
  config_path = File.join(Dir.home, ".ssh", "config")
  unless File.read(config_path).include?("Include cloud_config")
    File.write(config_path, "Include cloud_config\n\n" + File.read(config_path))
  end
end