Refactor editin guest /etc/hosts into a util

This commit is contained in:
sophia 2020-05-29 15:03:47 -04:00
parent fc79aa96dd
commit 85fd3b79fe
3 changed files with 55 additions and 30 deletions

View File

@ -0,0 +1,47 @@
module Vagrant
module Util
# Helper methods for modfiying guests /etc/hosts file
module GuestHosts
# Linux specific inspection helpers
module Linux
DEAFAULT_LOOPBACK_CHECK_LIMIT = 5.freeze
# Add hostname to a loopback address on /etc/hosts if not already there
# Will insert name at the first free address of the form 127.0.X.1, up to
# the loop_bound
#
# @param [Communicator]
# @param [String] full hostanme
# @param [int] (option) defines the upper bound for searching for an available loopback address
def add_hostname_to_loopback_interface(comm, name, loop_bound=DEAFAULT_LOOPBACK_CHECK_LIMIT)
basename = name.split(".", 2)[0]
comm.sudo <<-EOH.gsub(/^ {14}/, '')
grep -w '#{name}' /etc/hosts || {
for i in {1..#{loop_bound}}; do
grep -w "127.0.${i}.1" /etc/hosts || {
echo "127.0.${i}.1 #{name} #{basename}" >> /etc/hosts
break
}
done
}
EOH
end
# Remove any line in /etc/hosts that contains hostname,
# then add hostname with associated ip
#
# @param [Communicator]
# @param [String] full hostanme
# @param [String] target ip
def replace_host(comm, name, ip)
basename = name.split(".", 2)[0]
comm.sudo <<-EOH.gsub(/^ {14}/, '')
sed -i '/#{name}/d' /etc/hosts
sed -i'' '1i '#{ip}'\\t#{name}\\t#{basename}' /etc/hosts
EOH
end
end
end
end
end

View File

@ -1,7 +1,11 @@
require 'vagrant/util/guest_hosts'
module VagrantPlugins
module GuestSUSE
module Cap
class ChangeHostName
extend Vagrant::Util::GuestHosts::Linux
def self.change_host_name(machine, name)
comm = machine.communicate
basename = name.split(".", 2)[0]
@ -12,38 +16,12 @@ module VagrantPlugins
EOH
network_with_hostname = machine.config.vm.networks.map {|t, c| c if c[:hostname] }.compact[0]
if network_with_hostname
replace_host(comm, name, basename, network_with_hostname[:ip])
replace_host(comm, name, network_with_hostname[:ip])
else
add_hostname_to_loopback(comm, name, basename)
add_hostname_to_loopback_interface(comm, name)
end
end
end
def self.add_hostname_to_loopback(comm, name, basename)
# Add hostname to a loopback address on /etc/hosts if not already there
# Will insert name at the first free address of the form 127.0.X.1, up to
# the loop_bound
loop_bound = 5
comm.sudo <<-EOH.gsub(/^ {14}/, '')
grep -w '#{name}' /etc/hosts || {
for i in {1..#{loop_bound}}; do
grep -w "127.0.${i}.1" /etc/hosts || {
echo "127.0.${i}.1 #{name} #{basename}" >> /etc/hosts
break
}
done
}
EOH
end
def self.replace_host(comm, name, basename, ip)
# Remove any line in /etc/hosts that contains hostname,
# then add hostname with associated ip
comm.sudo <<-EOH.gsub(/^ {14}/, '')
sed -i '/#{name}/d' /etc/hosts
sed -i'' '1i '#{ip}'\\t#{name}\\t#{basename}' /etc/hosts
EOH
end
end
end
end

View File

@ -62,7 +62,7 @@ describe "VagrantPlugins::GuestSUSE::Cap::ChangeHostName" do
expect(cap).to receive(:replace_host)
expect(cap).to_not receive(:add_hostname_to_loopback)
expect(cap).to_not receive(:add_hostname_to_loopback_interface)
cap.change_host_name(machine, name)
end
@ -76,7 +76,7 @@ describe "VagrantPlugins::GuestSUSE::Cap::ChangeHostName" do
expect(cap).to_not receive(:replace_host)
expect(cap).to receive(:add_hostname_to_loopback).once
expect(cap).to receive(:add_hostname_to_loopback_interface).once
cap.change_host_name(machine, name)
end
end