Use hostname if no node_name is set

Fixes GH-7063
This commit is contained in:
Seth Vargo 2016-03-17 20:15:11 -05:00
parent cf925813a0
commit 8319f159b6
4 changed files with 51 additions and 11 deletions

View File

@ -24,8 +24,16 @@ module VagrantPlugins
@logger = Log4r::Logger.new("vagrant::provisioners::chef")
if !present?(@config.node_name)
# First attempt to get the node name from the hostname, and if that
# is not present, generate/retrieve a random hostname.
hostname = @machine.config.vm.hostname
if present?(hostname)
@machine.ui.info I18n.t("vagrant.provisioners.chef.using_hostname_node_name",
hostname: hostname,
)
@config.node_name = hostname
else
cache = @machine.data_dir.join("chef_node_name")
if !cache.exist?
@machine.ui.info I18n.t("vagrant.provisioners.chef.generating_node_name")
cache.open("w+") do |f|
@ -39,6 +47,7 @@ module VagrantPlugins
end
end
end
end
def install_chef
return if !config.install

View File

@ -133,7 +133,7 @@ module VagrantPlugins
end
def delete_from_chef_server(deletable)
node_name = @config.node_name || @machine.config.vm.hostname
node_name = @config.node_name
if !present?(node_name)
@machine.ui.warn(I18n.t("vagrant.provisioners.chef.missing_node_name",

View File

@ -1950,6 +1950,8 @@ en:
client_key_folder: "Creating folder to hold client key..."
generating_node_name: |-
Auto-generating node name for Chef...
using_hostname_node_name: |-
Using hostname "%{hostname}" as node name for Chef...
install_failed: |-
Vagrant could not detect Chef on the guest! Even after Vagrant
attempted to install Chef, it could still not find Chef on the system.

View File

@ -22,6 +22,35 @@ describe VagrantPlugins::Chef::Provisioner::Base do
allow(config).to receive(:node_name=)
end
describe "#node_name" do
let(:env) { double("env") }
let(:root_path) { "/my/root" }
before do
allow(machine).to receive(:env).and_return(env)
allow(env).to receive(:root_path).and_return(root_path)
end
it "defaults to node_name if given" do
config = OpenStruct.new(node_name: "name")
instance = described_class.new(machine, config)
expect(instance.config.node_name).to eq("name")
end
it "defaults to hostname if given" do
machine.config.vm.hostname = "by.hostname"
instance = described_class.new(machine, OpenStruct.new)
expect(instance.config.node_name).to eq("by.hostname")
end
it "generates a random name if no hostname or node_name is given" do
config = OpenStruct.new(node_name: nil)
machine.config.vm.hostname = nil
instance = described_class.new(machine, OpenStruct.new)
expect(instance.config.node_name).to match(/vagrant\-.+/)
end
end
describe "#encrypted_data_bag_secret_key_path" do
let(:env) { double("env") }
let(:root_path) { "/my/root" }