Merge pull request #11565 from soapy1/disable-host-modification

Add option to disable modification of /etc/hosts on guest
This commit is contained in:
Sophia Castellarin 2020-06-12 16:30:49 -05:00 committed by GitHub
commit 7dfb71c471
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 59 additions and 1 deletions

View File

@ -10,15 +10,19 @@ module Vagrant
class SetHostname
def initialize(app, env)
@app = app
@logger = Log4r::Logger.new("vagrant::action::builtin::set_hostname")
end
def call(env)
@app.call(env)
hostname = env[:machine].config.vm.hostname
if !hostname.nil?
allow_hosts_modification = env[:machine].config.vm.allow_hosts_modification
if !hostname.nil? && allow_hosts_modification
env[:ui].info I18n.t("vagrant.actions.vm.hostname.setting")
env[:machine].guest.capability(:change_host_name, hostname)
else
@logger.info("`allow_hosts_modification` set to false. Hosts modification has been disabled, skiping changing hostname.")
end
end
end

View File

@ -24,6 +24,7 @@ module VagrantPlugins
attr_accessor :allowed_synced_folder_types
attr_accessor :allow_fstab_modification
attr_accessor :allow_hosts_modification
attr_accessor :base_mac
attr_accessor :base_address
attr_accessor :boot_timeout
@ -77,6 +78,7 @@ module VagrantPlugins
@box_extra_download_options = UNSET_VALUE
@box_url = UNSET_VALUE
@box_version = UNSET_VALUE
@allow_hosts_modification = UNSET_VALUE
@clone = UNSET_VALUE
@communicator = UNSET_VALUE
@graceful_halt_timeout = UNSET_VALUE
@ -527,6 +529,7 @@ module VagrantPlugins
@box_version = nil if @box_version == UNSET_VALUE
@box_download_options = {} if @box_download_options == UNSET_VALUE
@box_extra_download_options = Vagrant::Util::MapCommandOptions.map_to_command_options(@box_download_options)
@allow_hosts_modification = true if @allow_hosts_modification == UNSET_VALUE
@clone = nil if @clone == UNSET_VALUE
@communicator = nil if @communicator == UNSET_VALUE
@graceful_halt_timeout = 60 if @graceful_halt_timeout == UNSET_VALUE
@ -996,6 +999,12 @@ module VagrantPlugins
option: "allow_fstab_modification", given: @allow_fstab_modification.class, required: "Boolean"
)
end
if ![TrueClass, FalseClass].include?(@allow_hosts_modification.class)
errors["vm"] << I18n.t("vagrant.config.vm.config_type",
option: "allow_hosts_modification", given: @allow_hosts_modification.class, required: "Boolean"
)
end
errors
end

View File

@ -49,6 +49,20 @@ describe VagrantPlugins::Kernel_V2::VMConfig do
assert_valid
end
it "validates disables_host_modification option" do
subject.allow_hosts_modification = true
subject.finalize!
assert_valid
subject.allow_hosts_modification = false
subject.finalize!
assert_valid
subject.allow_hosts_modification = "truthy"
subject.finalize!
assert_invalid
end
describe "#base_mac" do
it "defaults properly" do
subject.finalize!

View File

@ -0,0 +1,28 @@
require File.expand_path("../../../../base", __FILE__)
describe Vagrant::Action::Builtin::SetHostname do
let(:env) { { machine: machine, ui: ui } }
let(:app) { lambda { |env| } }
let(:machine) { double("machine") }
let(:ui) { double("ui") }
subject { described_class.new(app, env) }
before do
allow(machine).to receive_message_chain(:config, :vm, :hostname).and_return("whatever")
allow(machine).to receive_message_chain(:guest, :capability)
allow(ui).to receive(:info)
end
it "should change hostname if hosts modification enabled" do
allow(machine).to receive_message_chain(:config, :vm, :allow_hosts_modification).and_return(true)
expect(machine).to receive(:guest)
subject.call(env)
end
it "should not change hostname if hosts modification disabled" do
allow(machine).to receive_message_chain(:config, :vm, :allow_hosts_modification).and_return(false)
expect(machine).not_to receive(:guest)
subject.call(env)
end
end

View File

@ -21,6 +21,9 @@ machine that Vagrant manages.
by Vagrant. Note, this may mean that folders will not be automatically mounted
on machine reboot. Defaults to true.
- `config.vm.allow_hosts_modification` (boolean) - If false, will prevent Vagrant
from writing to `/etc/hosts`. Defaults to true.
- `config.vm.base_mac` (string) - The MAC address to be assigned to the default
NAT interface on the guest. _Support for this option is provider dependent._