diff options
author | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2006-02-24 20:01:01 +0000 |
---|---|---|
committer | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2006-02-24 20:01:01 +0000 |
commit | 2dffbee3965c01979880ae3eb99162607236337b (patch) | |
tree | ab0f9997b31c9ecb99a0a07581ecb78594484af2 | |
parent | 7e5cc76cf9afb4b18a412e6971ea5a4f4a44935e (diff) | |
download | puppet-2dffbee3965c01979880ae3eb99162607236337b.tar.gz puppet-2dffbee3965c01979880ae3eb99162607236337b.tar.xz puppet-2dffbee3965c01979880ae3eb99162607236337b.zip |
Adding redhat service type, to support enabling and disabling a service
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@938 980ebf18-57e1-0310-9a29-db15c13687c0
-rw-r--r-- | lib/puppet/type/service.rb | 164 | ||||
-rwxr-xr-x | lib/puppet/type/service/debian.rb | 16 | ||||
-rwxr-xr-x | lib/puppet/type/service/redhat.rb | 35 | ||||
-rwxr-xr-x | lib/puppet/type/service/smf.rb | 11 | ||||
-rw-r--r-- | test/types/service.rb | 85 |
5 files changed, 229 insertions, 82 deletions
diff --git a/lib/puppet/type/service.rb b/lib/puppet/type/service.rb index 631db4a1f..5cf0f9fc4 100644 --- a/lib/puppet/type/service.rb +++ b/lib/puppet/type/service.rb @@ -19,67 +19,105 @@ module Puppet attr_reader :stat -# newstate(:enabled) do -# desc "Whether a service should be enabled to start at boot. -# **true**/*false*/*runlevel*" -# -# def retrieve -# unless @parent.respond_to?(:enabled?) -# raise Puppet::Error, "Service %s does not support enabling" -# end -# @is = @parent.enabled? -# end -# -# munge do |should| -# @runlevel = nil -# case should -# when true: return :enabled -# when false: return :disabled -# when /^\d+$/: -# @runlevel = should -# return :enabled -# else -# raise Puppet::Error, "Invalid 'enabled' value %s" % should -# end -# end -# -# def sync -# case self.should -# when :enabled -# unless @parent.respond_to?(:enable) -# raise Puppet::Error, "Service %s does not support enabling" -# end -# @parent.enable(@runlevel) -# return :service_enabled -# when :disabled -# unless @parent.respond_to?(:disable) -# raise Puppet::Error, -# "Service %s does not support disabling" -# end -# @parent.disable -# return :service_disabled -# end -# end -# end + newstate(:enable) do + attr_reader :runlevel + desc "Whether a service should be enabled to start at boot. + This state behaves quite differently depending on the platform; + wherever possible, it relies on local tools to enable or disable + a given service. *true*/*false*/*runlevels*" + + newvalue(:true) do + unless @parent.respond_to?(:enable) + raise Puppet::Error, "Service %s does not support enabling" + end + @parent.enable + end - # Handle whether the service should actually be running right now. - newstate(:running) do - desc "Whether a service should be running. **true**/*false*" + newvalue(:false) do + unless @parent.respond_to?(:disable) + raise Puppet::Error, "Service %s does not support enabling" + end + @parent.disable + end + + def retrieve + unless @parent.respond_to?(:enabled?) + raise Puppet::Error, "Service %s does not support enabling" + end + @is = @parent.enabled? + end + + validate do |value| + unless value =~ /^\d+/ + super(value) + end + end munge do |should| - case should - when false,0,"0", "stopped", :stopped: - should = :stopped - when true,1,"1", :running, "running": - should = :running + @runlevel = nil + if should =~ /^\d+$/ + arity = @parent.method(:enable) + if @runlevel and arity != 1 + raise Puppet::Error, + "Services on %s do not accept runlevel specs" % + @parent.type + elsif arity != 0 + raise Puppet::Error, + "Services on %s must specify runlevels" % + @parent.type + end + @runlevel = should + return :true else - self.warning "%s: interpreting '%s' as false" % - [self.class,should.inspect] - should = 0 + super(should) end - return should end + def sync + case self.should + when :true + if @runlevel + @parent.enable(@runlevel) + else + @parent.enable() + end + return :service_enabled + when :false + @parent.disable + return :service_disabled + end + end + end + + # Handle whether the service should actually be running right now. + newstate(:ensure) do + desc "Whether a service should be running. **true**/*false*" + + newvalue(:stopped) do + @parent.stop + end + + newvalue(:running) do + @parent.start + end + + aliasvalue(:false, :stopped) + aliasvalue(:true, :running) + +# munge do |should| +# case should +# when false,0,"0", "stopped", :stopped: +# should = :stopped +# when true,1,"1", :running, "running": +# should = :running +# else +# self.warning "%s: interpreting '%s' as false" % +# [self.class,should.inspect] +# should = 0 +# end +# return should +# end + def retrieve self.is = @parent.status self.debug "Running value is '%s'" % self.is @@ -103,9 +141,22 @@ module Puppet end end + # Produce a warning, rather than just failing. + newparam(:running) do + desc "A place-holder parameter that wraps ``ensure``, because + ``running`` is deprecated. You should use ``ensure`` instead + of this, but using this will still work, albeit with a + warning." + + def should=(values) + @parent.warning "'running' is deprecated; please use 'ensure'" + @parent[:ensure] = values + end + end + newparam(:type) do desc "The service type. For most platforms, it does not make - sense to change set this parameter, as the default is based on + sense to set this parameter, as the default is based on the builtin service facilities. The service types available are: * ``base``: You must specify everything. @@ -286,6 +337,8 @@ module Puppet else @defsvctype = self.svctype(:smf) end + when "CentOS", "RedHat", "Fedora": + @defsvctype = self.svctype(:redhat) else if Facter["kernel"] == "Linux" Puppet.notice "Using service type %s for %s" % @@ -438,6 +491,7 @@ end require 'puppet/type/service/base' require 'puppet/type/service/init' require 'puppet/type/service/debian' +require 'puppet/type/service/redhat' require 'puppet/type/service/smf' # $Id$ diff --git a/lib/puppet/type/service/debian.rb b/lib/puppet/type/service/debian.rb index 7e6c6d35d..630a34b12 100755 --- a/lib/puppet/type/service/debian.rb +++ b/lib/puppet/type/service/debian.rb @@ -5,7 +5,7 @@ require 'puppet/type/service/init' Puppet.type(:service).newsvctype(:debian, :init) do # Remove the symlinks def disable - output = %x{update-rc.d -f #{self[:name]} remove 2>/dev/null} + output = %x{update-rc.d -f #{self[:name]} remove 2>&1} unless $? == 0 raise Puppet::Error, "Could not disable %s: %s" % @@ -14,7 +14,7 @@ Puppet.type(:service).newsvctype(:debian, :init) do end def enabled? - output = %x{update-rc.d -n -f #{self[:name]} remove 2>/dev/null} + output = %x{update-rc.d -n -f #{self[:name]} remove 2>&1} unless $? == 0 raise Puppet::Error, "Could not check %s: %s" % [self.name, output] @@ -23,21 +23,17 @@ Puppet.type(:service).newsvctype(:debian, :init) do # If it's enabled, then it will print output showing removal of # links. if output =~ /etc\/rc\d.d/ - return true + return :true else - return false + return :false end end def enable(runlevel) - if runlevel - raise Puppet::Error, "Specification of runlevels is not supported" - else - output = %x{update-rc.d #{self[:name]} defaults 2>/dev/null} - end + output = %x{update-rc.d #{self[:name]} defaults 2>&1} unless $? == 0 - raise Puppet::Error, "Could not check %s: %s" % + raise Puppet::Error, "Could not enable %s: %s" % [self.name, output] end end diff --git a/lib/puppet/type/service/redhat.rb b/lib/puppet/type/service/redhat.rb new file mode 100755 index 000000000..2674f5a21 --- /dev/null +++ b/lib/puppet/type/service/redhat.rb @@ -0,0 +1,35 @@ +require 'puppet/type/service/init' + +# Manage debian services. Start/stop is the same as InitSvc, but enable/disable +# is special. +Puppet.type(:service).newsvctype(:redhat, :init) do + # Remove the symlinks + def disable + output = %x{chkconfig #{self[:name]} off 2>&1} + + unless $? == 0 + raise Puppet::Error, "Could not disable %s: %s" % + [self.name, output] + end + end + + def enabled? + output = %x{chkconfig #{self[:name]} 2>&1}.chomp + if $? == 0 + return :true + else + return :false + end + end + + # Don't support them specifying runlevels; always use the runlevels + # in the init scripts. + def enable + output = %x{chkconfig #{self[:name]} on 2>&1} + + unless $? == 0 + raise Puppet::Error, "Could not enable %s: %s" % + [self.name, output] + end + end +end diff --git a/lib/puppet/type/service/smf.rb b/lib/puppet/type/service/smf.rb index 5f849a16a..71d7d3a46 100755 --- a/lib/puppet/type/service/smf.rb +++ b/lib/puppet/type/service/smf.rb @@ -1,6 +1,13 @@ -# Solaris 10 SMF-style services. This is not yet implemented, which is probably -# somewhat obvious. +# Solaris 10 SMF-style services. Puppet.type(:service).newsvctype(:smf) do + def enable + "svcadm enable %s" % self[:name] + end + + def dsiable + "svcadm disable %s" % self[:name] + end + def restartcmd "svcadm restart %s" % self[:name] end diff --git a/test/types/service.rb b/test/types/service.rb index 9078a3e59..a9fd69cfc 100644 --- a/test/types/service.rb +++ b/test/types/service.rb @@ -34,8 +34,8 @@ class TestInitService end def tstsvcs - case Facter["operatingsystem"].value - when "Solaris": + case Facter["operatingsystem"].value.downcase + when "solaris": return ["smtp", "xf"] end end @@ -43,7 +43,7 @@ class TestInitService def mksleeper(hash = {}) hash[:name] = "sleeper" hash[:path] = File.join($puppetbase,"examples/root/etc/init.d") - hash[:running] = true + hash[:ensure] = true hash[:hasstatus] = true #hash[:type] = "init" assert_nothing_raised() { @@ -74,7 +74,7 @@ class TestInitService # now stop it assert_nothing_raised() { - sleeper[:running] = 0 + sleeper[:ensure] = 0 } assert_nothing_raised() { sleeper.retrieve @@ -116,21 +116,24 @@ class TestLocalService < Test::Unit::TestCase end def mktestsvcs - tstsvcs.collect { |svc| - Puppet.type(:service).create( - :name => svc, - :check => [:running] - ) + tstsvcs.collect { |svc,svcargs| + args = svcargs.dup + args[:name] = svc + Puppet.type(:service).create(args) } end def tstsvcs - case Facter["operatingsystem"].value - when "Solaris": + case Facter["operatingsystem"].value.downcase + when "solaris": case Facter["operatingsystemrelease"].value when "5.10": - return ["smtp", "xfs"] + return {"smtp" => {}, "xfs" => {}} end + when "debian": + return {"hddtemp" => {}} + when "centos": + return {"cups" => {:hasstatus => true}} end Puppet.notice "No test services for %s-%s" % @@ -145,7 +148,7 @@ class TestLocalService < Test::Unit::TestCase } comp = newcomp("servicetst", service) - service[:running] = true + service[:ensure] = true Puppet.info "Starting %s" % service.name assert_apply(service) @@ -167,7 +170,7 @@ class TestLocalService < Test::Unit::TestCase # now stop it assert_nothing_raised() { - service[:running] = 0 + service[:ensure] = :stopped } assert_nothing_raised() { service.retrieve @@ -182,6 +185,42 @@ class TestLocalService < Test::Unit::TestCase assert(service.insync?, "Service %s has not stopped" % service.name) end + def cycleenable(service) + assert_nothing_raised() { + service.retrieve + } + + comp = newcomp("servicetst", service) + service[:enable] = true + + Puppet.info "Enabling %s" % service.name + assert_apply(service) + + # Some package systems background the work, so we need to give them + # time to do their work. + sleep(1.5) + assert_nothing_raised() { + service.retrieve + } + assert(service.insync?, "Service %s is not enabled" % service.name) + + # now stop it + assert_nothing_raised() { + service[:enable] = false + } + assert_nothing_raised() { + service.retrieve + } + assert(!service.insync?(), "Service %s is not enabled" % service.name) + Puppet.info "disabling %s" % service.name + assert_events([:service_disabled], comp) + sleep(1.5) + assert_nothing_raised() { + service.retrieve + } + assert(service.insync?, "Service %s has not been disabled" % service.name) + end + def test_status mktestsvcs.each { |svc| val = nil @@ -197,13 +236,29 @@ class TestLocalService < Test::Unit::TestCase else def test_servicestartstop mktestsvcs.each { |svc| + svc[:check] = :enable startstate = nil assert_nothing_raised("Could not get status") { startstate = svc.status } cycleservice(svc) - svc[:running] = startstate + svc[:ensure] = startstate + assert_apply(svc) + Puppet.type(:service).clear + Puppet.type(:component).clear + } + end + + def test_serviceenabledisable + mktestsvcs.each { |svc| + startstate = nil + assert_nothing_raised("Could not get status") { + startstate = svc.enabled? + } + cycleenable(svc) + + svc[:enable] = startstate assert_apply(svc) Puppet.type(:service).clear Puppet.type(:component).clear |