summaryrefslogtreecommitdiffstats
path: root/lib/puppet/indirector/facts/yaml.rb
blob: cb02f05c6bfe6068c5e5048b0829131dd1b04465 (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
Puppet::Indirector.register_terminus :facts, :yaml do
    desc "Store client facts as flat files, serialized using YAML."

    # Get a client's facts.
    def find(node)
        file = path(node)

        return nil unless FileTest.exists?(file)

        begin
            values = YAML::load(File.read(file))
        rescue => detail
            Puppet.err "Could not load facts for %s: %s" % [node, detail]
        end

        Puppet::Node::Facts.new(node, values)
    end

    def initialize
        Puppet.config.use(:yamlfacts)
    end

    # Store the facts to disk.
    def save(facts)
        File.open(path(facts.name), "w", 0600) do |f|
            begin
                f.print YAML::dump(facts.values)
            rescue => detail
                Puppet.err "Could not write facts for %s: %s" % [facts.name, detail]
            end
        end
        nil
    end

    private

    # Return the path to a given node's file.
    def path(name)
        File.join(Puppet[:yamlfactdir], name + ".yaml")
    end
end