summaryrefslogtreecommitdiffstats
path: root/lib/puppet/modules.rb
blob: 3d071b05a56d642b59139f08d924266bed7197b8 (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
# Support for modules
class Puppet::Module

    TEMPLATES = "templates"
    FILES = "files"

    # Return an array of paths by splitting the +modulepath+ config
    # parameter. Only consider paths that are absolute and existing
    # directories
    def self.modulepath
        dirs = ENV["PUPPETLIB"].split(":") + Puppet[:modulepath].split(":")
        dirs.select do |p|
            p =~ /^#{File::SEPARATOR}/ && File::directory?(p)
        end
    end

    # Find and return the +module+ that +path+ belongs to. If +path+ is
    # absolute, or if there is no module whose name is the first component
    # of +path+, return +nil+
    def self.find(path)
        if path =~ %r/^#{File::SEPARATOR}/
            return nil
        end

        modname, rest = path.split(File::SEPARATOR, 2)
        return nil if modname.nil? || modname.empty?

        modpath = modulepath.collect { |p|
            File::join(p, modname)
        }.find { |f| File::directory?(f) }
        return nil unless modpath

        return self.new(modname, modpath)
    end

    # Instance methods

    # Find the concrete file denoted by +file+. If +file+ is absolute,
    # return it directly. Otherwise try to find it as a template in a
    # module. If that fails, return it relative to the +templatedir+ config
    # param.
    # In all cases, an absolute path is returned, which does not
    # necessarily refer to an existing file
    def self.find_template(file)
        if file =~ /^#{File::SEPARATOR}/
            return file
        end

        mod = find(file)
        if mod
            return mod.template(file)
        else
            return File.join(Puppet[:templatedir], file)
        end
    end

    attr_reader :name, :path
    def initialize(name, path)
        @name = name
        @path = path
    end

    def strip(file)
        n, rest = file.split(File::SEPARATOR, 2)
        return rest
    end

    def template(file)
        return File::join(path, TEMPLATES, strip(file))
    end

    def files
        return File::join(path, FILES)
    end

    private :initialize
end