summaryrefslogtreecommitdiff
path: root/lib/chef/provider/service/solaris.rb
blob: b302441a604ada64ec54766e00ff38f38750bc44 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#
# Author:: Toomas Pelberg (<toomasp@gmx.net>)
# Copyright:: Copyright 2010-2016, Chef Software 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/provider/service"
require "chef/resource/service"
require "chef/mixin/command"

class Chef
  class Provider
    class Service
      class Solaris < Chef::Provider::Service
        attr_reader :maintenance

        provides :service, os: "solaris2"

        def initialize(new_resource, run_context=nil)
          super
          @init_command   = "/usr/sbin/svcadm"
          @status_command = "/bin/svcs"
          @maintenace     = false
        end

        def load_current_resource
          @current_resource = Chef::Resource::Service.new(@new_resource.name)
          @current_resource.service_name(@new_resource.service_name)

          [@init_command, @status_command].each do |cmd|
            unless ::File.executable? cmd then
              raise Chef::Exceptions::Service, "#{cmd} not executable!"
            end
          end
          @status = service_status.enabled

          @current_resource
        end

        def enable_service
          shell_out!(default_init_command, "clear", @new_resource.service_name) if @maintenance
          shell_out!(default_init_command, "enable", "-s", @new_resource.service_name)
        end

        def disable_service
          shell_out!(default_init_command, "disable", "-s", @new_resource.service_name)
        end

        alias_method :stop_service, :disable_service
        alias_method :start_service, :enable_service

        def reload_service
          shell_out!(default_init_command, "refresh", @new_resource.service_name)
        end

        def restart_service
          ## svcadm restart doesn't supports sync(-s) option
          disable_service
          return enable_service
        end

        def service_status
          cmd = shell_out!(@status_command, "-l", @current_resource.service_name, :returns => [0, 1])
          # Example output
          # $ svcs -l rsyslog
          # fmri         svc:/application/rsyslog:default
          # name         rsyslog logging utility
          # enabled      true
          # state        online
          # next_state   none
          # state_time   April  2, 2015 04:25:19 PM EDT
          # logfile      /var/svc/log/application-rsyslog:default.log
          # restarter    svc:/system/svc/restarter:default
          # contract_id  1115271
          # dependency   require_all/error svc:/milestone/multi-user:default (online)
          # $

          # load output into hash
          status = {}
          cmd.stdout.each_line do |line|
            key, value = line.strip.split(/\s+/, 2)
            status[key] = value
          end

          # check service state
          @maintenance = false
          case status["state"]
          when "online"
            @current_resource.enabled(true)
            @current_resource.running(true)
          when "maintenance"
            @maintenance = true
          end

          unless @current_resource.enabled
            @current_resource.enabled(false)
            @current_resource.running(false)
          end
          @current_resource
        end

      end
    end
  end
end