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(git_host, run_host, key_file) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/gleis/ssh.rb', line 24

def self.add_host_to_config(git_host, run_host, key_file)
  config_file = Dir.home + '/.ssh/config'
  ssh_host_line = "Host #{git_host} #{run_host}"
  # Check if SSH config for hosts already exists
  return if Utils.line_exists_in_file(key_file, ssh_host_line)

  f = File.open(config_file, 'a')
  f.puts ssh_host_line
  f.puts "\tIdentityFile #{key_file}"
  f.close
end

.generate_key(key_file, username) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# 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
  system('ssh-keygen', '-f', key_file, '-b 4096',
         "-C generated by Gleis CLI for #{username} by #{ENV['USER']}@#{hostname} on #{datetime}")
end

.load_public_key(key_file) ⇒ Object



19
20
21
22
# File 'lib/gleis/ssh.rb', line 19

def self.load_public_key(key_file)
  public_key_file = key_file + '.pub'
  return File.read(public_key_file).chomp if File.exist?(public_key_file)
end