diff options
| author | Luke Kanies <luke@madstop.com> | 2009-07-23 19:10:29 -0700 |
|---|---|---|
| committer | James Turnbull <james@lovedthanlost.net> | 2009-07-31 16:41:41 +1000 |
| commit | 76fc2b177a026e49d8370de2092f77108769110a (patch) | |
| tree | 1df20a9c74a2fd9431c9e8c31918462489ff45f4 | |
| parent | 832b6ff1e18cf403213cbeb42646b5740669e6a5 (diff) | |
| download | puppet-76fc2b177a026e49d8370de2092f77108769110a.tar.gz puppet-76fc2b177a026e49d8370de2092f77108769110a.tar.xz puppet-76fc2b177a026e49d8370de2092f77108769110a.zip | |
Fixing #2440 - catalogs can now be compiled on demand
This uses the locally cached yaml facts and prints the
catalog in json. It's meant to be used one-time, but
you have to use puppetmasterd since we assume it's the
executable correctly configured for compilation.
Signed-off-by: Luke Kanies <luke@madstop.com>
| -rw-r--r-- | lib/puppet/application/puppetmasterd.rb | 28 | ||||
| -rw-r--r-- | spec/unit/application/puppetmasterd.rb | 63 |
2 files changed, 90 insertions, 1 deletions
diff --git a/lib/puppet/application/puppetmasterd.rb b/lib/puppet/application/puppetmasterd.rb index e5051293f..40bc30663 100644 --- a/lib/puppet/application/puppetmasterd.rb +++ b/lib/puppet/application/puppetmasterd.rb @@ -14,6 +14,10 @@ Puppet::Application.new(:puppetmasterd) do # internal option, only to be used by ext/rack/config.ru option("--rack") + option("--compile host", "-c host") do |arg| + options[:node] = arg + end + option("--logdest DEST", "-l DEST") do |arg| begin Puppet::Util::Log.newdestination(arg) @@ -38,7 +42,29 @@ Puppet::Application.new(:puppetmasterd) do end dispatch do - return Puppet[:parseonly] ? :parseonly : :main + if options[:node] + :compile + elsif Puppet[:parseonly] + :parseonly + else + :main + end + end + + command(:compile) do + Puppet::Util::Log.newdestination :console + raise ArgumentError, "Cannot render compiled catalogs without json support" unless Puppet.features.json? + begin + unless catalog = Puppet::Resource::Catalog.find(options[:node]) + raise "Could not compile catalog for %s" % options[:node] + end + + $stdout.puts catalog.render(:json) + rescue => detail + $stderr.puts detail + exit(30) + end + exit(0) end command(:parseonly) do diff --git a/spec/unit/application/puppetmasterd.rb b/spec/unit/application/puppetmasterd.rb index 000b2d602..a4875c713 100644 --- a/spec/unit/application/puppetmasterd.rb +++ b/spec/unit/application/puppetmasterd.rb @@ -32,6 +32,10 @@ describe "PuppetMaster" do @puppetmasterd.should respond_to(:parseonly) end + it "should declare a compile command" do + @puppetmasterd.should respond_to(:compile) + end + it "should declare a preinit block" do @puppetmasterd.should respond_to(:run_preinit) end @@ -237,8 +241,14 @@ describe "PuppetMaster" do @puppetmasterd.get_command.should == :parseonly end + it "should dispatch to compile if called with --compile" do + @puppetmasterd.options[:node] = "foo" + @puppetmasterd.get_command.should == :compile + end + it "should dispatch to main if parseonly is not set" do Puppet.stubs(:[]).with(:parseonly).returns(false) + @puppetmasterd.options[:node] = nil @puppetmasterd.get_command.should == :main end @@ -276,6 +286,59 @@ describe "PuppetMaster" do end + describe "the compile command" do + before do + Puppet.stubs(:[]).with(:environment) + Puppet.stubs(:[]).with(:manifest).returns("site.pp") + @interpreter = stub_everything + Puppet.stubs(:err) + @puppetmasterd.stubs(:exit) + Puppet::Parser::Interpreter.stubs(:new).returns(@interpreter) + Puppet.features.stubs(:json?).returns true + end + + it "should fail if json isn't available" do + Puppet.features.expects(:json?).returns false + lambda { @puppetmasterd.compile }.should raise_error + end + + it "should compile a catalog for the specified node" do + @puppetmasterd.options[:node] = "foo" + Puppet::Resource::Catalog.expects(:find).with("foo").returns Puppet::Resource::Catalog.new + $stdout.stubs(:puts) + + @puppetmasterd.compile + end + + it "should render the catalog to json and print the output" do + @puppetmasterd.options[:node] = "foo" + catalog = Puppet::Resource::Catalog.new + catalog.expects(:render).with(:json).returns "myjson" + Puppet::Resource::Catalog.expects(:find).returns catalog + + $stdout.expects(:puts).with("myjson") + @puppetmasterd.compile + end + + it "should exit with error code 30 if no catalog can be found" do + @puppetmasterd.options[:node] = "foo" + Puppet::Resource::Catalog.expects(:find).returns nil + @puppetmasterd.expects(:exit).with(30) + $stderr.expects(:puts) + + @puppetmasterd.compile + end + + it "should exit with error code 30 if there's a failure" do + @puppetmasterd.options[:node] = "foo" + Puppet::Resource::Catalog.expects(:find).raises ArgumentError + @puppetmasterd.expects(:exit).with(30) + $stderr.expects(:puts) + + @puppetmasterd.compile + end + end + describe "the main command" do before :each do @puppetmasterd.run_preinit |
