]> git.immae.eu Git - perso/Immae/Projets/Puppet.git/blob - modules/base_installation/lib/puppet/reports/store_changed.rb
bb39beb6185766ca28839536914cf729131607bd
[perso/Immae/Projets/Puppet.git] / modules / base_installation / lib / puppet / reports / store_changed.rb
1 require 'puppet'
2 require 'fileutils'
3 require 'puppet/util'
4
5 SEPARATOR = [Regexp.escape(File::SEPARATOR.to_s), Regexp.escape(File::ALT_SEPARATOR.to_s)].join
6
7 Puppet::Reports.register_report(:store_changed) do
8 desc "Store the yaml report on disk. Each host sends its report as a YAML dump
9 and this just stores the file on disk, in the `reportdir` directory.
10
11 These files collect quickly -- one every half hour -- so it is a good idea
12 to perform some maintenance on them if you use this report (it's the only
13 default report)."
14
15 def process
16 validate_host(host)
17 if status == "unchanged"
18 return
19 end
20
21 dir = File.join(Puppet[:reportdir], host)
22
23 if ! Puppet::FileSystem.exist?(dir)
24 FileUtils.mkdir_p(dir)
25 FileUtils.chmod_R(0750, dir)
26 end
27
28 # Now store the report.
29 now = Time.now.gmtime
30 name = %w{year month day hour min}.collect do |method|
31 # Make sure we're at least two digits everywhere
32 "%02d" % now.send(method).to_s
33 end.join("") + ".yaml"
34
35 file = File.join(dir, name)
36
37 begin
38 Puppet::Util.replace_file(file, 0640) do |fh|
39 fh.print to_yaml
40 end
41 rescue => detail
42 Puppet.log_exception(detail, "Could not write report for #{host} at #{file}: #{detail}")
43 end
44
45 # Only testing cares about the return value
46 file
47 end
48
49 # removes all reports for a given host?
50 def self.destroy(host)
51 validate_host(host)
52
53 dir = File.join(Puppet[:reportdir], host)
54
55 if Puppet::FileSystem.exist?(dir)
56 Dir.entries(dir).each do |file|
57 next if ['.','..'].include?(file)
58 file = File.join(dir, file)
59 Puppet::FileSystem.unlink(file) if File.file?(file)
60 end
61 Dir.rmdir(dir)
62 end
63 end
64
65 def validate_host(host)
66 if host =~ Regexp.union(/[#{SEPARATOR}]/, /\A\.\.?\Z/)
67 raise ArgumentError, _("Invalid node name %{host}") % { host: host.inspect }
68 end
69 end
70 module_function :validate_host
71 end
72