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
10 def initialize(*args)
11   if ! Puppet.features.msgpack?
12     raise _("MessagePack terminus not supported without msgpack library")
13   end
14   super
15 end

Public Instance Methods

destroy(request) click to toggle source
   # File lib/puppet/indirector/msgpack.rb
30 def destroy(request)
31   Puppet::FileSystem.unlink(path(request.key))
32 rescue => detail
33   unless detail.is_a? Errno::ENOENT
34     raise Puppet::Error, _("Could not destroy %{name} %{request}: %{detail}") % { name: self.name, request: request.key, detail: detail }, detail.backtrace
35   end
36   1                           # emulate success...
37 end
find(request) click to toggle source
   # File lib/puppet/indirector/msgpack.rb
17 def find(request)
18   load_msgpack_from_file(path(request.key), request.key)
19 end
path(name, ext = '.msgpack') click to toggle source

Return the path to a given node's file.

   # File lib/puppet/indirector/msgpack.rb
46 def path(name, ext = '.msgpack')
47   if name =~ Puppet::Indirector::BadNameRegexp then
48     Puppet.crit(_("directory traversal detected in %{indirection}: %{name}") % { indirection: self.class, name: name.inspect })
49     raise ArgumentError, _("invalid key")
50   end
51 
52   base = Puppet.run_mode.server? ? Puppet[:server_datadir] : Puppet[:client_datadir]
53   File.join(base, self.class.indirection_name.to_s, name.to_s + ext)
54 end
save(request) click to toggle source
   # File lib/puppet/indirector/msgpack.rb
21 def save(request)
22   filename = path(request.key)
23   FileUtils.mkdir_p(File.dirname(filename))
24 
25   Puppet::FileSystem.replace_file(filename, 0660) {|f| f.print to_msgpack(request.instance) }
26 rescue TypeError => detail
27   Puppet.log_exception(detail, _("Could not save %{name} %{request}: %{detail}") % { name: self.name, request: request.key, detail: detail })
28 end

Private Instance Methods

from_msgpack(text) click to toggle source
   # File lib/puppet/indirector/msgpack.rb
77 def from_msgpack(text)
78   model.convert_from('msgpack', text)
79 end
load_msgpack_from_file(file, key) click to toggle source
   # File lib/puppet/indirector/msgpack.rb
58 def load_msgpack_from_file(file, key)
59   msgpack = nil
60 
61   begin
62     msgpack = Puppet::FileSystem.read(file, :encoding => 'utf-8')
63   rescue Errno::ENOENT
64     return nil
65   rescue => detail
66     #TRANSLATORS "MessagePack" is a program name and should not be translated
67     raise Puppet::Error, _("Could not read MessagePack data for %{indirection} %{key}: %{detail}") % { indirection: indirection.name, key: key, detail: detail }, detail.backtrace
68   end
69 
70   begin
71     return from_msgpack(msgpack)
72   rescue => detail
73     raise Puppet::Error, _("Could not parse MessagePack data for %{indirection} %{key}: %{detail}") % { indirection: indirection.name, key: key, detail: detail }, detail.backtrace
74   end
75 end
to_msgpack(object) click to toggle source
   # File lib/puppet/indirector/msgpack.rb
81 def to_msgpack(object)
82   object.render('msgpack')
83 end