Class: Gleis::SSH

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

Overview

This class manages the SSH config and keys on the client side

Class Method Summary collapse

Class Method Details

.add_host_to_config(host_name, private_key_file) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/gleis/ssh.rb', line 27

def self.add_host_to_config(host_name, private_key_file)
  config_file = File.join(Dir.home, '.ssh', 'config')
  ssh_host_line = "Host #{host_name}"
  # Do not continue if host already exists in current SSH config file
  return if File.exist?(config_file) && Utils.line_exists_in_file(config_file, ssh_host_line)

  f = File.open(config_file, 'a')
  f.puts "\n# Added by Gleis CLI on #{Time.now.strftime('%Y-%m-%d %H:%M')}"
  f.puts ssh_host_line
  f.puts "\tIdentityFile #{private_key_file}"
  f.close
end

.generate_key(key_file, username) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/gleis/ssh.rb', line 6

def self.generate_key(key_file, username)
  return if File.exist?(key_file)

  puts 'Could not find an existing public/private key pair'
  return unless Utils.prompt_yes_no('Would you like to generate one now?')

  hostname = Socket.gethostname
  datetime = Time.now.strftime('%Y-%m-%d %H:%M')
  # returns true on success and nil if command is not found or failed
  if Utils.which('ssh-keygen')
    system('ssh-keygen', '-f', key_file, '-b 4096',
           "-Cgenerated by Gleis CLI for #{username} by #{ENV['USER']}@#{hostname} on #{datetime}")
  else
    puts 'The SSH key generator command ssh-keygen is not installed on this system.'
  end
end

.load_public_key(public_key_file) ⇒ Object



23
24
25
# File 'lib/gleis/ssh.rb', line 23

def self.load_public_key(public_key_file)
  return File.read(public_key_file).chomp if File.exist?(public_key_file)
end