Skip to content

Commit

Permalink
Issue vagrant-landrush#321 Making sure route_localnet is enabled in k…
Browse files Browse the repository at this point in the history
…ernel

- Aligning some info messages format
  • Loading branch information
hferentschik committed Dec 3, 2018
1 parent bd5a940 commit aaefb59
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 28 deletions.
4 changes: 2 additions & 2 deletions features/commands.feature
@@ -1,5 +1,5 @@
Feature: Landrush reload
Landrush DNS server should restart on a 'vagrant reload'
Feature: Basic commands
Basic Landrush commands including reload.

Scenario Outline: booting a box and restarting it
Given a file named "Vagrantfile" with:
Expand Down
10 changes: 5 additions & 5 deletions features/dns_resolution.feature
@@ -1,4 +1,4 @@
Feature: dns_resolution
Feature: DNS resolution
Landrush should make a virtual machine's IP address DNS-resolvable.

Scenario Outline: booting a box
Expand Down Expand Up @@ -33,7 +33,7 @@ Feature: dns_resolution
Then Landrush is not running

Examples:
| box | provider |
| debian/jessie64 | virtualbox |
#| opensuse/openSUSE-42.1-x86_64 | virtualbox |
#| ubuntu/wily64 | virtualbox |
| box | provider |
| debian/jessie64 | virtualbox |
| ubuntu/xenial64 | virtualbox |
| ubuntu/bionic64 | virtualbox |
2 changes: 1 addition & 1 deletion features/docker_provider.feature
@@ -1,4 +1,4 @@
Feature: Landrush with Docker provider
Feature: Docker provider
Landrush should work with Docker provider

Scenario: Booting box with Docker provider
Expand Down
7 changes: 7 additions & 0 deletions features/landrush_ip.feature
Expand Up @@ -25,6 +25,9 @@ Feature: landrush-ip
When I successfully run `bundle exec vagrant up --provider <provider>`
Then the host visible IP address of the guest is the IP of interface "eth3"

When I successfully run `bundle exec vagrant landrush stop`
Then Landrush is not running

Examples:
| box | provider |
| debian/jessie64 | virtualbox |
Expand Down Expand Up @@ -52,6 +55,10 @@ Feature: landrush-ip
When I successfully run `bundle exec vagrant up --provider <provider>`
Then the host visible IP address of the guest is the IP of interface "eth4"

When I successfully run `bundle exec vagrant landrush stop`
Then Landrush is not running

Examples:
| box | provider |
| debian/jessie64 | virtualbox |

2 changes: 0 additions & 2 deletions lib/landrush/action/redirect_dns.rb
Expand Up @@ -13,9 +13,7 @@ def call(env)
end

def redirect_dns
info "setting up machine's DNS to point to our server"
machine.guest.capability(:redirect_dns, host: _target_host, port: Server.port)

machine.config.vm.networks.each do |type, options|
info "network: #{type.inspect}, #{options.inspect}"
end
Expand Down
4 changes: 2 additions & 2 deletions lib/landrush/action/setup.rb
Expand Up @@ -50,7 +50,7 @@ def record_dependent_vm
def add_prerequisite_network_interface
return unless virtualbox? && !private_network_exists?

info 'virtualbox requires an additional private network; adding it'
info 'Virtualbox requires an additional private network; adding it'
machine.config.vm.network :private_network, type: :dhcp
end

Expand Down Expand Up @@ -93,7 +93,7 @@ def record_machine_dns_entry
end

unless Store.hosts.has?(machine_hostname, ip_address)
info "adding machine entry: #{machine_hostname} => #{ip_address}"
info "Adding '#{machine_hostname} => #{ip_address}' to #{Store.hosts.backing_file}"
Store.hosts.set(machine_hostname, ip_address)
Store.hosts.set(IPAddr.new(ip_address).reverse, machine_hostname)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/landrush/action/teardown.rb
Expand Up @@ -25,7 +25,7 @@ def teardown
def teardown_machine_dns
return unless Store.hosts.has? machine_hostname

info "removing machine entry: #{machine_hostname}"
info "Removing machine entry: #{machine_hostname}"
Store.hosts.delete(machine_hostname)
end

Expand Down
12 changes: 7 additions & 5 deletions lib/landrush/cap/guest/all/read_host_visible_ip_address.rb
Expand Up @@ -42,8 +42,7 @@ def self.read_host_visible_ip_address(machine)
# This allows us to be more accurate, especially with logging what's going on.
unless machine.config.landrush.host_interface.nil?
addr = addresses.detect { |a| a['name'] == machine.config.landrush.host_interface }

machine.env.ui.warn "[landrush] Unable to find interface #{machine.config.landrush.host_interface}" if addr.nil?
log_with_prefix(:warn, "Unable to find interface #{machine.config.landrush.host_interface}", machine) if addr.nil?
end

if addr.nil?
Expand All @@ -64,11 +63,14 @@ def self.read_host_visible_ip_address(machine)
end

ip = IPAddr.new(addr[key])

machine.env.ui.info "[landrush] Using #{addr['name']} (#{addr[key]})"

log_with_prefix(:info, "Using #{addr['name']} (#{addr[key]})", machine)
ip.to_s
end

def self.log_with_prefix(level, msg, machine)
@prefix_ui = Vagrant::UI::Prefixed.new(machine.env.ui, machine.name) if @prefix_ui.nil?
@prefix_ui.send level, "[landrush] #{msg}"
end
end
end
end
Expand Down
34 changes: 24 additions & 10 deletions lib/landrush/cap/guest/linux/redirect_dns.rb
Expand Up @@ -2,20 +2,34 @@ module Landrush
module Cap
module Linux
module RedirectDns
def self.redirect_dns(machine, target = {})
dns_servers = machine.guest.capability(:configured_dns_servers)
%w[tcp udp].each do |proto|
class << self
def redirect_dns(machine, target = {})
prefix_ui = Vagrant::UI::Prefixed.new(machine.env.ui, machine.name)
dns_servers = machine.guest.capability(:configured_dns_servers)
dns_servers.each do |dns_server|
machine.guest.capability(
:add_iptables_rule,
_redirect_dns_rule(proto, dns_server, target.fetch(:host), target.fetch(:port))
)
prefix_ui.info("[landrush] Setting up iptables rule on guest for DNS server #{dns_server}")
enable_route_localnet(machine) if dns_server =~ /127\.0\.0\.\d+/
%w[tcp udp].each do |proto|
machine.guest.capability(:add_iptables_rule, redirect_dns_rule(proto, dns_server, target.fetch(:host), target.fetch(:port)))
end
end
end
end

def self._redirect_dns_rule(protocol, original_server, target_server, target_port)
"OUTPUT -t nat -p #{protocol} -d #{original_server} --dport 53 -j DNAT --to-destination #{target_server}:#{target_port}"
private

def redirect_dns_rule(protocol, original_server, target_server, target_port)
"OUTPUT -t nat -p #{protocol} -d #{original_server} --dport 53 -j DNAT --to-destination #{target_server}:#{target_port}"
end

def enable_route_localnet(machine)
command = "sh -c 'echo 1 > /proc/sys/net/ipv4/conf/all/route_localnet'"
machine.communicate.sudo(command) do |data, type|
if %i[stderr stdout].include?(type)
color = type == :stdout ? :green : :red
machine.env.ui.info(data.chomp, color: color, prefix: false)
end
end
end
end
end
end
Expand Down

0 comments on commit aaefb59

Please sign in to comment.