diff options
author | Nick Lewis <nick@puppetlabs.com> | 2011-03-22 12:54:52 -0700 |
---|---|---|
committer | Nick Lewis <nick@puppetlabs.com> | 2011-03-22 14:16:32 -0700 |
commit | e3d24865c89bccd0221f3d6d475d350f577ed3fb (patch) | |
tree | 01e2f42e8342767aa7f9e63e961a61bb81046b98 /lib/puppet/interface | |
parent | 45613e0f192778cd16f945d5d1eb109e6c8dee2d (diff) | |
download | puppet-e3d24865c89bccd0221f3d6d475d350f577ed3fb.tar.gz puppet-e3d24865c89bccd0221f3d6d475d350f577ed3fb.tar.xz puppet-e3d24865c89bccd0221f3d6d475d350f577ed3fb.zip |
(#6814) Create a dedicated Action class
This class will represents an action, and allows us to store metadata for an
action, and programmatically introspect and invoke them. A helper class
ActionBuilder represents the DSL for defining an action.
Also defined an "invoke" DSL method to handle the functionality of defining the
method for an action.
Reviewed-By: Daniel Pittman
Diffstat (limited to 'lib/puppet/interface')
-rw-r--r-- | lib/puppet/interface/action.rb | 16 | ||||
-rw-r--r-- | lib/puppet/interface/action_builder.rb | 29 | ||||
-rw-r--r-- | lib/puppet/interface/action_manager.rb | 26 | ||||
-rw-r--r-- | lib/puppet/interface/catalog.rb | 52 | ||||
-rw-r--r-- | lib/puppet/interface/catalog/select.rb | 12 | ||||
-rw-r--r-- | lib/puppet/interface/config.rb | 6 | ||||
-rw-r--r-- | lib/puppet/interface/configurer.rb | 12 | ||||
-rw-r--r-- | lib/puppet/interface/facts.rb | 16 | ||||
-rw-r--r-- | lib/puppet/interface/indirector.rb | 28 | ||||
-rw-r--r-- | lib/puppet/interface/report.rb | 16 |
10 files changed, 138 insertions, 75 deletions
diff --git a/lib/puppet/interface/action.rb b/lib/puppet/interface/action.rb new file mode 100644 index 000000000..e4c2a4666 --- /dev/null +++ b/lib/puppet/interface/action.rb @@ -0,0 +1,16 @@ +require 'puppet/interface' + +class Puppet::Interface::Action + attr_reader :name + + def initialize(interface, name) + name = name.to_s + raise "'#{name}' is an invalid action name" unless name =~ /^[a-z]\w*$/ + @interface = interface + @name = name + end + + def invoke(*args, &block) + @interface.method(name).call(*args,&block) + end +end diff --git a/lib/puppet/interface/action_builder.rb b/lib/puppet/interface/action_builder.rb new file mode 100644 index 000000000..777fcaf85 --- /dev/null +++ b/lib/puppet/interface/action_builder.rb @@ -0,0 +1,29 @@ +require 'puppet/interface' + +class Puppet::Interface::ActionBuilder + attr_reader :action + + def self.build(interface, name, &block) + name = name.to_s + raise "Action '#{name}' must specify a block" unless block + builder = new(interface, name, &block) + builder.action + end + + def initialize(interface, name, &block) + @interface = interface + @action = Puppet::Interface::Action.new(interface, name) + instance_eval(&block) + end + + # Ideally the method we're defining here would be added to the action, and a + # method on the interface would defer to it + def invoke(&block) + raise "Invoke called on an ActionBuilder with no corresponding Action" unless @action + if @interface.is_a?(Class) + @interface.define_method(@action.name, &block) + else + @interface.meta_def(@action.name, &block) + end + end +end diff --git a/lib/puppet/interface/action_manager.rb b/lib/puppet/interface/action_manager.rb index 27a982929..8629b4cbe 100644 --- a/lib/puppet/interface/action_manager.rb +++ b/lib/puppet/interface/action_manager.rb @@ -1,32 +1,36 @@ +require 'puppet/interface/action_builder' + module Puppet::Interface::ActionManager # Declare that this app can take a specific action, and provide # the code to do so. def action(name, &block) - @actions ||= [] + @actions ||= {} name = name.to_s.downcase.to_sym + raise "Action #{name} already defined for #{self}" if action?(name) - @actions << name - if self.is_a?(Class) - define_method(name, &block) - else - meta_def(name, &block) - end + action = Puppet::Interface::ActionBuilder.build(self, name, &block) + + @actions[name] = action end def actions - @actions ||= [] - result = @actions.dup + @actions ||= {} + result = @actions.keys if self.is_a?(Class) and superclass.respond_to?(:actions) result += superclass.actions elsif self.class.respond_to?(:actions) result += self.class.actions end - result.sort { |a,b| a.to_s <=> b.to_s } + result.sort + end + + def get_action(name) + @actions[name].dup end def action?(name) - actions.include?(name.to_sym) + actions.include?(name) end end diff --git a/lib/puppet/interface/catalog.rb b/lib/puppet/interface/catalog.rb index f99d0881a..6c235e2d2 100644 --- a/lib/puppet/interface/catalog.rb +++ b/lib/puppet/interface/catalog.rb @@ -1,36 +1,40 @@ require 'puppet/interface/indirector' Puppet::Interface::Indirector.new(:catalog) do - action(:apply) do |catalog| - report = Puppet::Transaction::Report.new("apply") - report.configuration_version = catalog.version + action(:apply) do + invoke do |catalog| + report = Puppet::Transaction::Report.new("apply") + report.configuration_version = catalog.version - Puppet::Util::Log.newdestination(report) + Puppet::Util::Log.newdestination(report) - begin - benchmark(:notice, "Finished catalog run") do - catalog.apply(:report => report) + begin + benchmark(:notice, "Finished catalog run") do + catalog.apply(:report => report) + end + rescue => detail + puts detail.backtrace if Puppet[:trace] + Puppet.err "Failed to apply catalog: #{detail}" end - rescue => detail - puts detail.backtrace if Puppet[:trace] - Puppet.err "Failed to apply catalog: #{detail}" - end - report.finalize_report - report + report.finalize_report + report + end end - action(:download) do |certname,facts| - Puppet::Resource::Catalog.terminus_class = :rest - 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) + action(:download) do + invoke do |certname,facts| + Puppet::Resource::Catalog.terminus_class = :rest + 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) + end + catalog = catalog.to_ral + catalog.finalize + catalog.retrieval_duration = retrieval_duration + catalog.write_class_file + catalog end - catalog = catalog.to_ral - catalog.finalize - catalog.retrieval_duration = retrieval_duration - catalog.write_class_file - catalog end end diff --git a/lib/puppet/interface/catalog/select.rb b/lib/puppet/interface/catalog/select.rb index 4bb49315c..349d9c5e0 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::Catalog.action :select do + invoke 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/config.rb b/lib/puppet/interface/config.rb index 501099a64..0aecc263f 100644 --- a/lib/puppet/interface/config.rb +++ b/lib/puppet/interface/config.rb @@ -1,10 +1,10 @@ require 'puppet/interface' Puppet::Interface.new(:config) do - action(:print) do |*args| - if name + action(:print) do + invoke do |*args| Puppet.settings[:configprint] = args.join(",") + Puppet.settings.print_config_options end - Puppet.settings.print_config_options end end diff --git a/lib/puppet/interface/configurer.rb b/lib/puppet/interface/configurer.rb index 42e950fa3..36953baac 100644 --- a/lib/puppet/interface/configurer.rb +++ b/lib/puppet/interface/configurer.rb @@ -1,13 +1,15 @@ require 'puppet/interface' Puppet::Interface.new(:configurer) do - action(:synchronize) do |certname| - facts = Puppet::Interface::Facts.find(certname) + action(:synchronize) do + invoke do |certname| + facts = Puppet::Interface::Facts.find(certname) - catalog = Puppet::Interface::Catalog.download(certname, facts) + catalog = Puppet::Interface::Catalog.download(certname, facts) - report = Puppet::Interface::Catalog.apply(catalog) + report = Puppet::Interface::Catalog.apply(catalog) - report + report + end end end diff --git a/lib/puppet/interface/facts.rb b/lib/puppet/interface/facts.rb index 326274545..8843d297d 100644 --- a/lib/puppet/interface/facts.rb +++ b/lib/puppet/interface/facts.rb @@ -5,12 +5,14 @@ Puppet::Interface::Indirector.new(:facts) do set_default_format :yaml # Upload our facts to the server - action(:upload) do |*args| - Puppet::Node::Facts.indirection.terminus_class = :facter - facts = Puppet::Node::Facts.indirection.find(Puppet[:certname]) - Puppet::Node::Facts.indirection.terminus_class = :rest - Puppet::Node::Facts.indirection.save(facts) - Puppet.notice "Uploaded facts for '#{Puppet[:certname]}'" - nil + action(:upload) do + invoke do |*args| + Puppet::Node::Facts.indirection.terminus_class = :facter + facts = Puppet::Node::Facts.indirection.find(Puppet[:certname]) + Puppet::Node::Facts.indirection.terminus_class = :rest + Puppet::Node::Facts.indirection.save(facts) + Puppet.notice "Uploaded facts for '#{Puppet[:certname]}'" + nil + end end end diff --git a/lib/puppet/interface/indirector.rb b/lib/puppet/interface/indirector.rb index 9c26cc37a..485af4779 100644 --- a/lib/puppet/interface/indirector.rb +++ b/lib/puppet/interface/indirector.rb @@ -10,28 +10,30 @@ class Puppet::Interface::Indirector < Puppet::Interface Puppet::Indirector::Terminus.terminus_classes(indirection.to_sym).collect { |t| t.to_s }.sort end - action :destroy do |*args| - call_indirection_method(:destroy, *args) + action :destroy do + invoke { |*args| call_indirection_method(:destroy, *args) } end - action :find do |*args| - call_indirection_method(:find, *args) + action :find do + invoke { |*args| call_indirection_method(:find, *args) } end - action :save do |*args| - call_indirection_method(:save, *args) + action :save do + invoke { |*args| call_indirection_method(:save, *args) } end - action :search do |*args| - call_indirection_method(:search, *args) + action :search do + invoke { |*args| call_indirection_method(:search, *args) } end # Print the configuration for the current terminus class - action :info 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}'" + action :info do + invoke 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 diff --git a/lib/puppet/interface/report.rb b/lib/puppet/interface/report.rb index 4923a4b67..e785ae22d 100644 --- a/lib/puppet/interface/report.rb +++ b/lib/puppet/interface/report.rb @@ -1,13 +1,15 @@ require 'puppet/interface/indirector' Puppet::Interface::Indirector.new(:report) do - action(:submit) do |report| - begin - Puppet::Transaction::Report.terminus_class = :rest - report.save - rescue => detail - puts detail.backtrace if Puppet[:trace] - Puppet.err "Could not send report: #{detail}" + action(:submit) do + invoke do |report| + begin + Puppet::Transaction::Report.terminus_class = :rest + report.save + rescue => detail + puts detail.backtrace if Puppet[:trace] + Puppet.err "Could not send report: #{detail}" + end end end end |