summaryrefslogtreecommitdiff
path: root/lib/chef/provider/service/init.rb
blob: 63ba8fa6ab83f5dc2a3eae810383127d90e0fdd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#
# Author:: AJ Christensen (<aj@hjksolutions.com>)
# Copyright:: Copyright (c) 2008 Opscode, Inc.
# 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 'chef/mixin/shell_out'
require 'chef/provider/service'
require 'chef/provider/service/simple'
require 'chef/mixin/command'

class Chef
  class Provider
    class Service
      class Init < Chef::Provider::Service::Simple

        include Chef::Mixin::ShellOut

        def initialize(new_resource, run_context)
          super
          @init_command = "/etc/init.d/#{@new_resource.service_name}"
        end

        def define_resource_requirements
          # do not call super here, inherit only shared_requirements
          shared_resource_requirements
          requirements.assert(:start, :stop, :restart, :reload) do |a|
            a.assertion do
              custom_command_for_action?(action) || ::File.exist?(default_init_command)
            end
            a.failure_message(Chef::Exceptions::Service, "#{default_init_command} does not exist!")
            a.whyrun("Init script '#{default_init_command}' doesn't exist, assuming a prior action would have created it.") do
              # blindly assume that the service exists but is stopped in why run mode:
              @status_load_success = false
            end
          end
        end

        def start_service
          if @new_resource.start_command
            super
          else
            shell_out!("#{default_init_command} start")
          end
        end

        def stop_service
          if @new_resource.stop_command
            super
          else
            shell_out!("#{default_init_command} stop")
          end
        end

        def restart_service
          if @new_resource.restart_command
            super
          elsif @new_resource.supports[:restart]
            shell_out!("#{default_init_command} restart")
          else
            stop_service
            sleep 1
            start_service
          end
        end

        def reload_service
          if @new_resource.reload_command
            super
          elsif @new_resource.supports[:reload]
            shell_out!("#{default_init_command} reload")
          end
        end
      end
    end
  end
end