module RHC::SSHKeyHelpers

Public Instance Methods

exe?(executable) click to toggle source
# File lib/rhc/ssh_key_helpers.rb, line 58
def exe?(executable)
  ENV['PATH'].split(File::PATH_SEPARATOR).any? do |directory|
    File.executable?(File.join(directory, executable.to_s))
  end
end
format(key, template) click to toggle source

Public: Format SSH key's core attributes (name, type, fingerprint) in a given ERB template

key - an object to be formatted template - ERB template

# File lib/rhc/ssh_key_helpers.rb, line 69
def format(key, template)
  template.result(binding)
end
generate_ssh_key_ruby(type="RSA", bits = 2048, comment = "OpenShift-Key") click to toggle source

Public: Generate an SSH key and store it in ~/.ssh/id_rsa

type - The String type RSA or DSS. bits - The Integer value for number of bits. comment - The String comment for the key

Examples

generate_ssh_key_ruby()
# => /home/user/.ssh/id_rsa.pub

Returns nil on failure or public key location as a String on success

# File lib/rhc/ssh_key_helpers.rb, line 36
def generate_ssh_key_ruby(type="RSA", bits = 2048, comment = "OpenShift-Key")
  key = RHC::Vendor::SSHKey.generate(:type => type,
                                     :bits => bits,
                                     :comment => comment)
  ssh_dir = "#{RHC::Config.home_dir}/.ssh"
  if File.exists?("#{ssh_dir}/id_rsa")
    say "SSH key already exists: #{ssh_dir}/id_rsa.  Reusing..."
    return nil
  else
    unless File.exists?(ssh_dir)
      FileUtils.mkdir_p(ssh_dir)
      File.chmod(0700, ssh_dir)
    end
    File.open("#{ssh_dir}/id_rsa", 'w') {|f| f.write(key.private_key)}
    File.chmod(0600, "#{ssh_dir}/id_rsa")
    File.open("#{ssh_dir}/id_rsa.pub", 'w') {|f| f.write(key.ssh_public_key)}

    ssh_add if exe?('ssh-add')
  end
  "#{ssh_dir}/id_rsa.pub"
end