From 86c6ec24f387fc70abc333fc4ac974b06b3ec80a Mon Sep 17 00:00:00 2001 From: Daniel Pittman Date: Mon, 2 May 2011 14:31:54 -0700 Subject: maint: move the indirector face base out of puppet/face We used to shove the base class Puppet::Face::Indirector next to the actual faces; this made a bunch of things, including testing, confusing. Instead, move it away into the indirector where it lives with the rest of the indirector related things. Reviewed-By: Nick Lewis --- lib/puppet/face/catalog.rb | 4 +- lib/puppet/face/certificate.rb | 4 +- lib/puppet/face/certificate_request.rb | 4 +- lib/puppet/face/certificate_revocation_list.rb | 4 +- lib/puppet/face/facts.rb | 4 +- lib/puppet/face/file.rb | 4 +- lib/puppet/face/indirector.rb | 95 -------------------------- lib/puppet/face/key.rb | 4 +- lib/puppet/face/node.rb | 4 +- lib/puppet/face/report.rb | 4 +- lib/puppet/face/resource.rb | 4 +- lib/puppet/face/resource_type.rb | 4 +- lib/puppet/face/status.rb | 4 +- lib/puppet/indirector/face.rb | 94 +++++++++++++++++++++++++ 14 files changed, 118 insertions(+), 119 deletions(-) delete mode 100644 lib/puppet/face/indirector.rb create mode 100644 lib/puppet/indirector/face.rb (limited to 'lib') diff --git a/lib/puppet/face/catalog.rb b/lib/puppet/face/catalog.rb index 98f550413..5f1f138ee 100644 --- a/lib/puppet/face/catalog.rb +++ b/lib/puppet/face/catalog.rb @@ -1,6 +1,6 @@ -require 'puppet/face/indirector' +require 'puppet/indirector/face' -Puppet::Face::Indirector.define(:catalog, '0.0.1') do +Puppet::Indirector::Face.define(:catalog, '0.0.1') do copyright "Puppet Labs", 2011 license "Apache 2 license; see COPYING" diff --git a/lib/puppet/face/certificate.rb b/lib/puppet/face/certificate.rb index 7f2998da2..0018c5fd3 100644 --- a/lib/puppet/face/certificate.rb +++ b/lib/puppet/face/certificate.rb @@ -1,7 +1,7 @@ -require 'puppet/face/indirector' +require 'puppet/indirector/face' require 'puppet/ssl/host' -Puppet::Face::Indirector.define(:certificate, '0.0.1') do +Puppet::Indirector::Face.define(:certificate, '0.0.1') do copyright "Puppet Labs", 2011 license "Apache 2 license; see COPYING" diff --git a/lib/puppet/face/certificate_request.rb b/lib/puppet/face/certificate_request.rb index 0f7f72205..809758423 100644 --- a/lib/puppet/face/certificate_request.rb +++ b/lib/puppet/face/certificate_request.rb @@ -1,6 +1,6 @@ -require 'puppet/face/indirector' +require 'puppet/indirector/face' -Puppet::Face::Indirector.define(:certificate_request, '0.0.1') do +Puppet::Indirector::Face.define(:certificate_request, '0.0.1') do copyright "Puppet Labs", 2011 license "Apache 2 license; see COPYING" diff --git a/lib/puppet/face/certificate_revocation_list.rb b/lib/puppet/face/certificate_revocation_list.rb index 9a8fe303d..9913fad4b 100644 --- a/lib/puppet/face/certificate_revocation_list.rb +++ b/lib/puppet/face/certificate_revocation_list.rb @@ -1,6 +1,6 @@ -require 'puppet/face/indirector' +require 'puppet/indirector/face' -Puppet::Face::Indirector.define(:certificate_revocation_list, '0.0.1') do +Puppet::Indirector::Face.define(:certificate_revocation_list, '0.0.1') do copyright "Puppet Labs", 2011 license "Apache 2 license; see COPYING" diff --git a/lib/puppet/face/facts.rb b/lib/puppet/face/facts.rb index 88e3c7ba9..9add63dd0 100644 --- a/lib/puppet/face/facts.rb +++ b/lib/puppet/face/facts.rb @@ -1,7 +1,7 @@ -require 'puppet/face/indirector' +require 'puppet/indirector/face' require 'puppet/node/facts' -Puppet::Face::Indirector.define(:facts, '0.0.1') do +Puppet::Indirector::Face.define(:facts, '0.0.1') do copyright "Puppet Labs", 2011 license "Apache 2 license; see COPYING" diff --git a/lib/puppet/face/file.rb b/lib/puppet/face/file.rb index 1b2e62b6d..1f2fc9f55 100644 --- a/lib/puppet/face/file.rb +++ b/lib/puppet/face/file.rb @@ -1,6 +1,6 @@ -require 'puppet/face/indirector' +require 'puppet/indirector/face' -Puppet::Face::Indirector.define(:file, '0.0.1') do +Puppet::Indirector::Face.define(:file, '0.0.1') do copyright "Puppet Labs", 2011 license "Apache 2 license; see COPYING" diff --git a/lib/puppet/face/indirector.rb b/lib/puppet/face/indirector.rb deleted file mode 100644 index 16ffcd311..000000000 --- a/lib/puppet/face/indirector.rb +++ /dev/null @@ -1,95 +0,0 @@ -require 'puppet' -require 'puppet/face' - -class Puppet::Face::Indirector < Puppet::Face - option "--terminus TERMINUS" do - description %q{ -REVISIT: You can select a terminus, which has some bigger effect -that we should describe in this file somehow. -}.strip - - before_action do |action, args, options| - set_terminus(options[:terminus]) - end - - after_action do |action, args, options| - indirection.reset_terminus_class - end - end - - def self.indirections - Puppet::Indirector::Indirection.instances.collect { |t| t.to_s }.sort - end - - def self.terminus_classes(indirection) - Puppet::Indirector::Terminus.terminus_classes(indirection.to_sym).collect { |t| t.to_s }.sort - end - - def call_indirection_method(method, key, options) - begin - result = indirection.__send__(method, key, options) - rescue => detail - puts detail.backtrace if Puppet[:trace] - raise "Could not call '#{method}' on '#{indirection_name}': #{detail}" - end - - return result - end - - action :destroy do - when_invoked { |key, options| call_indirection_method(:destroy, key, options) } - end - - action :find do - when_invoked { |key, options| call_indirection_method(:find, key, options) } - end - - action :save do - when_invoked { |key, options| call_indirection_method(:save, key, options) } - end - - action :search do - when_invoked { |key, options| call_indirection_method(:search, key, options) } - end - - # Print the configuration for the current terminus class - action :info do - when_invoked do |*args| - if t = indirection.terminus_class - puts "Run mode '#{Puppet.run_mode.name}': #{t}" - else - $stderr.puts "No default terminus class for run mode '#{Puppet.run_mode.name}'" - end - end - end - - attr_accessor :from - - def indirection_name - @indirection_name || name.to_sym - end - - # Here's your opportunity to override the indirection name. By default it - # will be the same name as the face. - def set_indirection_name(name) - @indirection_name = name - end - - # Return an indirection associated with a face, if one exists; - # One usually does. - def indirection - unless @indirection - @indirection = Puppet::Indirector::Indirection.instance(indirection_name) - @indirection or raise "Could not find terminus for #{indirection_name}" - end - @indirection - end - - def set_terminus(from) - begin - indirection.terminus_class = from - rescue => detail - raise "Could not set '#{indirection.name}' terminus to '#{from}' (#{detail}); valid terminus types are #{self.class.terminus_classes(indirection.name).join(", ") }" - end - end -end diff --git a/lib/puppet/face/key.rb b/lib/puppet/face/key.rb index 5d1a9ab26..148dc06c2 100644 --- a/lib/puppet/face/key.rb +++ b/lib/puppet/face/key.rb @@ -1,6 +1,6 @@ -require 'puppet/face/indirector' +require 'puppet/indirector/face' -Puppet::Face::Indirector.define(:key, '0.0.1') do +Puppet::Indirector::Face.define(:key, '0.0.1') do copyright "Puppet Labs", 2011 license "Apache 2 license; see COPYING" diff --git a/lib/puppet/face/node.rb b/lib/puppet/face/node.rb index 3591dd92b..c4cf30b98 100644 --- a/lib/puppet/face/node.rb +++ b/lib/puppet/face/node.rb @@ -1,5 +1,5 @@ -require 'puppet/face/indirector' -Puppet::Face::Indirector.define(:node, '0.0.1') do +require 'puppet/indirector/face' +Puppet::Indirector::Face.define(:node, '0.0.1') do copyright "Puppet Labs", 2011 license "Apache 2 license; see COPYING" diff --git a/lib/puppet/face/report.rb b/lib/puppet/face/report.rb index 9855f3d2d..dabf83702 100644 --- a/lib/puppet/face/report.rb +++ b/lib/puppet/face/report.rb @@ -1,6 +1,6 @@ -require 'puppet/face/indirector' +require 'puppet/indirector/face' -Puppet::Face::Indirector.define(:report, '0.0.1') do +Puppet::Indirector::Face.define(:report, '0.0.1') do copyright "Puppet Labs", 2011 license "Apache 2 license; see COPYING" diff --git a/lib/puppet/face/resource.rb b/lib/puppet/face/resource.rb index 55a14f280..9ded46c0c 100644 --- a/lib/puppet/face/resource.rb +++ b/lib/puppet/face/resource.rb @@ -1,6 +1,6 @@ -require 'puppet/face/indirector' +require 'puppet/indirector/face' -Puppet::Face::Indirector.define(:resource, '0.0.1') do +Puppet::Indirector::Face.define(:resource, '0.0.1') do copyright "Puppet Labs", 2011 license "Apache 2 license; see COPYING" diff --git a/lib/puppet/face/resource_type.rb b/lib/puppet/face/resource_type.rb index 8776dc105..648cf1191 100644 --- a/lib/puppet/face/resource_type.rb +++ b/lib/puppet/face/resource_type.rb @@ -1,6 +1,6 @@ -require 'puppet/face/indirector' +require 'puppet/indirector/face' -Puppet::Face::Indirector.define(:resource_type, '0.0.1') do +Puppet::Indirector::Face.define(:resource_type, '0.0.1') do copyright "Puppet Labs", 2011 license "Apache 2 license; see COPYING" diff --git a/lib/puppet/face/status.rb b/lib/puppet/face/status.rb index d35d7e1b3..7ef05fa0e 100644 --- a/lib/puppet/face/status.rb +++ b/lib/puppet/face/status.rb @@ -1,6 +1,6 @@ -require 'puppet/face/indirector' +require 'puppet/indirector/face' -Puppet::Face::Indirector.define(:status, '0.0.1') do +Puppet::Indirector::Face.define(:status, '0.0.1') do copyright "Puppet Labs", 2011 license "Apache 2 license; see COPYING" diff --git a/lib/puppet/indirector/face.rb b/lib/puppet/indirector/face.rb new file mode 100644 index 000000000..0fd44dfea --- /dev/null +++ b/lib/puppet/indirector/face.rb @@ -0,0 +1,94 @@ +require 'puppet/face' + +class Puppet::Indirector::Face < Puppet::Face + option "--terminus TERMINUS" do + description %q{ +REVISIT: You can select a terminus, which has some bigger effect +that we should describe in this file somehow. +}.strip + + before_action do |action, args, options| + set_terminus(options[:terminus]) + end + + after_action do |action, args, options| + indirection.reset_terminus_class + end + end + + def self.indirections + Puppet::Indirector::Indirection.instances.collect { |t| t.to_s }.sort + end + + def self.terminus_classes(indirection) + Puppet::Indirector::Terminus.terminus_classes(indirection.to_sym).collect { |t| t.to_s }.sort + end + + def call_indirection_method(method, key, options) + begin + result = indirection.__send__(method, key, options) + rescue => detail + puts detail.backtrace if Puppet[:trace] + raise "Could not call '#{method}' on '#{indirection_name}': #{detail}" + end + + return result + end + + action :destroy do + when_invoked { |key, options| call_indirection_method(:destroy, key, options) } + end + + action :find do + when_invoked { |key, options| call_indirection_method(:find, key, options) } + end + + action :save do + when_invoked { |key, options| call_indirection_method(:save, key, options) } + end + + action :search do + when_invoked { |key, options| call_indirection_method(:search, key, options) } + end + + # Print the configuration for the current terminus class + action :info do + when_invoked do |*args| + if t = indirection.terminus_class + puts "Run mode '#{Puppet.run_mode.name}': #{t}" + else + $stderr.puts "No default terminus class for run mode '#{Puppet.run_mode.name}'" + end + end + end + + attr_accessor :from + + def indirection_name + @indirection_name || name.to_sym + end + + # Here's your opportunity to override the indirection name. By default it + # will be the same name as the face. + def set_indirection_name(name) + @indirection_name = name + end + + # Return an indirection associated with a face, if one exists; + # One usually does. + def indirection + unless @indirection + @indirection = Puppet::Indirector::Indirection.instance(indirection_name) + @indirection or raise "Could not find terminus for #{indirection_name}" + end + @indirection + end + + def set_terminus(from) + begin + indirection.terminus_class = from + rescue => detail + raise "Could not set '#{indirection.name}' terminus to '#{from}' (#{detail}); valid terminus types are #{self.class.terminus_classes(indirection.name).join(", ") }" + end + end +end -- cgit From 1b42725b5caab6f8e457e11fec2488fbe94e8e43 Mon Sep 17 00:00:00 2001 From: Daniel Pittman Date: Mon, 2 May 2011 15:14:25 -0700 Subject: (#7314) Faces fail horribly when one has a syntax error. When we hit a syntax error in any face, a whole bunch of unrelated face things would blow up in horrible ways. Stack traces for all... Now, instead, we catch that fault but specifically only in the face file and report it through our error logs, then quietly ignore the face. Reviewed-By: Nick Lewis --- lib/puppet/interface/face_collection.rb | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) (limited to 'lib') diff --git a/lib/puppet/interface/face_collection.rb b/lib/puppet/interface/face_collection.rb index 6e6afc545..baa424692 100644 --- a/lib/puppet/interface/face_collection.rb +++ b/lib/puppet/interface/face_collection.rb @@ -10,21 +10,12 @@ module Puppet::Interface::FaceCollection unless @loaded @loaded = true $LOAD_PATH.each do |dir| - next unless FileTest.directory?(dir) - Dir.chdir(dir) do - Dir.glob("puppet/face/*.rb").collect { |f| f.sub(/\.rb/, '') }.each do |file| - iname = file.sub(/\.rb/, '') - begin - require iname - rescue Exception => detail - puts detail.backtrace if Puppet[:trace] - raise "Could not load #{iname} from #{dir}/#{file}: #{detail}" - end - end - end + Dir.glob("#{dir}/puppet/face/*.rb"). + collect {|f| File.basename(f, '.rb') }. + each {|name| self[name, :current] } end end - return @faces.keys.select {|name| @faces[name].length > 0 } + @faces.keys.select {|name| @faces[name].length > 0 } end def self.validate_version(version) @@ -124,6 +115,10 @@ module Puppet::Interface::FaceCollection rescue LoadError => e raise unless e.message =~ %r{-- puppet/face/#{name}$} # ...guess we didn't find the file; return a much better problem. + rescue SyntaxError => e + raise unless e.message =~ %r{puppet/face/#{name}\.rb:\d+: } + Puppet.err "Failed to load face #{name}:\n#{e}" + # ...but we just carry on after complaining. end return get_face(name, version) -- cgit