summaryrefslogtreecommitdiffstats
path: root/lib/puppet/provider/service/runit.rb
blob: b8b444e346ae016b27181b500165d4d45cf6fe1c (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
# Daemontools service management
#
# author Brice Figureau <brice-puppet@daysofwonder.com>
Puppet::Type.type(:service).provide :runit, :parent => :daemontools do
    desc "Runit service management.

  This provider manages daemons running supervised by Runit.
  It tries to detect the service directory, with by order of preference:

   * /service
   * /var/service
   * /etc/service

  The daemon directory should be placed in a directory that can be
  by default in:

   * /etc/sv

  or this can be overriden in the service resource parameters::

      service {
       \"myservice\":
         provider => \"runit\", path => \"/path/to/daemons\";
      }

  This provider supports out of the box:

   * start/stop
   * enable/disable
   * restart
   * status


"

    commands :sv => "/usr/bin/sv"

    class << self
        # this is necessary to autodetect a valid resource
        # default path, since there is no standard for such directory.
        def defpath(dummy_argument=:work_arround_for_ruby_GC_bug)
            unless defined?(@defpath) and @defpath
                ["/etc/sv", "/var/lib/service"].each do |path|
                    if FileTest.exist?(path)
                        @defpath = path
                        break
                    end
                end
                raise "Could not find the daemon directory (tested [/var/lib/service,/etc])" unless @defpath
            end
            @defpath
        end
    end

    # find the service dir on this node
    def servicedir
        unless defined?(@servicedir) and @servicedir
            ["/service", "/etc/service","/var/service"].each do |path|
                if FileTest.exist?(path)
                    @servicedir = path
                    break
                end
            end
            raise "Could not find service directory" unless @servicedir
        end
        @servicedir
    end

    def status
        begin
            output = sv "status", self.daemon
            return :running if output =~ /^run: /
        rescue Puppet::ExecutionFailure => detail
            unless detail.message =~ /(warning: |runsv not running$)/
                raise Puppet::Error.new( "Could not get status for service %s: %s" % [ resource.ref, detail] )
            end
        end
        return :stopped
    end

    def stop
        sv "stop", self.service
    end

    def start
        enable unless enabled? == :true
        sv "start", self.service
    end

    def restart
        sv "restart", self.service
    end

    # disable by removing the symlink so that runit
    # doesn't restart our service behind our back
    # note that runit doesn't need to perform a stop
    # before a disable
    def disable
        # unlink the daemon symlink to disable it
        File.unlink(self.service) if FileTest.symlink?(self.service)
    end
end