From 809aebec7a54be90990b9ee5fea1f85204598f17 Mon Sep 17 00:00:00 2001 From: Luke Kanies Date: Sun, 30 Jan 2011 17:33:28 -0800 Subject: Moving data executables to their own module Signed-off-by: Luke Kanies --- lib/puppet/interface/catalog.rb | 4 ++++ lib/puppet/interface/catalog/select.rb | 4 ++++ lib/puppet/interface/certificate.rb | 4 ++++ lib/puppet/interface/certificate_request.rb | 4 ++++ lib/puppet/interface/certificate_revocation_list.rb | 4 ++++ lib/puppet/interface/facts.rb | 10 ++++++++++ lib/puppet/interface/file_bucket_file.rb | 4 ++++ lib/puppet/interface/inventory.rb | 4 ++++ lib/puppet/interface/key.rb | 4 ++++ lib/puppet/interface/node.rb | 4 ++++ lib/puppet/interface/report.rb | 4 ++++ lib/puppet/interface/resource_type.rb | 4 ++++ lib/puppet/interface/status.rb | 4 ++++ 13 files changed, 58 insertions(+) create mode 100644 lib/puppet/interface/catalog.rb create mode 100644 lib/puppet/interface/catalog/select.rb create mode 100644 lib/puppet/interface/certificate.rb create mode 100644 lib/puppet/interface/certificate_request.rb create mode 100644 lib/puppet/interface/certificate_revocation_list.rb create mode 100644 lib/puppet/interface/facts.rb create mode 100644 lib/puppet/interface/file_bucket_file.rb create mode 100644 lib/puppet/interface/inventory.rb create mode 100644 lib/puppet/interface/key.rb create mode 100644 lib/puppet/interface/node.rb create mode 100644 lib/puppet/interface/report.rb create mode 100644 lib/puppet/interface/resource_type.rb create mode 100644 lib/puppet/interface/status.rb (limited to 'lib/puppet/interface') diff --git a/lib/puppet/interface/catalog.rb b/lib/puppet/interface/catalog.rb new file mode 100644 index 000000000..23e2b9cf5 --- /dev/null +++ b/lib/puppet/interface/catalog.rb @@ -0,0 +1,4 @@ +require 'puppet/interface' + +class Puppet::Interface::Catalog < Puppet::Interface +end diff --git a/lib/puppet/interface/catalog/select.rb b/lib/puppet/interface/catalog/select.rb new file mode 100644 index 000000000..6311a4a74 --- /dev/null +++ b/lib/puppet/interface/catalog/select.rb @@ -0,0 +1,4 @@ +# Select and show a list of resources of a given type. +Puppet::Interface::Catalog.action :select do |*args| + puts "Selecting #{args.inspect}" +end diff --git a/lib/puppet/interface/certificate.rb b/lib/puppet/interface/certificate.rb new file mode 100644 index 000000000..51e46c46b --- /dev/null +++ b/lib/puppet/interface/certificate.rb @@ -0,0 +1,4 @@ +require 'puppet/interface' + +class Puppet::Interface::Certificate < Puppet::Interface +end diff --git a/lib/puppet/interface/certificate_request.rb b/lib/puppet/interface/certificate_request.rb new file mode 100644 index 000000000..30ba5583a --- /dev/null +++ b/lib/puppet/interface/certificate_request.rb @@ -0,0 +1,4 @@ +require 'puppet/interface' + +class Puppet::Interface::Certificate_request < Puppet::Interface +end diff --git a/lib/puppet/interface/certificate_revocation_list.rb b/lib/puppet/interface/certificate_revocation_list.rb new file mode 100644 index 000000000..55a693918 --- /dev/null +++ b/lib/puppet/interface/certificate_revocation_list.rb @@ -0,0 +1,4 @@ +require 'puppet/interface' + +class Puppet::Interface::Certificate_revocation_list < Puppet::Interface +end diff --git a/lib/puppet/interface/facts.rb b/lib/puppet/interface/facts.rb new file mode 100644 index 000000000..411416710 --- /dev/null +++ b/lib/puppet/interface/facts.rb @@ -0,0 +1,10 @@ +require 'puppet/interface' + +class Puppet::Interface::Facts < Puppet::Interface + # Upload our facts to the server + action(:upload) do |*args| + Puppet::Node::Facts.indirection.terminus_class = :facter + Puppet::Node::Facts.indirection.cache_class = :rest + Puppet::Node::Facts.indirection.find(Puppet[:certname]) + end +end diff --git a/lib/puppet/interface/file_bucket_file.rb b/lib/puppet/interface/file_bucket_file.rb new file mode 100644 index 000000000..f34ebc4c4 --- /dev/null +++ b/lib/puppet/interface/file_bucket_file.rb @@ -0,0 +1,4 @@ +require 'puppet/interface' + +class Puppet::Interface::File_bucket_file < Puppet::Interface +end diff --git a/lib/puppet/interface/inventory.rb b/lib/puppet/interface/inventory.rb new file mode 100644 index 000000000..7521239d5 --- /dev/null +++ b/lib/puppet/interface/inventory.rb @@ -0,0 +1,4 @@ +require 'puppet/interface' + +class Puppet::Interface::Inventory < Puppet::Interface +end diff --git a/lib/puppet/interface/key.rb b/lib/puppet/interface/key.rb new file mode 100644 index 000000000..38f92c66b --- /dev/null +++ b/lib/puppet/interface/key.rb @@ -0,0 +1,4 @@ +require 'puppet/interface' + +class Puppet::Interface::Key < Puppet::Interface +end diff --git a/lib/puppet/interface/node.rb b/lib/puppet/interface/node.rb new file mode 100644 index 000000000..68e30698e --- /dev/null +++ b/lib/puppet/interface/node.rb @@ -0,0 +1,4 @@ +require 'puppet/interface' + +class Puppet::Interface::Node < Puppet::Interface +end diff --git a/lib/puppet/interface/report.rb b/lib/puppet/interface/report.rb new file mode 100644 index 000000000..72f1285ea --- /dev/null +++ b/lib/puppet/interface/report.rb @@ -0,0 +1,4 @@ +require 'puppet/interface' + +class Puppet::Interface::Report < Puppet::Interface +end diff --git a/lib/puppet/interface/resource_type.rb b/lib/puppet/interface/resource_type.rb new file mode 100644 index 000000000..619a4914b --- /dev/null +++ b/lib/puppet/interface/resource_type.rb @@ -0,0 +1,4 @@ +require 'puppet/interface' + +class Puppet::Interface::Resource_type < Puppet::Interface +end diff --git a/lib/puppet/interface/status.rb b/lib/puppet/interface/status.rb new file mode 100644 index 000000000..cdb1623ac --- /dev/null +++ b/lib/puppet/interface/status.rb @@ -0,0 +1,4 @@ +require 'puppet/interface' + +class Puppet::Interface::Status < Puppet::Interface +end -- cgit From efca35cbea836fac954fb655d76493f03b36e96f Mon Sep 17 00:00:00 2001 From: Luke Kanies Date: Tue, 8 Feb 2011 16:24:30 -0800 Subject: Finishing migration from puppet repo The whole system seems to work again, as long as you run it against 2.6.next. Signed-off-by: Luke Kanies --- lib/puppet/interface/catalog/select.rb | 6 +++++- lib/puppet/interface/resource.rb | 4 ++++ 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 lib/puppet/interface/resource.rb (limited to 'lib/puppet/interface') diff --git a/lib/puppet/interface/catalog/select.rb b/lib/puppet/interface/catalog/select.rb index 6311a4a74..4bb49315c 100644 --- a/lib/puppet/interface/catalog/select.rb +++ b/lib/puppet/interface/catalog/select.rb @@ -1,4 +1,8 @@ # Select and show a list of resources of a given type. Puppet::Interface::Catalog.action :select do |*args| - puts "Selecting #{args.inspect}" + host = args.shift + type = args.shift + catalog = Puppet::Resource::Catalog.indirection.find(host) + + catalog.resources.reject { |res| res.type != type }.each { |res| puts res } end diff --git a/lib/puppet/interface/resource.rb b/lib/puppet/interface/resource.rb new file mode 100644 index 000000000..b9b007d00 --- /dev/null +++ b/lib/puppet/interface/resource.rb @@ -0,0 +1,4 @@ +require 'puppet/interface' + +class Puppet::Interface::Resource < Puppet::Interface +end -- cgit From 3ffb9abd3a500f1fb3246e04f737b79d232c082d Mon Sep 17 00:00:00 2001 From: Luke Kanies Date: Tue, 8 Feb 2011 21:27:24 -0800 Subject: Moving 'format' support to the application This allows easier use of the Interfaces in ruby. Signed-off-by: Luke Kanies --- lib/puppet/interface/facts.rb | 2 ++ 1 file changed, 2 insertions(+) (limited to 'lib/puppet/interface') diff --git a/lib/puppet/interface/facts.rb b/lib/puppet/interface/facts.rb index 411416710..e40bb56d0 100644 --- a/lib/puppet/interface/facts.rb +++ b/lib/puppet/interface/facts.rb @@ -1,6 +1,8 @@ require 'puppet/interface' class Puppet::Interface::Facts < Puppet::Interface + set_default_format :yaml + # Upload our facts to the server action(:upload) do |*args| Puppet::Node::Facts.indirection.terminus_class = :facter -- cgit From b3f903af34c0e27dccb1d043d84c35ea68f44830 Mon Sep 17 00:00:00 2001 From: Luke Kanies Date: Wed, 16 Feb 2011 00:44:44 -0800 Subject: Enabling arbitrary interface names Previously the app, indirection, and interface names had to match exactly; now they can be arbitrary by just defining an overriding 'indirection_name' class method on the interface. I also renamed the file_bucket_file classes accordingly. Signed-off-by: Luke Kanies --- lib/puppet/interface/file.rb | 7 +++++++ lib/puppet/interface/file_bucket_file.rb | 4 ---- 2 files changed, 7 insertions(+), 4 deletions(-) create mode 100644 lib/puppet/interface/file.rb delete mode 100644 lib/puppet/interface/file_bucket_file.rb (limited to 'lib/puppet/interface') diff --git a/lib/puppet/interface/file.rb b/lib/puppet/interface/file.rb new file mode 100644 index 000000000..53c476d7c --- /dev/null +++ b/lib/puppet/interface/file.rb @@ -0,0 +1,7 @@ +require 'puppet/interface' + +class Puppet::Interface::File < Puppet::Interface + def self.indirection_name + :file_bucket_file + end +end diff --git a/lib/puppet/interface/file_bucket_file.rb b/lib/puppet/interface/file_bucket_file.rb deleted file mode 100644 index f34ebc4c4..000000000 --- a/lib/puppet/interface/file_bucket_file.rb +++ /dev/null @@ -1,4 +0,0 @@ -require 'puppet/interface' - -class Puppet::Interface::File_bucket_file < Puppet::Interface -end -- cgit From a54ee1e292238145bb0def2af6cf9ac22f2acd68 Mon Sep 17 00:00:00 2001 From: Dan Bode Date: Sun, 13 Feb 2011 02:55:42 -0600 Subject: (#2) Should not assume interfaces have indirectors The initial work assumed that all interfaces were just skins on an indirected data type, but some interfaces will be more abstract than that. This commit removes that assumption by extracting all of the indirector work into a new Indirector subclass of Interface and then makes all of the new interfaces a subclass of that rather than of Interface itself. --- lib/puppet/interface/catalog.rb | 4 +- lib/puppet/interface/certificate.rb | 4 +- lib/puppet/interface/certificate_request.rb | 4 +- .../interface/certificate_revocation_list.rb | 4 +- lib/puppet/interface/facts.rb | 4 +- lib/puppet/interface/file.rb | 4 +- lib/puppet/interface/indirector.rb | 82 ++++++++++++++++++++++ lib/puppet/interface/inventory.rb | 4 +- lib/puppet/interface/key.rb | 4 +- lib/puppet/interface/node.rb | 4 +- lib/puppet/interface/report.rb | 4 +- lib/puppet/interface/resource.rb | 4 +- lib/puppet/interface/resource_type.rb | 4 +- lib/puppet/interface/status.rb | 4 +- 14 files changed, 108 insertions(+), 26 deletions(-) create mode 100644 lib/puppet/interface/indirector.rb (limited to 'lib/puppet/interface') diff --git a/lib/puppet/interface/catalog.rb b/lib/puppet/interface/catalog.rb index 23e2b9cf5..85aa2f37a 100644 --- a/lib/puppet/interface/catalog.rb +++ b/lib/puppet/interface/catalog.rb @@ -1,4 +1,4 @@ -require 'puppet/interface' +require 'puppet/interface/indirector' -class Puppet::Interface::Catalog < Puppet::Interface +class Puppet::Interface::Catalog < Puppet::Interface::Indirector end diff --git a/lib/puppet/interface/certificate.rb b/lib/puppet/interface/certificate.rb index 51e46c46b..48ca2c20f 100644 --- a/lib/puppet/interface/certificate.rb +++ b/lib/puppet/interface/certificate.rb @@ -1,4 +1,4 @@ -require 'puppet/interface' +require 'puppet/interface/indirector' -class Puppet::Interface::Certificate < Puppet::Interface +class Puppet::Interface::Certificate < Puppet::Interface::Indirector end diff --git a/lib/puppet/interface/certificate_request.rb b/lib/puppet/interface/certificate_request.rb index 30ba5583a..29dc73b9a 100644 --- a/lib/puppet/interface/certificate_request.rb +++ b/lib/puppet/interface/certificate_request.rb @@ -1,4 +1,4 @@ -require 'puppet/interface' +require 'puppet/interface/indirector' -class Puppet::Interface::Certificate_request < Puppet::Interface +class Puppet::Interface::Certificate_request < Puppet::Interface::Indirector end diff --git a/lib/puppet/interface/certificate_revocation_list.rb b/lib/puppet/interface/certificate_revocation_list.rb index 55a693918..144d5ef61 100644 --- a/lib/puppet/interface/certificate_revocation_list.rb +++ b/lib/puppet/interface/certificate_revocation_list.rb @@ -1,4 +1,4 @@ -require 'puppet/interface' +require 'puppet/interface/indirector' -class Puppet::Interface::Certificate_revocation_list < Puppet::Interface +class Puppet::Interface::Certificate_revocation_list < Puppet::Interface::Indirector end diff --git a/lib/puppet/interface/facts.rb b/lib/puppet/interface/facts.rb index e40bb56d0..42ba1fb81 100644 --- a/lib/puppet/interface/facts.rb +++ b/lib/puppet/interface/facts.rb @@ -1,6 +1,6 @@ -require 'puppet/interface' +require 'puppet/interface/indirector' -class Puppet::Interface::Facts < Puppet::Interface +class Puppet::Interface::Facts < Puppet::Interface::Indirector set_default_format :yaml # Upload our facts to the server diff --git a/lib/puppet/interface/file.rb b/lib/puppet/interface/file.rb index 53c476d7c..98a869153 100644 --- a/lib/puppet/interface/file.rb +++ b/lib/puppet/interface/file.rb @@ -1,6 +1,6 @@ -require 'puppet/interface' +require 'puppet/interface/indirector' -class Puppet::Interface::File < Puppet::Interface +class Puppet::Interface::File < Puppet::Interface::Indirector def self.indirection_name :file_bucket_file end diff --git a/lib/puppet/interface/indirector.rb b/lib/puppet/interface/indirector.rb new file mode 100644 index 000000000..f0beb8a9c --- /dev/null +++ b/lib/puppet/interface/indirector.rb @@ -0,0 +1,82 @@ +require 'puppet' +require 'puppet/interface' + +class Puppet::Interface::Indirector < Puppet::Interface + + + # Here's your opportunity to override the indirection name. By default + # it will be the same name as the interface. + def self.indirection_name + name.to_sym + end + + # Return an indirection associated with an interface, if one exists + # One usually does. + def self.indirection + unless @indirection + Puppet.info("Could not find terminus for #{indirection_name}") unless @indirection = Puppet::Indirector::Indirection.instance(indirection_name) + end + @indirection + end + + attr_accessor :from, :indirection + + action :destroy do |name, *args| + call_indirection_method(:destroy, name, *args) + end + + action :find do |name, *args| + call_indirection_method(:find, name, *args) + end + + action :save do |name, *args| + call_indirection_method(:save, name, *args) + end + + action :search do |name, *args| + call_indirection_method(:search, name, *args) + end + + def indirection + self.class.indirection + end + + def initialize(options = {}) + options.each { |opt, val| send(opt.to_s + "=", val) } + + Puppet::Util::Log.newdestination :console + + self.class.load_actions + 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 #{terminus_classes(indirection.name).join(", ") }" + end + end + + def call_indirection_method(method, name, *args) + begin + result = indirection.send(method, name, *args) + rescue => detail + puts detail.backtrace if Puppet[:trace] + raise "Could not call #{method} on #{type}: #{detail}" + end + + unless result + raise "Could not #{method} #{indirection.name} for #{name}" + end + + result + end + + def indirections + Puppet::Indirector::Indirection.instances.collect { |t| t.to_s }.sort + end + + def terminus_classes(indirection) + Puppet::Indirector::Terminus.terminus_classes(indirection).collect { |t| t.to_s }.sort + end +end diff --git a/lib/puppet/interface/inventory.rb b/lib/puppet/interface/inventory.rb index 7521239d5..16b216b8b 100644 --- a/lib/puppet/interface/inventory.rb +++ b/lib/puppet/interface/inventory.rb @@ -1,4 +1,4 @@ -require 'puppet/interface' +require 'puppet/interface/indirector' -class Puppet::Interface::Inventory < Puppet::Interface +class Puppet::Interface::Inventory < Puppet::Interface::Indirector end diff --git a/lib/puppet/interface/key.rb b/lib/puppet/interface/key.rb index 38f92c66b..17b661da1 100644 --- a/lib/puppet/interface/key.rb +++ b/lib/puppet/interface/key.rb @@ -1,4 +1,4 @@ -require 'puppet/interface' +require 'puppet/interface/indirector' -class Puppet::Interface::Key < Puppet::Interface +class Puppet::Interface::Key < Puppet::Interface::Indirector end diff --git a/lib/puppet/interface/node.rb b/lib/puppet/interface/node.rb index 68e30698e..5d9efa932 100644 --- a/lib/puppet/interface/node.rb +++ b/lib/puppet/interface/node.rb @@ -1,4 +1,4 @@ -require 'puppet/interface' +require 'puppet/interface/indirector' -class Puppet::Interface::Node < Puppet::Interface +class Puppet::Interface::Node < Puppet::Interface::Indirector end diff --git a/lib/puppet/interface/report.rb b/lib/puppet/interface/report.rb index 72f1285ea..fd6f45f16 100644 --- a/lib/puppet/interface/report.rb +++ b/lib/puppet/interface/report.rb @@ -1,4 +1,4 @@ -require 'puppet/interface' +require 'puppet/interface/indirector' -class Puppet::Interface::Report < Puppet::Interface +class Puppet::Interface::Report < Puppet::Interface::Indirector end diff --git a/lib/puppet/interface/resource.rb b/lib/puppet/interface/resource.rb index b9b007d00..deed0a533 100644 --- a/lib/puppet/interface/resource.rb +++ b/lib/puppet/interface/resource.rb @@ -1,4 +1,4 @@ -require 'puppet/interface' +require 'puppet/interface/indirector' -class Puppet::Interface::Resource < Puppet::Interface +class Puppet::Interface::Resource < Puppet::Interface::Indirector end diff --git a/lib/puppet/interface/resource_type.rb b/lib/puppet/interface/resource_type.rb index 619a4914b..6892926f0 100644 --- a/lib/puppet/interface/resource_type.rb +++ b/lib/puppet/interface/resource_type.rb @@ -1,4 +1,4 @@ -require 'puppet/interface' +require 'puppet/interface/indirector' -class Puppet::Interface::Resource_type < Puppet::Interface +class Puppet::Interface::Resource_type < Puppet::Interface::Indirector end diff --git a/lib/puppet/interface/status.rb b/lib/puppet/interface/status.rb index cdb1623ac..86ccab6e1 100644 --- a/lib/puppet/interface/status.rb +++ b/lib/puppet/interface/status.rb @@ -1,4 +1,4 @@ -require 'puppet/interface' +require 'puppet/interface/indirector' -class Puppet::Interface::Status < Puppet::Interface +class Puppet::Interface::Status < Puppet::Interface::Indirector end -- cgit From cde1baa4a9a27ba95ad2a61bc8e46d43e708b081 Mon Sep 17 00:00:00 2001 From: Luke Kanies Date: Sun, 20 Feb 2011 15:39:54 -0800 Subject: Fixing Interface listing It got broke when the Indirector base class was extracted. Signed-off-by: Luke Kanies --- lib/puppet/interface/indirector.rb | 2 ++ 1 file changed, 2 insertions(+) (limited to 'lib/puppet/interface') diff --git a/lib/puppet/interface/indirector.rb b/lib/puppet/interface/indirector.rb index f0beb8a9c..507826b91 100644 --- a/lib/puppet/interface/indirector.rb +++ b/lib/puppet/interface/indirector.rb @@ -3,6 +3,8 @@ require 'puppet/interface' class Puppet::Interface::Indirector < Puppet::Interface + # This is just a base class. + @abstract = true # Here's your opportunity to override the indirection name. By default # it will be the same name as the interface. -- cgit From 04fb6de5e2108799e47a081e5331d932fcf53109 Mon Sep 17 00:00:00 2001 From: Luke Kanies Date: Tue, 22 Feb 2011 11:59:19 -0800 Subject: Switching Interfaces to be instances They were previously classes, which made a lot of things stupider than they needed to be. This will likely involve some porting, but not much. Signed-off-by: Luke Kanies --- lib/puppet/interface/action_manager.rb | 32 ++++++++++++ lib/puppet/interface/catalog.rb | 2 +- lib/puppet/interface/certificate.rb | 2 +- lib/puppet/interface/certificate_request.rb | 2 +- .../interface/certificate_revocation_list.rb | 2 +- lib/puppet/interface/facts.rb | 3 +- lib/puppet/interface/file.rb | 6 +-- lib/puppet/interface/indirector.rb | 58 ++++++++-------------- lib/puppet/interface/inventory.rb | 2 +- lib/puppet/interface/key.rb | 2 +- lib/puppet/interface/node.rb | 2 +- lib/puppet/interface/report.rb | 2 +- lib/puppet/interface/resource.rb | 2 +- lib/puppet/interface/resource_type.rb | 2 +- lib/puppet/interface/status.rb | 2 +- 15 files changed, 68 insertions(+), 53 deletions(-) create mode 100644 lib/puppet/interface/action_manager.rb (limited to 'lib/puppet/interface') diff --git a/lib/puppet/interface/action_manager.rb b/lib/puppet/interface/action_manager.rb new file mode 100644 index 000000000..27a982929 --- /dev/null +++ b/lib/puppet/interface/action_manager.rb @@ -0,0 +1,32 @@ +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 ||= [] + 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 + end + + def actions + @actions ||= [] + result = @actions.dup + + 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 } + end + + def action?(name) + actions.include?(name.to_sym) + end +end diff --git a/lib/puppet/interface/catalog.rb b/lib/puppet/interface/catalog.rb index 85aa2f37a..b2ed08f92 100644 --- a/lib/puppet/interface/catalog.rb +++ b/lib/puppet/interface/catalog.rb @@ -1,4 +1,4 @@ require 'puppet/interface/indirector' -class Puppet::Interface::Catalog < Puppet::Interface::Indirector +Puppet::Interface::Indirector.new(:catalog) do end diff --git a/lib/puppet/interface/certificate.rb b/lib/puppet/interface/certificate.rb index 48ca2c20f..52ba4e3b8 100644 --- a/lib/puppet/interface/certificate.rb +++ b/lib/puppet/interface/certificate.rb @@ -1,4 +1,4 @@ require 'puppet/interface/indirector' -class Puppet::Interface::Certificate < Puppet::Interface::Indirector +Puppet::Interface::Indirector.new(:certificate) do end diff --git a/lib/puppet/interface/certificate_request.rb b/lib/puppet/interface/certificate_request.rb index 29dc73b9a..77b485f8e 100644 --- a/lib/puppet/interface/certificate_request.rb +++ b/lib/puppet/interface/certificate_request.rb @@ -1,4 +1,4 @@ require 'puppet/interface/indirector' -class Puppet::Interface::Certificate_request < Puppet::Interface::Indirector +Puppet::Interface::Indirector.new(:certificate_request) do end diff --git a/lib/puppet/interface/certificate_revocation_list.rb b/lib/puppet/interface/certificate_revocation_list.rb index 144d5ef61..ee1e6a8c4 100644 --- a/lib/puppet/interface/certificate_revocation_list.rb +++ b/lib/puppet/interface/certificate_revocation_list.rb @@ -1,4 +1,4 @@ require 'puppet/interface/indirector' -class Puppet::Interface::Certificate_revocation_list < Puppet::Interface::Indirector +Puppet::Interface::Indirector.new(:certificate_revocation_list) do end diff --git a/lib/puppet/interface/facts.rb b/lib/puppet/interface/facts.rb index 42ba1fb81..7b269e0d0 100644 --- a/lib/puppet/interface/facts.rb +++ b/lib/puppet/interface/facts.rb @@ -1,6 +1,7 @@ require 'puppet/interface/indirector' +require 'puppet/node/facts' -class Puppet::Interface::Facts < Puppet::Interface::Indirector +Puppet::Interface::Indirector.new(:facts) do set_default_format :yaml # Upload our facts to the server diff --git a/lib/puppet/interface/file.rb b/lib/puppet/interface/file.rb index 98a869153..9060c4042 100644 --- a/lib/puppet/interface/file.rb +++ b/lib/puppet/interface/file.rb @@ -1,7 +1,5 @@ require 'puppet/interface/indirector' -class Puppet::Interface::File < Puppet::Interface::Indirector - def self.indirection_name - :file_bucket_file - end +class Puppet::Interface::Indirector.new(:file) do + set_indirection_name :file_bucket_file end diff --git a/lib/puppet/interface/indirector.rb b/lib/puppet/interface/indirector.rb index 507826b91..feb356d85 100644 --- a/lib/puppet/interface/indirector.rb +++ b/lib/puppet/interface/indirector.rb @@ -2,27 +2,14 @@ require 'puppet' require 'puppet/interface' class Puppet::Interface::Indirector < Puppet::Interface - - # This is just a base class. - @abstract = true - - # Here's your opportunity to override the indirection name. By default - # it will be the same name as the interface. - def self.indirection_name - name.to_sym + def self.indirections + Puppet::Indirector::Indirection.instances.collect { |t| t.to_s }.sort end - # Return an indirection associated with an interface, if one exists - # One usually does. - def self.indirection - unless @indirection - Puppet.info("Could not find terminus for #{indirection_name}") unless @indirection = Puppet::Indirector::Indirection.instance(indirection_name) - end - @indirection + def self.terminus_classes(indirection) + Puppet::Indirector::Terminus.terminus_classes(indirection.to_sym).collect { |t| t.to_s }.sort end - attr_accessor :from, :indirection - action :destroy do |name, *args| call_indirection_method(:destroy, name, *args) end @@ -39,16 +26,25 @@ class Puppet::Interface::Indirector < Puppet::Interface call_indirection_method(:search, name, *args) end - def indirection - self.class.indirection - end + attr_accessor :from - def initialize(options = {}) - options.each { |opt, val| send(opt.to_s + "=", val) } + def indirection_name + @indirection_name || name.to_sym + end - Puppet::Util::Log.newdestination :console + # Here's your opportunity to override the indirection name. By default + # it will be the same name as the interface. + def set_indirection_name(name) + @indirection_name = name + end - self.class.load_actions + # Return an indirection associated with an interface, if one exists + # One usually does. + def indirection + unless @indirection + Puppet.info("Could not find terminus for #{indirection_name}") unless @indirection = Puppet::Indirector::Indirection.instance(indirection_name) + end + @indirection end def set_terminus(from) @@ -64,21 +60,9 @@ class Puppet::Interface::Indirector < Puppet::Interface result = indirection.send(method, name, *args) rescue => detail puts detail.backtrace if Puppet[:trace] - raise "Could not call #{method} on #{type}: #{detail}" - end - - unless result - raise "Could not #{method} #{indirection.name} for #{name}" + raise "Could not call '#{method}' on '#{indirection_name}': #{detail}" end result end - - def indirections - Puppet::Indirector::Indirection.instances.collect { |t| t.to_s }.sort - end - - def terminus_classes(indirection) - Puppet::Indirector::Terminus.terminus_classes(indirection).collect { |t| t.to_s }.sort - end end diff --git a/lib/puppet/interface/inventory.rb b/lib/puppet/interface/inventory.rb index 16b216b8b..9b597c6ae 100644 --- a/lib/puppet/interface/inventory.rb +++ b/lib/puppet/interface/inventory.rb @@ -1,4 +1,4 @@ require 'puppet/interface/indirector' -class Puppet::Interface::Inventory < Puppet::Interface::Indirector +Puppet::Interface::Indirector.new(:inventory) do end diff --git a/lib/puppet/interface/key.rb b/lib/puppet/interface/key.rb index 17b661da1..9343891d0 100644 --- a/lib/puppet/interface/key.rb +++ b/lib/puppet/interface/key.rb @@ -1,4 +1,4 @@ require 'puppet/interface/indirector' -class Puppet::Interface::Key < Puppet::Interface::Indirector +Puppet::Interface::Indirector.new(:key) do end diff --git a/lib/puppet/interface/node.rb b/lib/puppet/interface/node.rb index 5d9efa932..7d7362d8b 100644 --- a/lib/puppet/interface/node.rb +++ b/lib/puppet/interface/node.rb @@ -1,4 +1,4 @@ require 'puppet/interface/indirector' -class Puppet::Interface::Node < Puppet::Interface::Indirector +Puppet::Interface::Indirector.new(:node) do end diff --git a/lib/puppet/interface/report.rb b/lib/puppet/interface/report.rb index fd6f45f16..e7b916527 100644 --- a/lib/puppet/interface/report.rb +++ b/lib/puppet/interface/report.rb @@ -1,4 +1,4 @@ require 'puppet/interface/indirector' -class Puppet::Interface::Report < Puppet::Interface::Indirector +Puppet::Interface::Indirector.new(:report) do end diff --git a/lib/puppet/interface/resource.rb b/lib/puppet/interface/resource.rb index deed0a533..65f2dec7a 100644 --- a/lib/puppet/interface/resource.rb +++ b/lib/puppet/interface/resource.rb @@ -1,4 +1,4 @@ require 'puppet/interface/indirector' -class Puppet::Interface::Resource < Puppet::Interface::Indirector +Puppet::Interface::Indirector.new(:resource) do end diff --git a/lib/puppet/interface/resource_type.rb b/lib/puppet/interface/resource_type.rb index 6892926f0..bf16652a8 100644 --- a/lib/puppet/interface/resource_type.rb +++ b/lib/puppet/interface/resource_type.rb @@ -1,4 +1,4 @@ require 'puppet/interface/indirector' -class Puppet::Interface::Resource_type < Puppet::Interface::Indirector +Puppet::Interface::Indirector.new(:resource_type) do end diff --git a/lib/puppet/interface/status.rb b/lib/puppet/interface/status.rb index 86ccab6e1..1a1d349d1 100644 --- a/lib/puppet/interface/status.rb +++ b/lib/puppet/interface/status.rb @@ -1,4 +1,4 @@ require 'puppet/interface/indirector' -class Puppet::Interface::Status < Puppet::Interface::Indirector +Puppet::Interface::Indirector.new(:status) do end -- cgit From c2715c0f20d916de0284e2d161eb5de32e508244 Mon Sep 17 00:00:00 2001 From: Luke Kanies Date: Tue, 22 Feb 2011 17:13:52 -0800 Subject: Splitting the Application base class We now have an indirection_base class along with interface_base. I've also added some basic tests for most of the interfaces. Signed-off-by: Luke Kanies --- lib/puppet/interface/file.rb | 2 +- lib/puppet/interface/inventory.rb | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-) delete mode 100644 lib/puppet/interface/inventory.rb (limited to 'lib/puppet/interface') diff --git a/lib/puppet/interface/file.rb b/lib/puppet/interface/file.rb index 9060c4042..859f92ca4 100644 --- a/lib/puppet/interface/file.rb +++ b/lib/puppet/interface/file.rb @@ -1,5 +1,5 @@ require 'puppet/interface/indirector' -class Puppet::Interface::Indirector.new(:file) do +Puppet::Interface::Indirector.new(:file) do set_indirection_name :file_bucket_file end diff --git a/lib/puppet/interface/inventory.rb b/lib/puppet/interface/inventory.rb deleted file mode 100644 index 9b597c6ae..000000000 --- a/lib/puppet/interface/inventory.rb +++ /dev/null @@ -1,4 +0,0 @@ -require 'puppet/interface/indirector' - -Puppet::Interface::Indirector.new(:inventory) do -end -- cgit From 368210e8a8a35cf2cae509b1d357337f9958cdff Mon Sep 17 00:00:00 2001 From: Luke Kanies Date: Tue, 22 Feb 2011 17:38:04 -0800 Subject: Adding a simple "config" app Signed-off-by: Luke Kanies --- lib/puppet/interface/config.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 lib/puppet/interface/config.rb (limited to 'lib/puppet/interface') diff --git a/lib/puppet/interface/config.rb b/lib/puppet/interface/config.rb new file mode 100644 index 000000000..501099a64 --- /dev/null +++ b/lib/puppet/interface/config.rb @@ -0,0 +1,10 @@ +require 'puppet/interface' + +Puppet::Interface.new(:config) do + action(:print) do |*args| + if name + Puppet.settings[:configprint] = args.join(",") + end + Puppet.settings.print_config_options + end +end -- cgit From 59a648502a8f09948bd2d25a72a9099f7740e108 Mon Sep 17 00:00:00 2001 From: Luke Kanies Date: Wed, 23 Feb 2011 00:20:15 -0800 Subject: Adding Application options to Interfaces This allows all of the actions to react to the CLI options. I've also removed the unnecessary 'name' variables I was using in various places - they were just the first of the arguments, and they weren't actually always names. Signed-off-by: Luke Kanies --- lib/puppet/interface/indirector.rb | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'lib/puppet/interface') diff --git a/lib/puppet/interface/indirector.rb b/lib/puppet/interface/indirector.rb index feb356d85..f106db4b8 100644 --- a/lib/puppet/interface/indirector.rb +++ b/lib/puppet/interface/indirector.rb @@ -10,20 +10,20 @@ class Puppet::Interface::Indirector < Puppet::Interface Puppet::Indirector::Terminus.terminus_classes(indirection.to_sym).collect { |t| t.to_s }.sort end - action :destroy do |name, *args| - call_indirection_method(:destroy, name, *args) + action :destroy do |*args| + call_indirection_method(:destroy, *args) end - action :find do |name, *args| - call_indirection_method(:find, name, *args) + action :find do |*args| + call_indirection_method(:find, *args) end - action :save do |name, *args| - call_indirection_method(:save, name, *args) + action :save do |*args| + call_indirection_method(:save, *args) end - action :search do |name, *args| - call_indirection_method(:search, name, *args) + action :search do |*args| + call_indirection_method(:search, *args) end attr_accessor :from @@ -55,9 +55,9 @@ class Puppet::Interface::Indirector < Puppet::Interface end end - def call_indirection_method(method, name, *args) + def call_indirection_method(method, *args) begin - result = indirection.send(method, name, *args) + result = indirection.send(method, *args) rescue => detail puts detail.backtrace if Puppet[:trace] raise "Could not call '#{method}' on '#{indirection_name}': #{detail}" -- cgit From 63263a41ab361985845ef514a3d1247a41f46475 Mon Sep 17 00:00:00 2001 From: Luke Kanies Date: Wed, 2 Mar 2011 17:19:36 -0800 Subject: Fixing #13 - showconfig moved to indirector I renamed it to 'info', too. It only showed indirector-related info, so this makes sense. Signed-off-by: Luke Kanies --- lib/puppet/interface/indirector.rb | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'lib/puppet/interface') diff --git a/lib/puppet/interface/indirector.rb b/lib/puppet/interface/indirector.rb index f106db4b8..9c26cc37a 100644 --- a/lib/puppet/interface/indirector.rb +++ b/lib/puppet/interface/indirector.rb @@ -26,6 +26,15 @@ class Puppet::Interface::Indirector < Puppet::Interface 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}'" + end + end + attr_accessor :from def indirection_name -- cgit From ece0c8e8defeec7af5aa28bb583bbb69aaba79a9 Mon Sep 17 00:00:00 2001 From: Luke Kanies Date: Wed, 2 Mar 2011 17:32:07 -0800 Subject: Fixing #16 - nodes default to yaml We don't have json support for node output yet. Signed-off-by: Luke Kanies --- lib/puppet/interface/node.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'lib/puppet/interface') diff --git a/lib/puppet/interface/node.rb b/lib/puppet/interface/node.rb index 7d7362d8b..0a0f57a1e 100644 --- a/lib/puppet/interface/node.rb +++ b/lib/puppet/interface/node.rb @@ -1,4 +1,5 @@ require 'puppet/interface/indirector' Puppet::Interface::Indirector.new(:node) do + set_default_format :yaml end -- cgit From f67e7fa39479751a7c5268bd32e503e35602ce4f Mon Sep 17 00:00:00 2001 From: Luke Kanies Date: Wed, 2 Mar 2011 18:16:41 -0800 Subject: Modifying Facts.upload a bit The functionality is basically the same, but we're no longer using caching, and we log that it happened. Signed-off-by: Luke Kanies --- lib/puppet/interface/facts.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'lib/puppet/interface') diff --git a/lib/puppet/interface/facts.rb b/lib/puppet/interface/facts.rb index 7b269e0d0..326274545 100644 --- a/lib/puppet/interface/facts.rb +++ b/lib/puppet/interface/facts.rb @@ -7,7 +7,10 @@ Puppet::Interface::Indirector.new(:facts) do # Upload our facts to the server action(:upload) do |*args| Puppet::Node::Facts.indirection.terminus_class = :facter - Puppet::Node::Facts.indirection.cache_class = :rest - Puppet::Node::Facts.indirection.find(Puppet[:certname]) + 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 -- cgit From 63f33d078429a9f589474f9c0778b21d82f38682 Mon Sep 17 00:00:00 2001 From: Nick Lewis Date: Mon, 21 Mar 2011 14:21:53 -0700 Subject: (#6805) Add a "configurer" application This application is similar in basic functionality to the "agent" application, but implemented in terms of interfaces. It currently will retrieve facts, retrieve a catalog, apply the catalog, and submit a report. Options such as noop and daemonize are still to come. Reviewed-By: Pieter van de Bruggen --- lib/puppet/interface/catalog.rb | 32 ++++++++++++++++++++++++++++++++ lib/puppet/interface/configurer.rb | 13 +++++++++++++ lib/puppet/interface/report.rb | 9 +++++++++ 3 files changed, 54 insertions(+) create mode 100644 lib/puppet/interface/configurer.rb (limited to 'lib/puppet/interface') diff --git a/lib/puppet/interface/catalog.rb b/lib/puppet/interface/catalog.rb index b2ed08f92..f99d0881a 100644 --- a/lib/puppet/interface/catalog.rb +++ b/lib/puppet/interface/catalog.rb @@ -1,4 +1,36 @@ 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 + + Puppet::Util::Log.newdestination(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 + + report.finalize_report + report + 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) + 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/configurer.rb b/lib/puppet/interface/configurer.rb new file mode 100644 index 000000000..42e950fa3 --- /dev/null +++ b/lib/puppet/interface/configurer.rb @@ -0,0 +1,13 @@ +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) + + report + end +end diff --git a/lib/puppet/interface/report.rb b/lib/puppet/interface/report.rb index e7b916527..4923a4b67 100644 --- a/lib/puppet/interface/report.rb +++ b/lib/puppet/interface/report.rb @@ -1,4 +1,13 @@ 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}" + end + end end -- cgit From a58bf959ec49c033e0498916a09e77e303c5792e Mon Sep 17 00:00:00 2001 From: Pieter van de Bruggen Date: Tue, 22 Mar 2011 13:19:25 -0700 Subject: (#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 --- lib/puppet/interface/catalog.rb | 2 +- lib/puppet/interface/catalog/select.rb | 12 +++++++----- lib/puppet/interface/configurer.rb | 9 +++------ 3 files changed, 11 insertions(+), 12 deletions(-) (limited to 'lib/puppet/interface') 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 -- cgit From e3d24865c89bccd0221f3d6d475d350f577ed3fb Mon Sep 17 00:00:00 2001 From: Nick Lewis Date: Tue, 22 Mar 2011 12:54:52 -0700 Subject: (#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 --- lib/puppet/interface/action.rb | 16 +++++++++++ lib/puppet/interface/action_builder.rb | 29 +++++++++++++++++++ lib/puppet/interface/action_manager.rb | 26 ++++++++++------- lib/puppet/interface/catalog.rb | 52 ++++++++++++++++++---------------- lib/puppet/interface/catalog/select.rb | 12 ++++---- lib/puppet/interface/config.rb | 6 ++-- lib/puppet/interface/configurer.rb | 12 ++++---- lib/puppet/interface/facts.rb | 16 ++++++----- lib/puppet/interface/indirector.rb | 28 +++++++++--------- lib/puppet/interface/report.rb | 16 ++++++----- 10 files changed, 138 insertions(+), 75 deletions(-) create mode 100644 lib/puppet/interface/action.rb create mode 100644 lib/puppet/interface/action_builder.rb (limited to 'lib/puppet/interface') 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 -- cgit From 50ba62d68ef3a6a81e00581dd7d74f6387bd73fc Mon Sep 17 00:00:00 2001 From: Nick Lewis Date: Tue, 22 Mar 2011 14:29:52 -0700 Subject: maint: Make args to Catalog.select explicit No other args are used, so passing *args and shifting is unnecessary. --- lib/puppet/interface/catalog/select.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'lib/puppet/interface') diff --git a/lib/puppet/interface/catalog/select.rb b/lib/puppet/interface/catalog/select.rb index 3f724d360..32d9b7c45 100644 --- a/lib/puppet/interface/catalog/select.rb +++ b/lib/puppet/interface/catalog/select.rb @@ -1,9 +1,7 @@ # Select and show a list of resources of a given type. Puppet::Interface.interface(:catalog) do action :select do - invoke do |*args| - host = args.shift - type = args.shift + invoke do |host,type| catalog = Puppet::Resource::Catalog.indirection.find(host) catalog.resources.reject { |res| res.type != type }.each { |res| puts res } -- cgit From ee66f36aa3c730819bd81d8352914628a811e516 Mon Sep 17 00:00:00 2001 From: Nick Lewis Date: Tue, 22 Mar 2011 14:33:59 -0700 Subject: (#6814) Add missing require for specs --- lib/puppet/interface/action_builder.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'lib/puppet/interface') diff --git a/lib/puppet/interface/action_builder.rb b/lib/puppet/interface/action_builder.rb index 777fcaf85..e76fb1c6e 100644 --- a/lib/puppet/interface/action_builder.rb +++ b/lib/puppet/interface/action_builder.rb @@ -1,4 +1,5 @@ require 'puppet/interface' +require 'puppet/interface/action' class Puppet::Interface::ActionBuilder attr_reader :action -- cgit From 847ac203f9c0b5fce299e87a63b0de5d3ef416f6 Mon Sep 17 00:00:00 2001 From: Nick Lewis Date: Tue, 22 Mar 2011 16:44:01 -0700 Subject: maint: Implement an InterfaceCollection class to manage interfaces Having an instance variable on class Interface is insufficient for Interface::Indirector. This also changes the semantics of "Interface.interface" to handle registration and loading actions, and for "Interface.new" to only instantiate an Interface. Thus, consumers of the API should typically use "Interface.interface", unless they have reasons to not want an interface automatically registered. Paired-With: Pieter van de Bruggen --- lib/puppet/interface/catalog.rb | 2 +- lib/puppet/interface/certificate.rb | 2 +- lib/puppet/interface/certificate_request.rb | 2 +- .../interface/certificate_revocation_list.rb | 2 +- lib/puppet/interface/config.rb | 2 +- lib/puppet/interface/configurer.rb | 2 +- lib/puppet/interface/facts.rb | 2 +- lib/puppet/interface/file.rb | 2 +- lib/puppet/interface/interface_collection.rb | 50 ++++++++++++++++++++++ lib/puppet/interface/key.rb | 2 +- lib/puppet/interface/node.rb | 2 +- lib/puppet/interface/report.rb | 2 +- lib/puppet/interface/resource.rb | 2 +- lib/puppet/interface/resource_type.rb | 2 +- lib/puppet/interface/status.rb | 2 +- 15 files changed, 64 insertions(+), 14 deletions(-) create mode 100644 lib/puppet/interface/interface_collection.rb (limited to 'lib/puppet/interface') diff --git a/lib/puppet/interface/catalog.rb b/lib/puppet/interface/catalog.rb index c0af53bac..defe32127 100644 --- a/lib/puppet/interface/catalog.rb +++ b/lib/puppet/interface/catalog.rb @@ -1,6 +1,6 @@ require 'puppet/interface/indirector' -Puppet::Interface::Indirector.new(:catalog) do +Puppet::Interface::Indirector.interface(:catalog) do action(:apply) do invoke do |catalog| report = Puppet::Transaction::Report.new("apply") diff --git a/lib/puppet/interface/certificate.rb b/lib/puppet/interface/certificate.rb index 52ba4e3b8..09da0a6c3 100644 --- a/lib/puppet/interface/certificate.rb +++ b/lib/puppet/interface/certificate.rb @@ -1,4 +1,4 @@ require 'puppet/interface/indirector' -Puppet::Interface::Indirector.new(:certificate) do +Puppet::Interface::Indirector.interface(:certificate) do end diff --git a/lib/puppet/interface/certificate_request.rb b/lib/puppet/interface/certificate_request.rb index 77b485f8e..b85c15fef 100644 --- a/lib/puppet/interface/certificate_request.rb +++ b/lib/puppet/interface/certificate_request.rb @@ -1,4 +1,4 @@ require 'puppet/interface/indirector' -Puppet::Interface::Indirector.new(:certificate_request) do +Puppet::Interface::Indirector.interface(:certificate_request) do end diff --git a/lib/puppet/interface/certificate_revocation_list.rb b/lib/puppet/interface/certificate_revocation_list.rb index ee1e6a8c4..956fb6494 100644 --- a/lib/puppet/interface/certificate_revocation_list.rb +++ b/lib/puppet/interface/certificate_revocation_list.rb @@ -1,4 +1,4 @@ require 'puppet/interface/indirector' -Puppet::Interface::Indirector.new(:certificate_revocation_list) do +Puppet::Interface::Indirector.interface(:certificate_revocation_list) do end diff --git a/lib/puppet/interface/config.rb b/lib/puppet/interface/config.rb index 0aecc263f..79d2ee7c1 100644 --- a/lib/puppet/interface/config.rb +++ b/lib/puppet/interface/config.rb @@ -1,6 +1,6 @@ require 'puppet/interface' -Puppet::Interface.new(:config) do +Puppet::Interface.interface(:config) do action(:print) do invoke do |*args| Puppet.settings[:configprint] = args.join(",") diff --git a/lib/puppet/interface/configurer.rb b/lib/puppet/interface/configurer.rb index 2fbde27f1..0d21c4d72 100644 --- a/lib/puppet/interface/configurer.rb +++ b/lib/puppet/interface/configurer.rb @@ -1,6 +1,6 @@ require 'puppet/interface' -Puppet::Interface.new(:configurer) do +Puppet::Interface.interface(:configurer) do action(:synchronize) do invoke do |certname| facts = Puppet::Interface.interface(:facts).find(certname) diff --git a/lib/puppet/interface/facts.rb b/lib/puppet/interface/facts.rb index 8843d297d..97e22714b 100644 --- a/lib/puppet/interface/facts.rb +++ b/lib/puppet/interface/facts.rb @@ -1,7 +1,7 @@ require 'puppet/interface/indirector' require 'puppet/node/facts' -Puppet::Interface::Indirector.new(:facts) do +Puppet::Interface::Indirector.interface(:facts) do set_default_format :yaml # Upload our facts to the server diff --git a/lib/puppet/interface/file.rb b/lib/puppet/interface/file.rb index 859f92ca4..f38af2b92 100644 --- a/lib/puppet/interface/file.rb +++ b/lib/puppet/interface/file.rb @@ -1,5 +1,5 @@ require 'puppet/interface/indirector' -Puppet::Interface::Indirector.new(:file) do +Puppet::Interface::Indirector.interface(:file) do set_indirection_name :file_bucket_file end diff --git a/lib/puppet/interface/interface_collection.rb b/lib/puppet/interface/interface_collection.rb new file mode 100644 index 000000000..47ed702aa --- /dev/null +++ b/lib/puppet/interface/interface_collection.rb @@ -0,0 +1,50 @@ +require 'puppet/interface' + +module Puppet::Interface::InterfaceCollection + @interfaces = {} + + def self.interfaces + unless @loaded + @loaded = true + $LOAD_PATH.each do |dir| + next unless FileTest.directory?(dir) + Dir.chdir(dir) do + Dir.glob("puppet/interface/*.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 + end + end + return @interfaces.keys + end + + def self.[](name) + @interfaces[underscorize(name)] if interface?(name) + end + + def self.interface?(name) + name = underscorize(name) + require "puppet/interface/#{name}" unless @interfaces.has_key? name + return @interfaces.has_key? name + rescue LoadError + return false + end + + def self.register(interface) + @interfaces[underscorize(interface.name)] = interface + end + + 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.downcase.split(/[-_]/).join('_').to_sym + end +end diff --git a/lib/puppet/interface/key.rb b/lib/puppet/interface/key.rb index 9343891d0..57519883d 100644 --- a/lib/puppet/interface/key.rb +++ b/lib/puppet/interface/key.rb @@ -1,4 +1,4 @@ require 'puppet/interface/indirector' -Puppet::Interface::Indirector.new(:key) do +Puppet::Interface::Indirector.interface(:key) do end diff --git a/lib/puppet/interface/node.rb b/lib/puppet/interface/node.rb index 0a0f57a1e..8940fd7dd 100644 --- a/lib/puppet/interface/node.rb +++ b/lib/puppet/interface/node.rb @@ -1,5 +1,5 @@ require 'puppet/interface/indirector' -Puppet::Interface::Indirector.new(:node) do +Puppet::Interface::Indirector.interface(:node) do set_default_format :yaml end diff --git a/lib/puppet/interface/report.rb b/lib/puppet/interface/report.rb index e785ae22d..56a58f6aa 100644 --- a/lib/puppet/interface/report.rb +++ b/lib/puppet/interface/report.rb @@ -1,6 +1,6 @@ require 'puppet/interface/indirector' -Puppet::Interface::Indirector.new(:report) do +Puppet::Interface::Indirector.interface(:report) do action(:submit) do invoke do |report| begin diff --git a/lib/puppet/interface/resource.rb b/lib/puppet/interface/resource.rb index 65f2dec7a..130f40fce 100644 --- a/lib/puppet/interface/resource.rb +++ b/lib/puppet/interface/resource.rb @@ -1,4 +1,4 @@ require 'puppet/interface/indirector' -Puppet::Interface::Indirector.new(:resource) do +Puppet::Interface::Indirector.interface(:resource) do end diff --git a/lib/puppet/interface/resource_type.rb b/lib/puppet/interface/resource_type.rb index bf16652a8..70bf3b95a 100644 --- a/lib/puppet/interface/resource_type.rb +++ b/lib/puppet/interface/resource_type.rb @@ -1,4 +1,4 @@ require 'puppet/interface/indirector' -Puppet::Interface::Indirector.new(:resource_type) do +Puppet::Interface::Indirector.interface(:resource_type) do end diff --git a/lib/puppet/interface/status.rb b/lib/puppet/interface/status.rb index 1a1d349d1..432d1ce54 100644 --- a/lib/puppet/interface/status.rb +++ b/lib/puppet/interface/status.rb @@ -1,4 +1,4 @@ require 'puppet/interface/indirector' -Puppet::Interface::Indirector.new(:status) do +Puppet::Interface::Indirector.interface(:status) do end -- cgit From 5d7715b0c56c6f06d916126e8470d7edb66d7687 Mon Sep 17 00:00:00 2001 From: Richard Crowley Date: Tue, 22 Mar 2011 23:46:21 +0000 Subject: Factoring cert status app back into certificate. --- lib/puppet/interface/certificate.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'lib/puppet/interface') diff --git a/lib/puppet/interface/certificate.rb b/lib/puppet/interface/certificate.rb index 09da0a6c3..c2101d926 100644 --- a/lib/puppet/interface/certificate.rb +++ b/lib/puppet/interface/certificate.rb @@ -1,4 +1,21 @@ require 'puppet/interface/indirector' Puppet::Interface::Indirector.interface(:certificate) do + + action :sign do |name| + unless indirection.terminus + raise ArgumentError, "You must have a CA specified; use --ca-location to specify the location (remote, local, only)" + end + + location = Puppet::SSL::Host.ca_location + if location == :local && !Puppet::SSL::CertificateAuthority.ca? + Puppet::Application[:certificate].class.run_mode("master") + set_run_mode Puppet::Application[:certificate].class.run_mode + end + + Puppet::SSL::CertificateStatus.indirection.save( + Puppet::SSL::CertificateStatus.new(name)) + + end + end -- cgit From 562bd0f10be966bef725896af9ec3cdc30771ac6 Mon Sep 17 00:00:00 2001 From: Richard Crowley Date: Wed, 23 Mar 2011 00:14:59 +0000 Subject: Use the new name for the terminus. --- lib/puppet/interface/certificate.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'lib/puppet/interface') diff --git a/lib/puppet/interface/certificate.rb b/lib/puppet/interface/certificate.rb index c2101d926..32415ea9d 100644 --- a/lib/puppet/interface/certificate.rb +++ b/lib/puppet/interface/certificate.rb @@ -13,8 +13,7 @@ Puppet::Interface::Indirector.interface(:certificate) do set_run_mode Puppet::Application[:certificate].class.run_mode end - Puppet::SSL::CertificateStatus.indirection.save( - Puppet::SSL::CertificateStatus.new(name)) + Puppet::SSL::Host.indirection.save(Puppet::SSL::Host.new(name)) end -- cgit From 562ae5fb9d78b1a6d46e79bb41d8498f29246f41 Mon Sep 17 00:00:00 2001 From: Luke Kanies Date: Tue, 22 Mar 2011 19:10:35 -0700 Subject: WIP - all tests fail Signed-off-by: Luke Kanies --- lib/puppet/interface/action_manager.rb | 2 +- lib/puppet/interface/certificate.rb | 22 +++++++++++----------- 2 files changed, 12 insertions(+), 12 deletions(-) (limited to 'lib/puppet/interface') diff --git a/lib/puppet/interface/action_manager.rb b/lib/puppet/interface/action_manager.rb index 8629b4cbe..0db82d612 100644 --- a/lib/puppet/interface/action_manager.rb +++ b/lib/puppet/interface/action_manager.rb @@ -31,6 +31,6 @@ module Puppet::Interface::ActionManager end def action?(name) - actions.include?(name) + actions.include?(name.to_sym) end end diff --git a/lib/puppet/interface/certificate.rb b/lib/puppet/interface/certificate.rb index 32415ea9d..9b9496977 100644 --- a/lib/puppet/interface/certificate.rb +++ b/lib/puppet/interface/certificate.rb @@ -1,20 +1,20 @@ require 'puppet/interface/indirector' Puppet::Interface::Indirector.interface(:certificate) do - action :sign do |name| - unless indirection.terminus - raise ArgumentError, "You must have a CA specified; use --ca-location to specify the location (remote, local, only)" - end + invoke do |name| + unless Puppet::SSL::Host.ca_location + raise ArgumentError, "You must have a CA location specified; use --ca-location to specify the location (remote, local, only)" + end - location = Puppet::SSL::Host.ca_location - if location == :local && !Puppet::SSL::CertificateAuthority.ca? - Puppet::Application[:certificate].class.run_mode("master") - set_run_mode Puppet::Application[:certificate].class.run_mode - end + location = Puppet::SSL::Host.ca_location + if location == :local && !Puppet::SSL::CertificateAuthority.ca? + Puppet::Application[:certificate].class.run_mode("master") + set_run_mode Puppet::Application[:certificate].class.run_mode + end - Puppet::SSL::Host.indirection.save(Puppet::SSL::Host.new(name)) + Puppet::SSL::Host.indirection.save(Puppet::SSL::Host.new(name)) + end end - end -- cgit From a1ce253d5896c6923165b5c00edef87003b16d7b Mon Sep 17 00:00:00 2001 From: Luke Kanies Date: Tue, 22 Mar 2011 19:17:57 -0700 Subject: Adding Certficate#generate Signed-off-by: Luke Kanies --- lib/puppet/interface/certificate.rb | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'lib/puppet/interface') diff --git a/lib/puppet/interface/certificate.rb b/lib/puppet/interface/certificate.rb index 9b9496977..6ba043e8c 100644 --- a/lib/puppet/interface/certificate.rb +++ b/lib/puppet/interface/certificate.rb @@ -1,6 +1,15 @@ require 'puppet/interface/indirector' Puppet::Interface::Indirector.interface(:certificate) do + action :generate do + invoke do |name| + require 'puppet/ssl/host' + + host = Puppet::SSL::Host.new(name) + host.generate + end + end + action :sign do |name| invoke do |name| unless Puppet::SSL::Host.ca_location @@ -9,8 +18,9 @@ Puppet::Interface::Indirector.interface(:certificate) do location = Puppet::SSL::Host.ca_location if location == :local && !Puppet::SSL::CertificateAuthority.ca? - Puppet::Application[:certificate].class.run_mode("master") - set_run_mode Puppet::Application[:certificate].class.run_mode + app = Puppet::Application[:certificate] + app.class.run_mode("master") + app.set_run_mode Puppet::Application[:certificate].class.run_mode end Puppet::SSL::Host.indirection.save(Puppet::SSL::Host.new(name)) -- cgit From a7a9e125018c7a3fd0f0afc543f7aeaea1c19525 Mon Sep 17 00:00:00 2001 From: Richard Crowley Date: Wed, 23 Mar 2011 17:44:58 +0000 Subject: Alter generate action to work on CSRs only. --- lib/puppet/interface/certificate.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'lib/puppet/interface') diff --git a/lib/puppet/interface/certificate.rb b/lib/puppet/interface/certificate.rb index 6ba043e8c..4088a4557 100644 --- a/lib/puppet/interface/certificate.rb +++ b/lib/puppet/interface/certificate.rb @@ -1,12 +1,13 @@ require 'puppet/interface/indirector' +require 'puppet/ssl/host' Puppet::Interface::Indirector.interface(:certificate) do + action :generate do invoke do |name| - require 'puppet/ssl/host' - host = Puppet::SSL::Host.new(name) - host.generate + host.generate_certificate_request + host.certificate_request.class.indirection.save(host.certificate_request) end end -- cgit From 961c7163f336c0dd96f7f72122af9155e1a2260a Mon Sep 17 00:00:00 2001 From: Richard Crowley Date: Wed, 23 Mar 2011 17:45:46 +0000 Subject: Added list action. The common tasks of checking the --ca-location argument and becoming a CA process if necessary (that is, acting like a master) have been abstracted into the Application where they belong. --- lib/puppet/interface/certificate.rb | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) (limited to 'lib/puppet/interface') diff --git a/lib/puppet/interface/certificate.rb b/lib/puppet/interface/certificate.rb index 4088a4557..5c06cdc29 100644 --- a/lib/puppet/interface/certificate.rb +++ b/lib/puppet/interface/certificate.rb @@ -11,21 +11,19 @@ Puppet::Interface::Indirector.interface(:certificate) do end end - action :sign do |name| - invoke do |name| - unless Puppet::SSL::Host.ca_location - raise ArgumentError, "You must have a CA location specified; use --ca-location to specify the location (remote, local, only)" - end - - location = Puppet::SSL::Host.ca_location - if location == :local && !Puppet::SSL::CertificateAuthority.ca? - app = Puppet::Application[:certificate] - app.class.run_mode("master") - app.set_run_mode Puppet::Application[:certificate].class.run_mode + action :list do + invoke do + Puppet::SSL::Host.indirection.search("*").each do |host| + puts host.inspect end + nil + end + end + action :sign do |name| + invoke do |name| Puppet::SSL::Host.indirection.save(Puppet::SSL::Host.new(name)) - end end + end -- cgit From 1187a0eb2550f04d9b6c3dcfdcacdfbb32de0e56 Mon Sep 17 00:00:00 2001 From: Pieter van de Bruggen Date: Wed, 23 Mar 2011 13:59:44 -0700 Subject: (#6770) Add basic versioning for interfaces. Reviewed-By: Nick Lewis --- lib/puppet/interface/catalog.rb | 40 ---------------------- lib/puppet/interface/catalog/select.rb | 10 ------ lib/puppet/interface/certificate.rb | 4 --- lib/puppet/interface/certificate_request.rb | 4 --- .../interface/certificate_revocation_list.rb | 4 --- lib/puppet/interface/config.rb | 10 ------ lib/puppet/interface/configurer.rb | 12 ------- lib/puppet/interface/facts.rb | 18 ---------- lib/puppet/interface/file.rb | 5 --- lib/puppet/interface/interface_collection.rb | 16 +++++---- lib/puppet/interface/key.rb | 4 --- lib/puppet/interface/node.rb | 5 --- lib/puppet/interface/report.rb | 15 -------- lib/puppet/interface/resource.rb | 4 --- lib/puppet/interface/resource_type.rb | 4 --- lib/puppet/interface/status.rb | 4 --- lib/puppet/interface/v1/catalog.rb | 40 ++++++++++++++++++++++ lib/puppet/interface/v1/catalog/select.rb | 10 ++++++ lib/puppet/interface/v1/certificate.rb | 4 +++ lib/puppet/interface/v1/certificate_request.rb | 4 +++ .../interface/v1/certificate_revocation_list.rb | 4 +++ lib/puppet/interface/v1/config.rb | 10 ++++++ lib/puppet/interface/v1/configurer.rb | 12 +++++++ lib/puppet/interface/v1/facts.rb | 18 ++++++++++ lib/puppet/interface/v1/file.rb | 5 +++ lib/puppet/interface/v1/key.rb | 4 +++ lib/puppet/interface/v1/node.rb | 5 +++ lib/puppet/interface/v1/report.rb | 15 ++++++++ lib/puppet/interface/v1/resource.rb | 4 +++ lib/puppet/interface/v1/resource_type.rb | 4 +++ lib/puppet/interface/v1/status.rb | 4 +++ 31 files changed, 152 insertions(+), 150 deletions(-) delete mode 100644 lib/puppet/interface/catalog.rb delete mode 100644 lib/puppet/interface/catalog/select.rb delete mode 100644 lib/puppet/interface/certificate.rb delete mode 100644 lib/puppet/interface/certificate_request.rb delete mode 100644 lib/puppet/interface/certificate_revocation_list.rb delete mode 100644 lib/puppet/interface/config.rb delete mode 100644 lib/puppet/interface/configurer.rb delete mode 100644 lib/puppet/interface/facts.rb delete mode 100644 lib/puppet/interface/file.rb delete mode 100644 lib/puppet/interface/key.rb delete mode 100644 lib/puppet/interface/node.rb delete mode 100644 lib/puppet/interface/report.rb delete mode 100644 lib/puppet/interface/resource.rb delete mode 100644 lib/puppet/interface/resource_type.rb delete mode 100644 lib/puppet/interface/status.rb create mode 100644 lib/puppet/interface/v1/catalog.rb create mode 100644 lib/puppet/interface/v1/catalog/select.rb create mode 100644 lib/puppet/interface/v1/certificate.rb create mode 100644 lib/puppet/interface/v1/certificate_request.rb create mode 100644 lib/puppet/interface/v1/certificate_revocation_list.rb create mode 100644 lib/puppet/interface/v1/config.rb create mode 100644 lib/puppet/interface/v1/configurer.rb create mode 100644 lib/puppet/interface/v1/facts.rb create mode 100644 lib/puppet/interface/v1/file.rb create mode 100644 lib/puppet/interface/v1/key.rb create mode 100644 lib/puppet/interface/v1/node.rb create mode 100644 lib/puppet/interface/v1/report.rb create mode 100644 lib/puppet/interface/v1/resource.rb create mode 100644 lib/puppet/interface/v1/resource_type.rb create mode 100644 lib/puppet/interface/v1/status.rb (limited to 'lib/puppet/interface') diff --git a/lib/puppet/interface/catalog.rb b/lib/puppet/interface/catalog.rb deleted file mode 100644 index defe32127..000000000 --- a/lib/puppet/interface/catalog.rb +++ /dev/null @@ -1,40 +0,0 @@ -require 'puppet/interface/indirector' - -Puppet::Interface::Indirector.interface(:catalog) do - action(:apply) do - invoke do |catalog| - report = Puppet::Transaction::Report.new("apply") - report.configuration_version = catalog.version - - Puppet::Util::Log.newdestination(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 - - report.finalize_report - report - end - end - - 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.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 - end -end diff --git a/lib/puppet/interface/catalog/select.rb b/lib/puppet/interface/catalog/select.rb deleted file mode 100644 index 32d9b7c45..000000000 --- a/lib/puppet/interface/catalog/select.rb +++ /dev/null @@ -1,10 +0,0 @@ -# Select and show a list of resources of a given type. -Puppet::Interface.interface(:catalog) do - action :select do - invoke do |host,type| - catalog = Puppet::Resource::Catalog.indirection.find(host) - - catalog.resources.reject { |res| res.type != type }.each { |res| puts res } - end - end -end diff --git a/lib/puppet/interface/certificate.rb b/lib/puppet/interface/certificate.rb deleted file mode 100644 index 09da0a6c3..000000000 --- a/lib/puppet/interface/certificate.rb +++ /dev/null @@ -1,4 +0,0 @@ -require 'puppet/interface/indirector' - -Puppet::Interface::Indirector.interface(:certificate) do -end diff --git a/lib/puppet/interface/certificate_request.rb b/lib/puppet/interface/certificate_request.rb deleted file mode 100644 index b85c15fef..000000000 --- a/lib/puppet/interface/certificate_request.rb +++ /dev/null @@ -1,4 +0,0 @@ -require 'puppet/interface/indirector' - -Puppet::Interface::Indirector.interface(:certificate_request) do -end diff --git a/lib/puppet/interface/certificate_revocation_list.rb b/lib/puppet/interface/certificate_revocation_list.rb deleted file mode 100644 index 956fb6494..000000000 --- a/lib/puppet/interface/certificate_revocation_list.rb +++ /dev/null @@ -1,4 +0,0 @@ -require 'puppet/interface/indirector' - -Puppet::Interface::Indirector.interface(:certificate_revocation_list) do -end diff --git a/lib/puppet/interface/config.rb b/lib/puppet/interface/config.rb deleted file mode 100644 index 79d2ee7c1..000000000 --- a/lib/puppet/interface/config.rb +++ /dev/null @@ -1,10 +0,0 @@ -require 'puppet/interface' - -Puppet::Interface.interface(:config) do - action(:print) do - invoke do |*args| - Puppet.settings[:configprint] = args.join(",") - Puppet.settings.print_config_options - end - end -end diff --git a/lib/puppet/interface/configurer.rb b/lib/puppet/interface/configurer.rb deleted file mode 100644 index 0d21c4d72..000000000 --- a/lib/puppet/interface/configurer.rb +++ /dev/null @@ -1,12 +0,0 @@ -require 'puppet/interface' - -Puppet::Interface.interface(:configurer) do - action(:synchronize) do - invoke do |certname| - 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 -end diff --git a/lib/puppet/interface/facts.rb b/lib/puppet/interface/facts.rb deleted file mode 100644 index 97e22714b..000000000 --- a/lib/puppet/interface/facts.rb +++ /dev/null @@ -1,18 +0,0 @@ -require 'puppet/interface/indirector' -require 'puppet/node/facts' - -Puppet::Interface::Indirector.interface(:facts) do - set_default_format :yaml - - # Upload our facts to the server - 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/file.rb b/lib/puppet/interface/file.rb deleted file mode 100644 index f38af2b92..000000000 --- a/lib/puppet/interface/file.rb +++ /dev/null @@ -1,5 +0,0 @@ -require 'puppet/interface/indirector' - -Puppet::Interface::Indirector.interface(:file) do - set_indirection_name :file_bucket_file -end diff --git a/lib/puppet/interface/interface_collection.rb b/lib/puppet/interface/interface_collection.rb index 47ed702aa..d626c4f72 100644 --- a/lib/puppet/interface/interface_collection.rb +++ b/lib/puppet/interface/interface_collection.rb @@ -1,7 +1,7 @@ require 'puppet/interface' module Puppet::Interface::InterfaceCollection - @interfaces = {} + @interfaces = Hash.new { |hash, key| hash[key] = {} } def self.interfaces unless @loaded @@ -24,20 +24,22 @@ module Puppet::Interface::InterfaceCollection return @interfaces.keys end - def self.[](name) - @interfaces[underscorize(name)] if interface?(name) + def self.[](name, version) + @interfaces[underscorize(name)][version] if interface?(name, version) end - def self.interface?(name) + def self.interface?(name, version) name = underscorize(name) - require "puppet/interface/#{name}" unless @interfaces.has_key? name - return @interfaces.has_key? name + unless @interfaces.has_key?(name) && @interfaces[name].has_key?(version) + require "puppet/interface/v#{version}/#{name}" + end + return @interfaces.has_key?(name) && @interfaces[name].has_key?(version) rescue LoadError return false end def self.register(interface) - @interfaces[underscorize(interface.name)] = interface + @interfaces[underscorize(interface.name)][interface.version] = interface end def self.underscorize(name) diff --git a/lib/puppet/interface/key.rb b/lib/puppet/interface/key.rb deleted file mode 100644 index 57519883d..000000000 --- a/lib/puppet/interface/key.rb +++ /dev/null @@ -1,4 +0,0 @@ -require 'puppet/interface/indirector' - -Puppet::Interface::Indirector.interface(:key) do -end diff --git a/lib/puppet/interface/node.rb b/lib/puppet/interface/node.rb deleted file mode 100644 index 8940fd7dd..000000000 --- a/lib/puppet/interface/node.rb +++ /dev/null @@ -1,5 +0,0 @@ -require 'puppet/interface/indirector' - -Puppet::Interface::Indirector.interface(:node) do - set_default_format :yaml -end diff --git a/lib/puppet/interface/report.rb b/lib/puppet/interface/report.rb deleted file mode 100644 index 56a58f6aa..000000000 --- a/lib/puppet/interface/report.rb +++ /dev/null @@ -1,15 +0,0 @@ -require 'puppet/interface/indirector' - -Puppet::Interface::Indirector.interface(:report) do - 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 diff --git a/lib/puppet/interface/resource.rb b/lib/puppet/interface/resource.rb deleted file mode 100644 index 130f40fce..000000000 --- a/lib/puppet/interface/resource.rb +++ /dev/null @@ -1,4 +0,0 @@ -require 'puppet/interface/indirector' - -Puppet::Interface::Indirector.interface(:resource) do -end diff --git a/lib/puppet/interface/resource_type.rb b/lib/puppet/interface/resource_type.rb deleted file mode 100644 index 70bf3b95a..000000000 --- a/lib/puppet/interface/resource_type.rb +++ /dev/null @@ -1,4 +0,0 @@ -require 'puppet/interface/indirector' - -Puppet::Interface::Indirector.interface(:resource_type) do -end diff --git a/lib/puppet/interface/status.rb b/lib/puppet/interface/status.rb deleted file mode 100644 index 432d1ce54..000000000 --- a/lib/puppet/interface/status.rb +++ /dev/null @@ -1,4 +0,0 @@ -require 'puppet/interface/indirector' - -Puppet::Interface::Indirector.interface(:status) do -end diff --git a/lib/puppet/interface/v1/catalog.rb b/lib/puppet/interface/v1/catalog.rb new file mode 100644 index 000000000..2ba642039 --- /dev/null +++ b/lib/puppet/interface/v1/catalog.rb @@ -0,0 +1,40 @@ +require 'puppet/interface/indirector' + +Puppet::Interface::Indirector.interface(:catalog, 1) do + action(:apply) do + invoke do |catalog| + report = Puppet::Transaction::Report.new("apply") + report.configuration_version = catalog.version + + Puppet::Util::Log.newdestination(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 + + report.finalize_report + report + end + end + + 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.interface(:catalog, 1).find(certname, facts_to_upload) + end + catalog = catalog.to_ral + catalog.finalize + catalog.retrieval_duration = retrieval_duration + catalog.write_class_file + catalog + end + end +end diff --git a/lib/puppet/interface/v1/catalog/select.rb b/lib/puppet/interface/v1/catalog/select.rb new file mode 100644 index 000000000..e37c841b5 --- /dev/null +++ b/lib/puppet/interface/v1/catalog/select.rb @@ -0,0 +1,10 @@ +# Select and show a list of resources of a given type. +Puppet::Interface.interface(:catalog, 1) do + action :select do + invoke do |host,type| + catalog = Puppet::Resource::Catalog.indirection.find(host) + + catalog.resources.reject { |res| res.type != type }.each { |res| puts res } + end + end +end diff --git a/lib/puppet/interface/v1/certificate.rb b/lib/puppet/interface/v1/certificate.rb new file mode 100644 index 000000000..9a88c628a --- /dev/null +++ b/lib/puppet/interface/v1/certificate.rb @@ -0,0 +1,4 @@ +require 'puppet/interface/indirector' + +Puppet::Interface::Indirector.interface(:certificate, 1) do +end diff --git a/lib/puppet/interface/v1/certificate_request.rb b/lib/puppet/interface/v1/certificate_request.rb new file mode 100644 index 000000000..868933e1f --- /dev/null +++ b/lib/puppet/interface/v1/certificate_request.rb @@ -0,0 +1,4 @@ +require 'puppet/interface/indirector' + +Puppet::Interface::Indirector.interface(:certificate_request, 1) do +end diff --git a/lib/puppet/interface/v1/certificate_revocation_list.rb b/lib/puppet/interface/v1/certificate_revocation_list.rb new file mode 100644 index 000000000..09efd8c7a --- /dev/null +++ b/lib/puppet/interface/v1/certificate_revocation_list.rb @@ -0,0 +1,4 @@ +require 'puppet/interface/indirector' + +Puppet::Interface::Indirector.interface(:certificate_revocation_list, 1) do +end diff --git a/lib/puppet/interface/v1/config.rb b/lib/puppet/interface/v1/config.rb new file mode 100644 index 000000000..a072e703c --- /dev/null +++ b/lib/puppet/interface/v1/config.rb @@ -0,0 +1,10 @@ +require 'puppet/interface' + +Puppet::Interface.interface(:config, 1) do + action(:print) do + invoke do |*args| + Puppet.settings[:configprint] = args.join(",") + Puppet.settings.print_config_options + end + end +end diff --git a/lib/puppet/interface/v1/configurer.rb b/lib/puppet/interface/v1/configurer.rb new file mode 100644 index 000000000..1deffce4e --- /dev/null +++ b/lib/puppet/interface/v1/configurer.rb @@ -0,0 +1,12 @@ +require 'puppet/interface' + +Puppet::Interface.interface(:configurer, 1) do + action(:synchronize) do + invoke do |certname| + facts = Puppet::Interface.interface(:facts, 1).find(certname) + catalog = Puppet::Interface.interface(:catalog, 1).download(certname, facts) + report = Puppet::Interface.interface(:catalog, 1).apply(catalog) + report + end + end +end diff --git a/lib/puppet/interface/v1/facts.rb b/lib/puppet/interface/v1/facts.rb new file mode 100644 index 000000000..0be23b781 --- /dev/null +++ b/lib/puppet/interface/v1/facts.rb @@ -0,0 +1,18 @@ +require 'puppet/interface/indirector' +require 'puppet/node/facts' + +Puppet::Interface::Indirector.interface(:facts, 1) do + set_default_format :yaml + + # Upload our facts to the server + 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/v1/file.rb b/lib/puppet/interface/v1/file.rb new file mode 100644 index 000000000..430413a56 --- /dev/null +++ b/lib/puppet/interface/v1/file.rb @@ -0,0 +1,5 @@ +require 'puppet/interface/indirector' + +Puppet::Interface::Indirector.interface(:file, 1) do + set_indirection_name :file_bucket_file +end diff --git a/lib/puppet/interface/v1/key.rb b/lib/puppet/interface/v1/key.rb new file mode 100644 index 000000000..fc82f4d68 --- /dev/null +++ b/lib/puppet/interface/v1/key.rb @@ -0,0 +1,4 @@ +require 'puppet/interface/indirector' + +Puppet::Interface::Indirector.interface(:key, 1) do +end diff --git a/lib/puppet/interface/v1/node.rb b/lib/puppet/interface/v1/node.rb new file mode 100644 index 000000000..c3e527856 --- /dev/null +++ b/lib/puppet/interface/v1/node.rb @@ -0,0 +1,5 @@ +require 'puppet/interface/indirector' + +Puppet::Interface::Indirector.interface(:node, 1) do + set_default_format :yaml +end diff --git a/lib/puppet/interface/v1/report.rb b/lib/puppet/interface/v1/report.rb new file mode 100644 index 000000000..fdc2e2c0f --- /dev/null +++ b/lib/puppet/interface/v1/report.rb @@ -0,0 +1,15 @@ +require 'puppet/interface/indirector' + +Puppet::Interface::Indirector.interface(:report, 1) do + 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 diff --git a/lib/puppet/interface/v1/resource.rb b/lib/puppet/interface/v1/resource.rb new file mode 100644 index 000000000..20dc2837c --- /dev/null +++ b/lib/puppet/interface/v1/resource.rb @@ -0,0 +1,4 @@ +require 'puppet/interface/indirector' + +Puppet::Interface::Indirector.interface(:resource, 1) do +end diff --git a/lib/puppet/interface/v1/resource_type.rb b/lib/puppet/interface/v1/resource_type.rb new file mode 100644 index 000000000..f180dc576 --- /dev/null +++ b/lib/puppet/interface/v1/resource_type.rb @@ -0,0 +1,4 @@ +require 'puppet/interface/indirector' + +Puppet::Interface::Indirector.interface(:resource_type, 1) do +end diff --git a/lib/puppet/interface/v1/status.rb b/lib/puppet/interface/v1/status.rb new file mode 100644 index 000000000..a2493b581 --- /dev/null +++ b/lib/puppet/interface/v1/status.rb @@ -0,0 +1,4 @@ +require 'puppet/interface/indirector' + +Puppet::Interface::Indirector.interface(:status, 1) do +end -- cgit From 635751d809af309c7c36c0c9d7a94a731ef8bd1c Mon Sep 17 00:00:00 2001 From: Richard Crowley Date: Wed, 23 Mar 2011 22:41:21 +0000 Subject: Propagating an argument to search out of core. Puppet::SSL::Host.search (which will proxy to the new indirection) accepts an array of classes (now also class names) to limit the search. Currently, `puppet certificate list` limits itself to certificate requests to mimic the behavior of `puppet cert -l`. --- lib/puppet/interface/certificate.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'lib/puppet/interface') diff --git a/lib/puppet/interface/certificate.rb b/lib/puppet/interface/certificate.rb index 5c06cdc29..86ac6d6ea 100644 --- a/lib/puppet/interface/certificate.rb +++ b/lib/puppet/interface/certificate.rb @@ -13,10 +13,9 @@ Puppet::Interface::Indirector.interface(:certificate) do action :list do invoke do - Puppet::SSL::Host.indirection.search("*").each do |host| - puts host.inspect - end - nil + Puppet::SSL::Host.indirection.search("*", { + :for => :certificate_request, + }).map { |h| h.inspect } end end -- cgit From 7aa8f2252c7b0512c929fb87a6c3a09a952a142a Mon Sep 17 00:00:00 2001 From: Pieter van de Bruggen Date: Wed, 23 Mar 2011 16:10:45 -0700 Subject: (#6770) Changing versioning to semver. More information about the versioning scheme can be found at http://semver.org. Paired-With: Nick Lewis --- lib/puppet/interface/v0.0.1/catalog.rb | 40 ++++++++++++++++++++++ lib/puppet/interface/v0.0.1/catalog/select.rb | 10 ++++++ lib/puppet/interface/v0.0.1/certificate.rb | 4 +++ lib/puppet/interface/v0.0.1/certificate_request.rb | 4 +++ .../v0.0.1/certificate_revocation_list.rb | 4 +++ lib/puppet/interface/v0.0.1/config.rb | 10 ++++++ lib/puppet/interface/v0.0.1/configurer.rb | 12 +++++++ lib/puppet/interface/v0.0.1/facts.rb | 18 ++++++++++ lib/puppet/interface/v0.0.1/file.rb | 5 +++ lib/puppet/interface/v0.0.1/key.rb | 4 +++ lib/puppet/interface/v0.0.1/node.rb | 5 +++ lib/puppet/interface/v0.0.1/report.rb | 15 ++++++++ lib/puppet/interface/v0.0.1/resource.rb | 4 +++ lib/puppet/interface/v0.0.1/resource_type.rb | 4 +++ lib/puppet/interface/v0.0.1/status.rb | 4 +++ lib/puppet/interface/v1/catalog.rb | 40 ---------------------- lib/puppet/interface/v1/catalog/select.rb | 10 ------ lib/puppet/interface/v1/certificate.rb | 4 --- lib/puppet/interface/v1/certificate_request.rb | 4 --- .../interface/v1/certificate_revocation_list.rb | 4 --- lib/puppet/interface/v1/config.rb | 10 ------ lib/puppet/interface/v1/configurer.rb | 12 ------- lib/puppet/interface/v1/facts.rb | 18 ---------- lib/puppet/interface/v1/file.rb | 5 --- lib/puppet/interface/v1/key.rb | 4 --- lib/puppet/interface/v1/node.rb | 5 --- lib/puppet/interface/v1/report.rb | 15 -------- lib/puppet/interface/v1/resource.rb | 4 --- lib/puppet/interface/v1/resource_type.rb | 4 --- lib/puppet/interface/v1/status.rb | 4 --- 30 files changed, 143 insertions(+), 143 deletions(-) create mode 100644 lib/puppet/interface/v0.0.1/catalog.rb create mode 100644 lib/puppet/interface/v0.0.1/catalog/select.rb create mode 100644 lib/puppet/interface/v0.0.1/certificate.rb create mode 100644 lib/puppet/interface/v0.0.1/certificate_request.rb create mode 100644 lib/puppet/interface/v0.0.1/certificate_revocation_list.rb create mode 100644 lib/puppet/interface/v0.0.1/config.rb create mode 100644 lib/puppet/interface/v0.0.1/configurer.rb create mode 100644 lib/puppet/interface/v0.0.1/facts.rb create mode 100644 lib/puppet/interface/v0.0.1/file.rb create mode 100644 lib/puppet/interface/v0.0.1/key.rb create mode 100644 lib/puppet/interface/v0.0.1/node.rb create mode 100644 lib/puppet/interface/v0.0.1/report.rb create mode 100644 lib/puppet/interface/v0.0.1/resource.rb create mode 100644 lib/puppet/interface/v0.0.1/resource_type.rb create mode 100644 lib/puppet/interface/v0.0.1/status.rb delete mode 100644 lib/puppet/interface/v1/catalog.rb delete mode 100644 lib/puppet/interface/v1/catalog/select.rb delete mode 100644 lib/puppet/interface/v1/certificate.rb delete mode 100644 lib/puppet/interface/v1/certificate_request.rb delete mode 100644 lib/puppet/interface/v1/certificate_revocation_list.rb delete mode 100644 lib/puppet/interface/v1/config.rb delete mode 100644 lib/puppet/interface/v1/configurer.rb delete mode 100644 lib/puppet/interface/v1/facts.rb delete mode 100644 lib/puppet/interface/v1/file.rb delete mode 100644 lib/puppet/interface/v1/key.rb delete mode 100644 lib/puppet/interface/v1/node.rb delete mode 100644 lib/puppet/interface/v1/report.rb delete mode 100644 lib/puppet/interface/v1/resource.rb delete mode 100644 lib/puppet/interface/v1/resource_type.rb delete mode 100644 lib/puppet/interface/v1/status.rb (limited to 'lib/puppet/interface') diff --git a/lib/puppet/interface/v0.0.1/catalog.rb b/lib/puppet/interface/v0.0.1/catalog.rb new file mode 100644 index 000000000..647206251 --- /dev/null +++ b/lib/puppet/interface/v0.0.1/catalog.rb @@ -0,0 +1,40 @@ +require 'puppet/interface/indirector' + +Puppet::Interface::Indirector.interface(:catalog, '0.0.1') do + action(:apply) do + invoke do |catalog| + report = Puppet::Transaction::Report.new("apply") + report.configuration_version = catalog.version + + Puppet::Util::Log.newdestination(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 + + report.finalize_report + report + end + end + + 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.interface(:catalog, '0.0.1').find(certname, facts_to_upload) + end + catalog = catalog.to_ral + catalog.finalize + catalog.retrieval_duration = retrieval_duration + catalog.write_class_file + catalog + end + end +end diff --git a/lib/puppet/interface/v0.0.1/catalog/select.rb b/lib/puppet/interface/v0.0.1/catalog/select.rb new file mode 100644 index 000000000..bc65069b8 --- /dev/null +++ b/lib/puppet/interface/v0.0.1/catalog/select.rb @@ -0,0 +1,10 @@ +# Select and show a list of resources of a given type. +Puppet::Interface.interface(:catalog, '0.0.1') do + action :select do + invoke do |host,type| + catalog = Puppet::Resource::Catalog.indirection.find(host) + + catalog.resources.reject { |res| res.type != type }.each { |res| puts res } + end + end +end diff --git a/lib/puppet/interface/v0.0.1/certificate.rb b/lib/puppet/interface/v0.0.1/certificate.rb new file mode 100644 index 000000000..aee123aa6 --- /dev/null +++ b/lib/puppet/interface/v0.0.1/certificate.rb @@ -0,0 +1,4 @@ +require 'puppet/interface/indirector' + +Puppet::Interface::Indirector.interface(:certificate, '0.0.1') do +end diff --git a/lib/puppet/interface/v0.0.1/certificate_request.rb b/lib/puppet/interface/v0.0.1/certificate_request.rb new file mode 100644 index 000000000..169d6e500 --- /dev/null +++ b/lib/puppet/interface/v0.0.1/certificate_request.rb @@ -0,0 +1,4 @@ +require 'puppet/interface/indirector' + +Puppet::Interface::Indirector.interface(:certificate_request, '0.0.1') do +end diff --git a/lib/puppet/interface/v0.0.1/certificate_revocation_list.rb b/lib/puppet/interface/v0.0.1/certificate_revocation_list.rb new file mode 100644 index 000000000..7f4f5176b --- /dev/null +++ b/lib/puppet/interface/v0.0.1/certificate_revocation_list.rb @@ -0,0 +1,4 @@ +require 'puppet/interface/indirector' + +Puppet::Interface::Indirector.interface(:certificate_revocation_list, '0.0.1') do +end diff --git a/lib/puppet/interface/v0.0.1/config.rb b/lib/puppet/interface/v0.0.1/config.rb new file mode 100644 index 000000000..2437712ca --- /dev/null +++ b/lib/puppet/interface/v0.0.1/config.rb @@ -0,0 +1,10 @@ +require 'puppet/interface' + +Puppet::Interface.interface(:config, '0.0.1') do + action(:print) do + invoke do |*args| + Puppet.settings[:configprint] = args.join(",") + Puppet.settings.print_config_options + end + end +end diff --git a/lib/puppet/interface/v0.0.1/configurer.rb b/lib/puppet/interface/v0.0.1/configurer.rb new file mode 100644 index 000000000..0ab71c47a --- /dev/null +++ b/lib/puppet/interface/v0.0.1/configurer.rb @@ -0,0 +1,12 @@ +require 'puppet/interface' + +Puppet::Interface.interface(:configurer, '0.0.1') do + action(:synchronize) do + invoke do |certname| + facts = Puppet::Interface.interface(:facts, '0.0.1').find(certname) + catalog = Puppet::Interface.interface(:catalog, '0.0.1').download(certname, facts) + report = Puppet::Interface.interface(:catalog, '0.0.1').apply(catalog) + report + end + end +end diff --git a/lib/puppet/interface/v0.0.1/facts.rb b/lib/puppet/interface/v0.0.1/facts.rb new file mode 100644 index 000000000..e987d0740 --- /dev/null +++ b/lib/puppet/interface/v0.0.1/facts.rb @@ -0,0 +1,18 @@ +require 'puppet/interface/indirector' +require 'puppet/node/facts' + +Puppet::Interface::Indirector.interface(:facts, '0.0.1') do + set_default_format :yaml + + # Upload our facts to the server + 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/v0.0.1/file.rb b/lib/puppet/interface/v0.0.1/file.rb new file mode 100644 index 000000000..2f27e434f --- /dev/null +++ b/lib/puppet/interface/v0.0.1/file.rb @@ -0,0 +1,5 @@ +require 'puppet/interface/indirector' + +Puppet::Interface::Indirector.interface(:file, '0.0.1') do + set_indirection_name :file_bucket_file +end diff --git a/lib/puppet/interface/v0.0.1/key.rb b/lib/puppet/interface/v0.0.1/key.rb new file mode 100644 index 000000000..ddd3a6ea8 --- /dev/null +++ b/lib/puppet/interface/v0.0.1/key.rb @@ -0,0 +1,4 @@ +require 'puppet/interface/indirector' + +Puppet::Interface::Indirector.interface(:key, '0.0.1') do +end diff --git a/lib/puppet/interface/v0.0.1/node.rb b/lib/puppet/interface/v0.0.1/node.rb new file mode 100644 index 000000000..898426846 --- /dev/null +++ b/lib/puppet/interface/v0.0.1/node.rb @@ -0,0 +1,5 @@ +require 'puppet/interface/indirector' + +Puppet::Interface::Indirector.interface(:node, '0.0.1') do + set_default_format :yaml +end diff --git a/lib/puppet/interface/v0.0.1/report.rb b/lib/puppet/interface/v0.0.1/report.rb new file mode 100644 index 000000000..3f4e17310 --- /dev/null +++ b/lib/puppet/interface/v0.0.1/report.rb @@ -0,0 +1,15 @@ +require 'puppet/interface/indirector' + +Puppet::Interface::Indirector.interface(:report, '0.0.1') do + 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 diff --git a/lib/puppet/interface/v0.0.1/resource.rb b/lib/puppet/interface/v0.0.1/resource.rb new file mode 100644 index 000000000..61c90cc85 --- /dev/null +++ b/lib/puppet/interface/v0.0.1/resource.rb @@ -0,0 +1,4 @@ +require 'puppet/interface/indirector' + +Puppet::Interface::Indirector.interface(:resource, '0.0.1') do +end diff --git a/lib/puppet/interface/v0.0.1/resource_type.rb b/lib/puppet/interface/v0.0.1/resource_type.rb new file mode 100644 index 000000000..9cc4bef28 --- /dev/null +++ b/lib/puppet/interface/v0.0.1/resource_type.rb @@ -0,0 +1,4 @@ +require 'puppet/interface/indirector' + +Puppet::Interface::Indirector.interface(:resource_type, '0.0.1') do +end diff --git a/lib/puppet/interface/v0.0.1/status.rb b/lib/puppet/interface/v0.0.1/status.rb new file mode 100644 index 000000000..d8cded899 --- /dev/null +++ b/lib/puppet/interface/v0.0.1/status.rb @@ -0,0 +1,4 @@ +require 'puppet/interface/indirector' + +Puppet::Interface::Indirector.interface(:status, '0.0.1') do +end diff --git a/lib/puppet/interface/v1/catalog.rb b/lib/puppet/interface/v1/catalog.rb deleted file mode 100644 index 2ba642039..000000000 --- a/lib/puppet/interface/v1/catalog.rb +++ /dev/null @@ -1,40 +0,0 @@ -require 'puppet/interface/indirector' - -Puppet::Interface::Indirector.interface(:catalog, 1) do - action(:apply) do - invoke do |catalog| - report = Puppet::Transaction::Report.new("apply") - report.configuration_version = catalog.version - - Puppet::Util::Log.newdestination(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 - - report.finalize_report - report - end - end - - 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.interface(:catalog, 1).find(certname, facts_to_upload) - end - catalog = catalog.to_ral - catalog.finalize - catalog.retrieval_duration = retrieval_duration - catalog.write_class_file - catalog - end - end -end diff --git a/lib/puppet/interface/v1/catalog/select.rb b/lib/puppet/interface/v1/catalog/select.rb deleted file mode 100644 index e37c841b5..000000000 --- a/lib/puppet/interface/v1/catalog/select.rb +++ /dev/null @@ -1,10 +0,0 @@ -# Select and show a list of resources of a given type. -Puppet::Interface.interface(:catalog, 1) do - action :select do - invoke do |host,type| - catalog = Puppet::Resource::Catalog.indirection.find(host) - - catalog.resources.reject { |res| res.type != type }.each { |res| puts res } - end - end -end diff --git a/lib/puppet/interface/v1/certificate.rb b/lib/puppet/interface/v1/certificate.rb deleted file mode 100644 index 9a88c628a..000000000 --- a/lib/puppet/interface/v1/certificate.rb +++ /dev/null @@ -1,4 +0,0 @@ -require 'puppet/interface/indirector' - -Puppet::Interface::Indirector.interface(:certificate, 1) do -end diff --git a/lib/puppet/interface/v1/certificate_request.rb b/lib/puppet/interface/v1/certificate_request.rb deleted file mode 100644 index 868933e1f..000000000 --- a/lib/puppet/interface/v1/certificate_request.rb +++ /dev/null @@ -1,4 +0,0 @@ -require 'puppet/interface/indirector' - -Puppet::Interface::Indirector.interface(:certificate_request, 1) do -end diff --git a/lib/puppet/interface/v1/certificate_revocation_list.rb b/lib/puppet/interface/v1/certificate_revocation_list.rb deleted file mode 100644 index 09efd8c7a..000000000 --- a/lib/puppet/interface/v1/certificate_revocation_list.rb +++ /dev/null @@ -1,4 +0,0 @@ -require 'puppet/interface/indirector' - -Puppet::Interface::Indirector.interface(:certificate_revocation_list, 1) do -end diff --git a/lib/puppet/interface/v1/config.rb b/lib/puppet/interface/v1/config.rb deleted file mode 100644 index a072e703c..000000000 --- a/lib/puppet/interface/v1/config.rb +++ /dev/null @@ -1,10 +0,0 @@ -require 'puppet/interface' - -Puppet::Interface.interface(:config, 1) do - action(:print) do - invoke do |*args| - Puppet.settings[:configprint] = args.join(",") - Puppet.settings.print_config_options - end - end -end diff --git a/lib/puppet/interface/v1/configurer.rb b/lib/puppet/interface/v1/configurer.rb deleted file mode 100644 index 1deffce4e..000000000 --- a/lib/puppet/interface/v1/configurer.rb +++ /dev/null @@ -1,12 +0,0 @@ -require 'puppet/interface' - -Puppet::Interface.interface(:configurer, 1) do - action(:synchronize) do - invoke do |certname| - facts = Puppet::Interface.interface(:facts, 1).find(certname) - catalog = Puppet::Interface.interface(:catalog, 1).download(certname, facts) - report = Puppet::Interface.interface(:catalog, 1).apply(catalog) - report - end - end -end diff --git a/lib/puppet/interface/v1/facts.rb b/lib/puppet/interface/v1/facts.rb deleted file mode 100644 index 0be23b781..000000000 --- a/lib/puppet/interface/v1/facts.rb +++ /dev/null @@ -1,18 +0,0 @@ -require 'puppet/interface/indirector' -require 'puppet/node/facts' - -Puppet::Interface::Indirector.interface(:facts, 1) do - set_default_format :yaml - - # Upload our facts to the server - 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/v1/file.rb b/lib/puppet/interface/v1/file.rb deleted file mode 100644 index 430413a56..000000000 --- a/lib/puppet/interface/v1/file.rb +++ /dev/null @@ -1,5 +0,0 @@ -require 'puppet/interface/indirector' - -Puppet::Interface::Indirector.interface(:file, 1) do - set_indirection_name :file_bucket_file -end diff --git a/lib/puppet/interface/v1/key.rb b/lib/puppet/interface/v1/key.rb deleted file mode 100644 index fc82f4d68..000000000 --- a/lib/puppet/interface/v1/key.rb +++ /dev/null @@ -1,4 +0,0 @@ -require 'puppet/interface/indirector' - -Puppet::Interface::Indirector.interface(:key, 1) do -end diff --git a/lib/puppet/interface/v1/node.rb b/lib/puppet/interface/v1/node.rb deleted file mode 100644 index c3e527856..000000000 --- a/lib/puppet/interface/v1/node.rb +++ /dev/null @@ -1,5 +0,0 @@ -require 'puppet/interface/indirector' - -Puppet::Interface::Indirector.interface(:node, 1) do - set_default_format :yaml -end diff --git a/lib/puppet/interface/v1/report.rb b/lib/puppet/interface/v1/report.rb deleted file mode 100644 index fdc2e2c0f..000000000 --- a/lib/puppet/interface/v1/report.rb +++ /dev/null @@ -1,15 +0,0 @@ -require 'puppet/interface/indirector' - -Puppet::Interface::Indirector.interface(:report, 1) do - 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 diff --git a/lib/puppet/interface/v1/resource.rb b/lib/puppet/interface/v1/resource.rb deleted file mode 100644 index 20dc2837c..000000000 --- a/lib/puppet/interface/v1/resource.rb +++ /dev/null @@ -1,4 +0,0 @@ -require 'puppet/interface/indirector' - -Puppet::Interface::Indirector.interface(:resource, 1) do -end diff --git a/lib/puppet/interface/v1/resource_type.rb b/lib/puppet/interface/v1/resource_type.rb deleted file mode 100644 index f180dc576..000000000 --- a/lib/puppet/interface/v1/resource_type.rb +++ /dev/null @@ -1,4 +0,0 @@ -require 'puppet/interface/indirector' - -Puppet::Interface::Indirector.interface(:resource_type, 1) do -end diff --git a/lib/puppet/interface/v1/status.rb b/lib/puppet/interface/v1/status.rb deleted file mode 100644 index a2493b581..000000000 --- a/lib/puppet/interface/v1/status.rb +++ /dev/null @@ -1,4 +0,0 @@ -require 'puppet/interface/indirector' - -Puppet::Interface::Indirector.interface(:status, 1) do -end -- cgit From c25fb94725c9abfb36e67938356f97823f8b605e Mon Sep 17 00:00:00 2001 From: Pieter van de Bruggen Date: Wed, 23 Mar 2011 16:28:28 -0700 Subject: (#6770) Rename Puppet::Interface::interface method. Puppet::Interface::interface is now Puppet::Interface::define, also aliased to Puppet::Interface::[] for convenience. Paired-With: Nick Lewis --- lib/puppet/interface/v0.0.1/catalog.rb | 4 ++-- lib/puppet/interface/v0.0.1/catalog/select.rb | 2 +- lib/puppet/interface/v0.0.1/certificate.rb | 2 +- lib/puppet/interface/v0.0.1/certificate_request.rb | 2 +- lib/puppet/interface/v0.0.1/certificate_revocation_list.rb | 2 +- lib/puppet/interface/v0.0.1/config.rb | 2 +- lib/puppet/interface/v0.0.1/configurer.rb | 8 ++++---- lib/puppet/interface/v0.0.1/facts.rb | 2 +- lib/puppet/interface/v0.0.1/file.rb | 2 +- lib/puppet/interface/v0.0.1/key.rb | 2 +- lib/puppet/interface/v0.0.1/node.rb | 2 +- lib/puppet/interface/v0.0.1/report.rb | 2 +- lib/puppet/interface/v0.0.1/resource.rb | 2 +- lib/puppet/interface/v0.0.1/resource_type.rb | 2 +- lib/puppet/interface/v0.0.1/status.rb | 2 +- 15 files changed, 19 insertions(+), 19 deletions(-) (limited to 'lib/puppet/interface') diff --git a/lib/puppet/interface/v0.0.1/catalog.rb b/lib/puppet/interface/v0.0.1/catalog.rb index 647206251..7d61528bc 100644 --- a/lib/puppet/interface/v0.0.1/catalog.rb +++ b/lib/puppet/interface/v0.0.1/catalog.rb @@ -1,6 +1,6 @@ require 'puppet/interface/indirector' -Puppet::Interface::Indirector.interface(:catalog, '0.0.1') do +Puppet::Interface::Indirector.define(:catalog, '0.0.1') do action(:apply) do invoke do |catalog| report = Puppet::Transaction::Report.new("apply") @@ -28,7 +28,7 @@ Puppet::Interface::Indirector.interface(:catalog, '0.0.1') 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.interface(:catalog, '0.0.1').find(certname, facts_to_upload) + catalog = Puppet::Interface[:catalog, '0.0.1'].find(certname, facts_to_upload) end catalog = catalog.to_ral catalog.finalize diff --git a/lib/puppet/interface/v0.0.1/catalog/select.rb b/lib/puppet/interface/v0.0.1/catalog/select.rb index bc65069b8..35f1a1e0b 100644 --- a/lib/puppet/interface/v0.0.1/catalog/select.rb +++ b/lib/puppet/interface/v0.0.1/catalog/select.rb @@ -1,5 +1,5 @@ # Select and show a list of resources of a given type. -Puppet::Interface.interface(:catalog, '0.0.1') do +Puppet::Interface.define(:catalog, '0.0.1') do action :select do invoke do |host,type| catalog = Puppet::Resource::Catalog.indirection.find(host) diff --git a/lib/puppet/interface/v0.0.1/certificate.rb b/lib/puppet/interface/v0.0.1/certificate.rb index aee123aa6..52019fddd 100644 --- a/lib/puppet/interface/v0.0.1/certificate.rb +++ b/lib/puppet/interface/v0.0.1/certificate.rb @@ -1,4 +1,4 @@ require 'puppet/interface/indirector' -Puppet::Interface::Indirector.interface(:certificate, '0.0.1') do +Puppet::Interface::Indirector.define(:certificate, '0.0.1') do end diff --git a/lib/puppet/interface/v0.0.1/certificate_request.rb b/lib/puppet/interface/v0.0.1/certificate_request.rb index 169d6e500..e5ed1b51e 100644 --- a/lib/puppet/interface/v0.0.1/certificate_request.rb +++ b/lib/puppet/interface/v0.0.1/certificate_request.rb @@ -1,4 +1,4 @@ require 'puppet/interface/indirector' -Puppet::Interface::Indirector.interface(:certificate_request, '0.0.1') do +Puppet::Interface::Indirector.define(:certificate_request, '0.0.1') do end diff --git a/lib/puppet/interface/v0.0.1/certificate_revocation_list.rb b/lib/puppet/interface/v0.0.1/certificate_revocation_list.rb index 7f4f5176b..f6d8a3d6d 100644 --- a/lib/puppet/interface/v0.0.1/certificate_revocation_list.rb +++ b/lib/puppet/interface/v0.0.1/certificate_revocation_list.rb @@ -1,4 +1,4 @@ require 'puppet/interface/indirector' -Puppet::Interface::Indirector.interface(:certificate_revocation_list, '0.0.1') do +Puppet::Interface::Indirector.define(:certificate_revocation_list, '0.0.1') do end diff --git a/lib/puppet/interface/v0.0.1/config.rb b/lib/puppet/interface/v0.0.1/config.rb index 2437712ca..b33e19bb4 100644 --- a/lib/puppet/interface/v0.0.1/config.rb +++ b/lib/puppet/interface/v0.0.1/config.rb @@ -1,6 +1,6 @@ require 'puppet/interface' -Puppet::Interface.interface(:config, '0.0.1') do +Puppet::Interface.define(:config, '0.0.1') do action(:print) do invoke do |*args| Puppet.settings[:configprint] = args.join(",") diff --git a/lib/puppet/interface/v0.0.1/configurer.rb b/lib/puppet/interface/v0.0.1/configurer.rb index 0ab71c47a..38536b684 100644 --- a/lib/puppet/interface/v0.0.1/configurer.rb +++ b/lib/puppet/interface/v0.0.1/configurer.rb @@ -1,11 +1,11 @@ require 'puppet/interface' -Puppet::Interface.interface(:configurer, '0.0.1') do +Puppet::Interface.define(:configurer, '0.0.1') do action(:synchronize) do invoke do |certname| - facts = Puppet::Interface.interface(:facts, '0.0.1').find(certname) - catalog = Puppet::Interface.interface(:catalog, '0.0.1').download(certname, facts) - report = Puppet::Interface.interface(:catalog, '0.0.1').apply(catalog) + facts = Puppet::Interface[:facts, '0.0.1'].find(certname) + catalog = Puppet::Interface[:catalog, '0.0.1'].download(certname, facts) + report = Puppet::Interface[:catalog, '0.0.1'].apply(catalog) report end end diff --git a/lib/puppet/interface/v0.0.1/facts.rb b/lib/puppet/interface/v0.0.1/facts.rb index e987d0740..c4bbad845 100644 --- a/lib/puppet/interface/v0.0.1/facts.rb +++ b/lib/puppet/interface/v0.0.1/facts.rb @@ -1,7 +1,7 @@ require 'puppet/interface/indirector' require 'puppet/node/facts' -Puppet::Interface::Indirector.interface(:facts, '0.0.1') do +Puppet::Interface::Indirector.define(:facts, '0.0.1') do set_default_format :yaml # Upload our facts to the server diff --git a/lib/puppet/interface/v0.0.1/file.rb b/lib/puppet/interface/v0.0.1/file.rb index 2f27e434f..91904e8e0 100644 --- a/lib/puppet/interface/v0.0.1/file.rb +++ b/lib/puppet/interface/v0.0.1/file.rb @@ -1,5 +1,5 @@ require 'puppet/interface/indirector' -Puppet::Interface::Indirector.interface(:file, '0.0.1') do +Puppet::Interface::Indirector.define(:file, '0.0.1') do set_indirection_name :file_bucket_file end diff --git a/lib/puppet/interface/v0.0.1/key.rb b/lib/puppet/interface/v0.0.1/key.rb index ddd3a6ea8..fbc9b67b1 100644 --- a/lib/puppet/interface/v0.0.1/key.rb +++ b/lib/puppet/interface/v0.0.1/key.rb @@ -1,4 +1,4 @@ require 'puppet/interface/indirector' -Puppet::Interface::Indirector.interface(:key, '0.0.1') do +Puppet::Interface::Indirector.define(:key, '0.0.1') do end diff --git a/lib/puppet/interface/v0.0.1/node.rb b/lib/puppet/interface/v0.0.1/node.rb index 898426846..4ecec1478 100644 --- a/lib/puppet/interface/v0.0.1/node.rb +++ b/lib/puppet/interface/v0.0.1/node.rb @@ -1,5 +1,5 @@ require 'puppet/interface/indirector' -Puppet::Interface::Indirector.interface(:node, '0.0.1') do +Puppet::Interface::Indirector.define(:node, '0.0.1') do set_default_format :yaml end diff --git a/lib/puppet/interface/v0.0.1/report.rb b/lib/puppet/interface/v0.0.1/report.rb index 3f4e17310..bacb46e70 100644 --- a/lib/puppet/interface/v0.0.1/report.rb +++ b/lib/puppet/interface/v0.0.1/report.rb @@ -1,6 +1,6 @@ require 'puppet/interface/indirector' -Puppet::Interface::Indirector.interface(:report, '0.0.1') do +Puppet::Interface::Indirector.define(:report, '0.0.1') do action(:submit) do invoke do |report| begin diff --git a/lib/puppet/interface/v0.0.1/resource.rb b/lib/puppet/interface/v0.0.1/resource.rb index 61c90cc85..1a6f3b69d 100644 --- a/lib/puppet/interface/v0.0.1/resource.rb +++ b/lib/puppet/interface/v0.0.1/resource.rb @@ -1,4 +1,4 @@ require 'puppet/interface/indirector' -Puppet::Interface::Indirector.interface(:resource, '0.0.1') do +Puppet::Interface::Indirector.define(:resource, '0.0.1') do end diff --git a/lib/puppet/interface/v0.0.1/resource_type.rb b/lib/puppet/interface/v0.0.1/resource_type.rb index 9cc4bef28..6f5547c4d 100644 --- a/lib/puppet/interface/v0.0.1/resource_type.rb +++ b/lib/puppet/interface/v0.0.1/resource_type.rb @@ -1,4 +1,4 @@ require 'puppet/interface/indirector' -Puppet::Interface::Indirector.interface(:resource_type, '0.0.1') do +Puppet::Interface::Indirector.define(:resource_type, '0.0.1') do end diff --git a/lib/puppet/interface/v0.0.1/status.rb b/lib/puppet/interface/v0.0.1/status.rb index d8cded899..7f4b56a2b 100644 --- a/lib/puppet/interface/v0.0.1/status.rb +++ b/lib/puppet/interface/v0.0.1/status.rb @@ -1,4 +1,4 @@ require 'puppet/interface/indirector' -Puppet::Interface::Indirector.interface(:status, '0.0.1') do +Puppet::Interface::Indirector.define(:status, '0.0.1') do end -- cgit From 633f63cdbc1d5630e546041bb0c1e714216158d0 Mon Sep 17 00:00:00 2001 From: Daniel Pittman Date: Thu, 24 Mar 2011 13:33:30 -0700 Subject: (#6833) support 'script' as a short form of 'action' At the moment the action method is a fairly heavy tool: it provides a DSL, and is designed to allow substantial metadata to be added to the action. For some users this is low on value, since they just want to write a little script that drives things a bit differently. Which there is substantial value in the metadata, adding the capability to do these light-weight things quickly is valid. To meet this we add a script action; the contrast is: action :foo do # other metadata goes here invoke do |args| # method body goes here end end script :bar do |args| # method body goes here end # ...and if you want metadata, you have to add it in more ugly, procedural # ways, which we are not going to encourage. Reviewed-By: Pieter van de Bruggen --- lib/puppet/interface/action.rb | 12 +++++++++++- lib/puppet/interface/action_builder.rb | 9 +++------ lib/puppet/interface/action_manager.rb | 9 +++++++++ 3 files changed, 23 insertions(+), 7 deletions(-) (limited to 'lib/puppet/interface') diff --git a/lib/puppet/interface/action.rb b/lib/puppet/interface/action.rb index e4c2a4666..1c19bd08c 100644 --- a/lib/puppet/interface/action.rb +++ b/lib/puppet/interface/action.rb @@ -3,9 +3,11 @@ require 'puppet/interface' class Puppet::Interface::Action attr_reader :name - def initialize(interface, name) + def initialize(interface, name, attrs = {}) name = name.to_s raise "'#{name}' is an invalid action name" unless name =~ /^[a-z]\w*$/ + + attrs.each do |k,v| send("#{k}=", v) end @interface = interface @name = name end @@ -13,4 +15,12 @@ class Puppet::Interface::Action def invoke(*args, &block) @interface.method(name).call(*args,&block) end + + def invoke=(block) + if @interface.is_a?(Class) + @interface.define_method(@name, &block) + else + @interface.meta_def(@name, &block) + end + end end diff --git a/lib/puppet/interface/action_builder.rb b/lib/puppet/interface/action_builder.rb index e76fb1c6e..e389ea3ea 100644 --- a/lib/puppet/interface/action_builder.rb +++ b/lib/puppet/interface/action_builder.rb @@ -18,13 +18,10 @@ class Puppet::Interface::ActionBuilder end # Ideally the method we're defining here would be added to the action, and a - # method on the interface would defer to it + # method on the interface would defer to it, but we can't get scope correct, + # so we stick with this. --daniel 2011-03-24 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 + @action.invoke = block end end diff --git a/lib/puppet/interface/action_manager.rb b/lib/puppet/interface/action_manager.rb index 0db82d612..8b2944bb1 100644 --- a/lib/puppet/interface/action_manager.rb +++ b/lib/puppet/interface/action_manager.rb @@ -14,6 +14,15 @@ module Puppet::Interface::ActionManager @actions[name] = action end + # This is the short-form of an action definition; it doesn't use the + # builder, just creates the action directly from the block. + def script(name, &block) + @actions ||= {} + name = name.to_s.downcase.to_sym + raise "Action #{name} already defined for #{self}" if action?(name) + @actions[name] = Puppet::Interface::Action.new(self, name, :invoke => block) + end + def actions @actions ||= {} result = @actions.keys -- cgit From 53b0656048c3227048bdc317c5e917ad0c39e850 Mon Sep 17 00:00:00 2001 From: Luke Kanies Date: Fri, 25 Mar 2011 08:55:24 -0700 Subject: Config#print action always returns nil We were returning 'true', which was getting printed unnecessarily. Signed-off-by: Luke Kanies --- lib/puppet/interface/v0.0.1/config.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'lib/puppet/interface') diff --git a/lib/puppet/interface/v0.0.1/config.rb b/lib/puppet/interface/v0.0.1/config.rb index b33e19bb4..7b74ce542 100644 --- a/lib/puppet/interface/v0.0.1/config.rb +++ b/lib/puppet/interface/v0.0.1/config.rb @@ -5,6 +5,7 @@ Puppet::Interface.define(:config, '0.0.1') do invoke do |*args| Puppet.settings[:configprint] = args.join(",") Puppet.settings.print_config_options + nil end end end -- cgit From 78371a739bcf1b1d76496e9038fa4b076a27032f Mon Sep 17 00:00:00 2001 From: Pieter van de Bruggen Date: Thu, 24 Mar 2011 09:25:33 -0700 Subject: (#6770) Refactor Puppet::Interface#initialize. P::I#initialize now takes a name and a version (and an optional block). The options hash has been removed, though it may be reintroduced if a legitimate use case can be made for it (so far, it's only been used for the version number). Reviewed-By: Jacob Helwig --- lib/puppet/interface/interface_collection.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/puppet/interface') diff --git a/lib/puppet/interface/interface_collection.rb b/lib/puppet/interface/interface_collection.rb index d626c4f72..51b7534a0 100644 --- a/lib/puppet/interface/interface_collection.rb +++ b/lib/puppet/interface/interface_collection.rb @@ -9,7 +9,7 @@ module Puppet::Interface::InterfaceCollection $LOAD_PATH.each do |dir| next unless FileTest.directory?(dir) Dir.chdir(dir) do - Dir.glob("puppet/interface/*.rb").collect { |f| f.sub(/\.rb/, '') }.each do |file| + Dir.glob("puppet/interface/v*/*.rb").collect { |f| f.sub(/\.rb/, '') }.each do |file| iname = file.sub(/\.rb/, '') begin require iname -- cgit From 1af9bb232ed73f16789f465e89a0d498c39e1b78 Mon Sep 17 00:00:00 2001 From: Pieter van de Bruggen Date: Thu, 24 Mar 2011 15:06:21 -0700 Subject: (#6770) Add version lookup and comparison. Reviewed-By: Jacob Helwig --- lib/puppet/interface/interface_collection.rb | 35 ++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'lib/puppet/interface') diff --git a/lib/puppet/interface/interface_collection.rb b/lib/puppet/interface/interface_collection.rb index 51b7534a0..115892397 100644 --- a/lib/puppet/interface/interface_collection.rb +++ b/lib/puppet/interface/interface_collection.rb @@ -1,6 +1,8 @@ require 'puppet/interface' module Puppet::Interface::InterfaceCollection + SEMVER_VERSION = /^(\d+)\.(\d+)\.(\d+)([A-Za-z][0-9A-Za-z-]*|)$/ + @interfaces = Hash.new { |hash, key| hash[key] = {} } def self.interfaces @@ -24,6 +26,39 @@ module Puppet::Interface::InterfaceCollection return @interfaces.keys end + def self.versions(name) + versions = [] + $LOAD_PATH.each do |dir| + next unless FileTest.directory?(dir) + v_dir = File.join dir, %w[puppet interface v*] + Dir.glob(File.join v_dir, "#{name}{.rb,/*.rb}").each do |f| + v = f.sub(%r[.*/v([^/]+?)/#{name}(?:(?:/[^/]+)?.rb)$], '\1') + if v =~ SEMVER_VERSION + versions << v + else + warn "'#{v}' (#{f}) is not a valid version string; skipping" + end + end + end + return versions.uniq.sort { |a, b| compare_versions(a, b) } + end + + def self.compare_versions(a, b) + a, b = [a, b].map do |x| + parts = SEMVER_VERSION.match(x).to_a[1..4] + parts[0..2] = parts[0..2].map { |e| e.to_i } + parts + end + + cmp = a[0..2] <=> b[0..2] + if cmp == 0 + cmp = a[3] <=> b[3] + cmp = +1 if a[3].empty? && !b[3].empty? + cmp = -1 if b[3].empty? && !a[3].empty? + end + cmp + end + def self.[](name, version) @interfaces[underscorize(name)][version] if interface?(name, version) end -- cgit From 6aea116701b8e03558ef7a5a15766b267af14281 Mon Sep 17 00:00:00 2001 From: Pieter van de Bruggen Date: Thu, 24 Mar 2011 18:55:32 -0700 Subject: (#6770) Add support for version :latest. Specifying a version of `:latest` will find the most recent version of the named interface installed in your RUBYLIB, and attempt to load that. This is unlikely to provide a stable dependency in the future, so should be used sparingly, acknowledging the dangers. Reviewed-By: Daniel Pittman --- lib/puppet/interface/interface_collection.rb | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'lib/puppet/interface') diff --git a/lib/puppet/interface/interface_collection.rb b/lib/puppet/interface/interface_collection.rb index 115892397..92e2933fe 100644 --- a/lib/puppet/interface/interface_collection.rb +++ b/lib/puppet/interface/interface_collection.rb @@ -33,7 +33,7 @@ module Puppet::Interface::InterfaceCollection v_dir = File.join dir, %w[puppet interface v*] Dir.glob(File.join v_dir, "#{name}{.rb,/*.rb}").each do |f| v = f.sub(%r[.*/v([^/]+?)/#{name}(?:(?:/[^/]+)?.rb)$], '\1') - if v =~ SEMVER_VERSION + if validate_version(v) versions << v else warn "'#{v}' (#{f}) is not a valid version string; skipping" @@ -43,6 +43,10 @@ module Puppet::Interface::InterfaceCollection return versions.uniq.sort { |a, b| compare_versions(a, b) } end + def self.validate_version(version) + !!(SEMVER_VERSION =~ version.to_s) + end + def self.compare_versions(a, b) a, b = [a, b].map do |x| parts = SEMVER_VERSION.match(x).to_a[1..4] @@ -60,11 +64,18 @@ module Puppet::Interface::InterfaceCollection end def self.[](name, version) - @interfaces[underscorize(name)][version] if interface?(name, version) + version = versions(name).last if version == :latest + unless version.nil? + @interfaces[underscorize(name)][version] if interface?(name, version) + end end def self.interface?(name, version) + version = versions(name).last if version == :latest + return false if version.nil? + name = underscorize(name) + unless @interfaces.has_key?(name) && @interfaces[name].has_key?(version) require "puppet/interface/v#{version}/#{name}" end -- cgit From 88aeb04a50d8997b5e1e0ed7a5a2239508b174ee Mon Sep 17 00:00:00 2001 From: Daniel Pittman Date: Fri, 25 Mar 2011 10:38:40 -0700 Subject: MAINT: fix the misordered invocations in action. When initializing we need to set the name and interface before we do anything else, since the reasonable assumption for users is that those invariants are there when their setter is called. This allows someone to override the interface or name by misusing the call to new, but they could already screw up by passing the wrong values, so whatever. --- lib/puppet/interface/action.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/puppet/interface') diff --git a/lib/puppet/interface/action.rb b/lib/puppet/interface/action.rb index 1c19bd08c..1a5730d1b 100644 --- a/lib/puppet/interface/action.rb +++ b/lib/puppet/interface/action.rb @@ -7,9 +7,9 @@ class Puppet::Interface::Action name = name.to_s raise "'#{name}' is an invalid action name" unless name =~ /^[a-z]\w*$/ - attrs.each do |k,v| send("#{k}=", v) end @interface = interface - @name = name + @name = name + attrs.each do |k,v| send("#{k}=", v) end end def invoke(*args, &block) -- cgit From b859baa04737644e40002f511c5941d002a956e3 Mon Sep 17 00:00:00 2001 From: Daniel Pittman Date: Sat, 26 Mar 2011 00:12:17 -0700 Subject: MAINT: the API is officially named "string" as of this moment. Now that we have settled on the final public name for the API, "Puppet::String", mass-rename and mass-edit all the files to follow. Reviewed-By: Randall Hansen --- lib/puppet/interface/action.rb | 26 ------ lib/puppet/interface/action_builder.rb | 27 ------ lib/puppet/interface/action_manager.rb | 45 ---------- lib/puppet/interface/indirector.rb | 79 ----------------- lib/puppet/interface/interface_collection.rb | 98 ---------------------- lib/puppet/interface/v0.0.1/catalog.rb | 40 --------- lib/puppet/interface/v0.0.1/catalog/select.rb | 10 --- lib/puppet/interface/v0.0.1/certificate.rb | 28 ------- lib/puppet/interface/v0.0.1/certificate_request.rb | 4 - .../v0.0.1/certificate_revocation_list.rb | 4 - lib/puppet/interface/v0.0.1/config.rb | 11 --- lib/puppet/interface/v0.0.1/configurer.rb | 12 --- lib/puppet/interface/v0.0.1/facts.rb | 18 ---- lib/puppet/interface/v0.0.1/file.rb | 5 -- lib/puppet/interface/v0.0.1/key.rb | 4 - lib/puppet/interface/v0.0.1/node.rb | 5 -- lib/puppet/interface/v0.0.1/report.rb | 15 ---- lib/puppet/interface/v0.0.1/resource.rb | 4 - lib/puppet/interface/v0.0.1/resource_type.rb | 4 - lib/puppet/interface/v0.0.1/status.rb | 4 - 20 files changed, 443 deletions(-) delete mode 100644 lib/puppet/interface/action.rb delete mode 100644 lib/puppet/interface/action_builder.rb delete mode 100644 lib/puppet/interface/action_manager.rb delete mode 100644 lib/puppet/interface/indirector.rb delete mode 100644 lib/puppet/interface/interface_collection.rb delete mode 100644 lib/puppet/interface/v0.0.1/catalog.rb delete mode 100644 lib/puppet/interface/v0.0.1/catalog/select.rb delete mode 100644 lib/puppet/interface/v0.0.1/certificate.rb delete mode 100644 lib/puppet/interface/v0.0.1/certificate_request.rb delete mode 100644 lib/puppet/interface/v0.0.1/certificate_revocation_list.rb delete mode 100644 lib/puppet/interface/v0.0.1/config.rb delete mode 100644 lib/puppet/interface/v0.0.1/configurer.rb delete mode 100644 lib/puppet/interface/v0.0.1/facts.rb delete mode 100644 lib/puppet/interface/v0.0.1/file.rb delete mode 100644 lib/puppet/interface/v0.0.1/key.rb delete mode 100644 lib/puppet/interface/v0.0.1/node.rb delete mode 100644 lib/puppet/interface/v0.0.1/report.rb delete mode 100644 lib/puppet/interface/v0.0.1/resource.rb delete mode 100644 lib/puppet/interface/v0.0.1/resource_type.rb delete mode 100644 lib/puppet/interface/v0.0.1/status.rb (limited to 'lib/puppet/interface') diff --git a/lib/puppet/interface/action.rb b/lib/puppet/interface/action.rb deleted file mode 100644 index 1a5730d1b..000000000 --- a/lib/puppet/interface/action.rb +++ /dev/null @@ -1,26 +0,0 @@ -require 'puppet/interface' - -class Puppet::Interface::Action - attr_reader :name - - def initialize(interface, name, attrs = {}) - name = name.to_s - raise "'#{name}' is an invalid action name" unless name =~ /^[a-z]\w*$/ - - @interface = interface - @name = name - attrs.each do |k,v| send("#{k}=", v) end - end - - def invoke(*args, &block) - @interface.method(name).call(*args,&block) - end - - def invoke=(block) - if @interface.is_a?(Class) - @interface.define_method(@name, &block) - else - @interface.meta_def(@name, &block) - end - end -end diff --git a/lib/puppet/interface/action_builder.rb b/lib/puppet/interface/action_builder.rb deleted file mode 100644 index e389ea3ea..000000000 --- a/lib/puppet/interface/action_builder.rb +++ /dev/null @@ -1,27 +0,0 @@ -require 'puppet/interface' -require 'puppet/interface/action' - -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, but we can't get scope correct, - # so we stick with this. --daniel 2011-03-24 - def invoke(&block) - raise "Invoke called on an ActionBuilder with no corresponding Action" unless @action - @action.invoke = block - end -end diff --git a/lib/puppet/interface/action_manager.rb b/lib/puppet/interface/action_manager.rb deleted file mode 100644 index 8b2944bb1..000000000 --- a/lib/puppet/interface/action_manager.rb +++ /dev/null @@ -1,45 +0,0 @@ -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 ||= {} - name = name.to_s.downcase.to_sym - - raise "Action #{name} already defined for #{self}" if action?(name) - - action = Puppet::Interface::ActionBuilder.build(self, name, &block) - - @actions[name] = action - end - - # This is the short-form of an action definition; it doesn't use the - # builder, just creates the action directly from the block. - def script(name, &block) - @actions ||= {} - name = name.to_s.downcase.to_sym - raise "Action #{name} already defined for #{self}" if action?(name) - @actions[name] = Puppet::Interface::Action.new(self, name, :invoke => block) - end - - def actions - @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 - end - - def get_action(name) - @actions[name].dup - end - - def action?(name) - actions.include?(name.to_sym) - end -end diff --git a/lib/puppet/interface/indirector.rb b/lib/puppet/interface/indirector.rb deleted file mode 100644 index 485af4779..000000000 --- a/lib/puppet/interface/indirector.rb +++ /dev/null @@ -1,79 +0,0 @@ -require 'puppet' -require 'puppet/interface' - -class Puppet::Interface::Indirector < Puppet::Interface - 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 - - action :destroy do - invoke { |*args| call_indirection_method(:destroy, *args) } - end - - action :find do - invoke { |*args| call_indirection_method(:find, *args) } - end - - action :save do - invoke { |*args| call_indirection_method(:save, *args) } - end - - action :search do - invoke { |*args| call_indirection_method(:search, *args) } - end - - # Print the configuration for the current terminus class - 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 - - 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 interface. - def set_indirection_name(name) - @indirection_name = name - end - - # Return an indirection associated with an interface, if one exists - # One usually does. - def indirection - unless @indirection - Puppet.info("Could not find terminus for #{indirection_name}") unless @indirection = Puppet::Indirector::Indirection.instance(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 #{terminus_classes(indirection.name).join(", ") }" - end - end - - def call_indirection_method(method, *args) - begin - result = indirection.send(method, *args) - rescue => detail - puts detail.backtrace if Puppet[:trace] - raise "Could not call '#{method}' on '#{indirection_name}': #{detail}" - end - - result - end -end diff --git a/lib/puppet/interface/interface_collection.rb b/lib/puppet/interface/interface_collection.rb deleted file mode 100644 index 92e2933fe..000000000 --- a/lib/puppet/interface/interface_collection.rb +++ /dev/null @@ -1,98 +0,0 @@ -require 'puppet/interface' - -module Puppet::Interface::InterfaceCollection - SEMVER_VERSION = /^(\d+)\.(\d+)\.(\d+)([A-Za-z][0-9A-Za-z-]*|)$/ - - @interfaces = Hash.new { |hash, key| hash[key] = {} } - - def self.interfaces - unless @loaded - @loaded = true - $LOAD_PATH.each do |dir| - next unless FileTest.directory?(dir) - Dir.chdir(dir) do - Dir.glob("puppet/interface/v*/*.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 - end - end - return @interfaces.keys - end - - def self.versions(name) - versions = [] - $LOAD_PATH.each do |dir| - next unless FileTest.directory?(dir) - v_dir = File.join dir, %w[puppet interface v*] - Dir.glob(File.join v_dir, "#{name}{.rb,/*.rb}").each do |f| - v = f.sub(%r[.*/v([^/]+?)/#{name}(?:(?:/[^/]+)?.rb)$], '\1') - if validate_version(v) - versions << v - else - warn "'#{v}' (#{f}) is not a valid version string; skipping" - end - end - end - return versions.uniq.sort { |a, b| compare_versions(a, b) } - end - - def self.validate_version(version) - !!(SEMVER_VERSION =~ version.to_s) - end - - def self.compare_versions(a, b) - a, b = [a, b].map do |x| - parts = SEMVER_VERSION.match(x).to_a[1..4] - parts[0..2] = parts[0..2].map { |e| e.to_i } - parts - end - - cmp = a[0..2] <=> b[0..2] - if cmp == 0 - cmp = a[3] <=> b[3] - cmp = +1 if a[3].empty? && !b[3].empty? - cmp = -1 if b[3].empty? && !a[3].empty? - end - cmp - end - - def self.[](name, version) - version = versions(name).last if version == :latest - unless version.nil? - @interfaces[underscorize(name)][version] if interface?(name, version) - end - end - - def self.interface?(name, version) - version = versions(name).last if version == :latest - return false if version.nil? - - name = underscorize(name) - - unless @interfaces.has_key?(name) && @interfaces[name].has_key?(version) - require "puppet/interface/v#{version}/#{name}" - end - return @interfaces.has_key?(name) && @interfaces[name].has_key?(version) - rescue LoadError - return false - end - - def self.register(interface) - @interfaces[underscorize(interface.name)][interface.version] = interface - end - - 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.downcase.split(/[-_]/).join('_').to_sym - end -end diff --git a/lib/puppet/interface/v0.0.1/catalog.rb b/lib/puppet/interface/v0.0.1/catalog.rb deleted file mode 100644 index 7d61528bc..000000000 --- a/lib/puppet/interface/v0.0.1/catalog.rb +++ /dev/null @@ -1,40 +0,0 @@ -require 'puppet/interface/indirector' - -Puppet::Interface::Indirector.define(:catalog, '0.0.1') do - action(:apply) do - invoke do |catalog| - report = Puppet::Transaction::Report.new("apply") - report.configuration_version = catalog.version - - Puppet::Util::Log.newdestination(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 - - report.finalize_report - report - end - end - - 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, '0.0.1'].find(certname, facts_to_upload) - end - catalog = catalog.to_ral - catalog.finalize - catalog.retrieval_duration = retrieval_duration - catalog.write_class_file - catalog - end - end -end diff --git a/lib/puppet/interface/v0.0.1/catalog/select.rb b/lib/puppet/interface/v0.0.1/catalog/select.rb deleted file mode 100644 index 35f1a1e0b..000000000 --- a/lib/puppet/interface/v0.0.1/catalog/select.rb +++ /dev/null @@ -1,10 +0,0 @@ -# Select and show a list of resources of a given type. -Puppet::Interface.define(:catalog, '0.0.1') do - action :select do - invoke do |host,type| - catalog = Puppet::Resource::Catalog.indirection.find(host) - - catalog.resources.reject { |res| res.type != type }.each { |res| puts res } - end - end -end diff --git a/lib/puppet/interface/v0.0.1/certificate.rb b/lib/puppet/interface/v0.0.1/certificate.rb deleted file mode 100644 index 2615e3d86..000000000 --- a/lib/puppet/interface/v0.0.1/certificate.rb +++ /dev/null @@ -1,28 +0,0 @@ -require 'puppet/interface/indirector' -require 'puppet/ssl/host' - -Puppet::Interface::Indirector.define(:certificate, '0.0.1') do - - action :generate do - invoke do |name| - host = Puppet::SSL::Host.new(name) - host.generate_certificate_request - host.certificate_request.class.indirection.save(host.certificate_request) - end - end - - action :list do - invoke do - Puppet::SSL::Host.indirection.search("*", { - :for => :certificate_request, - }).map { |h| h.inspect } - end - end - - action :sign do - invoke do |name| - Puppet::SSL::Host.indirection.save(Puppet::SSL::Host.new(name)) - end - end - -end diff --git a/lib/puppet/interface/v0.0.1/certificate_request.rb b/lib/puppet/interface/v0.0.1/certificate_request.rb deleted file mode 100644 index e5ed1b51e..000000000 --- a/lib/puppet/interface/v0.0.1/certificate_request.rb +++ /dev/null @@ -1,4 +0,0 @@ -require 'puppet/interface/indirector' - -Puppet::Interface::Indirector.define(:certificate_request, '0.0.1') do -end diff --git a/lib/puppet/interface/v0.0.1/certificate_revocation_list.rb b/lib/puppet/interface/v0.0.1/certificate_revocation_list.rb deleted file mode 100644 index f6d8a3d6d..000000000 --- a/lib/puppet/interface/v0.0.1/certificate_revocation_list.rb +++ /dev/null @@ -1,4 +0,0 @@ -require 'puppet/interface/indirector' - -Puppet::Interface::Indirector.define(:certificate_revocation_list, '0.0.1') do -end diff --git a/lib/puppet/interface/v0.0.1/config.rb b/lib/puppet/interface/v0.0.1/config.rb deleted file mode 100644 index 7b74ce542..000000000 --- a/lib/puppet/interface/v0.0.1/config.rb +++ /dev/null @@ -1,11 +0,0 @@ -require 'puppet/interface' - -Puppet::Interface.define(:config, '0.0.1') do - action(:print) do - invoke do |*args| - Puppet.settings[:configprint] = args.join(",") - Puppet.settings.print_config_options - nil - end - end -end diff --git a/lib/puppet/interface/v0.0.1/configurer.rb b/lib/puppet/interface/v0.0.1/configurer.rb deleted file mode 100644 index 38536b684..000000000 --- a/lib/puppet/interface/v0.0.1/configurer.rb +++ /dev/null @@ -1,12 +0,0 @@ -require 'puppet/interface' - -Puppet::Interface.define(:configurer, '0.0.1') do - action(:synchronize) do - invoke do |certname| - facts = Puppet::Interface[:facts, '0.0.1'].find(certname) - catalog = Puppet::Interface[:catalog, '0.0.1'].download(certname, facts) - report = Puppet::Interface[:catalog, '0.0.1'].apply(catalog) - report - end - end -end diff --git a/lib/puppet/interface/v0.0.1/facts.rb b/lib/puppet/interface/v0.0.1/facts.rb deleted file mode 100644 index c4bbad845..000000000 --- a/lib/puppet/interface/v0.0.1/facts.rb +++ /dev/null @@ -1,18 +0,0 @@ -require 'puppet/interface/indirector' -require 'puppet/node/facts' - -Puppet::Interface::Indirector.define(:facts, '0.0.1') do - set_default_format :yaml - - # Upload our facts to the server - 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/v0.0.1/file.rb b/lib/puppet/interface/v0.0.1/file.rb deleted file mode 100644 index 91904e8e0..000000000 --- a/lib/puppet/interface/v0.0.1/file.rb +++ /dev/null @@ -1,5 +0,0 @@ -require 'puppet/interface/indirector' - -Puppet::Interface::Indirector.define(:file, '0.0.1') do - set_indirection_name :file_bucket_file -end diff --git a/lib/puppet/interface/v0.0.1/key.rb b/lib/puppet/interface/v0.0.1/key.rb deleted file mode 100644 index fbc9b67b1..000000000 --- a/lib/puppet/interface/v0.0.1/key.rb +++ /dev/null @@ -1,4 +0,0 @@ -require 'puppet/interface/indirector' - -Puppet::Interface::Indirector.define(:key, '0.0.1') do -end diff --git a/lib/puppet/interface/v0.0.1/node.rb b/lib/puppet/interface/v0.0.1/node.rb deleted file mode 100644 index 4ecec1478..000000000 --- a/lib/puppet/interface/v0.0.1/node.rb +++ /dev/null @@ -1,5 +0,0 @@ -require 'puppet/interface/indirector' - -Puppet::Interface::Indirector.define(:node, '0.0.1') do - set_default_format :yaml -end diff --git a/lib/puppet/interface/v0.0.1/report.rb b/lib/puppet/interface/v0.0.1/report.rb deleted file mode 100644 index bacb46e70..000000000 --- a/lib/puppet/interface/v0.0.1/report.rb +++ /dev/null @@ -1,15 +0,0 @@ -require 'puppet/interface/indirector' - -Puppet::Interface::Indirector.define(:report, '0.0.1') do - 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 diff --git a/lib/puppet/interface/v0.0.1/resource.rb b/lib/puppet/interface/v0.0.1/resource.rb deleted file mode 100644 index 1a6f3b69d..000000000 --- a/lib/puppet/interface/v0.0.1/resource.rb +++ /dev/null @@ -1,4 +0,0 @@ -require 'puppet/interface/indirector' - -Puppet::Interface::Indirector.define(:resource, '0.0.1') do -end diff --git a/lib/puppet/interface/v0.0.1/resource_type.rb b/lib/puppet/interface/v0.0.1/resource_type.rb deleted file mode 100644 index 6f5547c4d..000000000 --- a/lib/puppet/interface/v0.0.1/resource_type.rb +++ /dev/null @@ -1,4 +0,0 @@ -require 'puppet/interface/indirector' - -Puppet::Interface::Indirector.define(:resource_type, '0.0.1') do -end diff --git a/lib/puppet/interface/v0.0.1/status.rb b/lib/puppet/interface/v0.0.1/status.rb deleted file mode 100644 index 7f4b56a2b..000000000 --- a/lib/puppet/interface/v0.0.1/status.rb +++ /dev/null @@ -1,4 +0,0 @@ -require 'puppet/interface/indirector' - -Puppet::Interface::Indirector.define(:status, '0.0.1') do -end -- cgit From 87ed3188e65d3f5f9c2c32a409b271d1b39684b9 Mon Sep 17 00:00:00 2001 From: Daniel Pittman Date: Thu, 7 Apr 2011 15:44:28 -0700 Subject: (#7012) Split plumbing into Puppet::Interface This splits out the plumbing into the Puppet::Interface namespace, and uses Puppet::Faces for all the public-facing code. The fault line is "what you care about if you are using or writing a face", which is public, against "what you care about to enable either of those two", which is the plumbing. --- lib/puppet/interface/action.rb | 119 ++++++++++++++++++++++++++++++ lib/puppet/interface/action_builder.rb | 31 ++++++++ lib/puppet/interface/action_manager.rb | 49 +++++++++++++ lib/puppet/interface/face_collection.rb | 125 ++++++++++++++++++++++++++++++++ lib/puppet/interface/option.rb | 82 +++++++++++++++++++++ lib/puppet/interface/option_builder.rb | 25 +++++++ lib/puppet/interface/option_manager.rb | 56 ++++++++++++++ 7 files changed, 487 insertions(+) create mode 100644 lib/puppet/interface/action.rb create mode 100644 lib/puppet/interface/action_builder.rb create mode 100644 lib/puppet/interface/action_manager.rb create mode 100644 lib/puppet/interface/face_collection.rb create mode 100644 lib/puppet/interface/option.rb create mode 100644 lib/puppet/interface/option_builder.rb create mode 100644 lib/puppet/interface/option_manager.rb (limited to 'lib/puppet/interface') diff --git a/lib/puppet/interface/action.rb b/lib/puppet/interface/action.rb new file mode 100644 index 000000000..e4a37a1f7 --- /dev/null +++ b/lib/puppet/interface/action.rb @@ -0,0 +1,119 @@ +# -*- coding: utf-8 -*- +require 'puppet/interface' +require 'puppet/interface/option' + +class Puppet::Interface::Action + def initialize(face, name, attrs = {}) + raise "#{name.inspect} is an invalid action name" unless name.to_s =~ /^[a-z]\w*$/ + @face = face + @name = name.to_sym + @options = {} + attrs.each do |k, v| send("#{k}=", v) end + end + + attr_reader :name + def to_s() "#{@face}##{@name}" end + + + # Initially, this was defined to allow the @action.invoke pattern, which is + # a very natural way to invoke behaviour given our introspection + # capabilities. Heck, our initial plan was to have the faces delegate to + # the action object for invocation and all. + # + # It turns out that we have a binding problem to solve: @face was bound to + # the parent class, not the subclass instance, and we don't pass the + # appropriate context or change the binding enough to make this work. + # + # We could hack around it, by either mandating that you pass the context in + # to invoke, or try to get the binding right, but that has probably got + # subtleties that we don't instantly think of – especially around threads. + # + # So, we are pulling this method for now, and will return it to life when we + # have the time to resolve the problem. For now, you should replace... + # + # @action = @face.get_action(name) + # @action.invoke(arg1, arg2, arg3) + # + # ...with... + # + # @action = @face.get_action(name) + # @face.send(@action.name, arg1, arg2, arg3) + # + # I understand that is somewhat cumbersome, but it functions as desired. + # --daniel 2011-03-31 + # + # PS: This code is left present, but commented, to support this chunk of + # documentation, for the benefit of the reader. + # + # def invoke(*args, &block) + # @face.send(name, *args, &block) + # end + + def when_invoked=(block) + # We need to build an instance method as a wrapper, using normal code, to + # be able to expose argument defaulting between the caller and definer in + # the Ruby API. An extra method is, sadly, required for Ruby 1.8 to work. + # + # In future this also gives us a place to hook in additional behaviour + # such as calling out to the action instance to validate and coerce + # parameters, which avoids any exciting context switching and all. + # + # Hopefully we can improve this when we finally shuffle off the last of + # Ruby 1.8 support, but that looks to be a few "enterprise" release eras + # away, so we are pretty stuck with this for now. + # + # Patches to make this work more nicely with Ruby 1.9 using runtime + # version checking and all are welcome, but they can't actually help if + # the results are not totally hidden away in here. + # + # Incidentally, we though about vendoring evil-ruby and actually adjusting + # the internal C structure implementation details under the hood to make + # this stuff work, because it would have been cleaner. Which gives you an + # idea how motivated we were to make this cleaner. Sorry. --daniel 2011-03-31 + + internal_name = "#{@name} implementation, required on Ruby 1.8".to_sym + file = __FILE__ + "+eval" + line = __LINE__ + 1 + wrapper = "def #{@name}(*args, &block) + args << {} unless args.last.is_a? Hash + args << block if block_given? + self.__send__(#{internal_name.inspect}, *args) + end" + + if @face.is_a?(Class) + @face.class_eval do eval wrapper, nil, file, line end + @face.define_method(internal_name, &block) + else + @face.instance_eval do eval wrapper, nil, file, line end + @face.meta_def(internal_name, &block) + end + end + + def add_option(option) + option.aliases.each do |name| + if conflict = get_option(name) then + raise ArgumentError, "Option #{option} conflicts with existing option #{conflict}" + elsif conflict = @face.get_option(name) then + raise ArgumentError, "Option #{option} conflicts with existing option #{conflict} on #{@face}" + end + end + + option.aliases.each do |name| + @options[name] = option + end + + option + end + + def option?(name) + @options.include? name.to_sym + end + + def options + (@options.keys + @face.options).sort + end + + def get_option(name) + @options[name.to_sym] || @face.get_option(name) + end +end diff --git a/lib/puppet/interface/action_builder.rb b/lib/puppet/interface/action_builder.rb new file mode 100644 index 000000000..b08c3d023 --- /dev/null +++ b/lib/puppet/interface/action_builder.rb @@ -0,0 +1,31 @@ +require 'puppet/interface' +require 'puppet/interface/action' + +class Puppet::Interface::ActionBuilder + attr_reader :action + + def self.build(face, name, &block) + raise "Action #{name.inspect} must specify a block" unless block + new(face, name, &block).action + end + + private + def initialize(face, name, &block) + @face = face + @action = Puppet::Interface::Action.new(face, name) + instance_eval(&block) + end + + # Ideally the method we're defining here would be added to the action, and a + # method on the face would defer to it, but we can't get scope correct, so + # we stick with this. --daniel 2011-03-24 + def when_invoked(&block) + raise "when_invoked on an ActionBuilder with no corresponding Action" unless @action + @action.when_invoked = block + end + + def option(*declaration, &block) + option = Puppet::Interface::OptionBuilder.build(@action, *declaration, &block) + @action.add_option(option) + end +end diff --git a/lib/puppet/interface/action_manager.rb b/lib/puppet/interface/action_manager.rb new file mode 100644 index 000000000..bb0e5bf57 --- /dev/null +++ b/lib/puppet/interface/action_manager.rb @@ -0,0 +1,49 @@ +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 ||= {} + raise "Action #{name} already defined for #{self}" if action?(name) + action = Puppet::Interface::ActionBuilder.build(self, name, &block) + @actions[action.name] = action + end + + # This is the short-form of an action definition; it doesn't use the + # builder, just creates the action directly from the block. + def script(name, &block) + @actions ||= {} + raise "Action #{name} already defined for #{self}" if action?(name) + @actions[name] = Puppet::Interface::Action.new(self, name, :when_invoked => block) + end + + def actions + @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 + end + + def get_action(name) + @actions ||= {} + result = @actions[name.to_sym] + if result.nil? + if self.is_a?(Class) and superclass.respond_to?(:get_action) + result = superclass.get_action(name) + elsif self.class.respond_to?(:get_action) + result = self.class.get_action(name) + end + end + return result + end + + def action?(name) + actions.include?(name.to_sym) + end +end diff --git a/lib/puppet/interface/face_collection.rb b/lib/puppet/interface/face_collection.rb new file mode 100644 index 000000000..9f7a499c2 --- /dev/null +++ b/lib/puppet/interface/face_collection.rb @@ -0,0 +1,125 @@ +# -*- coding: utf-8 -*- +require 'puppet/interface' + +module Puppet::Interface::FaceCollection + SEMVER_VERSION = /^(\d+)\.(\d+)\.(\d+)([A-Za-z][0-9A-Za-z-]*|)$/ + + @faces = Hash.new { |hash, key| hash[key] = {} } + + def self.faces + unless @loaded + @loaded = true + $LOAD_PATH.each do |dir| + next unless FileTest.directory?(dir) + Dir.chdir(dir) do + # REVISIT: This is wrong!!!! We don't name files like that ever, + # so we should no longer match things like this. Damnit!!! --daniel 2011-04-07 + Dir.glob("puppet/faces/v*/*.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 + end + end + return @faces.keys + end + + def self.validate_version(version) + !!(SEMVER_VERSION =~ version.to_s) + end + + def self.cmp_semver(a, b) + a, b = [a, b].map do |x| + parts = SEMVER_VERSION.match(x).to_a[1..4] + parts[0..2] = parts[0..2].map { |e| e.to_i } + parts + end + + cmp = a[0..2] <=> b[0..2] + if cmp == 0 + cmp = a[3] <=> b[3] + cmp = +1 if a[3].empty? && !b[3].empty? + cmp = -1 if b[3].empty? && !a[3].empty? + end + cmp + end + + def self.[](name, version) + @faces[underscorize(name)][version] if face?(name, version) + end + + def self.face?(name, version) + name = underscorize(name) + return true if @faces[name].has_key?(version) + + # We always load the current version file; the common case is that we have + # the expected version and any compatibility versions in the same file, + # the default. Which means that this is almost always the case. + # + # We use require to avoid executing the code multiple times, like any + # other Ruby library that we might want to use. --daniel 2011-04-06 + begin + require "puppet/faces/#{name}" + + # If we wanted :current, we need to index to find that; direct version + # requests just work™ as they go. --daniel 2011-04-06 + if version == :current then + # We need to find current out of this. This is the largest version + # number that doesn't have a dedicated on-disk file present; those + # represent "experimental" versions of faces, which we don't fully + # support yet. + # + # We walk the versions from highest to lowest and take the first version + # that is not defined in an explicitly versioned file on disk as the + # current version. + # + # This constrains us to only ship experimental versions with *one* + # version in the file, not multiple, but given you can't reliably load + # them except by side-effect when you ignore that rule this seems safe + # enough... + # + # Given those constraints, and that we are not going to ship a versioned + # interface that is not :current in this release, we are going to leave + # these thoughts in place, and just punt on the actual versioning. + # + # When we upgrade the core to support multiple versions we can solve the + # problems then; as lazy as possible. + # + # We do support multiple versions in the same file, though, so we sort + # versions here and return the last item in that set. + # + # --daniel 2011-04-06 + latest_ver = @faces[name].keys.sort {|a, b| cmp_semver(a, b) }.last + @faces[name][:current] = @faces[name][latest_ver] + end + rescue LoadError => e + raise unless e.message =~ %r{-- puppet/faces/#{name}$} + # ...guess we didn't find the file; return a much better problem. + end + + # Now, either we have the version in our set of faces, or we didn't find + # the version they were looking for. In the future we will support + # loading versioned stuff from some look-aside part of the Ruby load path, + # but we don't need that right now. + # + # So, this comment is a place-holder for that. --daniel 2011-04-06 + return !! @faces[name].has_key?(version) + end + + def self.register(face) + @faces[underscorize(face.name)][face.version] = face + end + + def self.underscorize(name) + unless name.to_s =~ /^[-_a-z]+$/i then + raise ArgumentError, "#{name.inspect} (#{name.class}) is not a valid face name" + end + + name.to_s.downcase.split(/[-_]/).join('_').to_sym + end +end diff --git a/lib/puppet/interface/option.rb b/lib/puppet/interface/option.rb new file mode 100644 index 000000000..ccc2fbba7 --- /dev/null +++ b/lib/puppet/interface/option.rb @@ -0,0 +1,82 @@ +require 'puppet/interface' + +class Puppet::Interface::Option + attr_reader :parent + attr_reader :name + attr_reader :aliases + attr_reader :optparse + attr_accessor :desc + + def takes_argument? + !!@argument + end + def optional_argument? + !!@optional_argument + end + + def initialize(parent, *declaration, &block) + @parent = parent + @optparse = [] + + # Collect and sort the arguments in the declaration. + dups = {} + declaration.each do |item| + if item.is_a? String and item.to_s =~ /^-/ then + unless item =~ /^-[a-z]\b/ or item =~ /^--[^-]/ then + raise ArgumentError, "#{item.inspect}: long options need two dashes (--)" + end + @optparse << item + + # Duplicate checking... + name = optparse_to_name(item) + if dup = dups[name] then + raise ArgumentError, "#{item.inspect}: duplicates existing alias #{dup.inspect} in #{@parent}" + else + dups[name] = item + end + else + raise ArgumentError, "#{item.inspect} is not valid for an option argument" + end + end + + if @optparse.empty? then + raise ArgumentError, "No option declarations found while building" + end + + # Now, infer the name from the options; we prefer the first long option as + # the name, rather than just the first option. + @name = optparse_to_name(@optparse.find do |a| a =~ /^--/ end || @optparse.first) + @aliases = @optparse.map { |o| optparse_to_name(o) } + + # Do we take an argument? If so, are we consistent about it, because + # incoherence here makes our life super-difficult, and we can more easily + # relax this rule later if we find a valid use case for it. --daniel 2011-03-30 + @argument = @optparse.any? { |o| o =~ /[ =]/ } + if @argument and not @optparse.all? { |o| o =~ /[ =]/ } then + raise ArgumentError, "Option #{@name} is inconsistent about taking an argument" + end + + # Is our argument optional? The rules about consistency apply here, also, + # just like they do to taking arguments at all. --daniel 2011-03-30 + @optional_argument = @optparse.any? { |o| o.include? "[" } + if @optional_argument and not @optparse.all? { |o| o.include? "[" } then + raise ArgumentError, "Option #{@name} is inconsistent about the argument being optional" + end + end + + # to_s and optparse_to_name are roughly mirrored, because they are used to + # transform options to name symbols, and vice-versa. This isn't a full + # bidirectional transformation though. --daniel 2011-04-07 + def to_s + @name.to_s.tr('_', '-') + end + + def optparse_to_name(declaration) + unless found = declaration.match(/^-+(?:\[no-\])?([^ =]+)/) then + raise ArgumentError, "Can't find a name in the declaration #{declaration.inspect}" + end + name = found.captures.first.tr('-', '_') + raise "#{name.inspect} is an invalid option name" unless name.to_s =~ /^[a-z]\w*$/ + name.to_sym + end +end diff --git a/lib/puppet/interface/option_builder.rb b/lib/puppet/interface/option_builder.rb new file mode 100644 index 000000000..83a1906b0 --- /dev/null +++ b/lib/puppet/interface/option_builder.rb @@ -0,0 +1,25 @@ +require 'puppet/interface/option' + +class Puppet::Interface::OptionBuilder + attr_reader :option + + def self.build(face, *declaration, &block) + new(face, *declaration, &block).option + end + + private + def initialize(face, *declaration, &block) + @face = face + @option = Puppet::Interface::Option.new(face, *declaration) + block and instance_eval(&block) + @option + end + + # Metaprogram the simple DSL from the option class. + Puppet::Interface::Option.instance_methods.grep(/=$/).each do |setter| + next if setter =~ /^=/ # special case, darn it... + + dsl = setter.sub(/=$/, '') + define_method(dsl) do |value| @option.send(setter, value) end + end +end diff --git a/lib/puppet/interface/option_manager.rb b/lib/puppet/interface/option_manager.rb new file mode 100644 index 000000000..56df9760f --- /dev/null +++ b/lib/puppet/interface/option_manager.rb @@ -0,0 +1,56 @@ +require 'puppet/interface/option_builder' + +module Puppet::Interface::OptionManager + # Declare that this app can take a specific option, and provide + # the code to do so. + def option(*declaration, &block) + add_option Puppet::Interface::OptionBuilder.build(self, *declaration, &block) + end + + def add_option(option) + option.aliases.each do |name| + if conflict = get_option(name) then + raise ArgumentError, "Option #{option} conflicts with existing option #{conflict}" + end + + actions.each do |action| + action = get_action(action) + if conflict = action.get_option(name) then + raise ArgumentError, "Option #{option} conflicts with existing option #{conflict} on #{action}" + end + end + end + + option.aliases.each { |name| @options[name] = option } + option + end + + def options + @options ||= {} + result = @options.keys + + if self.is_a?(Class) and superclass.respond_to?(:options) + result += superclass.options + elsif self.class.respond_to?(:options) + result += self.class.options + end + result.sort + end + + def get_option(name) + @options ||= {} + result = @options[name.to_sym] + unless result then + if self.is_a?(Class) and superclass.respond_to?(:get_option) + result = superclass.get_option(name) + elsif self.class.respond_to?(:get_option) + result = self.class.get_option(name) + end + end + return result + end + + def option?(name) + options.include? name.to_sym + end +end -- cgit