class Puppet::Indirector::Msgpack
The base class for MessagePack indirection terminus implementations.
This should generally be preferred to the PSON
base for any future implementations, since it is ~ 30 times faster
Public Class Methods
new(*args)
click to toggle source
Calls superclass method
Puppet::Indirector::Terminus::new
# File lib/puppet/indirector/msgpack.rb 9 def initialize(*args) 10 if ! Puppet.features.msgpack? 11 raise _("MessagePack terminus not supported without msgpack library") 12 end 13 super 14 end
Public Instance Methods
destroy(request)
click to toggle source
# File lib/puppet/indirector/msgpack.rb 29 def destroy(request) 30 Puppet::FileSystem.unlink(path(request.key)) 31 rescue => detail 32 unless detail.is_a? Errno::ENOENT 33 raise Puppet::Error, _("Could not destroy %{name} %{request}: %{detail}") % { name: self.name, request: request.key, detail: detail }, detail.backtrace 34 end 35 1 # emulate success... 36 end
find(request)
click to toggle source
# File lib/puppet/indirector/msgpack.rb 16 def find(request) 17 load_msgpack_from_file(path(request.key), request.key) 18 end
path(name, ext = '.msgpack')
click to toggle source
Return the path to a given node's file.
# File lib/puppet/indirector/msgpack.rb 45 def path(name, ext = '.msgpack') 46 if name =~ Puppet::Indirector::BadNameRegexp then 47 Puppet.crit(_("directory traversal detected in %{indirection}: %{name}") % { indirection: self.class, name: name.inspect }) 48 raise ArgumentError, _("invalid key") 49 end 50 51 base = Puppet.run_mode.server? ? Puppet[:server_datadir] : Puppet[:client_datadir] 52 File.join(base, self.class.indirection_name.to_s, name.to_s + ext) 53 end
save(request)
click to toggle source
# File lib/puppet/indirector/msgpack.rb 20 def save(request) 21 filename = path(request.key) 22 FileUtils.mkdir_p(File.dirname(filename)) 23 24 Puppet::FileSystem.replace_file(filename, 0660) {|f| f.print to_msgpack(request.instance) } 25 rescue TypeError => detail 26 Puppet.log_exception(detail, _("Could not save %{name} %{request}: %{detail}") % { name: self.name, request: request.key, detail: detail }) 27 end
search(request)
click to toggle source
# File lib/puppet/indirector/msgpack.rb 38 def search(request) 39 Dir.glob(path(request.key)).collect do |file| 40 load_msgpack_from_file(file, request.key) 41 end 42 end
Private Instance Methods
from_msgpack(text)
click to toggle source
# File lib/puppet/indirector/msgpack.rb 76 def from_msgpack(text) 77 model.convert_from('msgpack', text) 78 end
load_msgpack_from_file(file, key)
click to toggle source
# File lib/puppet/indirector/msgpack.rb 57 def load_msgpack_from_file(file, key) 58 msgpack = nil 59 60 begin 61 msgpack = Puppet::FileSystem.read(file, :encoding => 'utf-8') 62 rescue Errno::ENOENT 63 return nil 64 rescue => detail 65 #TRANSLATORS "MessagePack" is a program name and should not be translated 66 raise Puppet::Error, _("Could not read MessagePack data for %{indirection} %{key}: %{detail}") % { indirection: indirection.name, key: key, detail: detail }, detail.backtrace 67 end 68 69 begin 70 return from_msgpack(msgpack) 71 rescue => detail 72 raise Puppet::Error, _("Could not parse MessagePack data for %{indirection} %{key}: %{detail}") % { indirection: indirection.name, key: key, detail: detail }, detail.backtrace 73 end 74 end
to_msgpack(object)
click to toggle source
# File lib/puppet/indirector/msgpack.rb 80 def to_msgpack(object) 81 object.render('msgpack') 82 end