better reporting and debug support

This commit is contained in:
John Bintz 2011-07-05 19:43:36 -04:00
parent 217bb55774
commit 5a83d121f4
6 changed files with 123 additions and 9 deletions

View File

@ -1,7 +1,7 @@
Reapply your Puppet configs automatically using Guard! Awesome! Reapply your Puppet configs automatically using Guard! Awesome!
``` ruby ``` ruby
guard 'puppet', :verbose => true, :manifest => 'manifests/site.pp' do guard 'puppet' do
watch(%r{^(manifests|modules)}) watch(%r{^(manifests|modules)})
end 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, equivalent of `--confdir=$PWD` at the command line. Otherwise,
there's not much use of using Guard with Puppet. :) 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`) * `: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`) * `:manifest`: The main manifest file to run (default: `manifests/site.pp`)
Bugs and fixes? You know the drill. Bugs and fixes? You know the drill.

View File

@ -9,6 +9,8 @@ module ::Guard
def initialize(watchers = [], options = {}) def initialize(watchers = [], options = {})
super super
@options = options @options = options
UI.info "Guard::Puppet is watching for changes..."
end end
def run_all def run_all

24
lib/guard/puppet/log.rb Normal file
View File

@ -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

View File

@ -1,6 +1,7 @@
require 'guard/puppet' require 'guard/puppet'
require 'puppet/util/command_line' require 'puppet/util/command_line'
require 'puppet/application/apply' require 'puppet/application/apply'
require 'guard/puppet/log'
module Guard module Guard
class Puppet class Puppet
@ -12,21 +13,48 @@ module Guard
:verbose => true, :verbose => true,
:manifest => 'manifests/site.pp' :manifest => 'manifests/site.pp'
}.merge(options) }.merge(options)
end end
def run def run
messages = ::Puppet::Util::Log.newdestination(:guard)
begin
maybe_bundle_with_env do
::Puppet::Util::CommandLine.new('puppet', command_line_params).execute ::Puppet::Util::CommandLine.new('puppet', command_line_params).execute
end
0 0
rescue SystemExit => e rescue SystemExit => e
if e.status == 0
if messages.has_failed?
1
else
0
end
else
e.status e.status
end end
ensure
::Puppet::Util::Log.close(:guard)
end
end
def command_line_params def command_line_params
command = [ "apply", %{--confdir="#{Dir.pwd}"} ] command = [ "apply", %{--confdir="#{Dir.pwd}"} ]
command << "-v" if @options[:verbose] command << "-v" if @options[:verbose]
command << "-d" if @options[:debug]
command << @options[:manifest] if @options[:manifest] command << @options[:manifest] if @options[:manifest]
command command
end end
private
def maybe_bundle_with_env(&block)
if defined?(::Bundler)
Bundler.with_clean_env(&block)
else
yield
end
end
end end
end end
end end

View File

@ -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

View File

@ -23,13 +23,42 @@ describe Guard::Puppet::Runner do
it { should == [ 'apply', %{--confdir="#{Dir.pwd}"}, '-v', '123' ] } it { should == [ 'apply', %{--confdir="#{Dir.pwd}"}, '-v', '123' ] }
end end
context 'debug' do
let(:options) { { :debug => true} }
it { should == [ 'apply', %{--confdir="#{Dir.pwd}"}, '-v', '-d', 'manifests/site.pp' ] }
end
end end
describe '#run' do describe '#run' do
it 'should return the result of an exit call' do before do
::Puppet::Util::CommandLine.expects(:new).raises(SystemExit.new(10)) ::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 end
end end