diff --git a/lib/puppet/provider/service/systemd.rb b/lib/puppet/provider/service/systemd.rb new file mode 100755 index 000000000..b6b3b386f --- /dev/null +++ b/lib/puppet/provider/service/systemd.rb @@ -0,0 +1,64 @@ +# Manage systemd services using /bin/systemctl + +Puppet::Type.type(:service).provide :systemd, :parent => :base do + desc "Manage systemd services using /bin/systemctl" + + commands :systemctl => "/bin/systemctl" + + #defaultfor :operatingsystem => [:redhat, :fedora, :suse, :centos, :sles, :oel, :ovm] + + def self.instances + i = [] + output = `systemctl list-units --full --all --no-pager` + output.scan(/^(\S+)\s+(loaded|error)\s+(active|inactive)\s+(active|waiting|running|plugged|mounted|dead|exited|listening|elapsed)\s*?(\S.*?)?$/i).each do |m| + i << m[0] + end + return i + rescue Puppet::ExecutionFailure + return [] + end + + def disable + output = systemctl(:disable, @resource[:name]) + rescue Puppet::ExecutionFailure + raise Puppet::Error, "Could not disable #{self.name}: #{output}" + end + + def enabled? + begin + systemctl("is-enabled", @resource[:name]) + rescue Puppet::ExecutionFailure + return :false + end + + :true + end + + def status + begin + output = systemctl("is-active", @resource[:name]) + rescue Puppet::ExecutionFailure + return :stopped + end + return :running + end + + def enable + output = systemctl("enable", @resource[:name]) + rescue Puppet::ExecutionFailure + raise Puppet::Error, "Could not enable #{self.name}: #{output}" + end + + def restartcmd + [command(:systemctl), "restart", @resource[:name]] + end + + def startcmd + [command(:systemctl), "start", @resource[:name]] + end + + def stopcmd + [command(:systemctl), "stop", @resource[:name]] + end +end + diff --git a/spec/unit/provider/service/systemd_spec.rb b/spec/unit/provider/service/systemd_spec.rb new file mode 100755 index 000000000..98b6855a6 --- /dev/null +++ b/spec/unit/provider/service/systemd_spec.rb @@ -0,0 +1,25 @@ +#!/usr/bin/env rspec +# +# Unit testing for the RedHat service Provider +# +require 'spec_helper' + +provider_class = Puppet::Type.type(:service).provider(:systemd) + +describe provider_class do + before :each do + @class = Puppet::Type.type(:service).provider(:redhat) + @resource = stub 'resource' + @resource.stubs(:[]).returns(nil) + @resource.stubs(:[]).with(:name).returns "myservice.service" + @provider = provider_class.new + @resource.stubs(:provider).returns @provider + @provider.resource = @resource + end + + [:enabled?, :enable, :disable, :start, :stop, :status, :restart].each do |method| + it "should have a #{method} method" do + @provider.should respond_to(method) + end + end +end