Browse files

Write hosts configuration to /etc/hosts on Lion to make them work in …

…Safari.
  • Loading branch information...
1 parent de7fb56 commit f8c38b8b74d1b1fabba3570f9b1a9f61d38be6a4 @Manfred Manfred committed Aug 25, 2011
Showing with 105 additions and 0 deletions.
  1. +57 −0 lib/passenger_pane/directory_services.rb
  2. +40 −0 test/ppane/directory_services_test.rb
  3. +8 −0 test/ppane/test_helper.rb
View
57 lib/passenger_pane/directory_services.rb
@@ -15,5 +15,62 @@ def self.unregister(hosts)
system(trust("/usr/bin/dscl localhost -delete /Local/Default/Hosts/#{host}"))
end
end
+
+ def self.broken?
+ !(`sw_vers -productVersion`.strip < '10.7')
+ end
+
+ def self.hosts_as_string
+ registered_hosts.join(' ')
+ end
+
+ def self.open_and_lock(filename)
+ file = File.open(filename, File::RDWR)
+ begin
+ file.flock(File::LOCK_EX)
+ yield file
+ ensure
+ file.flock(File::LOCK_UN)
+ file.close
+ end
+ end
+
+ def self.etc_hosts_file
+ '/etc/hosts'
+ end
+
+ def self.marker
+ "Maintained by Passenger Pane"
+ end
+
+ def self.hosts_configuration_line
+ "127.0.0.1 #{hosts_as_string} # #{marker}"
+ end
+
+ def self.write_to_hosts_file
+ open_and_lock(etc_hosts_file) do |file|
+ contents = file.read
+ lines = contents.split("\n")
+
+ lines.each_with_index do |line, index|
+ if line.include?(marker)
+ lines.delete_at(index)
+ break
+ end
+ end if contents.include?(marker)
+
+ lines << "#{hosts_configuration_line}\n"
+ file.truncate(0)
+ file.seek(0)
+
+ file.write(lines.join("\n"))
+ end
+ rescue Errno::EACCES
+ puts "[!] Didn't write hosts configuration to #{etc_hosts_file}, because I couldn't open it"
+ end
+
+ def self.write_to_hosts_file_if_broken
+ write_to_hosts_file if broken?
+ end
end
end
View
40 test/ppane/directory_services_test.rb
@@ -22,4 +22,44 @@
end
PassengerPane::DirectoryServices.unregister(hosts)
end
+end
+
+describe "DirectoryServices, concerning a hosts file hack to make Safari find hosts on Lion" do
+ before do
+ PassengerPane::DirectoryServices.stubs(:broken?).returns(true)
+
+ @etc_hosts_file = File.join(temporary_directory, 'hosts')
+ PassengerPane::DirectoryServices.stubs(:etc_hosts_file).returns(@etc_hosts_file)
+ FileUtils.touch(@etc_hosts_file)
+
+ @hosts = %w(assets0.local assets1.local cms.local admin.cms.local)
+ PassengerPane::DirectoryServices.stubs(:`).with('/usr/bin/dscl localhost -list /Local/Default/Hosts').returns(@hosts.join("\n"))
+ end
+
+ it "returns the registered hosts separated by whitespace" do
+ PassengerPane::DirectoryServices.hosts_as_string.should == @hosts.join(' ')
+ end
+
+ it "writes registered hosts to a file" do
+ PassengerPane::DirectoryServices.write_to_hosts_file
+ File.read(@etc_hosts_file).should.include PassengerPane::DirectoryServices.hosts_configuration_line
+ end
+
+ it "does not touch the rest of the hosts file" do
+ original_contents = "127.0.0.1 localhost\n"
+ File.open(@etc_hosts_file, 'w') do |file|
+ file.write(original_contents)
+ end
+ PassengerPane::DirectoryServices.write_to_hosts_file
+ File.read(@etc_hosts_file).should.include(original_contents)
+ end
+
+ it "does not write a second configuration line when there already is one" do
+ original_contents = "#{PassengerPane::DirectoryServices.hosts_configuration_line}\n127.0.0.1 localhost\n"
+ File.open(@etc_hosts_file, 'w') do |file|
+ file.write(original_contents)
+ end
+ PassengerPane::DirectoryServices.write_to_hosts_file
+ File.read(@etc_hosts_file).should == "127.0.0.1 localhost\n#{PassengerPane::DirectoryServices.hosts_configuration_line}\n"
+ end
end
View
8 test/ppane/test_helper.rb
@@ -21,8 +21,16 @@
Kernel.add_allow_switch(:system)
Kernel.add_allow_switch(:`)
+module StubBrokenMethod
+ def setup
+ super
+ PassengerPane::DirectoryServices.stubs(:broken?).returns(false)
+ end
+end
+
module Test::Spec::TestCase::InstanceMethods
include TestHelper::CaptureOutput
include TestHelper::TemporaryDirectory
include TestHelper::FakeApacheDirectory
+ include StubBrokenMethod
end

0 comments on commit f8c38b8

Please sign in to comment.