From 5a83d121f482e0020f39b35b6d8651a3ecde4b33 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Tue, 5 Jul 2011 19:43:36 -0400 Subject: [PATCH] better reporting and debug support --- README.md | 5 ++-- lib/guard/puppet.rb | 2 ++ lib/guard/puppet/log.rb | 24 +++++++++++++++++++ lib/guard/puppet/runner.rb | 36 ++++++++++++++++++++++++---- spec/lib/guard/puppet/log_spec.rb | 30 +++++++++++++++++++++++ spec/lib/guard/puppet/runner_spec.rb | 35 ++++++++++++++++++++++++--- 6 files changed, 123 insertions(+), 9 deletions(-) create mode 100644 lib/guard/puppet/log.rb create mode 100644 spec/lib/guard/puppet/log_spec.rb diff --git a/README.md b/README.md index f2f2735..493da87 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ Reapply your Puppet configs automatically using Guard! Awesome! ``` ruby -guard 'puppet', :verbose => true, :manifest => 'manifests/site.pp' do +guard 'puppet' do watch(%r{^(manifests|modules)}) end ``` @@ -10,9 +10,10 @@ It's assumed your configs are all in the current folder, which is the equivalent of `--confdir=$PWD` at the command line. Otherwise, there's not much use of using Guard with Puppet. :) -Two options so far: +Three options so far: * `:verbose`: Show more output from Puppet (default: `true`) +* `:debug`: Show even more output from Puppet (default: `false`) * `:manifest`: The main manifest file to run (default: `manifests/site.pp`) Bugs and fixes? You know the drill. diff --git a/lib/guard/puppet.rb b/lib/guard/puppet.rb index a826d24..ed27ab4 100644 --- a/lib/guard/puppet.rb +++ b/lib/guard/puppet.rb @@ -9,6 +9,8 @@ module ::Guard def initialize(watchers = [], options = {}) super @options = options + + UI.info "Guard::Puppet is watching for changes..." end def run_all diff --git a/lib/guard/puppet/log.rb b/lib/guard/puppet/log.rb new file mode 100644 index 0000000..0f4f0fa --- /dev/null +++ b/lib/guard/puppet/log.rb @@ -0,0 +1,24 @@ +module Puppet; end + +require 'puppet/util' +require 'puppet/util/log' + +::Puppet::Util::Log.newdesttype :guard do + attr_reader :messages + + def initialize + close + end + + def handle(msg) + @messages << msg + end + + def close + @messages = [] + end + + def has_failed? + messages.find { |msg| (::Puppet::Util::Log.levels.index(msg.level)) >= 4 } + end +end diff --git a/lib/guard/puppet/runner.rb b/lib/guard/puppet/runner.rb index 896a798..bda7542 100644 --- a/lib/guard/puppet/runner.rb +++ b/lib/guard/puppet/runner.rb @@ -1,6 +1,7 @@ require 'guard/puppet' require 'puppet/util/command_line' require 'puppet/application/apply' +require 'guard/puppet/log' module Guard class Puppet @@ -12,21 +13,48 @@ module Guard :verbose => true, :manifest => 'manifests/site.pp' }.merge(options) + end def run - ::Puppet::Util::CommandLine.new('puppet', command_line_params).execute - 0 - rescue SystemExit => e - e.status + messages = ::Puppet::Util::Log.newdestination(:guard) + + begin + maybe_bundle_with_env do + ::Puppet::Util::CommandLine.new('puppet', command_line_params).execute + end + 0 + rescue SystemExit => e + if e.status == 0 + if messages.has_failed? + 1 + else + 0 + end + else + e.status + end + ensure + ::Puppet::Util::Log.close(:guard) + end end def command_line_params command = [ "apply", %{--confdir="#{Dir.pwd}"} ] command << "-v" if @options[:verbose] + command << "-d" if @options[:debug] command << @options[:manifest] if @options[:manifest] command end + + private + def maybe_bundle_with_env(&block) + if defined?(::Bundler) + Bundler.with_clean_env(&block) + else + yield + end + end end end end diff --git a/spec/lib/guard/puppet/log_spec.rb b/spec/lib/guard/puppet/log_spec.rb new file mode 100644 index 0000000..5e97dba --- /dev/null +++ b/spec/lib/guard/puppet/log_spec.rb @@ -0,0 +1,30 @@ +require 'spec_helper' +require 'guard/puppet/log' +require 'puppet/util/log' + +describe 'guard logging for puppet' do + let(:guard) { ::Puppet::Util::Log.destinations[:guard] } + + before do + ::Puppet::Util::Log.newdestination(:guard) + end + + it 'should receive a message' do + guard.handle("test") + guard.messages.should == [ "test" ] + end + + it 'should not be a failure' do + guard.handle(stub(:level => :info)) + guard.should_not have_failed + end + + it 'should be a failure' do + guard.handle(stub(:level => :err)) + guard.should have_failed + end + + after do + ::Puppet::Util::Log.close(:guard) + end +end diff --git a/spec/lib/guard/puppet/runner_spec.rb b/spec/lib/guard/puppet/runner_spec.rb index e710d8f..8136af9 100644 --- a/spec/lib/guard/puppet/runner_spec.rb +++ b/spec/lib/guard/puppet/runner_spec.rb @@ -23,13 +23,42 @@ describe Guard::Puppet::Runner do it { should == [ 'apply', %{--confdir="#{Dir.pwd}"}, '-v', '123' ] } end + + context 'debug' do + let(:options) { { :debug => true} } + + it { should == [ 'apply', %{--confdir="#{Dir.pwd}"}, '-v', '-d', 'manifests/site.pp' ] } + end end describe '#run' do - it 'should return the result of an exit call' do - ::Puppet::Util::CommandLine.expects(:new).raises(SystemExit.new(10)) + before do + ::Puppet::Util::CommandLine.expects(:new).raises(SystemExit.new(return_value)) + end - runner.run.should == 10 + context 'returns a non-zero value' do + let(:return_value) { 10 } + + it 'should return the result of an exit call' do + runner.run.should == return_value + end + end + + context 'returns a zero value' do + let(:return_value) { 0 } + let(:messages) do + messages = stub + messages.stubs(:has_failed?).returns(true) + messages + end + + before do + Puppet::Util::Log.stubs(:newdestination).returns(messages) + end + + it 'should check the status of the messages output' do + runner.run.should == 1 + end end end end