~ubuntu-branches/ubuntu/wily/puppet/wily

« back to all changes in this revision

Viewing changes to lib/puppet/indirector/msgpack.rb

  • Committer: Package Import Robot
  • Author(s): Stig Sandbeck Mathisen
  • Date: 2014-04-17 14:50:28 UTC
  • mfrom: (3.1.59 sid)
  • Revision ID: package-import@ubuntu.com-20140417145028-j3p3dwvp8ggpzvaf
Tags: 3.5.1-1
ImportedĀ upstreamĀ releaseĀ 3.5.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
require 'puppet/indirector/terminus'
 
2
require 'puppet/util'
 
3
 
 
4
# The base class for MessagePack indirection terminus implementations.
 
5
#
 
6
# This should generally be preferred to the PSON base for any future
 
7
# implementations, since it is ~ 30 times faster
 
8
class Puppet::Indirector::Msgpack < Puppet::Indirector::Terminus
 
9
  def initialize(*args)
 
10
    if ! Puppet.features.msgpack?
 
11
      raise "MessagePack terminus not supported without msgpack library"
 
12
    end
 
13
    super
 
14
  end
 
15
 
 
16
  def find(request)
 
17
    load_msgpack_from_file(path(request.key), request.key)
 
18
  end
 
19
 
 
20
  def save(request)
 
21
    filename = path(request.key)
 
22
    FileUtils.mkdir_p(File.dirname(filename))
 
23
 
 
24
    Puppet::Util.replace_file(filename, 0660) {|f| f.print to_msgpack(request.instance) }
 
25
  rescue TypeError => detail
 
26
    Puppet.log_exception "Could not save #{self.name} #{request.key}: #{detail}"
 
27
  end
 
28
 
 
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 #{self.name} #{request.key}: #{detail}", detail.backtrace
 
34
    end
 
35
    1                           # emulate success...
 
36
  end
 
37
 
 
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
 
43
 
 
44
  # Return the path to a given node's file.
 
45
  def path(name, ext = '.msgpack')
 
46
    if name =~ Puppet::Indirector::BadNameRegexp then
 
47
      Puppet.crit("directory traversal detected in #{self.class}: #{name.inspect}")
 
48
      raise ArgumentError, "invalid key"
 
49
    end
 
50
 
 
51
    base = Puppet.run_mode.master? ? Puppet[:server_datadir] : Puppet[:client_datadir]
 
52
    File.join(base, self.class.indirection_name.to_s, name.to_s + ext)
 
53
  end
 
54
 
 
55
  private
 
56
 
 
57
  def load_msgpack_from_file(file, key)
 
58
    msgpack = nil
 
59
 
 
60
    begin
 
61
      msgpack = File.read(file)
 
62
    rescue Errno::ENOENT
 
63
      return nil
 
64
    rescue => detail
 
65
      raise Puppet::Error, "Could not read MessagePack data for #{indirection.name} #{key}: #{detail}", detail.backtrace
 
66
    end
 
67
 
 
68
    begin
 
69
      return from_msgpack(msgpack)
 
70
    rescue => detail
 
71
      raise Puppet::Error, "Could not parse MessagePack data for #{indirection.name} #{key}: #{detail}", detail.backtrace
 
72
    end
 
73
  end
 
74
 
 
75
  def from_msgpack(text)
 
76
    model.convert_from('msgpack', text)
 
77
  end
 
78
 
 
79
  def to_msgpack(object)
 
80
    object.render('msgpack')
 
81
  end
 
82
end