summaryrefslogtreecommitdiffstats
path: root/lib/puppet/util/run_mode.rb
blob: bf745743ff2d4d0b41e6acd2adac1965480ff066 (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
module Puppet
    module Util
        class RunMode
            def initialize(name)
                @name = name.to_sym
            end

            @@run_modes = Hash.new {|h, k| h[k] = RunMode.new(k)}

            attr :name

            def self.[](name)
                @@run_modes[name]
            end

            def master?
                name == :master
            end

            def agent?
                name == :agent
            end

            def user?
                name == :user
            end

            def conf_dir
                which_dir(
                    (Puppet.features.microsoft_windows? ? File.join(Dir::WINDOWS, "puppet", "etc") : "/etc/puppet"),
                    "~/.puppet"
                )
            end

            def var_dir
                which_dir(
                    (Puppet.features.microsoft_windows? ? File.join(Dir::WINDOWS, "puppet", "var") : "/var/lib/puppet"),
                    "~/.puppet/var"
                )
            end

            def run_dir
                which_dir("/var/run/puppet", "~/.puppet/var")
            end

            def logopts
                if name == :master
                    {
                        :default => "$vardir/log",
                        :mode => 0750,
                        :owner => "service",
                        :group => "service",
                        :desc => "The Puppet log directory."
                    }
                else
                    ["$vardir/log", "The Puppet log directory."]
                end
            end

            private

            def which_dir( global, user )
                #FIXME: we should test if we're user "puppet"
                #       there's a comment that suggests that we do that
                #       and we currently don't.
                expand_path case
                when name == :master; global
                when Puppet.features.root?; global
                else user
                end
            end

            def expand_path( dir )
                require 'etc'
                ENV["HOME"] ||= Etc.getpwuid(Process.uid).dir
                File.expand_path(dir)
            end

        end
    end
end