summaryrefslogtreecommitdiff
path: root/lib/chef/provider/service/arch.rb
blob: 8c8216c37f026d7fa0003300ba8da4069617830a (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
#
# Author:: Jan Zimmek (<jan.zimmek@web.de>)
# Copyright:: Copyright (c) 2010 Jan Zimmek
# 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/init'
require 'chef/mixin/command'

class Chef::Provider::Service::Arch < Chef::Provider::Service::Init

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

  def load_current_resource
    raise Chef::Exceptions::Service, "Could not find /etc/rc.conf"  unless ::File.exists?("/etc/rc.conf")
    raise Chef::Exceptions::Service, "No DAEMONS found in /etc/rc.conf"  unless ::File.read("/etc/rc.conf").match(/DAEMONS=\((.*)\)/m)
    super

    @current_resource.enabled(daemons.include?(@current_resource.service_name))
    @current_resource
  end

  # Get list of all daemons from the file '/etc/rc.conf'.
  # Mutiple lines and background form are supported. Example:
  #   DAEMONS=(\
  #     foobar \
  #     @example \
  #     !net \
  #   )
  def daemons
    entries = []
    if ::File.read("/etc/rc.conf").match(/DAEMONS=\((.*)\)/m)
      entries += $1.gsub(/\\?[\r\n]/, ' ').gsub(/# *[^ ]+/,' ').split(' ') if $1.length > 0
    end

    yield(entries) if block_given?

    entries
  end

  # FIXME: Multiple entries of DAEMONS will cause very bad results :)
  def update_daemons(entries)
    content = ::File.read("/etc/rc.conf").gsub(/DAEMONS=\((.*)\)/m, "DAEMONS=(#{entries.join(' ')})")
    ::File.open("/etc/rc.conf", "w") do |f|
      f.write(content)
    end
  end

  def enable_service()
    new_daemons = []
    entries = daemons

    if entries.include?(new_resource.service_name) or entries.include?("@#{new_resource.service_name}")
      # exists and already enabled (or already enabled as a background service)
      # new_daemons += entries
    else
      if entries.include?("!#{new_resource.service_name}")
        # exists but disabled
        entries.each do |daemon|
          if daemon == "!#{new_resource.service_name}"
            new_daemons << new_resource.service_name
          else
            new_daemons << daemon
          end
        end
      else
        # does not exist
        new_daemons += entries
        new_daemons << new_resource.service_name
      end
      update_daemons(new_daemons)
    end
  end

  def disable_service()
    new_daemons = []
    entries = daemons

    if entries.include?("!#{new_resource.service_name}")
      # exists and disabled
      # new_daemons += entries
    else
      if entries.include?(new_resource.service_name) or entries.include?("@#{new_resource.service_name}")
        # exists but enabled (or enabled as a back-ground service)
        # FIXME: Does arch support !@foobar ?
        entries.each do |daemon|
          if [new_resource.service_name, "@#{new_resource.service_name}"].include?(daemon)
            new_daemons << "!#{new_resource.service_name}"
          else
            new_daemons << daemon
          end
        end
      end
      update_daemons(new_daemons)
    end
  end

end