How to use 'commands' method in ruby code outside of puppet?

22 views
Skip to first unread message

Jakov Sosic

unread,
Sep 12, 2014, 2:11:47 PM9/12/14
to puppe...@googlegroups.com
Hi,

I asked on puppet-users but I think I made a mistake, this seems to me
more like a question for puppet-dev. Sorry if it's not :(


So, to allow me to easier debug my providers, I sometimes wanna use
'command' in my own ruby code.

Trying to figure out how is problem.

This is how I use it inside a provider:

commands :wget => '/usr/bin/wget',
:cp => '/bin/cp'

Later, I can run the command with simple:

wget('http://example.com')



Now, I tried doing this:

something = Puppet::Provider.commands({:wget => '/usr/bin/wget'})
something.wget('http://example.com')

but, I get this:


test.rb:15: undefined method `wget' for {:wget=>"/usr/bin/wget"}:Hash
(NoMethodError)


I also tried running:

wget = Puppet::Provider::Command.new('wget', '/usr/bin/wget',
Puppet::Util, Puppet::Util::Execution)
puts Puppet::Util::Execution.execute(['wget','http://example.com'],{})

but the output is blank...


Any ideas?

Andy Parker

unread,
Sep 15, 2014, 7:36:57 PM9/15/14
to puppe...@googlegroups.com
The generated command method is an instance method on the provider. You can instantiate the provider and then call the method:

1.9.1 :005 > require 'puppet'
 => false
1.9.1 :006 > gem = Puppet::Type.type(:package).provider(:gem).new(nil)
 => (provider=gem)
1.9.1 :007 > gem.gemcmd("list")
 => "activemodel (3.2.19)\nactiverecord (3.2.19)\nactivesupport (3.2.19)\narel (3.0.3)\nbuilder (3.0.4)\nbundler (1.5.2)\nCFPropertyList (2.2.8)\ncoderay (1.1.0)\ncolumnize (0.8.9)\ncouchrest (1.2.0)\ndebugger (1.6.8)\ndebugger-linecache (1.2.0)\ndebugger-pry (0.1.1)\ndebugger-ruby_core_source (1.3.5)\ndiff-lcs (1.2.5)\ndocile (1.1.5)\nfacter (2.1.0 universal-darwin)\nhiera (1.3.4)\nhpricot (0.8.6)\ni18n (0.6.11)\njira-ruby (0.1.10)\njson-schema (2.1.1)\njson_pure (1.8.1)\nmetaclass (0.0.4)\nmethod_source (0.8.2)\nmime-types (1.25.1)\nmocha (0.10.5)\nmsgpack (0.5.8)\nmulti_json (1.7.7)\nmustache (0.99.6)\nnet-ssh (2.9.1)\noauth (0.4.7)\npry (0.10.0)\npuppet (3.7.1)\npuppet-lint (0.3.2)\npuppet-syntax (1.2.2)\npuppetlabs_spec_helper (0.8.0)\nracc (1.4.9)\nrack (1.5.2)\nrake (10.1.1)\nrdiscount (2.1.7.1)\nredcarpet (2.3.0)\nrest-client (1.6.7)\nronn (0.7.3)\nrspec (2.14.1)\nrspec-core (2.14.8)\nrspec-expectations (2.14.5)\nrspec-mocks (2.14.6)\nrspec-puppet (1.0.1)\nruby-prof (0.15.1)\nsimplecov (0.9.0)\nsimplecov-html (0.8.0)\nslop (3.6.0)\nsqlite3 (1.3.9)\nstomp (1.3.2)\ntzinfo (0.3.40)\nyard (0.8.7.4)\nyarjuf (1.0.6)\n"
1.9.1 :008 >
 
If you want to try to use the Puppet::Provider::Command class directly you do:

1.9.1 :011 >   cmd = Puppet::Provider::Command.new('ls', '/bin/ls', Puppet::Util, Puppet::Util::Execution)
 => #<Puppet::Provider::Command:0x007fd22d80abe0 @name="ls", @executable="/bin/ls", @resolver=Puppet::Util, @executor=Puppet::Util::Execution, @options={}>
1.9.1 :012 > cmd.execute()
 => "COMMITTERS.md\nCONTRIBUTING.md\nGemfile\nGemfile.2.7\nGemfile.local\nGemfile.lock\nLICENSE\nREADME.md\nRakefile\nacceptance\napi\nautotest\navatars\nbenchmarks\nbin\nbuilds.puppetlabs.lan\ncallgrind.defined_types4.1407092813.trace\nconf\ndefaults.pp\ndefined_types4.samples\ndemo\ndoc\ndocs\nerr.pp\nevaluations.samples\nexamples\next\ngravatars.pl\ninitial.prof\ninstall.rb\nlib\nman\npkg\npuppet.gemspec\npuppet.gemspec.2.7\nresource_expressions\nspec\nspec_order.txt\nt.pp\ntags\ntasks\ntesting\ntesting.file\nundef.pp\nutil\nyardoc\n"
1.9.1 :013 >

Notice that I called the #execute method on the command object.


Any ideas?

--
You received this message because you are subscribed to the Google Groups "Puppet Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-dev+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-dev/54133758.8060001%40gmail.com.

For more options, visit https://groups.google.com/d/optout.



--
Andrew Parker
Freenode: zaphod42
Twitter: @aparker42
Software Developer

Join us at PuppetConf 2014, September 20-24 in San Francisco - www.puppetconf.com 
Reply all
Reply to author
Forward
0 new messages