Class: Ding::Ssh
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #create_ssh_key(name, comment) ⇒ Object
- #delete_ssh_key(name) ⇒ Object
-
#initialize(options = {}) ⇒ Ssh
constructor
A new instance of Ssh.
- #list_ssh_keys ⇒ Object
- #ssh_key_exists?(name) ⇒ Boolean
- #ssh_private_key_file(name) ⇒ Object
- #ssh_public_key_file(name) ⇒ Object
- #update_config(host, name) ⇒ Object
Methods included from Helpers
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 = end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/ding/models/ssh.rb', line 7 def @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 #{[:type]} -C #{comment} -P '#{[: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_keys ⇒ Object
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
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 [:secure_host] end end |