The setting node_name_value may now be used for 'puppet apply' or 'puppet
agent' to specify the name for the node. This will not affect the certificate
used by the node, and the node will still be authenticated based on its
certname. The default value for node_name_value is the certname.

This is useful for eg. EC2 nodes whose random hostnames cannot be easily used
to classify them.

Paired-With: Jacob Helwig

Signed-off-by: Nick Lewis <[email protected]>
---
 .../tests/allow_arbitrary_node_name_for_agent.rb   |   29 ++++++++++++++++++++
 .../tests/allow_arbitrary_node_name_for_apply.rb   |   17 +++++++++++
 lib/puppet/application/apply.rb                    |    8 +++---
 lib/puppet/configurer.rb                           |    4 +-
 lib/puppet/configurer/fact_handler.rb              |    2 +-
 lib/puppet/defaults.rb                             |    1 +
 lib/puppet/transaction/report.rb                   |    2 +-
 spec/integration/defaults_spec.rb                  |    7 +++++
 spec/unit/application/apply_spec.rb                |    4 +-
 spec/unit/configurer/fact_handler_spec.rb          |   11 +++++++
 spec/unit/configurer_spec.rb                       |    6 ++--
 spec/unit/transaction/report_spec.rb               |    6 ++--
 12 files changed, 81 insertions(+), 16 deletions(-)
 create mode 100644 acceptance/tests/allow_arbitrary_node_name_for_agent.rb
 create mode 100644 acceptance/tests/allow_arbitrary_node_name_for_apply.rb

