summaryrefslogtreecommitdiffstats
path: root/lib/puppet/storage.rb
blob: 28d593db095d3fd3c5cf92707d6b85ef20715ed4 (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
# $Id$

module Puppet
    # a class for storing state
	class Storage
		include Singleton
		@@state = Hash.new { |hash,key|
            hash[key] = Hash.new(nil)
        }
		@@splitchar = "\t"
		
		def initialize
			self.class.load
		end

		def Storage.load
            # XXX I should probably use a better default state dir
            Puppet[:statefile] ||= "/var/tmp/puppetstate"
			return unless File.exists?(Puppet[:statefile])
			File.open(Puppet[:statefile]) { |file|
				file.gets { |line|
					myclass, key, value = line.split(@@splitchar)

					@@state[myclass][key] = Marshal::load(value)
				}
			}
		end

		def Storage.state(myclass)
            unless myclass.is_a? Class
                myclass = myclass.class
            end
            result = @@state[myclass]
            return result
		end

		def Storage.store
            unless FileTest.directory?(File.dirname(Puppet[:statefile]))
                begin
                    Puppet.recmkdir(Puppet[:statefile])
                    Puppet.info "Creating state directory %s" %
                        File.basename(Puppet[:statefile])
                rescue => detail
                    Puppet.err "Could not create state file: %s" % detail
                    return
                end
            end

            unless FileTest.exist?(Puppet[:statefile])
                Puppet.info "Creating state file %s" % Puppet[:statefile]
            end

			File.open(Puppet[:statefile], File::CREAT|File::WRONLY, 0600) { |file|
				@@state.each { |klass, thash|
                    thash.each { |key,value|
                        mvalue = Marshal::dump(value)
                        file.puts([klass,key,mvalue].join(@@splitchar))
                    }
				}
			}
		end
	end
end