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