Class: Ding::Ssh

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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Ssh

Returns a new instance of Ssh.



6
7
8
# File 'lib/ding/models/ssh.rb', line 6

def initialize(options={})
  @options = options
end

Instance Method Details

#create_ssh_key(name, comment) ⇒ Object



14
15
16
17
# File 'lib/ding/models/ssh.rb', line 14

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) ⇒ Object



19
20
21
# File 'lib/ding/models/ssh.rb', line 19

def delete_ssh_key(name)
  File.delete ssh_public_key_file(name), ssh_private_key_file(name) if ssh_key_exists? name
end

#list_ssh_keysObject



10
11
12
# File 'lib/ding/models/ssh.rb', line 10

def list_ssh_keys
  Dir.glob(File.join(ssh_config_path, '*.pub')).map {|f| File.basename f, '.pub'}
end

#ssh_key_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/ding/models/ssh.rb', line 39

def ssh_key_exists?(name)
  File.exists? ssh_private_key_file(name)
end

#ssh_private_key_file(name) ⇒ Object



43
44
45
# File 'lib/ding/models/ssh.rb', line 43

def ssh_private_key_file(name)
  File.join ssh_config_path, name
end

#ssh_public_key_file(name) ⇒ Object



47
48
49
# File 'lib/ding/models/ssh.rb', line 47

def ssh_public_key_file(name)
  "#{ssh_private_key_file name}.pub"
end

#update_config(host, name) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ding/models/ssh.rb', line 23

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