I am trying to write a custom facter for CentOS/RHEL that tells me how many updates are found on a run of ''yum check-update'' Facter.add(:cis_yum_check) do timeout = 300 setcode do File.open("/tmp/yum_check-update", ''w'') { |f| f.write(`/usr/bin/yum check-update`) } back_arr = [] f = `cat /tmp/yum_check-update` f.lines.reverse_each { |line| back_arr << line } "#{back_arr.index("\n")} updates" end end The output I get back in my fact reports only contains a string with " updates" in it, so no number. However, if I paste the code inside the setcode block into a test.rb script....it works just fine. Can anyone tell me why I''m not getting the number in my fact? Thanks, Matt -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
On Mon, Feb 20, 2012 at 5:33 PM, Matt Mencel <matt@techminer.net> wrote:> The output I get back in my fact reports only contains a string with " > updates" in it, so no number. However, if I paste the code inside the > setcode block into a test.rb script....it works just fine.Are you testing this code on the same version of EL that you are deploying to? AFACT #lines wasn''t added until 1.8.7 and if you are deploying to EL5 you don''t have that. -- Nathan Powell Linux System Administrator "Where else would you rather be than right here, right now?" ~ Marv Levy -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
On Mon, Feb 20, 2012 at 5:33 PM, Matt Mencel <matt@techminer.net> wrote:> I am trying to write a custom facter for CentOS/RHEL that tells me how many > updates are found on a run of ''yum check-update''Sorry I replied hastily. After replying I thought about what you''re trying to do. You don''t need all those gyrations to get this data Tested quickly on RHEL6 and CentOS5: `sudo yum check-update -q`.split(/\n/).reject{|i| i == ""}.length -- Nathan Powell Linux System Administrator "Where else would you rather be than right here, right now?" ~ Marv Levy -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
Hi Nathan, Excellent! Thanks for the tip. Matt On Mon, Feb 20, 2012 at 5:33 PM, Matt Mencel <matt@techminer.net> wrote:> I am trying to write a custom facter for CentOS/RHEL that tells me howmany> updates are found on a run of ''yum check-update''Sorry I replied hastily. After replying I thought about what you''re trying to do. You don''t need all those gyrations to get this data Tested quickly on RHEL6 and CentOS5: `sudo yum check-update -q`.split(/\n/).reject{|i| i == ""}.length -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
On Feb 20, 7:14 pm, Nathan Powell <nat...@nathanpowell.org> wrote:> On Mon, Feb 20, 2012 at 5:33 PM, Matt Mencel <m...@techminer.net> wrote: > > I am trying to write a custom facter for CentOS/RHEL that tells me how many > > updates are found on a run of ''yum check-update'' > > Sorry I replied hastily. After replying I thought about what you''re > trying to do. You don''t need all those gyrations to get this data > > Tested quickly on RHEL6 and CentOS5: > > `sudo yum check-update -q`.split(/\n/).reject{|i| i == ""}.lengthAlternatively, `yum -q check-update | grep ''\\w'' | wc --lines`.chomp sudo is not needed to run yum queries, and avoiding sudo when you don''t need it is good practice. Personally, I find that command a lot more legible than the Ruby split / reject / length business, too, though YMMV. Also, I think you need to set the timeout inside your setcode block, not outside. Only that way can you be sure that you''ll have the desired timeout when the fact actually runs. (Otherwise, you might get the timeout that some other fact sets.) John -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
On Tue, Feb 21, 2012 at 9:57 AM, jcbollinger <John.Bollinger@stjude.org> wrote:> Alternatively, > > `yum -q check-update | grep ''\\w'' | wc --lines`.chompYep, lots of ways to do it.> sudo is not needed to run yum queries, and avoiding sudo when you > don''t need it is good practice. Personally, I find that command a lotGood point. I was testing in irb as a regular user and just cut and paste what I did into the email. It shouldn''t have sudo in there.> more legible than the Ruby split / reject / length business, too, > though YMMV.I think method chaining and the functional aspects (reject) of Ruby are elegant and use them every chance I get. Both are fine and valid though. -- Nathan Powell Linux System Administrator "Where else would you rather be than right here, right now?" ~ Marv Levy -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
John, The only examples I''ve seen of timeout have it outside the setcode block. Can anyone confirm it''s behavior depending on whether it''s inside or outside setcode? The docs for it are pretty thin. Thanks, Matt On Tue, Feb 21, 2012 at 8:57 AM, jcbollinger <John.Bollinger@stjude.org>wrote:> > > On Feb 20, 7:14 pm, Nathan Powell <nat...@nathanpowell.org> wrote: > > On Mon, Feb 20, 2012 at 5:33 PM, Matt Mencel <m...@techminer.net> wrote: > > > I am trying to write a custom facter for CentOS/RHEL that tells me how > many > > > updates are found on a run of ''yum check-update'' > > > > Sorry I replied hastily. After replying I thought about what you''re > > trying to do. You don''t need all those gyrations to get this data > > > > Tested quickly on RHEL6 and CentOS5: > > > > `sudo yum check-update -q`.split(/\n/).reject{|i| i == ""}.length > > > Alternatively, > > `yum -q check-update | grep ''\\w'' | wc --lines`.chomp > > sudo is not needed to run yum queries, and avoiding sudo when you > don''t need it is good practice. Personally, I find that command a lot > more legible than the Ruby split / reject / length business, too, > though YMMV. > > Also, I think you need to set the timeout inside your setcode block, > not outside. Only that way can you be sure that you''ll have the > desired timeout when the fact actually runs. (Otherwise, you might > get the timeout that some other fact sets.) > > > John > > -- > You received this message because you are subscribed to the Google Groups > "Puppet Users" group. > To post to this group, send email to puppet-users@googlegroups.com. > To unsubscribe from this group, send email to > puppet-users+unsubscribe@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/puppet-users?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
On Feb 21, 9:21 am, Matt Mencel <m...@techminer.net> wrote:> The only examples I''ve seen of timeout have it outside the setcode block. > Can anyone confirm it''s behavior depending on whether it''s inside or > outside setcode? The docs for it are pretty thin.I can tell you this for sure: 1) the contents of the outer block are executed at the time the fact is installed into Facter, but the contents of the setcode block are not executed until later 2) the contents of the setcode block are executed in a scope that has the same access to the (same) timeout variable that contents of the outer block has On the other hand, I think I do have to retract my original assertion (my apologies). It looks like this variable is specific to each resolution of each fact, and moreover that it should appear outside the setcode block to be effective. As far as I can tell from reading the source code, it makes sense to put these things (and only these) into the outer block of a custom fact definition: 1) invocations of the confine method (optional) 2) setting the value of the timeout variable (optional) 3) invoking the setcode method (required) Timeouts are implemented, by the way, by passing the value of the timeout variable as the argument to Ruby''s Timeout.timeout() to control the execution of the setcode block (when it is executed). The default timeout is 0, which I guess lets the block run forever if it is inclined to do so. John -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
Awesome....thank you! On Tue, Feb 21, 2012 at 10:48 AM, jcbollinger <John.Bollinger@stjude.org>wrote:> > > On Feb 21, 9:21 am, Matt Mencel <m...@techminer.net> wrote: > > > The only examples I''ve seen of timeout have it outside the setcode block. > > Can anyone confirm it''s behavior depending on whether it''s inside or > > outside setcode? The docs for it are pretty thin. > > > I can tell you this for sure: > > 1) the contents of the outer block are executed at the time the fact > is installed into Facter, but the contents of the setcode block are > not executed until later > > 2) the contents of the setcode block are executed in a scope that has > the same access to the (same) timeout variable that contents of the > outer block has > > On the other hand, I think I do have to retract my original assertion > (my apologies). It looks like this variable is specific to each > resolution of each fact, and moreover that it should appear outside > the setcode block to be effective. > > As far as I can tell from reading the source code, it makes sense to > put these things (and only these) into the outer block of a custom > fact definition: > > 1) invocations of the confine method (optional) > 2) setting the value of the timeout variable (optional) > 3) invoking the setcode method (required) > > Timeouts are implemented, by the way, by passing the value of the > timeout variable as the argument to Ruby''s Timeout.timeout() to > control the execution of the setcode block (when it is executed). The > default timeout is 0, which I guess lets the block run forever if it > is inclined to do so. > > > John > > -- > You received this message because you are subscribed to the Google Groups > "Puppet Users" group. > To post to this group, send email to puppet-users@googlegroups.com. > To unsubscribe from this group, send email to > puppet-users+unsubscribe@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/puppet-users?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
On Tue, Feb 21, 2012 at 6:57 AM, jcbollinger <John.Bollinger@stjude.org> wrote:> > > On Feb 20, 7:14 pm, Nathan Powell <nat...@nathanpowell.org> wrote: >> On Mon, Feb 20, 2012 at 5:33 PM, Matt Mencel <m...@techminer.net> wrote: >> > I am trying to write a custom facter for CentOS/RHEL that tells me how many >> > updates are found on a run of ''yum check-update'' >> >> Sorry I replied hastily. After replying I thought about what you''re >> trying to do. You don''t need all those gyrations to get this data >> >> Tested quickly on RHEL6 and CentOS5: >> >> `sudo yum check-update -q`.split(/\n/).reject{|i| i == ""}.length > > > Alternatively, > > `yum -q check-update | grep ''\\w'' | wc --lines`.chompOne quick comment, in Puppet/Facter, please don''t use %x[] or `yum ...` to execute command, instead: Facter::Util::Resolution.exec(''yum -q check-update | grep ''\\w'' | wc --lines'') Thanks, Nan -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.