summaryrefslogtreecommitdiffstats
path: root/lib/puppet
diff options
context:
space:
mode:
authorLuke Kanies <luke@puppetlabs.com>2011-01-30 17:33:28 -0800
committerLuke Kanies <luke@puppetlabs.com>2011-01-30 17:33:28 -0800
commit809aebec7a54be90990b9ee5fea1f85204598f17 (patch)
tree9adc1e05d66fbec9772c7b379bbb52976b9b5753 /lib/puppet
downloadpuppet-809aebec7a54be90990b9ee5fea1f85204598f17.tar.gz
puppet-809aebec7a54be90990b9ee5fea1f85204598f17.tar.xz
puppet-809aebec7a54be90990b9ee5fea1f85204598f17.zip
Moving data executables to their own module
Signed-off-by: Luke Kanies <luke@puppetlabs.com>
Diffstat (limited to 'lib/puppet')
-rw-r--r--lib/puppet/application/catalog.rb4
-rw-r--r--lib/puppet/application/certificate.rb4
-rw-r--r--lib/puppet/application/certificate_request.rb4
-rw-r--r--lib/puppet/application/certificate_revocation_list.rb4
-rw-r--r--lib/puppet/application/data.rb84
-rw-r--r--lib/puppet/application/data_baseclass.rb80
-rw-r--r--lib/puppet/application/facts.rb4
-rw-r--r--lib/puppet/application/file_bucket_file.rb4
-rw-r--r--lib/puppet/application/inventory.rb4
-rw-r--r--lib/puppet/application/key.rb4
-rw-r--r--lib/puppet/application/node.rb4
-rw-r--r--lib/puppet/application/report.rb4
-rw-r--r--lib/puppet/application/resource_type.rb4
-rw-r--r--lib/puppet/application/status.rb4
-rw-r--r--lib/puppet/interface.rb145
-rw-r--r--lib/puppet/interface/catalog.rb4
-rw-r--r--lib/puppet/interface/catalog/select.rb4
-rw-r--r--lib/puppet/interface/certificate.rb4
-rw-r--r--lib/puppet/interface/certificate_request.rb4
-rw-r--r--lib/puppet/interface/certificate_revocation_list.rb4
-rw-r--r--lib/puppet/interface/facts.rb10
-rw-r--r--lib/puppet/interface/file_bucket_file.rb4
-rw-r--r--lib/puppet/interface/inventory.rb4
-rw-r--r--lib/puppet/interface/key.rb4
-rw-r--r--lib/puppet/interface/node.rb4
-rw-r--r--lib/puppet/interface/report.rb4
-rw-r--r--lib/puppet/interface/resource_type.rb4
-rw-r--r--lib/puppet/interface/status.rb4
28 files changed, 415 insertions, 0 deletions
diff --git a/lib/puppet/application/catalog.rb b/lib/puppet/application/catalog.rb
new file mode 100644
index 000000000..536d79c29
--- /dev/null
+++ b/lib/puppet/application/catalog.rb
@@ -0,0 +1,4 @@
+require 'puppet/application/data_baseclass'
+
+class Puppet::Application::Catalog < Puppet::Application::DataBaseclass
+end
diff --git a/lib/puppet/application/certificate.rb b/lib/puppet/application/certificate.rb
new file mode 100644
index 000000000..708de07bb
--- /dev/null
+++ b/lib/puppet/application/certificate.rb
@@ -0,0 +1,4 @@
+require 'puppet/application/data_baseclass'
+
+class Puppet::Application::Certificate < Puppet::Application::DataBaseclass
+end
diff --git a/lib/puppet/application/certificate_request.rb b/lib/puppet/application/certificate_request.rb
new file mode 100644
index 000000000..4363fc1ae
--- /dev/null
+++ b/lib/puppet/application/certificate_request.rb
@@ -0,0 +1,4 @@
+require 'puppet/application/data_baseclass'
+
+class Puppet::Application::Certificate_request < Puppet::Application::DataBaseclass
+end
diff --git a/lib/puppet/application/certificate_revocation_list.rb b/lib/puppet/application/certificate_revocation_list.rb
new file mode 100644
index 000000000..158ed7b20
--- /dev/null
+++ b/lib/puppet/application/certificate_revocation_list.rb
@@ -0,0 +1,4 @@
+require 'puppet/application/data_baseclass'
+
+class Puppet::Application::Certificate_revocation_list < Puppet::Application::DataBaseclass
+end
diff --git a/lib/puppet/application/data.rb b/lib/puppet/application/data.rb
new file mode 100644
index 000000000..cfbf4305a
--- /dev/null
+++ b/lib/puppet/application/data.rb
@@ -0,0 +1,84 @@
+require 'puppet/application'
+
+class Puppet::Application::Data < Puppet::Application
+
+ should_parse_config
+ run_mode :agent
+
+ option("--debug", "-d") do |arg|
+ Puppet::Util::Log.level = :debug
+ end
+
+ option("--verbose", "-v") do
+ Puppet::Util::Log.level = :info
+ end
+
+ def list(*arguments)
+ if arguments.empty?
+ arguments = %w{terminuses actions}
+ end
+ indirections.each do |ind|
+ str = "#{ind}:\n"
+ if arguments.include?("terminuses")
+ begin
+ terms = terminus_classes(ind.to_sym)
+ str << "\tTerminuses: #{terms.join(", ")}\n"
+ rescue => detail
+ $stderr.puts "Could not load terminuses for #{ind}: #{detail}"
+ end
+ end
+
+ if arguments.include?("actions")
+ begin
+ actions = actions(ind.to_sym)
+ str << "\tActions: #{actions.join(", ")}\n"
+ rescue => detail
+ $stderr.puts "Could not load actions for #{ind}: #{detail}"
+ end
+ end
+
+ print str
+ end
+ exit(0)
+ end
+
+ attr_accessor :verb, :name, :arguments
+
+ def main
+ # Call the method associated with the provided action (e.g., 'find').
+ send(verb, *arguments)
+ end
+
+ def setup
+ Puppet::Util::Log.newdestination :console
+
+ @verb, @arguments = command_line.args
+ @arguments ||= []
+
+ validate
+ end
+
+ def validate
+ unless verb
+ raise "You must specify 'find', 'search', 'save', or 'destroy' as a verb; 'save' probably does not work right now"
+ end
+
+ unless respond_to?(verb)
+ raise "Command '#{verb}' not found for 'data'"
+ end
+ 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
+
+ def actions(indirection)
+ return [] unless app = Puppet::Application.find(indirection)
+ return app.actions.sort { |a,b| a.to_s <=> b.to_s }
+ end
+end
+
diff --git a/lib/puppet/application/data_baseclass.rb b/lib/puppet/application/data_baseclass.rb
new file mode 100644
index 000000000..95142b8ba
--- /dev/null
+++ b/lib/puppet/application/data_baseclass.rb
@@ -0,0 +1,80 @@
+require 'puppet/application'
+require 'puppet/interface'
+
+class Puppet::Application::DataBaseclass < Puppet::Application
+ should_parse_config
+ run_mode :agent
+
+ option("--debug", "-d") do |arg|
+ Puppet::Util::Log.level = :debug
+ end
+
+ option("--verbose", "-v") do
+ Puppet::Util::Log.level = :info
+ end
+
+ option("--from TERMINUS", "-f") do |arg|
+ @from = arg
+ end
+
+ option("--format FORMAT") do |arg|
+ @format = arg
+ end
+
+ # XXX this doesn't work, I think
+ option("--list") do
+ indirections.each do |ind|
+ begin
+ classes = terminus_classes(ind.to_sym)
+ rescue => detail
+ $stderr.puts "Could not load terminuses for #{ind}: #{detail}"
+ next
+ end
+ puts "%-30s: #{classes.join(", ")}" % ind
+ end
+ exit(0)
+ end
+
+ option("--mode RUNMODE", "-r") do |arg|
+ raise "Invalid run mode #{arg}; supported modes are user, agent, master" unless %w{user agent master}.include?(arg)
+ self.class.run_mode(arg.to_sym)
+ set_run_mode self.class.run_mode
+ end
+
+
+ attr_accessor :interface, :from, :type, :verb, :name, :arguments, :indirection, :format
+
+ def main
+ # Call the method associated with the provided action (e.g., 'find').
+ interface.send(verb, name, *arguments)
+ end
+
+ def setup
+ @format ||= :yaml
+
+ Puppet::Util::Log.newdestination :console
+
+ @verb, @name, @arguments = command_line.args
+ @arguments ||= []
+
+ @type = self.class.name.to_s.sub(/.+:/, '').downcase.to_sym
+
+ @interface = Puppet::Interface.interface(@type).new
+
+ validate
+
+ raise "Could not find data type #{type} for application #{self.class.name}" unless @indirection = Puppet::Indirector::Indirection.instance(type)
+
+ @interface.set_terminus(from) if from
+ end
+
+ def validate
+ unless verb
+ raise "You must specify #{interface.actions.join(", ")} as a verb; 'save' probably does not work right now"
+ end
+
+ unless interface.action?(verb)
+ raise "Command '#{verb}' not found for #{type}"
+ end
+ end
+end
diff --git a/lib/puppet/application/facts.rb b/lib/puppet/application/facts.rb
new file mode 100644
index 000000000..dd79a00d9
--- /dev/null
+++ b/lib/puppet/application/facts.rb
@@ -0,0 +1,4 @@
+require 'puppet/application/data_baseclass'
+
+class Puppet::Application::Facts < Puppet::Application::DataBaseclass
+end
diff --git a/lib/puppet/application/file_bucket_file.rb b/lib/puppet/application/file_bucket_file.rb
new file mode 100644
index 000000000..f08a37f90
--- /dev/null
+++ b/lib/puppet/application/file_bucket_file.rb
@@ -0,0 +1,4 @@
+require 'puppet/application/data_baseclass'
+
+class Puppet::Application::File_bucket_file < Puppet::Application::DataBaseclass
+end
diff --git a/lib/puppet/application/inventory.rb b/lib/puppet/application/inventory.rb
new file mode 100644
index 000000000..f54708f24
--- /dev/null
+++ b/lib/puppet/application/inventory.rb
@@ -0,0 +1,4 @@
+require 'puppet/application/data_baseclass'
+
+class Puppet::Application::Inventory < Puppet::Application::DataBaseclass
+end
diff --git a/lib/puppet/application/key.rb b/lib/puppet/application/key.rb
new file mode 100644
index 000000000..1197ae026
--- /dev/null
+++ b/lib/puppet/application/key.rb
@@ -0,0 +1,4 @@
+require 'puppet/application/data_baseclass'
+
+class Puppet::Application::Key < Puppet::Application::DataBaseclass
+end
diff --git a/lib/puppet/application/node.rb b/lib/puppet/application/node.rb
new file mode 100644
index 000000000..4d7de1ab2
--- /dev/null
+++ b/lib/puppet/application/node.rb
@@ -0,0 +1,4 @@
+require 'puppet/application/data_baseclass'
+
+class Puppet::Application::Node < Puppet::Application::DataBaseclass
+end
diff --git a/lib/puppet/application/report.rb b/lib/puppet/application/report.rb
new file mode 100644
index 000000000..e4b5cf440
--- /dev/null
+++ b/lib/puppet/application/report.rb
@@ -0,0 +1,4 @@
+require 'puppet/application/data_baseclass'
+
+class Puppet::Application::Report < Puppet::Application::DataBaseclass
+end
diff --git a/lib/puppet/application/resource_type.rb b/lib/puppet/application/resource_type.rb
new file mode 100644
index 000000000..5bd001e88
--- /dev/null
+++ b/lib/puppet/application/resource_type.rb
@@ -0,0 +1,4 @@
+require 'puppet/application/data_baseclass'
+
+class Puppet::Application::Resource_type < Puppet::Application::DataBaseclass
+end
diff --git a/lib/puppet/application/status.rb b/lib/puppet/application/status.rb
new file mode 100644
index 000000000..382532f7f
--- /dev/null
+++ b/lib/puppet/application/status.rb
@@ -0,0 +1,4 @@
+require 'puppet/application/data_baseclass'
+
+class Puppet::Application::Status < Puppet::Application::DataBaseclass
+end
diff --git a/lib/puppet/interface.rb b/lib/puppet/interface.rb
new file mode 100644
index 000000000..2be3df5d6
--- /dev/null
+++ b/lib/puppet/interface.rb
@@ -0,0 +1,145 @@
+require 'puppet'
+
+class Puppet::Interface
+ # This is just so we can search for actions. We only use its
+ # list of directories to search.
+ def self.autoloader
+ require 'puppet/util/autoload'
+ @autoloader ||= Puppet::Util::Autoload.new(:application, "puppet/interface")
+ end
+
+ # Declare that this app can take a specific action, and provide
+ # the code to do so.
+ def self.action(name, &block)
+ @actions ||= []
+ name = name.to_s.downcase.to_sym
+ raise "Action #{name} already defined for #{name}" if actions.include?(name)
+
+ @actions << name
+
+ define_method(name, &block)
+ end
+
+ def self.actions
+ @actions ||= []
+ (if superclass.respond_to?(:actions)
+ @actions + superclass.actions
+ else
+ @actions
+ end).sort { |a,b| a.to_s <=> b.to_s }
+ end
+
+ # Return an indirection associated with an interface, if one exists
+ # One usually does.
+ def self.indirection
+ unless @indirection
+ raise "Could not find data type '#{name}' for interface '#{name}'" unless @indirection = Puppet::Indirector::Indirection.instance(name.to_sym)
+ end
+ @indirection
+ end
+
+ # Return an interface by name, loading from disk if necessary.
+ def self.interface(name)
+ require "puppet/interface/#{name.to_s.downcase}"
+ self.const_get(name.to_s.capitalize)
+ rescue Exception => detail
+ $stderr.puts "Unable to find interface '#{name.to_s}': #{detail}."
+ Kernel::exit(1)
+ end
+
+ # Return the interface name.
+ def self.name
+ @name || self.to_s.sub(/.+::/, '').downcase
+ end
+
+ attr_accessor :from, :type, :verb, :name, :arguments, :indirection, :format
+
+ def action?(name)
+ self.class.actions.include?(name.to_sym)
+ end
+
+ # Print the configuration for the current terminus class
+ action :showconfig do |*args|
+ if t = indirection.terminus_class
+ puts "Run mode #{Puppet.run_mode}: #{t}"
+ else
+ $stderr.puts "No default terminus class for run mode #{Puppet.run_mode}"
+ end
+ end
+
+ 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) }
+
+ @format ||= :yaml
+
+ Puppet::Util::Log.newdestination :console
+
+ 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 #{verb} #{type} for #{name}"
+ end
+
+ puts result.render(format.to_sym)
+ end
+
+ # Try to find actions defined in other files.
+ def load_actions
+ path = "puppet/interface/#{self.class.name}"
+
+ self.class.autoloader.search_directories.each do |dir|
+ fdir = File.join(dir, path)
+ next unless FileTest.directory?(fdir)
+
+ Dir.glob("#{fdir}/*.rb").each do |file|
+ Puppet.info "Loading actions for '#{self.class.name}' from '#{file}'"
+ require file
+ end
+ end
+ 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/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