class DevopsToolkit::Tasks::SSH

Constants

COMMAND
DESC

Public Instance Methods

known_hosts() click to toggle source
# File lib/devops_toolkit/tasks/ssh.rb, line 11
def known_hosts
  full_known_hosts_path = File.expand_path(options[:known_hosts])
  puts "Removing line #{options[:line]} from #{full_known_hosts_path}"
  begin
    if !options[:line].is_a?(Integer)
      raise ArgumentError, 'line must be an integer'
    end
    # Should be not too big of a file, so we probably
    # can read all of it to memory
    lines = File.readlines(full_known_hosts_path).map {|l| l.strip}
    deleted_line = lines.delete_at(options[:line] - 1)
    puts "Deleted: #{deleted_line}"
    File.write(full_known_hosts_path, "#{lines.join("\n")}\n")
  rescue Exception => e
    puts "#{e.class}: #{e.message}"
  end
end