summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNick Lewis <nick@puppetlabs.com>2010-11-11 13:57:42 -0800
committerNick Lewis <nick@puppetlabs.com>2010-11-11 13:57:42 -0800
commit0d24ea3583c4cd6a4583f4788686a4f9e02cb994 (patch)
tree91e50b5e6132ff2c856b59f40c76bcae6c5b00a7
parent7c7808059a18832234742f0dc3af3f1a034e1033 (diff)
parentb0acb02b568598ec8dba41aa856f74658b2033dd (diff)
downloadpuppet-0d24ea3583c4cd6a4583f4788686a4f9e02cb994.tar.gz
puppet-0d24ea3583c4cd6a4583f4788686a4f9e02cb994.tar.xz
puppet-0d24ea3583c4cd6a4583f4788686a4f9e02cb994.zip
Merge branch 'feature/next/3747' into next
-rwxr-xr-xlib/puppet/provider/service/upstart.rb73
-rw-r--r--spec/unit/provider/service/upstart.rb50
2 files changed, 123 insertions, 0 deletions
diff --git a/lib/puppet/provider/service/upstart.rb b/lib/puppet/provider/service/upstart.rb
new file mode 100755
index 000000000..54971eeac
--- /dev/null
+++ b/lib/puppet/provider/service/upstart.rb
@@ -0,0 +1,73 @@
+Puppet::Type.type(:service).provide :upstart, :parent => :init do
+ desc "Ubuntu service manager upstart.
+
+ This provider manages upstart jobs which have replaced initd.
+
+ See:
+ * http://upstart.ubuntu.com/
+ "
+ # confine to :ubuntu for now because I haven't tested on other platforms
+ confine :operatingsystem => :ubuntu #[:ubuntu, :fedora, :debian]
+
+ commands :start => "/sbin/start",
+ :stop => "/sbin/stop",
+ :restart => "/sbin/restart",
+ :status_exec => "/sbin/status",
+ :initctl => "/sbin/initctl"
+
+ # upstart developer haven't implemented initctl enable/disable yet:
+ # http://www.linuxplanet.com/linuxplanet/tutorials/7033/2/
+ # has_feature :enableable
+
+ def self.instances
+ instances = []
+ execpipe("#{command(:initctl)} list") { |process|
+ process.each { |line|
+ # needs special handling of services such as network-interface:
+ # initctl list:
+ # network-interface (lo) start/running
+ # network-interface (eth0) start/running
+ # network-interface-security start/running
+ name = \
+ if matcher = line.match(/^(network-interface)\s\(([^\)]+)\)/)
+ "#{matcher[1]} INTERFACE=#{matcher[2]}"
+ else
+ line.split.first
+ end
+ instances << new(:name => name)
+ }
+ }
+ instances
+ end
+
+ def startcmd
+ [command(:start), @resource[:name]]
+ end
+
+ def stopcmd
+ [command(:stop), @resource[:name]]
+ end
+
+ def restartcmd
+ (@resource[:hasrestart] == :true) && [command(:restart), @resource[:name]]
+ end
+
+ def status
+ # allows user override of status command
+ if @resource[:status]
+ ucommand(:status, false)
+ if $?.exitstatus == 0
+ return :running
+ else
+ return :stopped
+ end
+ else
+ output = status_exec(@resource[:name].split)
+ if (! $?.nil?) && (output =~ /start\//)
+ return :running
+ else
+ return :stopped
+ end
+ end
+ end
+end
diff --git a/spec/unit/provider/service/upstart.rb b/spec/unit/provider/service/upstart.rb
new file mode 100644
index 000000000..439fd2c79
--- /dev/null
+++ b/spec/unit/provider/service/upstart.rb
@@ -0,0 +1,50 @@
+#!/usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../../../spec_helper'
+
+provider_class = Puppet::Type.type(:service).provider(:upstart)
+
+describe provider_class do
+ describe "#instances" do
+ it "should be able to find all instances" do
+ processes = ["rc stop/waiting", "ssh start/running, process 712"]
+ provider_class.stubs(:execpipe).yields(processes)
+ provider_class.instances.map {|provider| provider.name}.should =~ ["rc","ssh"]
+ end
+
+ it "should attach the interface name for network interfaces" do
+ processes = ["network-interface (eth0)"]
+ provider_class.stubs(:execpipe).yields(processes)
+ provider_class.instances.first.name.should == "network-interface INTERFACE=eth0"
+ end
+ end
+
+ describe "#status" do
+ it "should allow the user to override the status command" do
+ resource = Puppet::Type.type(:service).new(:name => "foo", :provider => :upstart, :status => "/bin/foo")
+ provider = provider_class.new(resource)
+
+ Process::Status.any_instance.stubs(:exitstatus).returns(0)
+ provider.expects(:ucommand)
+ provider.status.should == :running
+ end
+
+ it "should use the default status command if none is specified" do
+ resource = Puppet::Type.type(:service).new(:name => "foo", :provider => :upstart)
+ provider = provider_class.new(resource)
+
+ provider.expects(:status_exec).with(["foo"]).returns("foo start/running, process 1000")
+ Process::Status.any_instance.stubs(:exitstatus).returns(0)
+ provider.status.should == :running
+ end
+
+ it "should properly handle services with 'start' in their name" do
+ resource = Puppet::Type.type(:service).new(:name => "foostartbar", :provider => :upstart)
+ provider = provider_class.new(resource)
+
+ provider.expects(:status_exec).with(["foostartbar"]).returns("foostartbar stop/waiting")
+ Process::Status.any_instance.stubs(:exitstatus).returns(0)
+ provider.status.should == :stopped
+ end
+ end
+end