diff --git a/acceptance/tests/allow_arbitrary_node_name_for_agent.rb 
b/acceptance/tests/allow_arbitrary_node_name_for_agent.rb
new file mode 100644
index 0000000..f5e0276
--- /dev/null
+++ b/acceptance/tests/allow_arbitrary_node_name_for_agent.rb
@@ -0,0 +1,29 @@
+test_name "node_name_value should be used as the node name for puppet agent"
+
+success_message = "node_name_value setting was correctly used as the node name"
+
+authfile = "/tmp/auth.conf-2128-#{$$}"
+create_remote_file master, authfile, <<AUTHCONF
+path /catalog/specified_node_name
+auth yes
+allow *
+AUTHCONF
+
+manifest_file = "/tmp/node_name_value-test-#{$$}.pp"
+create_remote_file master, manifest_file, <<MANIFEST
+  Exec { path => "/usr/bin:/bin" }
+  node default {
+    exec { "false": }
+  }
+  node specified_node_name {
+    exec { "echo #{success_message}": }
+  }
+MANIFEST
+
+on master, "chmod 644 #{authfile} #{manifest_file}"
+
+with_master_running_on(master, "--rest_authconfig #{authfile} --manifest 
#{manifest_file} --daemonize --autosign true") do
+  run_agent_on(agents, "--no-daemonize --verbose --onetime --node_name_value 
specified_node_name --server #{master}") do
+    assert_match(success_message, stdout)
+  end
+end
diff --git a/acceptance/tests/allow_arbitrary_node_name_for_apply.rb 
b/acceptance/tests/allow_arbitrary_node_name_for_apply.rb
new file mode 100644
index 0000000..4daa8a6
--- /dev/null
+++ b/acceptance/tests/allow_arbitrary_node_name_for_apply.rb
@@ -0,0 +1,17 @@
+test_name "node_name_value should be used as the node name for puppet apply"
+
+success_message = "node_name_value setting was correctly used as the node name"
+
+manifest = %Q[
+  Exec { path => "/usr/bin:/bin" }
+  node default {
+    exec { "false": }
+  }
+  node a_different_node_name {
+    exec { "echo #{success_message}": }
+  }
+]
+
+on agents, puppet_apply("--verbose --node_name_value a_different_node_name"), 
:stdin => manifest do
+  assert_match(success_message, stdout)
+end
diff --git a/lib/puppet/application/apply.rb b/lib/puppet/application/apply.rb
index 7f0b95a..3f423a4 100644
--- a/lib/puppet/application/apply.rb
+++ b/lib/puppet/application/apply.rb
@@ -85,13 +85,13 @@ class Puppet::Application::Apply < Puppet::Application
     end
 
     # Collect our facts.
-    unless facts = Puppet::Node::Facts.find(Puppet[:certname])
-      raise "Could not find facts for #{Puppet[:certname]}"
+    unless facts = Puppet::Node::Facts.find(Puppet[:node_name_value])
+      raise "Could not find facts for #{Puppet[:node_name_value]}"
     end
 
     # Find our Node
-    unless node = Puppet::Node.find(Puppet[:certname])
-      raise "Could not find node #{Puppet[:certname]}"
+    unless node = Puppet::Node.find(Puppet[:node_name_value])
+      raise "Could not find node #{Puppet[:node_name_value]}"
     end
 
     # Merge in the facts.
diff --git a/lib/puppet/configurer.rb b/lib/puppet/configurer.rb
index 9f68ca4..d0251de 100644
--- a/lib/puppet/configurer.rb
+++ b/lib/puppet/configurer.rb
@@ -220,7 +220,7 @@ class Puppet::Configurer
   def retrieve_catalog_from_cache(fact_options)
     result = nil
     @duration = thinmark do
-      result = Puppet::Resource::Catalog.find(Puppet[:certname], 
fact_options.merge(:ignore_terminus => true))
+      result = Puppet::Resource::Catalog.find(Puppet[:node_name_value], 
fact_options.merge(:ignore_terminus => true))
     end
     Puppet.notice "Using cached catalog"
     result
@@ -233,7 +233,7 @@ class Puppet::Configurer
   def retrieve_new_catalog(fact_options)
     result = nil
     @duration = thinmark do
-      result = Puppet::Resource::Catalog.find(Puppet[:certname], 
fact_options.merge(:ignore_cache => true))
+      result = Puppet::Resource::Catalog.find(Puppet[:node_name_value], 
fact_options.merge(:ignore_cache => true))
     end
     result
   rescue SystemExit,NoMemoryError
diff --git a/lib/puppet/configurer/fact_handler.rb 
b/lib/puppet/configurer/fact_handler.rb
index 075a594..4d80e17 100644
--- a/lib/puppet/configurer/fact_handler.rb
+++ b/lib/puppet/configurer/fact_handler.rb
@@ -16,7 +16,7 @@ module Puppet::Configurer::FactHandler
     # compile them and then "cache" them on the server.
     begin
       reload_facter
-      Puppet::Node::Facts.find(Puppet[:certname])
+      Puppet::Node::Facts.find(Puppet[:node_name_value])
     rescue SystemExit,NoMemoryError
       raise
     rescue Exception => detail
diff --git a/lib/puppet/defaults.rb b/lib/puppet/defaults.rb
index 2a1ded5..17c2850 100644
--- a/lib/puppet/defaults.rb
+++ b/lib/puppet/defaults.rb
@@ -486,6 +486,7 @@ module Puppet
   )
 
   setdefaults(:agent,
+    :node_name_value => ["$certname", "The name of the node."],
     :localconfig => { :default => "$statedir/localconfig",
       :owner => "root",
       :mode => 0660,
diff --git a/lib/puppet/transaction/report.rb b/lib/puppet/transaction/report.rb
index 16fee42..841c569 100644
--- a/lib/puppet/transaction/report.rb
+++ b/lib/puppet/transaction/report.rb
@@ -67,7 +67,7 @@ class Puppet::Transaction::Report
     @logs = []
     @resource_statuses = {}
     @external_times ||= {}
-    @host = Puppet[:certname]
+    @host = Puppet[:node_name_value]
     @time = Time.now
     @kind = kind
     @report_format = 2
diff --git a/spec/integration/defaults_spec.rb 
b/spec/integration/defaults_spec.rb
index 2f30014..572d98c 100755
--- a/spec/integration/defaults_spec.rb
+++ b/spec/integration/defaults_spec.rb
@@ -23,6 +23,13 @@ describe "Puppet defaults" do
     end
   end
 
+  describe "when setting :node_name_value" do
+    it "should default to the value of :certname" do
+      Puppet.settings[:certname] = 'blargle'
+      Puppet.settings[:node_name_value].should == 'blargle'
+    end
+  end
+
   describe "when configuring the :crl" do
     it "should warn if :cacrl is set to false" do
       Puppet.expects(:warning)
diff --git a/spec/unit/application/apply_spec.rb 
b/spec/unit/application/apply_spec.rb
index 5bd902a..83a5ded 100755
--- a/spec/unit/application/apply_spec.rb
+++ b/spec/unit/application/apply_spec.rb
@@ -192,10 +192,10 @@ describe Puppet::Application::Apply do
         Puppet::Node::Facts.terminus_class = :memory
         Puppet::Node.terminus_class = :memory
 
-        @facts = Puppet::Node::Facts.new(Puppet[:certname])
+        @facts = Puppet::Node::Facts.new(Puppet[:node_name_value])
         @facts.save
 
-        @node = Puppet::Node.new(Puppet[:certname])
+        @node = Puppet::Node.new(Puppet[:node_name_value])
         @node.save
 
         @catalog = Puppet::Resource::Catalog.new
diff --git a/spec/unit/configurer/fact_handler_spec.rb 
b/spec/unit/configurer/fact_handler_spec.rb
index 241da57..ddb5411 100755
--- a/spec/unit/configurer/fact_handler_spec.rb
+++ b/spec/unit/configurer/fact_handler_spec.rb
@@ -50,6 +50,17 @@ describe Puppet::Configurer::FactHandler do
       Puppet::Node::Facts.terminus_class = :memory
     end
 
+    it "should use the node name value to retrieve the facts" do
+      foo_facts = Puppet::Node::Facts.new('foo')
+      bar_facts = Puppet::Node::Facts.new('bar')
+      foo_facts.save
+      bar_facts.save
+      Puppet[:certname] = 'foo'
+      Puppet[:node_name_value] = 'bar'
+
+      @facthandler.find_facts.should == bar_facts
+    end
+
     it "should reload Facter before finding facts" do
       @facthandler.expects(:reload_facter)
 
diff --git a/spec/unit/configurer_spec.rb b/spec/unit/configurer_spec.rb
index 1504ae5..6c4f9b9 100755
--- a/spec/unit/configurer_spec.rb
+++ b/spec/unit/configurer_spec.rb
@@ -77,7 +77,7 @@ describe Puppet::Configurer do
       Puppet.settings.stubs(:use).returns(true)
       @agent.stubs(:prepare)
       Puppet::Node::Facts.terminus_class = :memory
-      @facts = Puppet::Node::Facts.new(Puppet[:certname])
+      @facts = Puppet::Node::Facts.new(Puppet[:node_name_value])
       @facts.save
 
       @catalog = Puppet::Resource::Catalog.new
@@ -392,9 +392,9 @@ describe Puppet::Configurer do
       @agent.retrieve_catalog
     end
 
-    it "should use its certname to retrieve the catalog" do
+    it "should use its node_name_value to retrieve the catalog" do
       Facter.stubs(:value).returns "eh"
-      Puppet.settings[:certname] = "myhost.domain.com"
+      Puppet.settings[:node_name_value] = "myhost.domain.com"
       Puppet::Resource::Catalog.expects(:find).with { |name, options| name == 
"myhost.domain.com" }.returns @catalog
 
       @agent.retrieve_catalog
diff --git a/spec/unit/transaction/report_spec.rb 
b/spec/unit/transaction/report_spec.rb
index 81efa34..26d90ac 100755
--- a/spec/unit/transaction/report_spec.rb
+++ b/spec/unit/transaction/report_spec.rb
@@ -9,9 +9,9 @@ describe Puppet::Transaction::Report do
     Puppet::Util::Storage.stubs(:store)
   end
 
-  it "should set its host name to the certname" do
-    Puppet.settings.expects(:value).with(:certname).returns "myhost"
-    Puppet::Transaction::Report.new("apply").host.should == "myhost"
+  it "should set its host name to the node_name_value" do
+    Puppet[:node_name_value] = 'mynode'
+    Puppet::Transaction::Report.new("apply").host.should == "mynode"
   end
 
   it "should return its host name as its name" do
-- 
1.7.5.1

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/puppet-dev?hl=en.

Reply via email to