summaryrefslogtreecommitdiffstats
path: root/lib/puppet/reports/store.rb
blob: 82eebff398b6962c00a620ed0a8b4556eb27898e (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
require 'puppet'

Puppet::Server::Report.newreport(:store, :useyaml => true) do
    Puppet.setdefaults(:reporting,
        :reportdir => {:default => "$vardir/reports",
                :mode => 0750,
                :owner => "$user",
                :group => "$group",
                :desc => "The directory in which to store reports received from the
            client.  Each client gets a separate subdirectory."}
    )

    Puppet.config.use(:reporting)

    desc "Store the yaml report on disk"

    def mkclientdir(client, dir)
        config = Puppet::Config.new
        config.setdefaults("reportclient-#{client}",
            "clientdir-#{client}" => { :default => dir,
                :mode => 0750,
                :owner => "$user",
                :group => "$group"
            }
        )

        config.use("reportclient-#{client}")
    end

    def process(yaml)
        # We don't want any tracking back in the fs.  Unlikely, but there
        # you go.
        client = self.host.gsub("..",".")

        dir = File.join(Puppet[:reportdir], client)

        unless FileTest.exists?(dir)
            mkclientdir(client, dir)
        end

        # Now store the report.
        now = Time.now.gmtime
        name = %w{year month day hour min}.collect do |method|
            # Make sure we're at least two digits everywhere
            "%02d" % now.send(method).to_s
        end.join("") + ".yaml"

        file = File.join(dir, name)

        begin
            File.open(file, "w", 0640) do |f|
                f.puts yaml
            end
        rescue => detail
            if Puppet[:trace]
                puts detail.backtrace
            end
            Puppet.warning "Could not write report for %s at %s: %s" %
                [client, file, detail]
        end

        # Only testing cares about the return value
        return file
    end
end

# $Id$