summaryrefslogtreecommitdiffstats
path: root/lib/puppet
diff options
context:
space:
mode:
authorDaniel Pittman <daniel@puppetlabs.com>2011-04-07 17:58:07 -0700
committerDaniel Pittman <daniel@puppetlabs.com>2011-04-07 18:01:33 -0700
commit0f24db1c3fd0aa6601ba032aedbe25be36010954 (patch)
tree08af1180a4468145af66ee429ce3a80236e08fff /lib/puppet
parentaf792351a62399f8bbeba0d2425f2b88eabb3dff (diff)
parent79f4774182046d2fdf392c1eb27ee78505659199 (diff)
downloadpuppet-0f24db1c3fd0aa6601ba032aedbe25be36010954.tar.gz
puppet-0f24db1c3fd0aa6601ba032aedbe25be36010954.tar.xz
puppet-0f24db1c3fd0aa6601ba032aedbe25be36010954.zip
Merge puppet-interfaces into puppet.
This joins the two repositories, including full history, into a single run, as well as landing the interfaces work on the next branch ready for release.
Diffstat (limited to 'lib/puppet')
-rw-r--r--lib/puppet/application/catalog.rb4
-rw-r--r--lib/puppet/application/certificate.rb18
-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/config.rb4
-rw-r--r--lib/puppet/application/configurer.rb23
-rw-r--r--lib/puppet/application/faces.rb95
-rw-r--r--lib/puppet/application/faces_base.rb150
-rw-r--r--lib/puppet/application/facts.rb4
-rw-r--r--lib/puppet/application/file.rb4
-rw-r--r--lib/puppet/application/indirection_base.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/faces.rb12
-rw-r--r--lib/puppet/faces/catalog.rb40
-rw-r--r--lib/puppet/faces/catalog/select.rb10
-rw-r--r--lib/puppet/faces/certificate.rb46
-rw-r--r--lib/puppet/faces/certificate_request.rb4
-rw-r--r--lib/puppet/faces/certificate_revocation_list.rb4
-rw-r--r--lib/puppet/faces/config.rb12
-rw-r--r--lib/puppet/faces/configurer.rb12
-rw-r--r--lib/puppet/faces/facts.rb18
-rw-r--r--lib/puppet/faces/file.rb5
-rw-r--r--lib/puppet/faces/indirector.rb94
-rw-r--r--lib/puppet/faces/key.rb4
-rw-r--r--lib/puppet/faces/node.rb5
-rw-r--r--lib/puppet/faces/report.rb15
-rw-r--r--lib/puppet/faces/resource.rb4
-rw-r--r--lib/puppet/faces/resource_type.rb4
-rw-r--r--lib/puppet/faces/status.rb4
-rw-r--r--lib/puppet/interface.rb115
-rw-r--r--lib/puppet/interface/action.rb119
-rw-r--r--lib/puppet/interface/action_builder.rb31
-rw-r--r--lib/puppet/interface/action_manager.rb49
-rw-r--r--lib/puppet/interface/face_collection.rb125
-rw-r--r--lib/puppet/interface/option.rb82
-rw-r--r--lib/puppet/interface/option_builder.rb25
-rw-r--r--lib/puppet/interface/option_manager.rb56
41 files changed, 1229 insertions, 0 deletions
diff --git a/lib/puppet/application/catalog.rb b/lib/puppet/application/catalog.rb
new file mode 100644
index 000000000..10ce05be7
--- /dev/null
+++ b/lib/puppet/application/catalog.rb
@@ -0,0 +1,4 @@
+require 'puppet/application/indirection_base'
+
+class Puppet::Application::Catalog < Puppet::Application::IndirectionBase
+end
diff --git a/lib/puppet/application/certificate.rb b/lib/puppet/application/certificate.rb
new file mode 100644
index 000000000..eacb830b2
--- /dev/null
+++ b/lib/puppet/application/certificate.rb
@@ -0,0 +1,18 @@
+require 'puppet/application/indirection_base'
+
+class Puppet::Application::Certificate < Puppet::Application::IndirectionBase
+ def setup
+ unless options[:ca_location]
+ raise ArgumentError, "You must have a CA location specified;\n" +
+ "use --ca-location to specify the location (remote, local, only)"
+ end
+
+ location = Puppet::SSL::Host.ca_location
+ if location == :local && !Puppet::SSL::CertificateAuthority.ca?
+ self.class.run_mode("master")
+ self.set_run_mode self.class.run_mode
+ end
+
+ super
+ end
+end
diff --git a/lib/puppet/application/certificate_request.rb b/lib/puppet/application/certificate_request.rb
new file mode 100644
index 000000000..1b1b0830c
--- /dev/null
+++ b/lib/puppet/application/certificate_request.rb
@@ -0,0 +1,4 @@
+require 'puppet/application/indirection_base'
+
+class Puppet::Application::Certificate_request < Puppet::Application::IndirectionBase
+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..60b9d97d6
--- /dev/null
+++ b/lib/puppet/application/certificate_revocation_list.rb
@@ -0,0 +1,4 @@
+require 'puppet/application/indirection_base'
+
+class Puppet::Application::Certificate_revocation_list < Puppet::Application::IndirectionBase
+end
diff --git a/lib/puppet/application/config.rb b/lib/puppet/application/config.rb
new file mode 100644
index 000000000..41a46c339
--- /dev/null
+++ b/lib/puppet/application/config.rb
@@ -0,0 +1,4 @@
+require 'puppet/application/faces_base'
+
+class Puppet::Application::Config < Puppet::Application::FacesBase
+end
diff --git a/lib/puppet/application/configurer.rb b/lib/puppet/application/configurer.rb
new file mode 100644
index 000000000..751e6b4d7
--- /dev/null
+++ b/lib/puppet/application/configurer.rb
@@ -0,0 +1,23 @@
+require 'puppet/application'
+require 'puppet/faces'
+
+class Puppet::Application::Configurer < Puppet::Application
+ should_parse_config
+ run_mode :agent
+
+ option("--debug", "-d")
+ option("--verbose", "-v")
+
+ def setup
+ if options[:debug] or options[:verbose]
+ Puppet::Util::Log.level = options[:debug] ? :debug : :info
+ end
+
+ Puppet::Util::Log.newdestination(:console)
+ end
+
+ def run_command
+ report = Puppet::Faces[:configurer, '0.0.1'].synchronize(Puppet[:certname])
+ Puppet::Faces[:report, '0.0.1'].submit(report)
+ end
+end
diff --git a/lib/puppet/application/faces.rb b/lib/puppet/application/faces.rb
new file mode 100644
index 000000000..904a0cccc
--- /dev/null
+++ b/lib/puppet/application/faces.rb
@@ -0,0 +1,95 @@
+require 'puppet/application'
+require 'puppet/faces'
+
+class Puppet::Application::Faces < 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
+ faces.each do |name|
+ str = "#{name}:\n"
+ if arguments.include?("terminuses")
+ begin
+ terms = terminus_classes(name.to_sym)
+ str << "\tTerminuses: #{terms.join(", ")}\n"
+ rescue => detail
+ puts detail.backtrace if Puppet[:trace]
+ $stderr.puts "Could not load terminuses for #{name}: #{detail}"
+ end
+ end
+
+ if arguments.include?("actions")
+ begin
+ actions = actions(name.to_sym)
+ str << "\tActions: #{actions.join(", ")}\n"
+ rescue => detail
+ puts detail.backtrace if Puppet[:trace]
+ $stderr.puts "Could not load actions for #{name}: #{detail}"
+ end
+ end
+
+ print str
+ end
+ 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
+
+ load_applications # Call this to load all of the apps
+
+ @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 'faces'"
+ end
+ end
+
+ def faces
+ Puppet::Faces.faces
+ end
+
+ def terminus_classes(indirection)
+ Puppet::Indirector::Terminus.terminus_classes(indirection).collect { |t| t.to_s }.sort
+ end
+
+ def actions(indirection)
+ return [] unless faces = Puppet::Faces[indirection, '0.0.1']
+ faces.load_actions
+ return faces.actions.sort { |a, b| a.to_s <=> b.to_s }
+ end
+
+ def load_applications
+ command_line.available_subcommands.each do |app|
+ command_line.require_application app
+ end
+ end
+end
+
diff --git a/lib/puppet/application/faces_base.rb b/lib/puppet/application/faces_base.rb
new file mode 100644
index 000000000..288b50048
--- /dev/null
+++ b/lib/puppet/application/faces_base.rb
@@ -0,0 +1,150 @@
+require 'puppet/application'
+require 'puppet/faces'
+
+class Puppet::Application::FacesBase < 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("--format FORMAT") do |arg|
+ @format = arg.to_sym
+ 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 :face, :action, :type, :arguments, :format
+ attr_writer :exit_code
+
+ # This allows you to set the exit code if you don't want to just exit
+ # immediately but you need to indicate a failure.
+ def exit_code
+ @exit_code || 0
+ end
+
+ # Override this if you need custom rendering.
+ def render(result)
+ render_method = Puppet::Network::FormatHandler.format(format).render_method
+ if render_method == "to_pson"
+ jj result
+ exit(0)
+ else
+ result.send(render_method)
+ end
+ end
+
+ def preinit
+ super
+ Signal.trap(:INT) do
+ $stderr.puts "Cancelling Face"
+ exit(0)
+ end
+
+ # We need to parse enough of the command line out early, to identify what
+ # the action is, so that we can obtain the full set of options to parse.
+
+ # TODO: These should be configurable versions, through a global
+ # '--version' option, but we don't implement that yet... --daniel 2011-03-29
+ @type = self.class.name.to_s.sub(/.+:/, '').downcase.to_sym
+ @face = Puppet::Faces[@type, :current]
+ @format = @face.default_format
+
+ # Now, walk the command line and identify the action. We skip over
+ # arguments based on introspecting the action and all, and find the first
+ # non-option word to use as the action.
+ action = nil
+ index = -1
+ until @action or (index += 1) >= command_line.args.length do
+ item = command_line.args[index]
+ if item =~ /^-/ then
+ option = @face.options.find do |name|
+ item =~ /^-+#{name.to_s.gsub(/[-_]/, '[-_]')}(?:[ =].*)?$/
+ end
+ if option then
+ option = @face.get_option(option)
+ # If we have an inline argument, just carry on. We don't need to
+ # care about optional vs mandatory in that case because we do a real
+ # parse later, and that will totally take care of raising the error
+ # when we get there. --daniel 2011-04-04
+ if option.takes_argument? and !item.index('=') then
+ index += 1 unless
+ (option.optional_argument? and command_line.args[index + 1] =~ /^-/)
+ end
+ elsif option = find_global_settings_argument(item) then
+ unless Puppet.settings.boolean? option.name then
+ # As far as I can tell, we treat non-bool options as always having
+ # a mandatory argument. --daniel 2011-04-05
+ index += 1 # ...so skip the argument.
+ end
+ else
+ raise ArgumentError, "Unknown option #{item.sub(/=.*$/, '').inspect}"
+ end
+ else
+ action = @face.get_action(item.to_sym)
+ if action.nil? then
+ raise ArgumentError, "#{@face} does not have an #{item.inspect} action!"
+ end
+ @action = action
+ end
+ end
+
+ @action or raise ArgumentError, "No action given on the command line!"
+
+ # Finally, we can interact with the default option code to build behaviour
+ # around the full set of options we now know we support.
+ @action.options.each do |option|
+ option = @action.get_option(option) # make it the object.
+ self.class.option(*option.optparse) # ...and make the CLI parse it.
+ end
+ end
+
+ def find_global_settings_argument(item)
+ Puppet.settings.each do |name, object|
+ object.optparse_args.each do |arg|
+ next unless arg =~ /^-/
+ # sadly, we have to emulate some of optparse here...
+ pattern = /^#{arg.sub('[no-]', '').sub(/[ =].*$/, '')}(?:[ =].*)?$/
+ pattern.match item and return object
+ end
+ end
+ return nil # nothing found.
+ end
+
+ def setup
+ Puppet::Util::Log.newdestination :console
+
+ @arguments = command_line.args
+
+ # Note: because of our definition of where the action is set, we end up
+ # with it *always* being the first word of the remaining set of command
+ # line arguments. So, strip that off when we construct the arguments to
+ # pass down to the face action. --daniel 2011-04-04
+ @arguments.delete_at(0)
+
+ # We copy all of the app options to the end of the call; This allows each
+ # action to read in the options. This replaces the older model where we
+ # would invoke the action with options set as global state in the
+ # interface object. --daniel 2011-03-28
+ @arguments << options
+ end
+
+
+ def main
+ # Call the method associated with the provided action (e.g., 'find').
+ if result = @face.send(@action.name, *arguments)
+ puts render(result)
+ end
+ exit(exit_code)
+ end
+end
diff --git a/lib/puppet/application/facts.rb b/lib/puppet/application/facts.rb
new file mode 100644
index 000000000..d18b21ea7
--- /dev/null
+++ b/lib/puppet/application/facts.rb
@@ -0,0 +1,4 @@
+require 'puppet/application/indirection_base'
+
+class Puppet::Application::Facts < Puppet::Application::IndirectionBase
+end
diff --git a/lib/puppet/application/file.rb b/lib/puppet/application/file.rb
new file mode 100644
index 000000000..32a81c7c6
--- /dev/null
+++ b/lib/puppet/application/file.rb
@@ -0,0 +1,4 @@
+require 'puppet/application/indirection_base'
+
+class Puppet::Application::File < Puppet::Application::IndirectionBase
+end
diff --git a/lib/puppet/application/indirection_base.rb b/lib/puppet/application/indirection_base.rb
new file mode 100644
index 000000000..7455ebedf
--- /dev/null
+++ b/lib/puppet/application/indirection_base.rb
@@ -0,0 +1,4 @@
+require 'puppet/application/faces_base'
+
+class Puppet::Application::IndirectionBase < Puppet::Application::FacesBase
+end
diff --git a/lib/puppet/application/key.rb b/lib/puppet/application/key.rb
new file mode 100644
index 000000000..57835b627
--- /dev/null
+++ b/lib/puppet/application/key.rb
@@ -0,0 +1,4 @@
+require 'puppet/application/indirection_base'
+
+class Puppet::Application::Key < Puppet::Application::IndirectionBase
+end
diff --git a/lib/puppet/application/node.rb b/lib/puppet/application/node.rb
new file mode 100644
index 000000000..38c1f8610
--- /dev/null
+++ b/lib/puppet/application/node.rb
@@ -0,0 +1,4 @@
+require 'puppet/application/indirection_base'
+
+class Puppet::Application::Node < Puppet::Application::IndirectionBase
+end
diff --git a/lib/puppet/application/report.rb b/lib/puppet/application/report.rb
new file mode 100644
index 000000000..f7f961edd
--- /dev/null
+++ b/lib/puppet/application/report.rb
@@ -0,0 +1,4 @@
+require 'puppet/application/indirection_base'
+
+class Puppet::Application::Report < Puppet::Application::IndirectionBase
+end
diff --git a/lib/puppet/application/resource_type.rb b/lib/puppet/application/resource_type.rb
new file mode 100644
index 000000000..59594262c
--- /dev/null
+++ b/lib/puppet/application/resource_type.rb
@@ -0,0 +1,4 @@
+require 'puppet/application/indirection_base'
+
+class Puppet::Application::Resource_type < Puppet::Application::IndirectionBase
+end
diff --git a/lib/puppet/application/status.rb b/lib/puppet/application/status.rb
new file mode 100644
index 000000000..1c3ca054e
--- /dev/null
+++ b/lib/puppet/application/status.rb
@@ -0,0 +1,4 @@
+require 'puppet/application/indirection_base'
+
+class Puppet::Application::Status < Puppet::Application::IndirectionBase
+end
diff --git a/lib/puppet/faces.rb b/lib/puppet/faces.rb
new file mode 100644
index 000000000..947eecf24
--- /dev/null
+++ b/lib/puppet/faces.rb
@@ -0,0 +1,12 @@
+# The public name of this feature is 'faces', but we have hidden all the
+# plumbing over in the 'interfaces' namespace to make clear the distinction
+# between the two.
+#
+# This file exists to ensure that the public name is usable without revealing
+# the details of the implementation; you really only need go look at anything
+# under Interfaces if you are looking to extend the implementation.
+#
+# It isn't hidden to gratuitously hide things, just to make it easier to
+# separate out the interests people will have. --daniel 2011-04-07
+require 'puppet/interface'
+Puppet::Faces = Puppet::Interface
diff --git a/lib/puppet/faces/catalog.rb b/lib/puppet/faces/catalog.rb
new file mode 100644
index 000000000..3353d5d04
--- /dev/null
+++ b/lib/puppet/faces/catalog.rb
@@ -0,0 +1,40 @@
+require 'puppet/faces/indirector'
+
+Puppet::Faces::Indirector.define(:catalog, '0.0.1') do
+ action(:apply) do
+ when_invoked do |catalog, options|
+ 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
+ when_invoked do |certname, facts, options|
+ Puppet::Resource::Catalog.indirection.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::Faces[: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/faces/catalog/select.rb b/lib/puppet/faces/catalog/select.rb
new file mode 100644
index 000000000..e29d19970
--- /dev/null
+++ b/lib/puppet/faces/catalog/select.rb
@@ -0,0 +1,10 @@
+# Select and show a list of resources of a given type.
+Puppet::Faces.define(:catalog, '0.0.1') do
+ action :select do
+ when_invoked do |host, type, options|
+ 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/faces/certificate.rb b/lib/puppet/faces/certificate.rb
new file mode 100644
index 000000000..b10bee579
--- /dev/null
+++ b/lib/puppet/faces/certificate.rb
@@ -0,0 +1,46 @@
+require 'puppet/faces/indirector'
+require 'puppet/ssl/host'
+
+Puppet::Faces::Indirector.define(:certificate, '0.0.1') do
+ # REVISIT: This should use a pre-invoke hook to run the common code that
+ # needs to happen before we invoke any action; that would be much nicer than
+ # the "please repeat yourself" stuff found in here right now.
+ #
+ # option "--ca-location LOCATION" do
+ # type [:whatever, :location, :symbols]
+ # hook :before do |value|
+ # Puppet::SSL::Host.ca_location = value
+ # end
+ # end
+ #
+ # ...but should I pass the arguments as well?
+ # --daniel 2011-04-05
+ option "--ca-location LOCATION"
+
+ action :generate do
+ when_invoked do |name, options|
+ Puppet::SSL::Host.ca_location = options[:ca_location].to_sym
+ host = Puppet::SSL::Host.new(name)
+ host.generate_certificate_request
+ host.certificate_request.class.indirection.save(host.certificate_request)
+ end
+ end
+
+ action :list do
+ when_invoked do |options|
+ Puppet::SSL::Host.ca_location = options[:ca_location].to_sym
+ Puppet::SSL::Host.indirection.search("*", {
+ :for => :certificate_request,
+ }).map { |h| h.inspect }
+ end
+ end
+
+ action :sign do
+ when_invoked do |name, options|
+ Puppet::SSL::Host.ca_location = options[:ca_location].to_sym
+ host = Puppet::SSL::Host.new(name)
+ host.desired_state = 'signed'
+ Puppet::SSL::Host.indirection.save(host)
+ end
+ end
+end
diff --git a/lib/puppet/faces/certificate_request.rb b/lib/puppet/faces/certificate_request.rb
new file mode 100644
index 000000000..5e91bdb7f
--- /dev/null
+++ b/lib/puppet/faces/certificate_request.rb
@@ -0,0 +1,4 @@
+require 'puppet/faces/indirector'
+
+Puppet::Faces::Indirector.define(:certificate_request, '0.0.1') do
+end
diff --git a/lib/puppet/faces/certificate_revocation_list.rb b/lib/puppet/faces/certificate_revocation_list.rb
new file mode 100644
index 000000000..2f2d72874
--- /dev/null
+++ b/lib/puppet/faces/certificate_revocation_list.rb
@@ -0,0 +1,4 @@
+require 'puppet/faces/indirector'
+
+Puppet::Faces::Indirector.define(:certificate_revocation_list, '0.0.1') do
+end
diff --git a/lib/puppet/faces/config.rb b/lib/puppet/faces/config.rb
new file mode 100644
index 000000000..647bf5052
--- /dev/null
+++ b/lib/puppet/faces/config.rb
@@ -0,0 +1,12 @@
+require 'puppet/faces'
+
+Puppet::Faces.define(:config, '0.0.1') do
+ action(:print) do
+ when_invoked do |*args|
+ options = args.pop
+ Puppet.settings[:configprint] = args.join(",")
+ Puppet.settings.print_config_options
+ nil
+ end
+ end
+end
diff --git a/lib/puppet/faces/configurer.rb b/lib/puppet/faces/configurer.rb
new file mode 100644
index 000000000..d40987697
--- /dev/null
+++ b/lib/puppet/faces/configurer.rb
@@ -0,0 +1,12 @@
+require 'puppet/faces'
+
+Puppet::Faces.define(:configurer, '0.0.1') do
+ action(:synchronize) do
+ when_invoked do |certname, options|
+ facts = Puppet::Faces[:facts, '0.0.1'].find(certname)
+ catalog = Puppet::Faces[:catalog, '0.0.1'].download(certname, facts)
+ report = Puppet::Faces[:catalog, '0.0.1'].apply(catalog)
+ report
+ end
+ end
+end
diff --git a/lib/puppet/faces/facts.rb b/lib/puppet/faces/facts.rb
new file mode 100644
index 000000000..33eacef38
--- /dev/null
+++ b/lib/puppet/faces/facts.rb
@@ -0,0 +1,18 @@
+require 'puppet/faces/indirector'
+require 'puppet/node/facts'
+
+Puppet::Faces::Indirector.define(:facts, '0.0.1') do
+ set_default_format :yaml
+
+ # Upload our facts to the server
+ action(:upload) do
+ when_invoked do |options|
+ 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/faces/file.rb b/lib/puppet/faces/file.rb
new file mode 100644
index 000000000..e8ad18c17
--- /dev/null
+++ b/lib/puppet/faces/file.rb
@@ -0,0 +1,5 @@
+require 'puppet/faces/indirector'
+
+Puppet::Faces::Indirector.define(:file, '0.0.1') do
+ set_indirection_name :file_bucket_file
+end
diff --git a/lib/puppet/faces/indirector.rb b/lib/puppet/faces/indirector.rb
new file mode 100644
index 000000000..7e4e0f00f
--- /dev/null
+++ b/lib/puppet/faces/indirector.rb
@@ -0,0 +1,94 @@
+require 'puppet'
+require 'puppet/faces'
+
+class Puppet::Faces::Indirector < Puppet::Faces
+ option "--terminus TERMINUS" do
+ desc "REVISIT: You can select a terminus, which has some bigger effect
+that we should describe in this file somehow."
+ end
+
+ def self.indirections
+ Puppet::Indirector::Indirection.instances.collect { |t| t.to_s }.sort
+ end
+
+ def self.terminus_classes(indirection)
+ Puppet::Indirector::Terminus.terminus_classes(indirection.to_sym).collect { |t| t.to_s }.sort
+ end
+
+ def call_indirection_method(method, *args)
+ options = args.last
+ options.has_key?(:terminus) and set_terminus(options[:terminus])
+
+ begin
+ result = indirection.__send__(method, *args)
+ rescue => detail
+ puts detail.backtrace if Puppet[:trace]
+ raise "Could not call '#{method}' on '#{indirection_name}': #{detail}"
+ end
+
+ indirection.reset_terminus_class
+ return result
+ end
+
+ action :destroy do
+ when_invoked { |*args| call_indirection_method(:destroy, *args) }
+ end
+
+ action :find do
+ when_invoked { |*args| call_indirection_method(:find, *args) }
+ end
+
+ action :save do
+ when_invoked { |*args| call_indirection_method(:save, *args) }
+ end
+
+ action :search do
+ when_invoked { |*args| call_indirection_method(:search, *args) }
+ end
+
+ # Print the configuration for the current terminus class
+ action :info do
+ when_invoked do |*args|
+ options = args.pop
+ options.has_key?(:terminus) and set_terminus(options[:terminus])
+
+ 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
+
+ indirection.reset_terminus_class
+ end
+ end
+
+ attr_accessor :from
+
+ def indirection_name
+ @indirection_name || name.to_sym
+ end
+
+ # Here's your opportunity to override the indirection name. By default it
+ # will be the same name as the face.
+ def set_indirection_name(name)
+ @indirection_name = name
+ end
+
+ # Return an indirection associated with a face, if one exists;
+ # One usually does.
+ def indirection
+ unless @indirection
+ @indirection = Puppet::Indirector::Indirection.instance(indirection_name)
+ @indirection or raise "Could not find terminus for #{indirection_name}"
+ end
+ @indirection
+ end
+
+ def set_terminus(from)
+ begin
+ indirection.terminus_class = from
+ rescue => detail
+ raise "Could not set '#{indirection.name}' terminus to '#{from}' (#{detail}); valid terminus types are #{self.class.terminus_classes(indirection.name).join(", ") }"
+ end
+ end
+end
diff --git a/lib/puppet/faces/key.rb b/lib/puppet/faces/key.rb
new file mode 100644
index 000000000..7b6ad52ac
--- /dev/null
+++ b/lib/puppet/faces/key.rb
@@ -0,0 +1,4 @@
+require 'puppet/faces/indirector'
+
+Puppet::Faces::Indirector.define(:key, '0.0.1') do
+end
diff --git a/lib/puppet/faces/node.rb b/lib/puppet/faces/node.rb
new file mode 100644
index 000000000..7eed0df91
--- /dev/null
+++ b/lib/puppet/faces/node.rb
@@ -0,0 +1,5 @@
+require 'puppet/faces/indirector'
+
+Puppet::Faces::Indirector.define(:node, '0.0.1') do
+ set_default_format :yaml
+end
diff --git a/lib/puppet/faces/report.rb b/lib/puppet/faces/report.rb
new file mode 100644
index 000000000..23a518981
--- /dev/null
+++ b/lib/puppet/faces/report.rb
@@ -0,0 +1,15 @@
+require 'puppet/faces/indirector'
+
+Puppet::Faces::Indirector.define(:report, '0.0.1') do
+ action(:submit) do
+ when_invoked do |report, options|
+ 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/faces/resource.rb b/lib/puppet/faces/resource.rb
new file mode 100644
index 000000000..60b0d94db
--- /dev/null
+++ b/lib/puppet/faces/resource.rb
@@ -0,0 +1,4 @@
+require 'puppet/faces/indirector'
+
+Puppet::Faces::Indirector.define(:resource, '0.0.1') do
+end
diff --git a/lib/puppet/faces/resource_type.rb b/lib/puppet/faces/resource_type.rb
new file mode 100644
index 000000000..4321d65e7
--- /dev/null
+++ b/lib/puppet/faces/resource_type.rb
@@ -0,0 +1,4 @@
+require 'puppet/faces/indirector'
+
+Puppet::Faces::Indirector.define(:resource_type, '0.0.1') do
+end
diff --git a/lib/puppet/faces/status.rb b/lib/puppet/faces/status.rb
new file mode 100644
index 000000000..e035f281f
--- /dev/null
+++ b/lib/puppet/faces/status.rb
@@ -0,0 +1,4 @@
+require 'puppet/faces/indirector'
+
+Puppet::Faces::Indirector.define(:status, '0.0.1') do
+end
diff --git a/lib/puppet/interface.rb b/lib/puppet/interface.rb
new file mode 100644
index 000000000..07e27efa8
--- /dev/null
+++ b/lib/puppet/interface.rb
@@ -0,0 +1,115 @@
+require 'puppet'
+require 'puppet/util/autoload'
+
+class Puppet::Interface
+ require 'puppet/interface/face_collection'
+
+ require 'puppet/interface/action_manager'
+ include Puppet::Interface::ActionManager
+ extend Puppet::Interface::ActionManager
+
+ require 'puppet/interface/option_manager'
+ include Puppet::Interface::OptionManager
+ extend Puppet::Interface::OptionManager
+
+ include Puppet::Util
+
+ class << self
+ # This is just so we can search for actions. We only use its
+ # list of directories to search.
+ # Can't we utilize an external autoloader, or simply use the $LOAD_PATH? -pvb
+ def autoloader
+ @autoloader ||= Puppet::Util::Autoload.new(:application, "puppet/faces")
+ end
+
+ def faces
+ Puppet::Interface::FaceCollection.faces
+ end
+
+ def face?(name, version)
+ Puppet::Interface::FaceCollection.face?(name, version)
+ end
+
+ def register(instance)
+ Puppet::Interface::FaceCollection.register(instance)
+ end
+
+ def define(name, version, &block)
+ if face?(name, version)
+ face = Puppet::Interface::FaceCollection[name, version]
+ else
+ face = self.new(name, version)
+ Puppet::Interface::FaceCollection.register(face)
+ # REVISIT: Shouldn't this be delayed until *after* we evaluate the
+ # current block, not done before? --daniel 2011-04-07
+ face.load_actions
+ end
+
+ face.instance_eval(&block) if block_given?
+
+ return face
+ end
+
+ def [](name, version)
+ unless face = Puppet::Interface::FaceCollection[name, version]
+ if current = Puppet::Interface::FaceCollection[name, :current]
+ raise Puppet::Error, "Could not find version #{version} of #{current}"
+ else
+ raise Puppet::Error, "Could not find Puppet Face #{name.inspect}"
+ end
+ end
+ face
+ end
+ end
+
+ attr_accessor :default_format
+
+ def set_default_format(format)
+ self.default_format = format.to_sym
+ end
+
+ attr_accessor :type, :verb, :version, :arguments
+ attr_reader :name
+
+ def initialize(name, version, &block)
+ unless Puppet::Interface::FaceCollection.validate_version(version)
+ raise ArgumentError, "Cannot create face #{name.inspect} with invalid version number '#{version}'!"
+ end
+
+ @name = Puppet::Interface::FaceCollection.underscorize(name)
+ @version = version
+ @default_format = :pson
+
+ instance_eval(&block) if block_given?
+ end
+
+ # Try to find actions defined in other files.
+ def load_actions
+ path = "puppet/faces/#{name}"
+
+ loaded = []
+ [path, "#{name}@#{version}/#{path}"].each do |path|
+ Puppet::Interface.autoloader.search_directories.each do |dir|
+ fdir = ::File.join(dir, path)
+ next unless FileTest.directory?(fdir)
+
+ Dir.chdir(fdir) do
+ Dir.glob("*.rb").each do |file|
+ aname = file.sub(/\.rb/, '')
+ if loaded.include?(aname)
+ Puppet.debug "Not loading duplicate action '#{aname}' for '#{name}' from '#{fdir}/#{file}'"
+ next
+ end
+ loaded << aname
+ Puppet.debug "Loading action '#{aname}' for '#{name}' from '#{fdir}/#{file}'"
+ require "#{Dir.pwd}/#{aname}"
+ end
+ end
+ end
+ end
+ end
+
+ def to_s
+ "Puppet::Faces[#{name.inspect}, #{version.inspect}]"
+ end
+end
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