Add option to disable modification of /etc/hosts on guest
This commit is contained in:
parent
899035b614
commit
ff0aea4493
@ -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?
|
||||
disable_hosts_modification = env[:machine].config.vm.disable_hosts_modification
|
||||
if !hostname.nil? && !disable_hosts_modification
|
||||
env[:ui].info I18n.t("vagrant.actions.vm.hostname.setting")
|
||||
env[:machine].guest.capability(:change_host_name, hostname)
|
||||
else
|
||||
@logger.info("`disable_hosts_modification` set to true. Hosts modification has been disabled, skiping changing hostname.")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -42,6 +42,7 @@ module VagrantPlugins
|
||||
attr_accessor :box_download_location_trusted
|
||||
attr_accessor :box_download_options
|
||||
attr_accessor :communicator
|
||||
attr_accessor :disable_hosts_modification
|
||||
attr_accessor :graceful_halt_timeout
|
||||
attr_accessor :guest
|
||||
attr_accessor :hostname
|
||||
@ -77,6 +78,7 @@ module VagrantPlugins
|
||||
@box_extra_download_options = UNSET_VALUE
|
||||
@box_url = UNSET_VALUE
|
||||
@box_version = UNSET_VALUE
|
||||
@disable_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)
|
||||
@disable_hosts_modification = false if @disable_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
|
||||
@ -994,6 +997,9 @@ module VagrantPlugins
|
||||
if ![TrueClass, FalseClass].include?(@allow_fstab_modification.class)
|
||||
errors["vm"] << I18n.t("vagrant.config.vm.config_type",
|
||||
option: "allow_fstab_modification", given: @allow_fstab_modification.class, required: "Boolean"
|
||||
if ![TrueClass, FalseClass].include?(@disable_hosts_modification.class)
|
||||
errors["vm"] << I18n.t("vagrant.config.vm.disable_host_modification_type",
|
||||
given: @disable_hosts_modification.class
|
||||
)
|
||||
end
|
||||
|
||||
|
||||
@ -1941,6 +1941,8 @@ en:
|
||||
Checksum type specified but "box_download_checksum" is blank
|
||||
box_download_checksum_notblank: |-
|
||||
Checksum specified but must also specify "box_download_checksum_type"
|
||||
disable_host_modification_type: |-
|
||||
Expected configuration `disable_hosts_modification` to be type Bool, got %{given}
|
||||
box_missing: "A box must be specified."
|
||||
box_download_options_type: |-
|
||||
Found "box_download_options" specified as type '%{type}', should be a Hash
|
||||
|
||||
@ -49,6 +49,20 @@ describe VagrantPlugins::Kernel_V2::VMConfig do
|
||||
assert_valid
|
||||
end
|
||||
|
||||
it "validates disables_host_modification option" do
|
||||
subject.disable_hosts_modification = true
|
||||
subject.finalize!
|
||||
assert_valid
|
||||
|
||||
subject.disable_hosts_modification = false
|
||||
subject.finalize!
|
||||
assert_valid
|
||||
|
||||
subject.disable_hosts_modification = "truthy"
|
||||
subject.finalize!
|
||||
assert_invalid
|
||||
end
|
||||
|
||||
describe "#base_mac" do
|
||||
it "defaults properly" do
|
||||
subject.finalize!
|
||||
|
||||
28
test/unit/vagrant/action/builtin/set_hostname_test.rb
Normal file
28
test/unit/vagrant/action/builtin/set_hostname_test.rb
Normal 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, :disable_hosts_modification).and_return(false)
|
||||
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, :disable_hosts_modification).and_return(true)
|
||||
expect(machine).not_to receive(:guest)
|
||||
subject.call(env)
|
||||
end
|
||||
end
|
||||
Loading…
x
Reference in New Issue
Block a user