Richard K. Miller
2013-Oct-03 00:42 UTC
[Puppet Users] Idiom for throwing an error if a command fails
Hi, What''s the best idiom for executing a command on every Puppet run and triggering an error if the command fails? For example, the following code throws an error if the machine has anything other than 8 cores. exec { "echo ''This machine does not have 8 cores!''; exit 1": unless => "facter processorcount | grep -q 8", } The "; exit 1" changes this from a Notice to an Error, but it seems a bit hackish. I couldn''t discover a way to do this with err() or notify{}. I''m envisioning something like this: notify { ''This machine does not have 8 cores'': error_level => warning, unless => "facter processorcount | grep -q 8", } Not mission-critical. Just curious... Richard -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users. For more options, visit https://groups.google.com/groups/opt_out.
jcbollinger
2013-Oct-03 13:35 UTC
[Puppet Users] Re: Idiom for throwing an error if a command fails
On Wednesday, October 2, 2013 7:42:59 PM UTC-5, Richard K. Miller wrote:> > Hi, > > What''s the best idiom for executing a command on every Puppet run and > triggering an error if the command fails? > >It depends on what you mean by "triggering an error". If you run a command via an Exec resource, and the exit code is not zero (or whatever other code you specify should be expected) then that resource fails and the failure will be recorded in the agent''s log. If reporting is enabled then the failure will also be noted in the agent''s report. If you invert the sense of the command''s exit code (for instance, by running using the "shell" provider and prefixing the command with ''!'') then you can set up other resources, such as Notify, to be applied only when the inverted command succeeds (i.e. when the underlying command fails). Or you can roll the failure action into the command itself.> For example, the following code throws an error if the machine has > anything other than 8 cores. > > exec { "echo ''This machine does not have 8 cores!''; exit 1": > unless => "facter processorcount | grep -q 8", > } > > The "; exit 1" changes this from a Notice to an Error, but it seems a bit > hackish. I couldn''t discover a way to do this with err() or notify{}. > >That''s a bizarre way of doing things. Your node''s facts are reported to the master to inform catalog compilation, so you should not need to run facter again when you apply resources. I would probably write that particular check something like this: if $::processorcount != 8 { exec { "Expected 8 processors but have ${::processorcount}": command => ''/bin/false'' } } or like this if $::processorcount != 8 { notify { "Expected 8 processors but have ${::processorcount}": } } or like this if $::processorcount != 8 { exec { "log wrong processor count": command => "logger -p local0.warn ''Expected 8 processors but have ${::processorcount}''" } } depending on what exactly I want. You cannot use the err() or warning() functions for this if you want the message to appear in the agent''s log, because Puppet functions run on the master during catalog compilation, not on the agent. If logging to the master''s log would do the trick, however, then you could do this: if $::processorcount != 8 { warning("Expected 8 processors on ${::hostname}, but Facter reports ${::processorcount}") } John -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users. For more options, visit https://groups.google.com/groups/opt_out.
Richard K Miller
2013-Oct-03 16:39 UTC
Re: [Puppet Users] Idiom for throwing an error if a command fails
> For example, the following code throws an error if the machine has anything other than 8 cores. > > exec { "echo ''This machine does not have 8 cores!''; exit 1": > unless => "facter processorcount | grep -q 8", > } > > The "; exit 1" changes this from a Notice to an Error, but it seems a bit hackish. I couldn''t discover a way to do this with err() or notify{}. > > > > That''s a bizarre way of doing things. Your node''s facts are reported to the master to inform catalog compilation, so you should not need to run facter again when you apply resources. I would probably write that particular check something like this:Exactly. I figured there was a more idiomatic way of doing this.> if $::processorcount != 8 { > exec { "Expected 8 processors but have ${::processorcount}": > command => ''/bin/false'' > } > } >This works, and I like your using the Puppet fact natively instead of calling Facter manually. My only slight complaint is that the message (in Puppet Dashboard) is a bit opaque: Level: err Message: "change from notrun to 0 failed: /bin/false returned 1 instead of one of [0]" Source: "/Stage[main]/Common/Exec[1. Expected 8 processors but have 4]/returns"> or like this > > if $::processorcount != 8 { > notify { "Expected 8 processors but have ${::processorcount}": } > } >I like this approach the best. However, because it triggers a "notice" level, it doesn''t get reported by email or in Puppet Dashboard as a failure. It seems there ought to exist a Puppet type called "error", parallel in functionality to notify, that triggers at the error level.> or like this > > if $::processorcount != 8 { > exec { "log wrong processor count": > command => "logger -p local0.warn ''Expected 8 processors but have ${::processorcount}''" > } > }Interesting approach that I didn''t even remotely consider.> You cannot use the err() or warning() functions for this if you want the message to appear in the agent''s log, because Puppet functions run on the master during catalog compilation, not on the agent. If logging to the master''s log would do the trick, however, then you could do this: > > if $::processorcount != 8 { > warning("Expected 8 processors on ${::hostname}, but Facter reports ${::processorcount}") > }You''re right; this won''t work because I want the client to report an error. Thanks for your thoughts on this. I''m left wishing Puppet supported an "error" type because that seems like the most native idiom. Richard -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users. For more options, visit https://groups.google.com/groups/opt_out.