Class: Vpsb::Helpers::Ssh
- Inherits:
-
Object
- Object
- Vpsb::Helpers::Ssh
- Includes:
- AskSupport
- Defined in:
- lib/vpsb/helpers/ssh.rb
Instance Method Summary collapse
- #add_host_to_config(host, ssh_path, user) ⇒ Object
- #ask_for_ssh_path ⇒ Object
- #generate_ssh(path) ⇒ Object
- #local_ssh_path ⇒ Object
- #local_ssh_pub ⇒ Object
- #pub_key(path) ⇒ Object
Methods included from AskSupport
#ask, #ask_loop, #ask_to_confirm
Instance Method Details
#add_host_to_config(host, ssh_path, user) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/vpsb/helpers/ssh.rb', line 38 def add_host_to_config(host, ssh_path, user) make_ssh_config_copy original_config = file(config_path) unless start_place = original_config =~ /Host #{host} ##{user}/ File.open(config_path, 'a') { |f| f << host_config(host, ssh_path, user) } return remove_config_copy end return unless ask_to_confirm("Host #{host} already defined in your ssh config file. Update?") next_host_start_place = original_config[start_place + 1, original_config.size] =~ /Host / new_config = [ original_config[0, start_place - 1], "\n\n", host_config(host, ssh_path, user), "\n", (next_host_start_place.nil? ? '' : original_config[next_host_start_place + start_place, original_config.size]) ].join(' ') begin File.open(config_path, 'w+') { |f| f.write(new_config) } rescue => e puts "Error occured when updaing ssh config. Message: #{e.message}"; restore_config_copy; puts "Original configuration restored" end remove_config_copy end |
#ask_for_ssh_path ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/vpsb/helpers/ssh.rb', line 15 def ask_for_ssh_path Dir.mkdir(local_ssh_path) unless Dir.exist?(local_ssh_path) return local_ssh_path unless local_ssh_pub # no ssh keys yet return local_ssh_path if ask_to_confirm('You have local ssh keys. Do you want to use it? If no, new keys will be generated') begin ssh_folder = Dir.home + '/.ssh/' + ask { puts "In what folder keep your keys?" } can_continue = true if Dir.exist?(ssh_folder) if ask_to_confirm("Folder #{ssh_folder} already taken. Overwrite?") FileUtils.rm_rf ssh_folder else can_continue = false end end end until can_continue Dir.mkdir ssh_folder ssh_folder end |
#generate_ssh(path) ⇒ Object
9 10 11 12 13 |
# File 'lib/vpsb/helpers/ssh.rb', line 9 def generate_ssh(path) return if path == local_ssh_path && local_ssh_pub puts 'Start generating new keys' system("ssh-keygen -t rsa -f #{path}/id_rsa") end |
#local_ssh_path ⇒ Object
65 66 67 |
# File 'lib/vpsb/helpers/ssh.rb', line 65 def local_ssh_path "#{Dir.home}/.ssh" end |
#local_ssh_pub ⇒ Object
69 70 71 |
# File 'lib/vpsb/helpers/ssh.rb', line 69 def local_ssh_pub pub_key(local_ssh_path) end |
#pub_key(path) ⇒ Object
73 74 75 76 |
# File 'lib/vpsb/helpers/ssh.rb', line 73 def pub_key(path) path = path[-1] == '/' ? "#{path}id_rsa.pub" : "#{path}/id_rsa.pub" file(path) end |