Skip to content
This repository has been archived by the owner on Jun 25, 2023. It is now read-only.

Commit

Permalink
Issue #177 Allow multiple TLD and resolver configs
Browse files Browse the repository at this point in the history
- This will take effect on macOs and Linuz for now
  • Loading branch information
Mark Heiges authored and hferentschik committed Sep 12, 2018
1 parent d21f489 commit 01b47a0
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 42 deletions.
23 changes: 17 additions & 6 deletions doc/Usage.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,15 @@ To disable this functionality:
config.landrush.host_redirect_dns = false
....

* OS X
* macOS
+
If you are on an OS X host, we use a nice trick to unobtrusively add a
If you are on an macOS host, we use a nice trick to unobtrusively add a
secondary DNS server only for specific domains. During startup Landrush automatically adds
a file into `/etc/resolver` that points
lookups for hostnames ending in your `config.landrush.tld` domain to its
DNS server. (See `man 5 resolver` on your Mac OS X host for more
DNS server. (See `man 5 resolver` on your host for more
information on this file's syntax.)

+
* Linux
+
Landrush tries to achieve the same behavior on Linux hosts using
Expand Down Expand Up @@ -195,8 +195,7 @@ Windows 10 and VirtualBox.
When running VirtualBox on Windows in combination with Landrush the
Network Connections
(`Control Panel\Network and Internet\Network Connections`) looks
somewhat like this after a successful `vagrant up`: +
+
somewhat like this after a successful `vagrant up`:
+
image:img/network-connections.png[Network
Connections,title="Network Connections"] +
Expand Down Expand Up @@ -268,4 +267,16 @@ You might want to resolve Landrush's DNS-entries on _additional_
computing devices, like a mobile phone.
Please refer to link:ProxyMobile.adoc[mobile instructions] for further details.

[TIP]
====
Multiple TLDs can also be provided as an array.
On Linux and macOS a dnsmasq resp. resolver configuration will be created for each TLD.
....
config.landrush.tld = ['acme.com', 'example.com']
config.landrush.host 'www.acme.com', '1.2.3.4'
config.landrush.host 'www.foo.bar', '2.3.4.5'
....
====

You can refer to the link:Troubleshooting.adoc[Troubleshooting guide] if you encounter any problems while using Landrush.
2 changes: 1 addition & 1 deletion lib/landrush/action/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def machine_hostname
def read_machine_hostname
return machine.config.vm.hostname if machine.config.vm.hostname

"#{Pathname.pwd.basename}.#{config.tld}"
"#{Pathname.pwd.basename}.#{config.tld_as_array[0]}"
end

def enabled?
Expand Down
15 changes: 11 additions & 4 deletions lib/landrush/action/setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def post_boot_setup
Server.start
return unless machine.config.landrush.host_redirect_dns?

env[:host].capability(:configure_visibility_on_host, host_ip_address, config.tld)
env[:host].capability(:configure_visibility_on_host, host_ip_address, config.tld_as_array)
end

def record_dependent_vm
Expand Down Expand Up @@ -80,9 +80,16 @@ def ip_address?(value)
def record_machine_dns_entry
ip_address = machine.config.landrush.host_ip_address || host_ip_address

unless machine_hostname.match(config.tld)
log :error, "hostname #{machine_hostname} does not match the configured TLD: #{config.tld}"
log :error, "You will not be able to access #{machine_hostname} from the host"
tld_match = false
config.tld_as_array.each do |tld|
if machine_hostname.match(tld)
tld_match = true
break
end
end
unless tld_match
log :error, "hostname #{machine_hostname} does not match any of the configured TLD: #{config.tld_as_array}"
log :error, "You will not be able to access #{machine_hostname} from your host"
end

unless Store.hosts.has?(machine_hostname, ip_address)
Expand Down
39 changes: 21 additions & 18 deletions lib/landrush/cap/host/darwin/configure_visibility_on_host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ class ConfigureVisibilityOnHost
class << self
attr_writer :sudo, :config_dir

def configure_visibility_on_host(env, _ip, tld)
def configure_visibility_on_host(env, _ip, tlds)
@env = env
@tld = tld
if contents_match?
info 'Host DNS resolver config looks good.'
else
info 'Need to configure the host.'
write_config!

tlds.each do |tld|
if contents_match?(tld)
info "Host DNS resolver config for TLD '#{tld}' looks good."
else
info "Need to create /etc/resolver entry for TLD '#{tld}'"
write_config!(tld)
end
end
end

Expand All @@ -23,7 +25,11 @@ def sudo
end

def config_dir
@config_dir ||= Pathname('/etc/resolver')
@config_dir ||= '/etc/resolver'
end

def config_file(tld)
File.join(config_dir, tld)
end

def info(msg)
Expand All @@ -38,22 +44,19 @@ def desired_contents
EOS
end

def config_file
config_dir.join(@tld)
end

def contents_match?
config_file.exist? && File.read(config_file) == desired_contents
def contents_match?(tld)
config_file = config_file(tld)
File.exist?(config_file) && File.read(config_file) == desired_contents
end

def write_config!
def write_config!(tld)
info 'Momentarily using sudo to put the host config in place...'
system "#{sudo} mkdir #{config_dir}" unless config_dir.directory?
system "#{sudo} mkdir #{config_dir}" unless File.directory?(config_dir)
Tempfile.open('vagrant_landrush_host_config') do |f|
f.write(desired_contents)
f.close
system "#{sudo} cp #{f.path} #{config_file}"
system "#{sudo} chmod 644 #{config_file}"
system "#{sudo} cp #{f.path} #{config_file(tld)}"
system "#{sudo} chmod 644 #{config_file(tld)}"
end
end
end
Expand Down
6 changes: 4 additions & 2 deletions lib/landrush/cap/host/linux/configure_visibility_on_host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ module Cap
module Linux
class ConfigureVisibilityOnHost
class << self
def configure_visibility_on_host(env, ip, tld)
def configure_visibility_on_host(env, ip, tlds)
env.host.capability(:install_dnsmasq) unless env.host.capability(:dnsmasq_installed)
env.host.capability(:create_dnsmasq_config, ip, tld)
tlds.each do |tld|
env.host.capability(:create_dnsmasq_config, ip, tld)
end
env.host.capability(:restart_dnsmasq)
rescue Vagrant::Errors::CapabilityNotFound => e
env.ui.info("Your host was detected as '#{e.extra_data[:host]}' for which the host capability " \
Expand Down
6 changes: 4 additions & 2 deletions lib/landrush/cap/host/windows/configure_visibility_on_host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ class ConfigureVisibilityOnHost
INTERFACES = 'SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces'.freeze

class << self
def configure_visibility_on_host(env, ip, tld)
def configure_visibility_on_host(env, ip, tlds)
@env = env
update_network_adapter(ip, tld) if ensure_prerequisites
# tlds is an array. See also issue #177. For now we only support single TLD on windows, hence we select the
# first element
update_network_adapter(ip, tlds[0]) if ensure_prerequisites
end

# If this registry query succeeds we assume we have Admin rights
Expand Down
6 changes: 5 additions & 1 deletion lib/landrush/config.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module Landrush
class Config < Vagrant.plugin('2', :config)
attr_accessor :hosts
attr_accessor :enabled
attr_accessor :tld
attr_accessor :enabled
attr_accessor :upstream_servers
attr_accessor :host_ip_address
attr_accessor :guest_redirect_dns
Expand Down Expand Up @@ -63,6 +63,10 @@ def host(hostname, ip_address = nil)
@hosts[hostname] = ip_address
end

def tld_as_array
Array(@tld)
end

def upstream(ip, port = 53, protocol = nil)
@upstream_servers = [] if @upstream_servers == UNSET_VALUE

Expand Down
16 changes: 10 additions & 6 deletions test/landrush/cap/host/darwin/configure_visibility_on_host_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ module Darwin
# also disable 'sudo'
ConfigureVisibilityOnHost.stubs(:sudo).returns('')

ConfigureVisibilityOnHost.configure_visibility_on_host(env, '42.42.42.42', 'vagrant.test')
ConfigureVisibilityOnHost.configure_visibility_on_host(env, '42.42.42.42', ['vagrant.test'])

Dir["#{dir}/*"].empty?.must_equal false
File.exist?(File.join(dir, 'vagrant.test')).must_equal true
Pathname(File.join(dir, 'vagrant.test')).read.must_equal CONFIG
end
end

it 'the config file is named after the configured tld' do
it 'multiple tlds can be specified' do
Dir.mktmpdir('landrush-test-dir-') do |dir|
# puts "Using #{dir} for testing"
Dir["#{dir}/*"].empty?.must_equal true
Expand All @@ -41,11 +41,15 @@ module Darwin
# also disable 'sudo'
ConfigureVisibilityOnHost.stubs(:sudo).returns('')

ConfigureVisibilityOnHost.configure_visibility_on_host(env, '42.42.42.42', 'foo.bar')
tlds = %w[foo.bar acme.com example.com]

Dir["#{dir}/*"].empty?.must_equal false
File.exist?(File.join(dir, 'foo.bar')).must_equal true
Pathname(File.join(dir, 'foo.bar')).read.must_equal CONFIG
ConfigureVisibilityOnHost.configure_visibility_on_host(env, '42.42.42.42', tlds)

tlds.each do |tld|
Dir["#{dir}/*"].empty?.must_equal false
File.exist?(File.join(dir, tld)).must_equal true
Pathname(File.join(dir, tld)).read.must_equal CONFIG
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module Linux
skip('Only supported on Linux') unless Vagrant::Util::Platform.linux?
File.exist?(TEST_CONFIG).must_equal false

Landrush::Cap::Linux::ConfigureVisibilityOnHost.configure_visibility_on_host(Vagrant::Environment.new, TEST_IP, TEST_TLD)
Landrush::Cap::Linux::ConfigureVisibilityOnHost.configure_visibility_on_host(Vagrant::Environment.new, TEST_IP, [TEST_TLD])

File.exist?(TEST_CONFIG).must_equal true
Pathname(TEST_CONFIG).read.must_equal CONFIG
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ module Windows
network_name = get_network_name(old_network_state, new_network_state)

get_dns_for_name(network_name).must_be_nil
Landrush::Cap::Windows::ConfigureVisibilityOnHost.configure_visibility_on_host(fake_environment, TEST_IP, 'landrush.test')
Landrush::Cap::Windows::ConfigureVisibilityOnHost.configure_visibility_on_host(fake_environment, TEST_IP, ['landrush.test'])
get_dns_for_name(network_name).must_equal '127.0.0.1'
rescue StandardError
delete_test_interface network_description
Expand Down

0 comments on commit 01b47a0

Please sign in to comment.