Merge pull request #7153 from mitchellh/sethvargo/chef_node_name

Use hostname if no node_name is set
This commit is contained in:
Seth Vargo 2016-03-17 20:31:37 -05:00
commit 74fad20d09
4 changed files with 51 additions and 11 deletions

View File

@ -24,8 +24,16 @@ 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)
# 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") cache = @machine.data_dir.join("chef_node_name")
if !cache.exist? if !cache.exist?
@machine.ui.info I18n.t("vagrant.provisioners.chef.generating_node_name") @machine.ui.info I18n.t("vagrant.provisioners.chef.generating_node_name")
cache.open("w+") do |f| cache.open("w+") do |f|
@ -39,6 +47,7 @@ module VagrantPlugins
end end
end end
end end
end
def install_chef def install_chef
return if !config.install return if !config.install

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" }