Class: Ding::Ssh

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#run_cmd

Constructor Details

#initialize(options = {}) ⇒ Ssh

Returns a new instance of Ssh.



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

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

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

#create_ssh_key(name, comment) ⇒ Object



17
18
19
20
# 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) ⇒ Object



22
23
24
25
# 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_keysObject



13
14
15
# 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) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# 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) ⇒ Object



47
48
49
# 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) ⇒ Object



51
52
53
# 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) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 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