Skip to content

Commit

Permalink
Merge branch 'tickets/3.x/14136' of https://github.com/domcleal/puppet
Browse files Browse the repository at this point in the history
…into 3.0rc

(#14136) Emit debug/warnings when Augeas provider fails to load files
Originally submitted as #733 against master, re-targeting to 3.x now the
branch exists.

The messages and information from /augeas//error are now printed to
debug when the provider opens Augeas, allowing file parse errors to be
found.

The provider can also restrict the files that are loaded (from the
default of everything). In these cases, the provider now emits a warning
and then prints all the detailed information to debug. Since a few parse
errors are common, no warning is given when these optimisations aren't
in use.

Conflicts:
	CHANGELOG
	lib/puppet/provider/augeas/augeas.rb
  • Loading branch information
zaphod42 committed Jun 26, 2012
2 parents 624cb1d + 911e2d4 commit a4451fb
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -30,7 +30,7 @@ Generally, you need the following things installed:
available separately. You could probably just compile and install that one
library, though.

* Facter => 1.5.1 (available via your package manager or from the [Facter site](http://puppetlabs.com/projects/facter)).
* Facter => 2.0.0 (available via your package manager or from the [Facter site](http://puppetlabs.com/projects/facter)).

License
-------
Expand Down
10 changes: 7 additions & 3 deletions conf/redhat/puppet.spec
Expand Up @@ -12,9 +12,9 @@ Summary: A network tool for managing many disparate systems
License: ASL 2.0
URL: http://puppetlabs.com
#Source0: http://puppetlabs.com/downloads/%{name}/%{name}-%{version}.tar.gz
Source0: http://puppetlabs.com/downloads/%{name}/%{name}-%{version}rc4.tar.gz
Source0: http://puppetlabs.com/downloads/%{name}/%{name}-%{version}rc1.tar.gz
#Source1: http://puppetlabs.com/downloads/%{name}/%{name}-%{version}.tar.gz.asc
Source1: http://puppetlabs.com/downloads/%{name}/%{name}-%{version}rc4.tar.gz.asc
Source1: http://puppetlabs.com/downloads/%{name}/%{name}-%{version}rc1.tar.gz.asc

Group: System Environment/Base

Expand Down Expand Up @@ -69,7 +69,7 @@ The server can also function as a certificate authority and file server.

%prep
#%setup -q -n %{name}-%{version}
%setup -q -n %{name}-%{version}rc4
%setup -q -n %{name}-%{version}rc1
patch -s -p1 < conf/redhat/rundir-perms.patch


Expand Down Expand Up @@ -162,6 +162,7 @@ echo "D /var/run/%{name} 0755 %{name} %{name} -" > \
%{_mandir}/man8/puppet-apply.8.gz
%{_mandir}/man8/puppet-catalog.8.gz
%{_mandir}/man8/puppet-describe.8.gz
%{_mandir}/man8/puppet-ca.8.gz
%{_mandir}/man8/puppet-cert.8.gz
%{_mandir}/man8/puppet-certificate.8.gz
%{_mandir}/man8/puppet-certificate_request.8.gz
Expand Down Expand Up @@ -266,6 +267,9 @@ fi
rm -rf %{buildroot}

%changelog
* Wed Jun 06 2012 Matthaus Litteken <matthaus@puppetlabs.com> - 2.7.16-0.1rc1
- Update for 2.7.16rc1, added generated manpages

* Fri Jun 01 2012 Matthaus Litteken <matthaus@puppetlabs.com> - 3.0.0-0.1rc3
- Puppet 3.0.0rc3 Release

Expand Down
25 changes: 23 additions & 2 deletions lib/puppet/provider/augeas/augeas.rb
Expand Up @@ -164,21 +164,27 @@ def open_augeas
glob_avail = !aug.match("/augeas/version/pathx/functions/glob").empty?
opt_ctx = resource[:context].match("^/files/[^'\"\\[\\]]+$") if resource[:context]

restricted = false
if resource[:incl]
aug.set("/augeas/load/Xfm/lens", resource[:lens])
aug.set("/augeas/load/Xfm/incl", resource[:incl])
elsif glob_avail and opt_ctx
restricted = true
# Optimize loading if the context is given, requires the glob function
# from Augeas 0.8.2 or up
ctx_path = resource[:context].sub(/^\/files(.*?)\/?$/, '\1/')
load_path = "/augeas/load/*['%s' !~ glob(incl) + regexp('/.*')]" % ctx_path

if aug.match(load_path).size < aug.match("/augeas/load/*").size
aug.rm(load_path)
restricted = true
else
# This will occur if the context is less specific than any glob
debug("Unable to optimize files loaded by context path, no glob matches")
end
end
aug.load
print_load_errors(:warning => restricted)
end
@aug
end
Expand Down Expand Up @@ -282,14 +288,29 @@ def set_augeas_save_mode(mode)
@aug.set("/augeas/save", mode)
end

def print_load_errors(args={})
errors = @aug.match("/augeas//error")
unless errors.empty?
if args[:warning]
warning("Loading failed for one or more files, see debug for /augeas//error output")
else
debug("Loading failed for one or more files, output from /augeas//error:")
end
end
print_errors(errors)
end

def print_put_errors
errors = @aug.match("/augeas//error[. = 'put_failed']")
debug("Put failed on one or more files, output from /augeas//error:") unless errors.empty?
print_errors(errors)
end

def print_errors(errors)
errors.each do |errnode|
@aug.match("#{errnode}/*").each do |subnode|
sublabel = subnode.split("/")[-1]
subvalue = @aug.get(subnode)
debug("#{sublabel} = #{subvalue}")
debug("#{subnode} = #{subvalue}")
end
end
end
Expand Down
32 changes: 30 additions & 2 deletions spec/unit/provider/augeas/augeas_spec.rb
Expand Up @@ -608,14 +608,34 @@
end
end

describe "save failure reporting" do
describe "load/save failure reporting" do
before do
@augeas = stub("augeas")
@augeas.stubs("close")
@provider.aug = @augeas
end

it "should find errors and output to debug" do
describe "should find load errors" do
before do
@augeas.expects(:match).with("/augeas//error").returns(["/augeas/files/foo/error"])
@augeas.expects(:match).with("/augeas/files/foo/error/*").returns(["/augeas/files/foo/error/path", "/augeas/files/foo/error/message"])
@augeas.expects(:get).with("/augeas/files/foo/error/path").returns("/foo")
@augeas.expects(:get).with("/augeas/files/foo/error/message").returns("Failed to...")
end

it "and output to debug" do
@provider.expects(:debug).times(4)
@provider.print_load_errors
end

it "and output a warning and to debug" do
@provider.expects(:warning).once()
@provider.expects(:debug).times(3)
@provider.print_load_errors(:warning => true)
end
end

it "should find save errors and output to debug" do
@augeas.expects(:match).with("/augeas//error[. = 'put_failed']").returns(["/augeas/files/foo/error"])
@augeas.expects(:match).with("/augeas/files/foo/error/*").returns(["/augeas/files/foo/error/path", "/augeas/files/foo/error/message"])
@augeas.expects(:get).with("/augeas/files/foo/error/path").returns("/foo")
Expand All @@ -638,11 +658,18 @@
aug.match("/files/etc/test").should == []
end

it "should report load errors to debug only" do
@provider.expects(:print_load_errors).with(:warning => false)
aug = @provider.open_augeas
aug.should_not == nil
end

# Only the file specified should be loaded
it "should load one file if incl/lens used" do
@resource[:incl] = "/etc/hosts"
@resource[:lens] = "Hosts.lns"

@provider.expects(:print_load_errors).with(:warning => true)
aug = @provider.open_augeas
aug.should_not == nil
aug.match("/files/etc/fstab").should == []
Expand All @@ -665,6 +692,7 @@
it "should only load one file if relevant context given" do
@resource[:context] = "/files/etc/fstab"

@provider.expects(:print_load_errors).with(:warning => true)
aug = @provider.open_augeas
aug.should_not == nil
aug.match("/files/etc/fstab").should == ["/files/etc/fstab"]
Expand Down

0 comments on commit a4451fb

Please sign in to comment.