From ff0aea4493a8c6391d781ef29e1314aa3e2055e2 Mon Sep 17 00:00:00 2001 From: sophia Date: Wed, 29 Apr 2020 11:03:13 -0400 Subject: [PATCH] Add option to disable modification of /etc/hosts on guest --- lib/vagrant/action/builtin/set_hostname.rb | 6 +++- plugins/kernel_v2/config/vm.rb | 6 ++++ templates/locales/en.yml | 2 ++ test/unit/plugins/kernel_v2/config/vm_test.rb | 14 ++++++++++ .../action/builtin/set_hostname_test.rb | 28 +++++++++++++++++++ 5 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 test/unit/vagrant/action/builtin/set_hostname_test.rb diff --git a/lib/vagrant/action/builtin/set_hostname.rb b/lib/vagrant/action/builtin/set_hostname.rb index 45458db0b..fac86c60c 100644 --- a/lib/vagrant/action/builtin/set_hostname.rb +++ b/lib/vagrant/action/builtin/set_hostname.rb @@ -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 diff --git a/plugins/kernel_v2/config/vm.rb b/plugins/kernel_v2/config/vm.rb index 422ce6239..990b0b1b8 100644 --- a/plugins/kernel_v2/config/vm.rb +++ b/plugins/kernel_v2/config/vm.rb @@ -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 diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 47ee108b5..a5aad61f2 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -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 diff --git a/test/unit/plugins/kernel_v2/config/vm_test.rb b/test/unit/plugins/kernel_v2/config/vm_test.rb index c170c873f..709fd42f4 100644 --- a/test/unit/plugins/kernel_v2/config/vm_test.rb +++ b/test/unit/plugins/kernel_v2/config/vm_test.rb @@ -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! diff --git a/test/unit/vagrant/action/builtin/set_hostname_test.rb b/test/unit/vagrant/action/builtin/set_hostname_test.rb new file mode 100644 index 000000000..f11d141a3 --- /dev/null +++ b/test/unit/vagrant/action/builtin/set_hostname_test.rb @@ -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