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 <[email protected]>
---
 lib/puppet/application/puppetmasterd.rb |   28 +++++++++++++-
 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 e505129..40bc306 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 000b2d6..a4875c7 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
-- 
1.6.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