module Puppet::Interface::FaceCollection

Public Class Methods

[](name, version) click to toggle source
   # File lib/puppet/interface/face_collection.rb
17 def self.[](name, version)
18   name = underscorize(name)
19   get_face(name, version) or load_face(name, version)
20 end
faces() click to toggle source
   # File lib/puppet/interface/face_collection.rb
 6 def self.faces
 7   unless @loaded
 8     @loaded = true
 9     names = @loader.files_to_load(Puppet.lookup(:current_environment)).map do |fn|
10       ::File.basename(fn, '.rb')
11     end.uniq
12     names.each {|name| self[name, :current]}
13   end
14   @faces.keys.select {|name| @faces[name].length > 0 }
15 end
find_matching(range, versions) click to toggle source
   # File lib/puppet/interface/face_collection.rb
54 def self.find_matching(range, versions)
55   versions.select { |v| range === v }.sort.last
56 end
get_action_for_face(name, action_name, version) click to toggle source
   # File lib/puppet/interface/face_collection.rb
22 def self.get_action_for_face(name, action_name, version)
23   name = underscorize(name)
24 
25   # If the version they request specifically doesn't exist, don't search
26   # elsewhere.  Usually this will start from :current and all...
27   face = self[name, version]
28   return nil unless face
29   action = face.get_action(action_name)
30   unless action
31     # ...we need to search for it bound to an o{lder,ther} version.  Since
32     # we load all actions when the face is first references, this will be in
33     # memory in the known set of versions of the face.
34     (@faces[name].keys - [ :current ]).sort.reverse_each do |vers|
35       action = @faces[name][vers].get_action(action_name)
36       break if action
37     end
38   end
39 
40   return action
41 end
get_face(name, pattern) click to toggle source

get face from memory, without loading.

   # File lib/puppet/interface/face_collection.rb
44 def self.get_face(name, pattern)
45   return nil unless @faces.has_key? name
46   return @faces[name][:current] if pattern == :current
47 
48   versions = @faces[name].keys - [ :current ]
49   range = pattern.is_a?(SemanticPuppet::Version) ? SemanticPuppet::VersionRange.new(pattern, pattern) : SemanticPuppet::VersionRange.parse(pattern)
50   found = find_matching(range, versions)
51   return @faces[name][found]
52 end
load_face(name, version) click to toggle source

try to load the face, and return it.

    # File lib/puppet/interface/face_collection.rb
 59 def self.load_face(name, version)
 60   # We always load the current version file; the common case is that we have
 61   # the expected version and any compatibility versions in the same file,
 62   # the default.  Which means that this is almost always the case.
 63   #
 64   # We use require to avoid executing the code multiple times, like any
 65   # other Ruby library that we might want to use.  --daniel 2011-04-06
 66   if safely_require name then
 67     # If we wanted :current, we need to index to find that; direct version
 68     # requests just work as they go. --daniel 2011-04-06
 69     if version == :current then
 70       # We need to find current out of this.  This is the largest version
 71       # number that doesn't have a dedicated on-disk file present; those
 72       # represent "experimental" versions of faces, which we don't fully
 73       # support yet.
 74       #
 75       # We walk the versions from highest to lowest and take the first version
 76       # that is not defined in an explicitly versioned file on disk as the
 77       # current version.
 78       #
 79       # This constrains us to only ship experimental versions with *one*
 80       # version in the file, not multiple, but given you can't reliably load
 81       # them except by side-effect when you ignore that rule this seems safe
 82       # enough...
 83       #
 84       # Given those constraints, and that we are not going to ship a versioned
 85       # interface that is not :current in this release, we are going to leave
 86       # these thoughts in place, and just punt on the actual versioning.
 87       #
 88       # When we upgrade the core to support multiple versions we can solve the
 89       # problems then; as lazy as possible.
 90       #
 91       # We do support multiple versions in the same file, though, so we sort
 92       # versions here and return the last item in that set.
 93       #
 94       # --daniel 2011-04-06
 95       latest_ver = @faces[name].keys.sort.last
 96       @faces[name][:current] = @faces[name][latest_ver]
 97     end
 98   end
 99 
100   unless version == :current or get_face(name, version)
101     # Try an obsolete version of the face, if needed, to see if that helps?
102     safely_require name, version
103   end
104 
105   return get_face(name, version)
106 end
register(face) click to toggle source
    # File lib/puppet/interface/face_collection.rb
124 def self.register(face)
125   @faces[underscorize(face.name)][face.version] = face
126 end
safely_require(name, version = nil) click to toggle source
    # File lib/puppet/interface/face_collection.rb
108 def self.safely_require(name, version = nil)
109   path = @loader.expand(version ? ::File.join(version.to_s, name.to_s) : name)
110   require path
111   true
112 
113 rescue LoadError => e
114   raise unless e.message =~ %r{-- #{path}$}
115   # ...guess we didn't find the file; return a much better problem.
116   nil
117 rescue SyntaxError => e
118   raise unless e.message =~ %r{#{path}\.rb:\d+: }
119   Puppet.err _("Failed to load face %{name}:\n%{detail}") % { name: name, detail: e }
120   # ...but we just carry on after complaining.
121   nil
122 end
underscorize(name) click to toggle source
    # File lib/puppet/interface/face_collection.rb
128 def self.underscorize(name)
129   unless name.to_s =~ /^[-_a-z][-_a-z0-9]*$/i then
130     #TRANSLATORS 'face' refers to a programming API in Puppet
131     raise ArgumentError, _("%{name} (%{class_name}) is not a valid face name") %
132         { name: name.inspect, class_name: name.class }
133   end
134 
135   name.to_s.downcase.split(/[-_]/).join('_').to_sym
136 end