class Vpsb::Helpers::Ssh
Public Instance Methods
add_host_to_config(host, ssh_path, user)
click to toggle source
# 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()
click to toggle source
# 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)
click to toggle source
# 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()
click to toggle source
# File lib/vpsb/helpers/ssh.rb, line 65 def local_ssh_path "#{Dir.home}/.ssh" end
local_ssh_pub()
click to toggle source
# File lib/vpsb/helpers/ssh.rb, line 69 def local_ssh_pub pub_key(local_ssh_path) end
pub_key(path)
click to toggle source
# 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
Private Instance Methods
config_copy_path()
click to toggle source
# File lib/vpsb/helpers/ssh.rb, line 84 def config_copy_path config_path + '_copy' end
config_path()
click to toggle source
# File lib/vpsb/helpers/ssh.rb, line 80 def config_path "#{local_ssh_path}/config" end
file(path)
click to toggle source
# File lib/vpsb/helpers/ssh.rb, line 88 def file(path) File.open(path, 'r') { |f| f.read.chomp } rescue nil end
host_config(host, ssh_path, user)
click to toggle source
# File lib/vpsb/helpers/ssh.rb, line 92 def host_config(host, ssh_path, user) <<-EOS Host #{host} ##{user} HostName #{host} IdentityFile #{ssh_path} # User #{user} EOS end
make_ssh_config_copy()
click to toggle source
# File lib/vpsb/helpers/ssh.rb, line 101 def make_ssh_config_copy system("touch #{config_path}") unless Dir.exist?(config_path) FileUtils.cp config_path, config_copy_path end
remove_config_copy()
click to toggle source
# File lib/vpsb/helpers/ssh.rb, line 106 def remove_config_copy if File.exist?(config_copy_path) FileUtils.rm config_copy_path end end
restore_config_copy()
click to toggle source
# File lib/vpsb/helpers/ssh.rb, line 112 def restore_config_copy FileUtils.cp config_copy_path, config_path end