diff options
author | AJ Christensen <aj@junglist.gen.nz> | 2008-09-24 19:58:52 +1200 |
---|---|---|
committer | AJ Christensen <aj@junglist.gen.nz> | 2008-10-01 09:34:27 +1300 |
commit | 58e06810b1e172192353d1d8218af5411280bfb7 (patch) | |
tree | 0a1c8534e707c86a5ee211f9e69c28a63314eeec /lib | |
parent | a43c47d226ca13fbd14114d14953b5ed95e894aa (diff) | |
download | chef-58e06810b1e172192353d1d8218af5411280bfb7.tar.gz |
All of my service resource work so far.
TODO: make it work
Diffstat (limited to 'lib')
-rw-r--r-- | lib/chef/provider/service.rb | 48 | ||||
-rw-r--r-- | lib/chef/provider/service/base.rb | 42 | ||||
-rw-r--r-- | lib/chef/provider/service/debian.rb | 37 | ||||
-rw-r--r-- | lib/chef/provider/service/init.rb | 42 | ||||
-rw-r--r-- | lib/chef/resource/service.rb | 7 |
5 files changed, 172 insertions, 4 deletions
diff --git a/lib/chef/provider/service.rb b/lib/chef/provider/service.rb index 9d1d009b17..79041727eb 100644 --- a/lib/chef/provider/service.rb +++ b/lib/chef/provider/service.rb @@ -24,6 +24,54 @@ class Chef include Chef::Mixin::Command + def initialize(node, new_resource) + super(node, new_resource) + @enabled = nil + end + + def action_enable + do_service = false + if @current_resource.enabled == nil + do_service = true + elsif @new_resource.enabled != nil + if @new_resource.enabled != @current_resource.enabled + do_service = true + end + end + + if do_package + status = enable_service(@new_resource.service_name) + if status + @new_resource.enabled = true + Chef::Log.info("Enabled service #{@new_resource} successfully") + end + end + end + + def action_disable + if @current_resource.enabled == true + disable_service(@new_resource.service_name) + @new_resource.enabled = false + Chef::Log.info("Disabled service #{@new_resource} succesfully") + end + end + + def action_start + if @current_resource.running == false + start_service(@new_resource.service_name) + @new_resource.running = true + Chef::Log.info("Started service #{@new_resource} succesfully") + end + end + + def action_stop + if @current_resource.running == true + start_service(@new_resource.service_name) + @new_resource.running = false + Chef::Log.info("Stopped service #{@new_resource} succesfully") + end + end + end end end diff --git a/lib/chef/provider/service/base.rb b/lib/chef/provider/service/base.rb new file mode 100644 index 0000000000..d194c33fc7 --- /dev/null +++ b/lib/chef/provider/service/base.rb @@ -0,0 +1,42 @@ +# +# Author:: AJ Christensen (<aj@hjksolutions.com>) +# Copyright:: Copyright (c) 2008 HJK Solutions, LLC +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require File.join(File.dirname(__FILE__), "..", "service") +require File.join(File.dirname(__FILE__), "..", "..", "mixin", "command") + +class Chef + class Provider + class Service + class Base < Chef::Provider::Service + + def load_current_resource + @current_resource + end + + def start_service + run_command(:command => "/etc/init.d/#{name} start") + end + + def stop_service + run_command(:command => "/etc/init.d/#{name} stop") + end + + end + end + end +end diff --git a/lib/chef/provider/service/debian.rb b/lib/chef/provider/service/debian.rb index 81faaa43f0..aae1631543 100644 --- a/lib/chef/provider/service/debian.rb +++ b/lib/chef/provider/service/debian.rb @@ -21,10 +21,39 @@ require File.join(File.dirname(__FILE__), "..", "..", "mixin", "command") class Chef class Provider - class Debian < Chef::Provider::Service - - include Chef::Mixin::Command - + class Service + class Debian < Chef::Provider::Service::Init + + def load_current_resource + @current_resource = Chef::Resource::Service.new(@new_resource.name) + @current_resource.service_name(@new_resource.service_name) + + status = popen4("update-rc.d -n -f #{@new_resource.service_name}") do |pid, stdin, stdout, stderr| + stdin.close + if stdout.gets(nil) =~ /etc\/rc[\dS].d\/S|not installed/ + @current_resource.enabled(true) + else + @current_resource.enabled(false) + end + end + + unless status.exitstatus = 0 + raise Chef::Exception::Service, "update-rc.d -n -f #{@new_resource.service_name} failed - #{status.inspect}" + end + + @current_resource + end + + def enable_service(name) + run_command(:command => "update-rc.d #{name} defaults") + end + + def disable_service + run_command(:command => "update-rc.d -f #{name} remove") + run_command(:command => "update-rc.d #{name} stop 00 1 2 3 4 5 6 .") + end + end + end end end diff --git a/lib/chef/provider/service/init.rb b/lib/chef/provider/service/init.rb new file mode 100644 index 0000000000..aa673e15e8 --- /dev/null +++ b/lib/chef/provider/service/init.rb @@ -0,0 +1,42 @@ +# +# Author:: AJ Christensen (<aj@hjksolutions.com>) +# Copyright:: Copyright (c) 2008 HJK Solutions, LLC +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require File.join(File.dirname(__FILE__), "..", "service") +require File.join(File.dirname(__FILE__), "..", "..", "mixin", "command") + +class Chef + class Provider + class Service + class Init < Chef::Provider::Service::Base + + def load_current_resource + @current_resource + end + + def start_service + run_command(:command => "/etc/init.d/#{name} start") + end + + def stop_service + run_command(:command => "/etc/init.d/#{name} stop") + end + + end + end + end +end diff --git a/lib/chef/resource/service.rb b/lib/chef/resource/service.rb index 601ef2c5f3..a94b1f4fb5 100644 --- a/lib/chef/resource/service.rb +++ b/lib/chef/resource/service.rb @@ -24,6 +24,13 @@ class Chef super(name, collection, node) @resource_name = :service @service_name = name + @enabled = nil + @hasstatus = nil + @hasrestart = nil + @pattern = nil + @start = nil + @stop = nil + @status = nil @action = "enable" @allowed_actions.push(:enable, :disable, :start, :stop) end |