Last night I was thinking about writing scripts to manage services, packages etc in a heterogeneous environment. This is hard because the operating systems all behave differently.
This is of course a problem that Puppet solves with it’s providers system, after some chatting with Luke on IRC I now have a pretty nice solution.
Assuming you don’t mind shell scripting in Ruby here’s a pretty decent bit of Ruby to manage a service on many different environments.
require 'puppet' service = ARGV.shift action = ARGV.shift ARGV.length > 0 ? hasstatus = true : hasstatus = false begin svc = Puppet::Type.type(:service).new(:name => service, :hasstatus => hasstatus).provider svc.send action puts("#{service} #{action} status: #{svc.status}") rescue Exception => e puts("Could not #{action} service #{service}: #{e}") end
# service.rb httpd stop true httpd stop status: stopped # service.rb httpd start true httpd start status: running
You’d probably want to put in support for the pattern parameter to keep certain broken Operating Systems happy, but this is pretty nice and platform independent.

Wow, that is actually pretty ingenious, would have never even thought of this.
Nice, elegant solution. I guess this is what you are using in mcollective?
Hanno,
Yeah the service and package agents for mcollective use this, it’s working great. The code above though is 0.25.x specific I think someone on the Puppet list mentioned how to make it work with 0.24.x
Hi,
I am new to Puppet.I want get client system details…Please help me to do..
Thanks
Babu
I get this:
Could not start service httpd: private method `new’ called for Puppet::Type::Service:Class
what’s wrong ?
As mentioned in the comments the code is only for 0.25.x.
See line 41 and 42 of this code for 0.24.x version.