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,18 +24,27 @@ module VagrantPlugins
@logger = Log4r::Logger.new("vagrant::provisioners::chef") @logger = Log4r::Logger.new("vagrant::provisioners::chef")
if !present?(@config.node_name) if !present?(@config.node_name)
cache = @machine.data_dir.join("chef_node_name") # First attempt to get the node name from the hostname, and if that
# is not present, generate/retrieve a random hostname.
if !cache.exist? hostname = @machine.config.vm.hostname
@machine.ui.info I18n.t("vagrant.provisioners.chef.generating_node_name") if present?(hostname)
cache.open("w+") do |f| @machine.ui.info I18n.t("vagrant.provisioners.chef.using_hostname_node_name",
f.write("vagrant-#{SecureRandom.hex(4)}") 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|
f.write("vagrant-#{SecureRandom.hex(4)}")
end
end end
end
if cache.file? if cache.file?
@logger.info("Loading cached node_name...") @logger.info("Loading cached node_name...")
@config.node_name = cache.read.strip @config.node_name = cache.read.strip
end
end end
end end
end end

View File

@ -133,7 +133,7 @@ module VagrantPlugins
end end
def delete_from_chef_server(deletable) def delete_from_chef_server(deletable)
node_name = @config.node_name || @machine.config.vm.hostname node_name = @config.node_name
if !present?(node_name) if !present?(node_name)
@machine.ui.warn(I18n.t("vagrant.provisioners.chef.missing_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..." client_key_folder: "Creating folder to hold client key..."
generating_node_name: |- generating_node_name: |-
Auto-generating node name for Chef... Auto-generating node name for Chef...
using_hostname_node_name: |-
Using hostname "%{hostname}" as node name for Chef...
install_failed: |- install_failed: |-
Vagrant could not detect Chef on the guest! Even after Vagrant Vagrant could not detect Chef on the guest! Even after Vagrant
attempted to install Chef, it could still not find Chef on the system. 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=) allow(config).to receive(:node_name=)
end 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 describe "#encrypted_data_bag_secret_key_path" do
let(:env) { double("env") } let(:env) { double("env") }
let(:root_path) { "/my/root" } let(:root_path) { "/my/root" }