class Ding::Ssh

Attributes

options[R]

Public Class Methods

new(options={}) click to toggle source
# File lib/ding/models/ssh.rb, line 9
def initialize(options={})
  @options = options
end

Public Instance Methods

create_ssh_key(name, comment) click to toggle source
# File lib/ding/models/ssh.rb, line 17
def create_ssh_key(name, comment)
  raise "ssh key #{name} already exists!" if ssh_key_exists? name
  run_cmd "ssh-keygen -t #{options[:type]} -C #{comment} -P '#{options[:passphrase]}' -f #{File.join(ssh_config_path, name)}"
end
delete_ssh_key(name) click to toggle source
# File lib/ding/models/ssh.rb, line 22
def delete_ssh_key(name)
  raise "ssh key #{name} does not exist!" unless ssh_key_exists? name
  File.delete ssh_public_key_file(name), ssh_private_key_file(name)
end
list_ssh_keys() click to toggle source
# File lib/ding/models/ssh.rb, line 13
def list_ssh_keys
  Dir.glob(File.join(ssh_config_path, '*.pub')).map {|f| File.basename f, '.pub'}
end
ssh_key_exists?(name) click to toggle source
# File lib/ding/models/ssh.rb, line 43
def ssh_key_exists?(name)
  File.exists? ssh_private_key_file(name)
end
ssh_private_key_file(name) click to toggle source
# File lib/ding/models/ssh.rb, line 47
def ssh_private_key_file(name)
  File.join ssh_config_path, name
end
ssh_public_key_file(name) click to toggle source
# File lib/ding/models/ssh.rb, line 51
def ssh_public_key_file(name)
  "#{ssh_private_key_file name}.pub"
end
update_config(host, name) click to toggle source
# File lib/ding/models/ssh.rb, line 27
def update_config(host, name)
  if File.exists?(ssh_config_file)
    config = File.open(ssh_config_file).read
    raise "Host #{host} already configured in ssh config" if config.include?(host)
    raise "Key #{name} already configured in ssh config" if config.include?(name)
  else
    FileUtils.mkdir_p ssh_config_path
  end

  File.open(ssh_config_file, 'a') do |f|
    f.puts "Host #{host}"
    f.puts "  IdentityFile #{ssh_private_key_file name}"
    f.puts "  StrictHostKeyChecking no" if options[:secure_host]
  end
end

Private Instance Methods

ssh_config_exists?() click to toggle source
# File lib/ding/models/ssh.rb, line 57
def ssh_config_exists?
  File.exists? ssh_config_file
end
ssh_config_file() click to toggle source
# File lib/ding/models/ssh.rb, line 65
def ssh_config_file
  @ssh_config_file ||= options[:ssh_config_file] || File.join(ssh_config_path, 'config')
end
ssh_config_path() click to toggle source
# File lib/ding/models/ssh.rb, line 61
def ssh_config_path
  @ssh_config_path ||= options[:ssh_config_path] || File.join(ENV['HOME'], '.ssh')
end