summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorPieter van de Bruggen <pieter@puppetlabs.com>2011-03-22 13:19:25 -0700
committerPieter van de Bruggen <pieter@puppetlabs.com>2011-03-22 14:05:47 -0700
commita58bf959ec49c033e0498916a09e77e303c5792e (patch)
tree5f0a049580cc9d2a7fe568c3fd9c1cef312ac841 /lib
parent45613e0f192778cd16f945d5d1eb109e6c8dee2d (diff)
downloadpuppet-a58bf959ec49c033e0498916a09e77e303c5792e.tar.gz
puppet-a58bf959ec49c033e0498916a09e77e303c5792e.tar.xz
puppet-a58bf959ec49c033e0498916a09e77e303c5792e.zip
(#6786) Change interface storage and access.
Ruby's namespace mechanism introduced a number of problems, including incorrect name resolution for common and simple cases. Given that, we've refactored back to class-level data structures with accessor methods available. The current method names are unlikely to be the final UI. Reviewed-By: Daniel Pittman
Diffstat (limited to 'lib')
-rw-r--r--lib/puppet/application/configurer.rb4
-rw-r--r--lib/puppet/application/interface_base.rb3
-rw-r--r--lib/puppet/interface.rb30
-rw-r--r--lib/puppet/interface/catalog.rb2
-rw-r--r--lib/puppet/interface/catalog/select.rb12
-rw-r--r--lib/puppet/interface/configurer.rb9
6 files changed, 32 insertions, 28 deletions
diff --git a/lib/puppet/application/configurer.rb b/lib/puppet/application/configurer.rb
index 70d24814e..378364430 100644
--- a/lib/puppet/application/configurer.rb
+++ b/lib/puppet/application/configurer.rb
@@ -17,7 +17,7 @@ class Puppet::Application::Configurer < Puppet::Application
end
def run_command
- report = Puppet::Interface::Configurer.synchronize(Puppet[:certname])
- Puppet::Interface::Report.submit(report)
+ report = Puppet::Interface.interface(:configurer).synchronize(Puppet[:certname])
+ Puppet::Interface.interface(:report).submit(report)
end
end
diff --git a/lib/puppet/application/interface_base.rb b/lib/puppet/application/interface_base.rb
index f2c147f1f..654674df5 100644
--- a/lib/puppet/application/interface_base.rb
+++ b/lib/puppet/application/interface_base.rb
@@ -75,9 +75,10 @@ class Puppet::Application::InterfaceBase < Puppet::Application
@type = self.class.name.to_s.sub(/.+:/, '').downcase.to_sym
- unless @interface = Puppet::Interface.const_get(@type)
+ unless Puppet::Interface.interface?(@type)
raise "Could not find interface '#{@type}'"
end
+ @interface = Puppet::Interface.interface(@type)
@format ||= @interface.default_format
# We copy all of the app options to the interface.
diff --git a/lib/puppet/interface.rb b/lib/puppet/interface.rb
index 38841d948..0ec0f803f 100644
--- a/lib/puppet/interface.rb
+++ b/lib/puppet/interface.rb
@@ -9,6 +9,8 @@ class Puppet::Interface
include Puppet::Util
+ @interfaces = {}
+
# This is just so we can search for actions. We only use its
# list of directories to search.
# Can't we utilize an external autoloader, or simply use the $LOAD_PATH? -pvb
@@ -34,31 +36,33 @@ class Puppet::Interface
end
end
end
- Puppet::Interface.constants.map { |c| c.to_s.downcase }
+ return @interfaces.keys
end
- def self.const_missing(name)
- require "puppet/interface/#{name.to_s.downcase}"
- const_get(name) if const_defined?(name)
+ def self.interface?(name)
+ name = underscorize(name)
+ require "puppet/interface/#{name}" unless @interfaces.has_key? name
+ return @interfaces.has_key? name
rescue LoadError
- nil
+ return false
end
- def self.const_get(name)
- name = constantize(name)
- super(name)
+ def self.interface(name, &blk)
+ interface = interface?(name) ? @interfaces[underscorize(name)] : new(name)
+ interface.instance_eval(&blk) if block_given?
+ return interface
end
def self.register_interface(name, instance)
- const_set(constantize(name), instance)
+ @interfaces[underscorize(name)] = instance
end
- def self.constantize(name)
+ def self.underscorize(name)
unless name.to_s =~ /^[-_a-z]+$/i then
raise ArgumentError, "#{name.inspect} (#{name.class}) is not a valid interface name"
end
- name.to_s.split(/[-_]/).map { |x| x.capitalize }.join
+ name.to_s.downcase.split(/[-_]/).join('_').to_sym
end
attr_accessor :default_format
@@ -75,7 +79,7 @@ class Puppet::Interface
attr_accessor :type, :verb, :name, :arguments, :options
def initialize(name, options = {}, &block)
- @name = name
+ @name = self.class.underscorize(name)
@default_format = :pson
options.each { |opt, val| send(opt.to_s + "=", val) }
@@ -118,6 +122,6 @@ class Puppet::Interface
end
def to_s
- name.to_s
+ "Puppet::Interface(#{name})"
end
end
diff --git a/lib/puppet/interface/catalog.rb b/lib/puppet/interface/catalog.rb
index f99d0881a..34a1d8119 100644
--- a/lib/puppet/interface/catalog.rb
+++ b/lib/puppet/interface/catalog.rb
@@ -25,7 +25,7 @@ Puppet::Interface::Indirector.new(:catalog) do
facts_to_upload = {:facts_format => :b64_zlib_yaml, :facts => CGI.escape(facts.render(:b64_zlib_yaml))}
catalog = nil
retrieval_duration = thinmark do
- catalog = Puppet::Interface::Catalog.find(certname, facts_to_upload)
+ catalog = Puppet::Interface.interface(:catalog).find(certname, facts_to_upload)
end
catalog = catalog.to_ral
catalog.finalize
diff --git a/lib/puppet/interface/catalog/select.rb b/lib/puppet/interface/catalog/select.rb
index 4bb49315c..082d93c34 100644
--- a/lib/puppet/interface/catalog/select.rb
+++ b/lib/puppet/interface/catalog/select.rb
@@ -1,8 +1,10 @@
# Select and show a list of resources of a given type.
-Puppet::Interface::Catalog.action :select do |*args|
- host = args.shift
- type = args.shift
- catalog = Puppet::Resource::Catalog.indirection.find(host)
+Puppet::Interface.interface(:catalog) do
+ action :select do |*args|
+ host = args.shift
+ type = args.shift
+ catalog = Puppet::Resource::Catalog.indirection.find(host)
- catalog.resources.reject { |res| res.type != type }.each { |res| puts res }
+ catalog.resources.reject { |res| res.type != type }.each { |res| puts res }
+ end
end
diff --git a/lib/puppet/interface/configurer.rb b/lib/puppet/interface/configurer.rb
index 42e950fa3..c1a28b2e7 100644
--- a/lib/puppet/interface/configurer.rb
+++ b/lib/puppet/interface/configurer.rb
@@ -2,12 +2,9 @@ require 'puppet/interface'
Puppet::Interface.new(:configurer) do
action(:synchronize) do |certname|
- facts = Puppet::Interface::Facts.find(certname)
-
- catalog = Puppet::Interface::Catalog.download(certname, facts)
-
- report = Puppet::Interface::Catalog.apply(catalog)
-
+ facts = Puppet::Interface.interface(:facts).find(certname)
+ catalog = Puppet::Interface.interface(:catalog).download(certname, facts)
+ report = Puppet::Interface.interface(:catalog).apply(catalog)
report
end
end