class Gleis::SSH

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

Public Class Methods

add_host_to_config(host_name, private_key_file) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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