Class: Puppet::Indirector::File
- Defined in:
- lib/puppet/indirector/file.rb
Overview
Store instances as files, usually serialized using some format.
Instance Attribute Summary
Attributes included from Util::Docs
Instance Method Summary collapse
-
#data_directory ⇒ Object
Where do we store our data?.
-
#destroy(request) ⇒ Object
Remove files on disk.
- #file_format(path) ⇒ Object
- #file_path(request) ⇒ Object
-
#find(request) ⇒ Object
Return a model instance for a given file on disk.
- #latest_path(request) ⇒ Object
- #path(key) ⇒ Object
-
#save(request) ⇒ Object
Save a new file to disk.
- #serialization_format ⇒ Object
Methods inherited from Terminus
abstract_terminus?, const2name, #indirection, indirection_name, inherited, #initialize, mark_as_abstract_terminus, #model, model, #name, name2const, register_terminus_class, terminus_class, terminus_classes, #terminus_type
Methods included from Util::InstanceLoader
#instance_docs, #instance_hash, #instance_load, #instance_loader, #instance_loading?, #loaded_instance, #loaded_instances
Methods included from Util
activerecord_version, benchmark, chuser, classproxy, #execfail, #execpipe, execute, logmethods, memory, proxy, recmkdir, secure_open, symbolize, symbolizehash, symbolizehash!, synchronize_on, thinmark, #threadlock, which, withumask
Methods included from Util::POSIX
#get_posix_field, #gid, #idfield, #methodbyid, #methodbyname, #search_posix_field, #uid
Methods included from Util::Docs
#desc, #dochook, #doctable, #nodoc?, #pad, scrub
Constructor Details
This class inherits a constructor from Puppet::Indirector::Terminus
Instance Method Details
#data_directory ⇒ Object
Where do we store our data?
6 7 8 9 10 |
# File 'lib/puppet/indirector/file.rb', line 6 def data_directory name = Puppet.run_mode.master? ? :server_datadir : :client_datadir File.join(Puppet.settings[name], self.class.indirection_name.to_s) end |
#destroy(request) ⇒ Object
Remove files on disk.
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/puppet/indirector/file.rb', line 33 def destroy(request) begin removed = false Dir.glob(File.join(data_directory, request.key.to_s + ".*")).each do |file| removed = true File.unlink(file) end rescue => detail raise Puppet::Error, "Could not remove #{request.key}: #{detail}" end raise Puppet::Error, "Could not find files for #{request.key} to remove" unless removed end |
#file_format(path) ⇒ Object
12 13 14 |
# File 'lib/puppet/indirector/file.rb', line 12 def file_format(path) path =~ /\.(\w+)$/ and return $1 end |
#file_path(request) ⇒ Object
16 17 18 |
# File 'lib/puppet/indirector/file.rb', line 16 def file_path(request) File.join(data_directory, request.key + ".#{serialization_format}") end |
#find(request) ⇒ Object
Return a model instance for a given file on disk.
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/puppet/indirector/file.rb', line 48 def find(request) return nil unless path = latest_path(request) format = file_format(path) raise ArgumentError, "File format #{format} is not supported by #{self.class.indirection_name}" unless model.support_format?(format) begin return model.convert_from(format, File.read(path)) rescue => detail raise Puppet::Error, "Could not convert path #{path} into a #{self.class.indirection_name}: #{detail}" end end |
#latest_path(request) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/puppet/indirector/file.rb', line 20 def latest_path(request) files = Dir.glob(File.join(data_directory, request.key + ".*")) return nil if files.empty? # Return the newest file. files.sort { |a, b| File.stat(b).mtime <=> File.stat(a).mtime }[0] end |
#path(key) ⇒ Object
76 77 78 |
# File 'lib/puppet/indirector/file.rb', line 76 def path(key) key end |
#save(request) ⇒ Object
Save a new file to disk.
62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/puppet/indirector/file.rb', line 62 def save(request) path = file_path(request) dir = File.dirname(path) raise Puppet::Error.new("Cannot save #{request.key}; parent directory #{dir} does not exist") unless File.directory?(dir) begin File.open(path, "w") { |f| f.print request.instance.render(serialization_format) } rescue => detail raise Puppet::Error, "Could not write #{request.key}: #{detail}" % [request.key, detail] end end |
#serialization_format ⇒ Object
28 29 30 |
# File 'lib/puppet/indirector/file.rb', line 28 def serialization_format model.default_format end |