lib/fulmar/infrastructure/service/ssh_config_service.rb in fulmar-1.7.5 vs lib/fulmar/infrastructure/service/ssh_config_service.rb in fulmar-1.8.0

- old
+ new

@@ -5,10 +5,19 @@ module Service # Adds entries to the ssh config and checks for existing ones class SSHConfigService CONFIG_FILE = "#{ENV['HOME']}/.ssh/config" KNOWN_HOST_FILE = "#{ENV['HOME']}/.ssh/known_hosts" + CONFIG_MAP = { + hostname: 'Hostname', + port: 'Port', + user: 'User', + proxycommand: 'ProxyCommand', + checkhostip: 'CheckHostIP', + stricthostkeychecking: 'StrictHostKeyChecking', + identityfile: 'IdentityFile' + } def initialize(config) @config = config end @@ -19,20 +28,20 @@ next end if host_exists?(data[:hostname]) puts "Host #{data[:hostname]} exists, skipping..." if @config[:debug] else - add_host(data[:hostname], data) + add_host(data[:hostname], data[:ssh_config]) end end end def remove_known_host(hostname) input_file = File.open(KNOWN_HOST_FILE, 'r') output_file = File.open(KNOWN_HOST_FILE + '.temp', 'w') while (line = input_file.gets) - output_file.puts(line) unless /^\[?#{hostname.gsub('.', '\\.')}(?:\]:\d+)? /.match(line) + output_file.puts(line) unless /^\[?#{hostname.gsub('.', '\\.')}(?:\]:\d+)?[ ,]/.match(line) end input_file.close output_file.close FileUtils.mv(KNOWN_HOST_FILE + '.temp', KNOWN_HOST_FILE) end @@ -49,28 +58,31 @@ config_file.close false end # Adds a host to the ssh config file - def add_host(hostname, host_config = {}) - puts "Adding host #{host_config[:hostname]}..." if @config[:debug] + def add_host(hostname, ssh_config = {}) + puts "Adding host #{hostname}..." if @config[:debug] config_file = File.open(CONFIG_FILE, 'a') + unless ssh_config[:identityfile].blank? or ssh_config[:identityfile][0, 1] == '/' + ssh_config[:identityfile] = @config.base_path + '/' + ssh_config[:identityfile] + end + config_file.puts "\n" # Add some space between this and the second last entry config_file.puts "# Automatically generated by fulmar for project #{@config.project.description}" config_file.puts "Host #{hostname}" - config_file.puts " Hostname #{host_config[:config_hostname]}" unless host_config[:config_hostname].blank? - config_file.puts " Port #{host_config[:config_port]}" unless host_config[:config_port].blank? - config_file.puts " User #{host_config[:config_user]}" unless host_config[:config_user].blank? - config_file.puts " ProxyCommand #{host_config[:config_proxycommand]}" unless host_config[:config_proxycommand].blank? + CONFIG_MAP.keys.each do |key| + config_file.puts " #{CONFIG_MAP[key]} #{ssh_config[key]}" unless ssh_config[key].blank? + end config_file.close end private def config_valid?(host_config) - (!host_config[:hostname].blank? && !host_config[:config_hostname].blank?) + (!host_config[:hostname].blank? && !host_config[:ssh_config].nil?) end end end end end