Skip to content

Commit

Permalink
Merge pull request chef#1216 from opscode/lcg/add-machinename
Browse files Browse the repository at this point in the history
add ohai[:machinename]
  • Loading branch information
lamont-granquist committed Mar 5, 2014
2 parents ad6abcc + 7bd5a12 commit 202facd
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
7 changes: 2 additions & 5 deletions lib/chef/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -293,13 +293,10 @@ def run_ohai
end

def node_name
name = Chef::Config[:node_name] || ohai[:fqdn] || ohai[:hostname]
name = Chef::Config[:node_name] || ohai[:fqdn] || ohai[:machinename] || ohai[:hostname]
Chef::Config[:node_name] = name

unless name
msg = "Unable to determine node name: configure node_name or configure the system's hostname and fqdn"
raise Chef::Exceptions::CannotDetermineNodeName, msg
end
raise Chef::Exceptions::CannotDetermineNodeName unless name

# node names > 90 bytes only work with authentication protocol >= 1.1
# see discussion in config.rb.
Expand Down
8 changes: 7 additions & 1 deletion lib/chef/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ class SearchIndex < RuntimeError; end
class Override < RuntimeError; end
class UnsupportedAction < RuntimeError; end
class MissingLibrary < RuntimeError; end
class CannotDetermineNodeName < RuntimeError; end

class CannotDetermineNodeName < RuntimeError
def initialize
super "Unable to determine node name: configure node_name or configure the system's hostname and fqdn"
end
end

class User < RuntimeError; end
class Group < RuntimeError; end
class Link < RuntimeError; end
Expand Down
37 changes: 37 additions & 0 deletions spec/unit/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@
describe Chef::Client do

let(:hostname) { "hostname" }
let(:machinename) { "machinename.example.org" }
let(:fqdn) { "hostname.example.org" }

let(:ohai_data) do
{ :fqdn => fqdn,
:hostname => hostname,
:machinename => machinename,
:platform => 'example-platform',
:platform_version => 'example-platform-1.0',
:data => {}
Expand Down Expand Up @@ -560,5 +562,40 @@ def stub_for_sync_cookbooks
end
end

describe "setting node name" do
context "when machinename, hostname and fqdn are all set" do
it "favors the fqdn" do
expect(client.node_name).to eql(fqdn)
end
end

context "when fqdn is missing" do
# ohai 7 should always have machinename == return of hostname
let(:fqdn) { nil }
it "favors the machinename" do
expect(client.node_name).to eql(machinename)
end
end

context "when fqdn and machinename are missing" do
# ohai 6 will not have machinename, return the short hostname
let(:fqdn) { nil }
let(:machinename) { nil }
it "falls back to hostname" do
expect(client.node_name).to eql(hostname)
end
end

context "when they're all missing" do
let(:machinename) { nil }
let(:hostname) { nil }
let(:fqdn) { nil }

it "throws an exception" do
expect { client.node_name }.to raise_error(Chef::Exceptions::CannotDetermineNodeName)
end
end

end
end

0 comments on commit 202facd

Please sign in to comment.