summaryrefslogtreecommitdiffstats
path: root/lib/puppet/provider/service
diff options
context:
space:
mode:
authorMarkus Roberts <Markus@reality.com>2010-07-09 18:06:37 -0700
committerMarkus Roberts <Markus@reality.com>2010-07-09 18:06:37 -0700
commit07b15bf6fa2a2183f73fcb9b6740c7df75c8b47b (patch)
treef4f20be112f0c8832cb2e9681935a04bb0d197cf /lib/puppet/provider/service
parent8d1fbe4586c91682cdda0cb271649e918fd9778b (diff)
downloadpuppet-07b15bf6fa2a2183f73fcb9b6740c7df75c8b47b.tar.gz
puppet-07b15bf6fa2a2183f73fcb9b6740c7df75c8b47b.tar.xz
puppet-07b15bf6fa2a2183f73fcb9b6740c7df75c8b47b.zip
Code smell: Avoid unneeded blocks
Replaced 45 occurances of (DEF) begin (LINES) rescue(.*) (LINES) end end with 3 Examples: The code: def find(name) begin self.const_get(name.to_s.capitalize) rescue puts "Unable to find application '#{name.to_s}'." Kernel::exit(1) end end becomes: def find(name) self.const_get(name.to_s.capitalize) rescue puts "Unable to find application '#{name.to_s}'." Kernel::exit(1) end The code: def exit_on_fail(message, code = 1) begin yield rescue RuntimeError, NotImplementedError => detail puts detail.backtrace if Puppet[:trace] $stderr.puts "Could not #{message}: #{detail}" exit(code) end end becomes: def exit_on_fail(message, code = 1) yield rescue RuntimeError, NotImplementedError => detail puts detail.backtrace if Puppet[:trace] $stderr.puts "Could not #{message}: #{detail}" exit(code) end The code: def start begin case ssl when :tls @connection = LDAP::SSLConn.new(host, port, true) when true @connection = LDAP::SSLConn.new(host, port) else @connection = LDAP::Conn.new(host, port) end @connection.set_option(LDAP::LDAP_OPT_PROTOCOL_VERSION, 3) @connection.set_option(LDAP::LDAP_OPT_REFERRALS, LDAP::LDAP_OPT_ON) @connection.simple_bind(user, password) rescue => detail raise Puppet::Error, "Could not connect to LDAP: #{detail}" end end becomes: def start case ssl when :tls @connection = LDAP::SSLConn.new(host, port, true) when true @connection = LDAP::SSLConn.new(host, port) else @connection = LDAP::Conn.new(host, port) end @connection.set_option(LDAP::LDAP_OPT_PROTOCOL_VERSION, 3) @connection.set_option(LDAP::LDAP_OPT_REFERRALS, LDAP::LDAP_OPT_ON) @connection.simple_bind(user, password) rescue => detail raise Puppet::Error, "Could not connect to LDAP: #{detail}" end
Diffstat (limited to 'lib/puppet/provider/service')
-rw-r--r--lib/puppet/provider/service/daemontools.rb8
-rw-r--r--lib/puppet/provider/service/gentoo.rb8
-rwxr-xr-xlib/puppet/provider/service/redhat.rb8
-rwxr-xr-xlib/puppet/provider/service/smf.rb4
-rwxr-xr-xlib/puppet/provider/service/src.rb8
5 files changed, 9 insertions, 27 deletions
diff --git a/lib/puppet/provider/service/daemontools.rb b/lib/puppet/provider/service/daemontools.rb
index f5f2607e3..934e96aea 100644
--- a/lib/puppet/provider/service/daemontools.rb
+++ b/lib/puppet/provider/service/daemontools.rb
@@ -125,16 +125,14 @@ Puppet::Type.type(:service).provide :daemontools, :parent => :base do
end
def setupservice
- begin
if resource[:manifest]
Puppet.notice "Configuring #{resource[:name]}"
command = [ resource[:manifest], resource[:name] ]
#texecute("setupservice", command)
rv = system("#{command}")
end
- rescue Puppet::ExecutionFailure => detail
+ rescue Puppet::ExecutionFailure => detail
raise Puppet::Error.new( "Cannot config #{self.service} to enable it: #{detail}" )
- end
end
def enabled?
@@ -149,7 +147,6 @@ Puppet::Type.type(:service).provide :daemontools, :parent => :base do
end
def enable
- begin
if ! FileTest.directory?(self.daemon)
Puppet.notice "No daemon dir, calling setupservice for #{resource[:name]}"
self.setupservice
@@ -160,9 +157,8 @@ Puppet::Type.type(:service).provide :daemontools, :parent => :base do
File.symlink(self.daemon, self.service)
end
end
- rescue Puppet::ExecutionFailure => detail
+ rescue Puppet::ExecutionFailure => detail
raise Puppet::Error.new( "No daemon directory found for #{self.service}")
- end
end
def disable
diff --git a/lib/puppet/provider/service/gentoo.rb b/lib/puppet/provider/service/gentoo.rb
index a3d477751..109524bc0 100644
--- a/lib/puppet/provider/service/gentoo.rb
+++ b/lib/puppet/provider/service/gentoo.rb
@@ -18,11 +18,9 @@ Puppet::Type.type(:service).provide :gentoo, :parent => :init do
end
def disable
- begin
output = update :del, @resource[:name], :default
- rescue Puppet::ExecutionFailure
+ rescue Puppet::ExecutionFailure
raise Puppet::Error, "Could not disable #{self.name}: #{output}"
- end
end
def enabled?
@@ -45,11 +43,9 @@ Puppet::Type.type(:service).provide :gentoo, :parent => :init do
end
def enable
- begin
output = update :add, @resource[:name], :default
- rescue Puppet::ExecutionFailure
+ rescue Puppet::ExecutionFailure
raise Puppet::Error, "Could not enable #{self.name}: #{output}"
- end
end
end
diff --git a/lib/puppet/provider/service/redhat.rb b/lib/puppet/provider/service/redhat.rb
index b31faa586..3a25db3ac 100755
--- a/lib/puppet/provider/service/redhat.rb
+++ b/lib/puppet/provider/service/redhat.rb
@@ -22,11 +22,9 @@ Puppet::Type.type(:service).provide :redhat, :parent => :init, :source => :init
# Remove the symlinks
def disable
- begin
output = chkconfig(@resource[:name], :off)
- rescue Puppet::ExecutionFailure
+ rescue Puppet::ExecutionFailure
raise Puppet::Error, "Could not disable #{self.name}: #{output}"
- end
end
def enabled?
@@ -48,11 +46,9 @@ Puppet::Type.type(:service).provide :redhat, :parent => :init, :source => :init
# Don't support them specifying runlevels; always use the runlevels
# in the init scripts.
def enable
- begin
output = chkconfig(@resource[:name], :on)
- rescue Puppet::ExecutionFailure => detail
+ rescue Puppet::ExecutionFailure => detail
raise Puppet::Error, "Could not enable #{self.name}: #{detail}"
- end
end
def initscript
diff --git a/lib/puppet/provider/service/smf.rb b/lib/puppet/provider/service/smf.rb
index 0407c8479..ca7af9449 100755
--- a/lib/puppet/provider/service/smf.rb
+++ b/lib/puppet/provider/service/smf.rb
@@ -19,7 +19,6 @@ Puppet::Type.type(:service).provide :smf, :parent => :base do
commands :svccfg => "/usr/sbin/svccfg"
def setupservice
- begin
if resource[:manifest]
[command(:svcs), "-l", @resource[:name]]
if $CHILD_STATUS.exitstatus == 1
@@ -27,9 +26,8 @@ Puppet::Type.type(:service).provide :smf, :parent => :base do
svccfg :import, resource[:manifest]
end
end
- rescue Puppet::ExecutionFailure => detail
+ rescue Puppet::ExecutionFailure => detail
raise Puppet::Error.new( "Cannot config #{self.service} to enable it: #{detail}" )
- end
end
def enable
diff --git a/lib/puppet/provider/service/src.rb b/lib/puppet/provider/service/src.rb
index 135edcb65..aed88d531 100755
--- a/lib/puppet/provider/service/src.rb
+++ b/lib/puppet/provider/service/src.rb
@@ -31,7 +31,6 @@ Puppet::Type.type(:service).provide :src, :parent => :base do
end
def restart
- begin
execute([command(:lssrc), "-Ss", @resource[:name]]).each do |line|
args = line.split(":")
@@ -59,13 +58,11 @@ Puppet::Type.type(:service).provide :src, :parent => :base do
end
end
self.fail("No such service found")
- rescue Puppet::ExecutionFailure => detail
+ rescue Puppet::ExecutionFailure => detail
raise Puppet::Error.new("Cannot get status of #{@resource[:name]}, error was: #{detail}" )
- end
end
def status
- begin
execute([command(:lssrc), "-s", @resource[:name]]).each do |line|
args = line.split
@@ -82,9 +79,8 @@ Puppet::Type.type(:service).provide :src, :parent => :base do
return state
end
self.fail("No such service found")
- rescue Puppet::ExecutionFailure => detail
+ rescue Puppet::ExecutionFailure => detail
raise Puppet::Error.new("Cannot get status of #{@resource[:name]}, error was: #{detail}" )
- end
end
end