From 4609e203fd47f8159118bb74a8308f9c6aee179f Mon Sep 17 00:00:00 2001 From: Pieter van de Bruggen Date: Fri, 25 Mar 2011 14:52:48 -0700 Subject: (#6770) Change versioning; adopt :current over :latest. As per discussion with Luke, versions of an interface are first looked up by requiring 'puppet/interface/{name}', and secondarily looked up by requiring '{name}@{version}/puppet/interface/{name}' if the first failed. A version of `:current` can be used to represent the version living in 'puppet/interface/{name}'. Paired-With: Nick Lewis --- README.markdown | 26 ++-- lib/puppet/application/string_base.rb | 4 +- lib/puppet/string.rb | 30 ++-- lib/puppet/string/catalog.rb | 40 +++++ lib/puppet/string/catalog/select.rb | 10 ++ lib/puppet/string/certificate.rb | 28 ++++ lib/puppet/string/certificate_request.rb | 4 + lib/puppet/string/certificate_revocation_list.rb | 4 + lib/puppet/string/config.rb | 11 ++ lib/puppet/string/configurer.rb | 12 ++ lib/puppet/string/facts.rb | 18 +++ lib/puppet/string/file.rb | 5 + lib/puppet/string/key.rb | 4 + lib/puppet/string/node.rb | 5 + lib/puppet/string/report.rb | 15 ++ lib/puppet/string/resource.rb | 4 + lib/puppet/string/resource_type.rb | 4 + lib/puppet/string/status.rb | 4 + lib/puppet/string/string_collection.rb | 66 +++----- lib/puppet/string/v0.0.1/catalog.rb | 40 ----- lib/puppet/string/v0.0.1/catalog/select.rb | 10 -- lib/puppet/string/v0.0.1/certificate.rb | 28 ---- lib/puppet/string/v0.0.1/certificate_request.rb | 4 - .../string/v0.0.1/certificate_revocation_list.rb | 4 - lib/puppet/string/v0.0.1/config.rb | 11 -- lib/puppet/string/v0.0.1/configurer.rb | 12 -- lib/puppet/string/v0.0.1/facts.rb | 18 --- lib/puppet/string/v0.0.1/file.rb | 5 - lib/puppet/string/v0.0.1/key.rb | 4 - lib/puppet/string/v0.0.1/node.rb | 5 - lib/puppet/string/v0.0.1/report.rb | 15 -- lib/puppet/string/v0.0.1/resource.rb | 4 - lib/puppet/string/v0.0.1/resource_type.rb | 4 - lib/puppet/string/v0.0.1/status.rb | 4 - spec/unit/application/string_base_spec.rb | 15 +- spec/unit/string/string_collection_spec.rb | 167 +++++++-------------- spec/unit/string_spec.rb | 3 +- 37 files changed, 282 insertions(+), 365 deletions(-) create mode 100644 lib/puppet/string/catalog.rb create mode 100644 lib/puppet/string/catalog/select.rb create mode 100644 lib/puppet/string/certificate.rb create mode 100644 lib/puppet/string/certificate_request.rb create mode 100644 lib/puppet/string/certificate_revocation_list.rb create mode 100644 lib/puppet/string/config.rb create mode 100644 lib/puppet/string/configurer.rb create mode 100644 lib/puppet/string/facts.rb create mode 100644 lib/puppet/string/file.rb create mode 100644 lib/puppet/string/key.rb create mode 100644 lib/puppet/string/node.rb create mode 100644 lib/puppet/string/report.rb create mode 100644 lib/puppet/string/resource.rb create mode 100644 lib/puppet/string/resource_type.rb create mode 100644 lib/puppet/string/status.rb delete mode 100644 lib/puppet/string/v0.0.1/catalog.rb delete mode 100644 lib/puppet/string/v0.0.1/catalog/select.rb delete mode 100644 lib/puppet/string/v0.0.1/certificate.rb delete mode 100644 lib/puppet/string/v0.0.1/certificate_request.rb delete mode 100644 lib/puppet/string/v0.0.1/certificate_revocation_list.rb delete mode 100644 lib/puppet/string/v0.0.1/config.rb delete mode 100644 lib/puppet/string/v0.0.1/configurer.rb delete mode 100644 lib/puppet/string/v0.0.1/facts.rb delete mode 100644 lib/puppet/string/v0.0.1/file.rb delete mode 100644 lib/puppet/string/v0.0.1/key.rb delete mode 100644 lib/puppet/string/v0.0.1/node.rb delete mode 100644 lib/puppet/string/v0.0.1/report.rb delete mode 100644 lib/puppet/string/v0.0.1/resource.rb delete mode 100644 lib/puppet/string/v0.0.1/resource_type.rb delete mode 100644 lib/puppet/string/v0.0.1/status.rb diff --git a/README.markdown b/README.markdown index 29ff41430..28289ee10 100644 --- a/README.markdown +++ b/README.markdown @@ -1,11 +1,11 @@ -Puppet Interfaces +Puppet Strings ================= A set of executables that provide complete CLI access to Puppet's -core data types. They also provide Interface classes for +core data types. They also provide String classes for each of the core data types, which are extensible via plugins. For instance, you can create a new action for catalogs at -lib/puppet/interface/catalog/$action.rb. +lib/puppet/string/catalog/$action.rb. This is a Puppet module and should work fine if you install it in Puppet's module path. @@ -22,7 +22,7 @@ Usage ----- The general usage is: - $ puppet + $ puppet So, e.g.: @@ -31,7 +31,7 @@ So, e.g.: You can use it to list all known data types and the available terminus classes: - $ puppet interface list + $ puppet string list catalog : active_record, compiler, queue, rest, yaml certificate : ca, file, rest certificate_request : ca, file, rest @@ -82,21 +82,21 @@ This compiles a test catalog (assuming that ~/bin/test.pp exists) and returns it Or use IRB to do the same thing: $ irb - >> require 'puppet/interface' + >> require 'puppet/string' => true - >> interface = Puppet::Interface[:facts, '1.0.0'] - => # - >> facts = interface.find("myhost") + >> string = Puppet::String[:facts, '1.0.0'] + => # + >> facts = string.find("myhost") Like I said, a prototype, but I'd love it if people would play it with some and make some recommendations. Extending --------- -Like most parts of Puppet, these are easy to extend. Just drop a new action into a given interface's directory. E.g.: +Like most parts of Puppet, these are easy to extend. Just drop a new action into a given string's directory. E.g.: - $ cat lib/puppet/interface/catalog/select.rb + $ cat lib/puppet/string/catalog/select.rb # Select and show a list of resources of a given type. - Puppet::Interface.define(:catalog, '1.0.0') do + Puppet::String.define(:catalog, '1.0.0') do action :select do invoke do |host,type| catalog = Puppet::Resource::Catalog.indirection.find(host) @@ -112,4 +112,4 @@ Like most parts of Puppet, these are easy to extend. Just drop a new action int Notice that this gets loaded automatically when you try to use it. So, if you have a simple command you've written, such as for cleaning up nodes or diffing catalogs, you an port it to this framework and it should fit cleanly. -Also note that interfaces are versioned. These version numbers are interpreted according to Semantic Versioning (http://semver.org). +Also note that strings are versioned. These version numbers are interpreted according to Semantic Versioning (http://semver.org). diff --git a/lib/puppet/application/string_base.rb b/lib/puppet/application/string_base.rb index 5b701597d..bc627adde 100644 --- a/lib/puppet/application/string_base.rb +++ b/lib/puppet/application/string_base.rb @@ -72,10 +72,10 @@ class Puppet::Application::StringBase < Puppet::Application @type = self.class.name.to_s.sub(/.+:/, '').downcase.to_sym # TODO: These should be configurable versions. - unless Puppet::String.string?(@type, :latest) + unless Puppet::String.string?(@type, :current) raise "Could not find any version of string '#{@type}'" end - @string = Puppet::String[@type, :latest] + @string = Puppet::String[@type, :current] @format ||= @string.default_format # We copy all of the app options to the string. diff --git a/lib/puppet/string.rb b/lib/puppet/string.rb index b5f7b9048..9a223a40c 100644 --- a/lib/puppet/string.rb +++ b/lib/puppet/string.rb @@ -70,23 +70,25 @@ class Puppet::String # Try to find actions defined in other files. def load_actions - path = "puppet/string/v#{version}/#{name}" + path = "puppet/string/#{name}" loaded = [] - Puppet::String.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 + [path, "#{name}@#{version}/#{path}"].each do |path| + Puppet::String.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 "#{path}/#{aname}" end - loaded << aname - Puppet.debug "Loading action '#{aname}' for '#{name}' from '#{fdir}/#{file}'" - require "#{path}/#{aname}" end end end diff --git a/lib/puppet/string/catalog.rb b/lib/puppet/string/catalog.rb new file mode 100644 index 000000000..0ddd83176 --- /dev/null +++ b/lib/puppet/string/catalog.rb @@ -0,0 +1,40 @@ +require 'puppet/string/indirector' + +Puppet::String::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::String[: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/string/catalog/select.rb b/lib/puppet/string/catalog/select.rb new file mode 100644 index 000000000..52c77d3ce --- /dev/null +++ b/lib/puppet/string/catalog/select.rb @@ -0,0 +1,10 @@ +# Select and show a list of resources of a given type. +Puppet::String.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/string/certificate.rb b/lib/puppet/string/certificate.rb new file mode 100644 index 000000000..7b2e5f397 --- /dev/null +++ b/lib/puppet/string/certificate.rb @@ -0,0 +1,28 @@ +require 'puppet/string/indirector' +require 'puppet/ssl/host' + +Puppet::String::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/string/certificate_request.rb b/lib/puppet/string/certificate_request.rb new file mode 100644 index 000000000..218b40b98 --- /dev/null +++ b/lib/puppet/string/certificate_request.rb @@ -0,0 +1,4 @@ +require 'puppet/string/indirector' + +Puppet::String::Indirector.define(:certificate_request, '0.0.1') do +end diff --git a/lib/puppet/string/certificate_revocation_list.rb b/lib/puppet/string/certificate_revocation_list.rb new file mode 100644 index 000000000..9731b4f2d --- /dev/null +++ b/lib/puppet/string/certificate_revocation_list.rb @@ -0,0 +1,4 @@ +require 'puppet/string/indirector' + +Puppet::String::Indirector.define(:certificate_revocation_list, '0.0.1') do +end diff --git a/lib/puppet/string/config.rb b/lib/puppet/string/config.rb new file mode 100644 index 000000000..ae1a408cf --- /dev/null +++ b/lib/puppet/string/config.rb @@ -0,0 +1,11 @@ +require 'puppet/string' + +Puppet::String.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/string/configurer.rb b/lib/puppet/string/configurer.rb new file mode 100644 index 000000000..a6ea74b6a --- /dev/null +++ b/lib/puppet/string/configurer.rb @@ -0,0 +1,12 @@ +require 'puppet/string' + +Puppet::String.define(:configurer, '0.0.1') do + action(:synchronize) do + invoke do |certname| + facts = Puppet::String[:facts, '0.0.1'].find(certname) + catalog = Puppet::String[:catalog, '0.0.1'].download(certname, facts) + report = Puppet::String[:catalog, '0.0.1'].apply(catalog) + report + end + end +end diff --git a/lib/puppet/string/facts.rb b/lib/puppet/string/facts.rb new file mode 100644 index 000000000..73acb0df6 --- /dev/null +++ b/lib/puppet/string/facts.rb @@ -0,0 +1,18 @@ +require 'puppet/string/indirector' +require 'puppet/node/facts' + +Puppet::String::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/string/file.rb b/lib/puppet/string/file.rb new file mode 100644 index 000000000..cc5737f28 --- /dev/null +++ b/lib/puppet/string/file.rb @@ -0,0 +1,5 @@ +require 'puppet/string/indirector' + +Puppet::String::Indirector.define(:file, '0.0.1') do + set_indirection_name :file_bucket_file +end diff --git a/lib/puppet/string/key.rb b/lib/puppet/string/key.rb new file mode 100644 index 000000000..95aceade5 --- /dev/null +++ b/lib/puppet/string/key.rb @@ -0,0 +1,4 @@ +require 'puppet/string/indirector' + +Puppet::String::Indirector.define(:key, '0.0.1') do +end diff --git a/lib/puppet/string/node.rb b/lib/puppet/string/node.rb new file mode 100644 index 000000000..bc31a2cf3 --- /dev/null +++ b/lib/puppet/string/node.rb @@ -0,0 +1,5 @@ +require 'puppet/string/indirector' + +Puppet::String::Indirector.define(:node, '0.0.1') do + set_default_format :yaml +end diff --git a/lib/puppet/string/report.rb b/lib/puppet/string/report.rb new file mode 100644 index 000000000..55a008533 --- /dev/null +++ b/lib/puppet/string/report.rb @@ -0,0 +1,15 @@ +require 'puppet/string/indirector' + +Puppet::String::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/string/resource.rb b/lib/puppet/string/resource.rb new file mode 100644 index 000000000..9838be0fa --- /dev/null +++ b/lib/puppet/string/resource.rb @@ -0,0 +1,4 @@ +require 'puppet/string/indirector' + +Puppet::String::Indirector.define(:resource, '0.0.1') do +end diff --git a/lib/puppet/string/resource_type.rb b/lib/puppet/string/resource_type.rb new file mode 100644 index 000000000..8ca31ea6c --- /dev/null +++ b/lib/puppet/string/resource_type.rb @@ -0,0 +1,4 @@ +require 'puppet/string/indirector' + +Puppet::String::Indirector.define(:resource_type, '0.0.1') do +end diff --git a/lib/puppet/string/status.rb b/lib/puppet/string/status.rb new file mode 100644 index 000000000..41de2bb99 --- /dev/null +++ b/lib/puppet/string/status.rb @@ -0,0 +1,4 @@ +require 'puppet/string/indirector' + +Puppet::String::Indirector.define(:status, '0.0.1') do +end diff --git a/lib/puppet/string/string_collection.rb b/lib/puppet/string/string_collection.rb index e9cba7f55..45a192703 100644 --- a/lib/puppet/string/string_collection.rb +++ b/lib/puppet/string/string_collection.rb @@ -26,61 +26,39 @@ module Puppet::String::StringCollection return @strings.keys end - def self.versions(name) - versions = [] - $LOAD_PATH.each do |dir| - next unless FileTest.directory?(dir) - v_dir = File.join dir, %w[puppet string 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? - @strings[underscorize(name)][version] if string?(name, version) - end + @strings[underscorize(name)][version] if string?(name, version) end def self.string?(name, version) - version = versions(name).last if version == :latest - return false if version.nil? - name = underscorize(name) + cache = @strings[name] + return true if cache.has_key?(version) - unless @strings.has_key?(name) && @strings[name].has_key?(version) - require "puppet/string/v#{version}/#{name}" + loaded = cache.keys + + files = ["puppet/string/#{name}"] + unless version == :current + files << "#{name}@#{version}/puppet/string/#{name}" end - return @strings.has_key?(name) && @strings[name].has_key?(version) - rescue LoadError + + files.each do |file| + begin + require file + if version == :current || !file.include?('@') + loaded = (cache.keys - loaded).first + cache[:current] = cache[loaded] unless loaded.nil? + end + return true if cache.has_key?(version) + rescue LoadError + # pass + end + end + return false end diff --git a/lib/puppet/string/v0.0.1/catalog.rb b/lib/puppet/string/v0.0.1/catalog.rb deleted file mode 100644 index 0ddd83176..000000000 --- a/lib/puppet/string/v0.0.1/catalog.rb +++ /dev/null @@ -1,40 +0,0 @@ -require 'puppet/string/indirector' - -Puppet::String::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::String[: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/string/v0.0.1/catalog/select.rb b/lib/puppet/string/v0.0.1/catalog/select.rb deleted file mode 100644 index 52c77d3ce..000000000 --- a/lib/puppet/string/v0.0.1/catalog/select.rb +++ /dev/null @@ -1,10 +0,0 @@ -# Select and show a list of resources of a given type. -Puppet::String.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/string/v0.0.1/certificate.rb b/lib/puppet/string/v0.0.1/certificate.rb deleted file mode 100644 index 7b2e5f397..000000000 --- a/lib/puppet/string/v0.0.1/certificate.rb +++ /dev/null @@ -1,28 +0,0 @@ -require 'puppet/string/indirector' -require 'puppet/ssl/host' - -Puppet::String::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/string/v0.0.1/certificate_request.rb b/lib/puppet/string/v0.0.1/certificate_request.rb deleted file mode 100644 index 218b40b98..000000000 --- a/lib/puppet/string/v0.0.1/certificate_request.rb +++ /dev/null @@ -1,4 +0,0 @@ -require 'puppet/string/indirector' - -Puppet::String::Indirector.define(:certificate_request, '0.0.1') do -end diff --git a/lib/puppet/string/v0.0.1/certificate_revocation_list.rb b/lib/puppet/string/v0.0.1/certificate_revocation_list.rb deleted file mode 100644 index 9731b4f2d..000000000 --- a/lib/puppet/string/v0.0.1/certificate_revocation_list.rb +++ /dev/null @@ -1,4 +0,0 @@ -require 'puppet/string/indirector' - -Puppet::String::Indirector.define(:certificate_revocation_list, '0.0.1') do -end diff --git a/lib/puppet/string/v0.0.1/config.rb b/lib/puppet/string/v0.0.1/config.rb deleted file mode 100644 index ae1a408cf..000000000 --- a/lib/puppet/string/v0.0.1/config.rb +++ /dev/null @@ -1,11 +0,0 @@ -require 'puppet/string' - -Puppet::String.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/string/v0.0.1/configurer.rb b/lib/puppet/string/v0.0.1/configurer.rb deleted file mode 100644 index a6ea74b6a..000000000 --- a/lib/puppet/string/v0.0.1/configurer.rb +++ /dev/null @@ -1,12 +0,0 @@ -require 'puppet/string' - -Puppet::String.define(:configurer, '0.0.1') do - action(:synchronize) do - invoke do |certname| - facts = Puppet::String[:facts, '0.0.1'].find(certname) - catalog = Puppet::String[:catalog, '0.0.1'].download(certname, facts) - report = Puppet::String[:catalog, '0.0.1'].apply(catalog) - report - end - end -end diff --git a/lib/puppet/string/v0.0.1/facts.rb b/lib/puppet/string/v0.0.1/facts.rb deleted file mode 100644 index 73acb0df6..000000000 --- a/lib/puppet/string/v0.0.1/facts.rb +++ /dev/null @@ -1,18 +0,0 @@ -require 'puppet/string/indirector' -require 'puppet/node/facts' - -Puppet::String::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/string/v0.0.1/file.rb b/lib/puppet/string/v0.0.1/file.rb deleted file mode 100644 index cc5737f28..000000000 --- a/lib/puppet/string/v0.0.1/file.rb +++ /dev/null @@ -1,5 +0,0 @@ -require 'puppet/string/indirector' - -Puppet::String::Indirector.define(:file, '0.0.1') do - set_indirection_name :file_bucket_file -end diff --git a/lib/puppet/string/v0.0.1/key.rb b/lib/puppet/string/v0.0.1/key.rb deleted file mode 100644 index 95aceade5..000000000 --- a/lib/puppet/string/v0.0.1/key.rb +++ /dev/null @@ -1,4 +0,0 @@ -require 'puppet/string/indirector' - -Puppet::String::Indirector.define(:key, '0.0.1') do -end diff --git a/lib/puppet/string/v0.0.1/node.rb b/lib/puppet/string/v0.0.1/node.rb deleted file mode 100644 index bc31a2cf3..000000000 --- a/lib/puppet/string/v0.0.1/node.rb +++ /dev/null @@ -1,5 +0,0 @@ -require 'puppet/string/indirector' - -Puppet::String::Indirector.define(:node, '0.0.1') do - set_default_format :yaml -end diff --git a/lib/puppet/string/v0.0.1/report.rb b/lib/puppet/string/v0.0.1/report.rb deleted file mode 100644 index 55a008533..000000000 --- a/lib/puppet/string/v0.0.1/report.rb +++ /dev/null @@ -1,15 +0,0 @@ -require 'puppet/string/indirector' - -Puppet::String::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/string/v0.0.1/resource.rb b/lib/puppet/string/v0.0.1/resource.rb deleted file mode 100644 index 9838be0fa..000000000 --- a/lib/puppet/string/v0.0.1/resource.rb +++ /dev/null @@ -1,4 +0,0 @@ -require 'puppet/string/indirector' - -Puppet::String::Indirector.define(:resource, '0.0.1') do -end diff --git a/lib/puppet/string/v0.0.1/resource_type.rb b/lib/puppet/string/v0.0.1/resource_type.rb deleted file mode 100644 index 8ca31ea6c..000000000 --- a/lib/puppet/string/v0.0.1/resource_type.rb +++ /dev/null @@ -1,4 +0,0 @@ -require 'puppet/string/indirector' - -Puppet::String::Indirector.define(:resource_type, '0.0.1') do -end diff --git a/lib/puppet/string/v0.0.1/status.rb b/lib/puppet/string/v0.0.1/status.rb deleted file mode 100644 index 41de2bb99..000000000 --- a/lib/puppet/string/v0.0.1/status.rb +++ /dev/null @@ -1,4 +0,0 @@ -require 'puppet/string/indirector' - -Puppet::String::Indirector.define(:status, '0.0.1') do -end diff --git a/spec/unit/application/string_base_spec.rb b/spec/unit/application/string_base_spec.rb index bc563e11d..86f9c09aa 100755 --- a/spec/unit/application/string_base_spec.rb +++ b/spec/unit/application/string_base_spec.rb @@ -2,13 +2,19 @@ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') require 'puppet/application/string_base' +require 'tmpdir' + +class Puppet::Application::StringBase::Basetest < Puppet::Application::StringBase +end describe Puppet::Application::StringBase do before :all do @dir = Dir.mktmpdir $LOAD_PATH.push(@dir) - FileUtils.mkdir_p(File.join @dir, 'puppet', 'string', 'v0.0.1') - FileUtils.touch(File.join @dir, 'puppet', 'string', 'v0.0.1', 'basetest.rb') + FileUtils.mkdir_p(File.join @dir, 'puppet', 'string') + File.open(File.join(@dir, 'puppet', 'string', 'basetest.rb'), 'w') do |f| + f.puts "Puppet::String.define(:basetest, '0.0.1')" + end end after :all do @@ -16,13 +22,8 @@ describe Puppet::Application::StringBase do $LOAD_PATH.pop end - base_string = Puppet::String.define(:basetest, '0.0.1') - class Puppet::Application::StringBase::Basetest < Puppet::Application::StringBase - end - before do @app = Puppet::Application::StringBase::Basetest.new - @app.stubs(:string).returns base_string @app.stubs(:exit) @app.stubs(:puts) Puppet::Util::Log.stubs(:newdestination) diff --git a/spec/unit/string/string_collection_spec.rb b/spec/unit/string/string_collection_spec.rb index 46c431f75..63ddf7c5e 100755 --- a/spec/unit/string/string_collection_spec.rb +++ b/spec/unit/string/string_collection_spec.rb @@ -5,64 +5,19 @@ require 'tmpdir' describe Puppet::String::StringCollection do before :all do - @strings = subject.instance_variable_get("@strings").dup + @strings = subject.instance_variable_get("@strings") + @strings_backup = @strings.dup end - before :each do - subject.instance_variable_get("@strings").clear - end + before { @strings.clear } after :all do - subject.instance_variable_set("@strings", @strings) + subject.instance_variable_set("@strings", @strings_backup) end describe "::strings" do end - describe "::versions" do - before :each do - @dir = Dir.mktmpdir - @lib = FileUtils.mkdir_p(File.join @dir, 'puppet', 'string') - $LOAD_PATH.push(@dir) - end - - after :each do - FileUtils.remove_entry_secure @dir - $LOAD_PATH.pop - end - - it "should return an empty array when no versions are loadable" do - subject.versions(:fozzie).should == [] - end - - it "should return versions loadable as puppet/string/v{version}/{name}" do - FileUtils.mkdir_p(File.join @lib, 'v1.0.0') - FileUtils.touch(File.join @lib, 'v1.0.0', 'fozzie.rb') - subject.versions(:fozzie).should == ['1.0.0'] - end - - it "should an ordered list of all versions loadable as puppet/string/v{version}/{name}" do - %w[ 1.2.1rc2 1.2.1beta1 1.2.1rc1 1.2.1 1.2.2 ].each do |version| - FileUtils.mkdir_p(File.join @lib, "v#{version}") - FileUtils.touch(File.join @lib, "v#{version}", 'fozzie.rb') - end - subject.versions(:fozzie).should == %w[ 1.2.1beta1 1.2.1rc1 1.2.1rc2 1.2.1 1.2.2 ] - end - - it "should not return a version for an empty puppet/string/v{version}/{name}" do - FileUtils.mkdir_p(File.join @lib, 'v1.0.0', 'fozzie') - subject.versions(:fozzie).should == [] - end - - it "should an ordered list of all versions loadable as puppet/string/v{version}/{name}/*.rb" do - %w[ 1.2.1rc2 1.2.1beta1 1.2.1rc1 1.2.1 1.2.2 ].each do |version| - FileUtils.mkdir_p(File.join @lib, "v#{version}", "fozzie") - FileUtils.touch(File.join @lib, "v#{version}", 'fozzie', 'action.rb') - end - subject.versions(:fozzie).should == %w[ 1.2.1beta1 1.2.1rc1 1.2.1rc2 1.2.1 1.2.2 ] - end - end - describe "::validate_version" do it 'should permit three number versions' do subject.validate_version('10.10.10').should == true @@ -89,63 +44,6 @@ describe Puppet::String::StringCollection do end end - describe "::compare_versions" do - # (a <=> b) should be: - # -1 if a < b - # 0 if a == b - # 1 if a > b - it 'should sort major version numbers numerically' do - subject.compare_versions('1.0.0', '2.0.0').should == -1 - subject.compare_versions('2.0.0', '1.1.1').should == 1 - subject.compare_versions('2.0.0', '10.0.0').should == -1 - end - - it 'should sort minor version numbers numerically' do - subject.compare_versions('0.1.0', '0.2.0').should == -1 - subject.compare_versions('0.2.0', '0.1.1').should == 1 - subject.compare_versions('0.2.0', '0.10.0').should == -1 - end - - it 'should sort tiny version numbers numerically' do - subject.compare_versions('0.0.1', '0.0.2').should == -1 - subject.compare_versions('0.0.2', '0.0.1').should == 1 - subject.compare_versions('0.0.2', '0.0.10').should == -1 - end - - it 'should sort major version before minor version' do - subject.compare_versions('1.1.0', '1.2.0').should == -1 - subject.compare_versions('1.2.0', '1.1.1').should == 1 - subject.compare_versions('1.2.0', '1.10.0').should == -1 - - subject.compare_versions('1.1.0', '2.2.0').should == -1 - subject.compare_versions('2.2.0', '1.1.1').should == 1 - subject.compare_versions('2.2.0', '1.10.0').should == 1 - end - - it 'should sort minor version before tiny version' do - subject.compare_versions('0.1.1', '0.1.2').should == -1 - subject.compare_versions('0.1.2', '0.1.1').should == 1 - subject.compare_versions('0.1.2', '0.1.10').should == -1 - - subject.compare_versions('0.1.1', '0.2.2').should == -1 - subject.compare_versions('0.2.2', '0.1.1').should == 1 - subject.compare_versions('0.2.2', '0.1.10').should == 1 - end - - it 'should sort appended strings asciibetically' do - subject.compare_versions('0.0.0a', '0.0.0b').should == -1 - subject.compare_versions('0.0.0beta1', '0.0.0beta2').should == -1 - subject.compare_versions('0.0.0beta1', '0.0.0rc1').should == -1 - subject.compare_versions('0.0.0beta1', '0.0.0alpha1').should == 1 - subject.compare_versions('0.0.0beta1', '0.0.0beta1').should == 0 - end - - it "should sort appended strings before 'whole' versions" do - subject.compare_versions('0.0.1a', '0.0.1').should == -1 - subject.compare_versions('0.0.1', '0.0.1beta').should == 1 - end - end - describe "::[]" do before :each do subject.instance_variable_get("@strings")[:foo]['0.0.1'] = 10 @@ -167,17 +65,15 @@ describe Puppet::String::StringCollection do end it "should attempt to load the string if it isn't found" do - subject.expects(:require).with('puppet/string/v0.0.1/bar') + subject.expects(:require).with('puppet/string/bar') + subject.expects(:require).with('bar@0.0.1/puppet/string/bar') subject["bar", '0.0.1'] end - it "should attempt to load the string with the greatest version for specified version :latest" do - %w[ 1.2.1 1.2.2 ].each do |version| - FileUtils.mkdir_p(File.join @lib, "v#{version}") - FileUtils.touch(File.join @lib, "v#{version}", 'fozzie.rb') - end - subject.expects(:require).with('puppet/string/v1.2.2/fozzie') - subject['fozzie', :latest] + it "should attempt to load the default string for the specified version :current" do + subject.expects(:require).never # except... + subject.expects(:require).with('puppet/string/fozzie') + subject['fozzie', :current] end end @@ -191,14 +87,25 @@ describe Puppet::String::StringCollection do end it "should attempt to require the string if it is not registered" do - subject.expects(:require).with('puppet/string/v0.0.1/bar') - subject.string?("bar", '0.0.1') + subject.expects(:require).with do |file| + @strings[:bar]['0.0.1'] = true + file == 'puppet/string/bar' + end + subject.string?("bar", '0.0.1').should == true end it "should return true if requiring the string registered it" do subject.stubs(:require).with do subject.instance_variable_get("@strings")[:bar]['0.0.1'] = 20 end + end + + it "should require the string by version if the 'current' version isn't it" do + subject.expects(:require).with('puppet/string/bar').raises(LoadError) + subject.expects(:require).with do |file| + @strings[:bar]['0.0.1'] = true + file == 'bar@0.0.1/puppet/string/bar' + end subject.string?("bar", '0.0.1').should == true end @@ -211,6 +118,34 @@ describe Puppet::String::StringCollection do subject.stubs(:require).raises(LoadError) subject.string?("bar", '0.0.1').should == false end + + it "should register the version loaded by `:current` as `:current`" do + subject.expects(:require).with do |file| + @strings[:huzzah]['2.0.1'] = :huzzah_string + file == 'puppet/string/huzzah' + end + subject.string?("huzzah", :current) + @strings[:huzzah][:current].should == :huzzah_string + end + + it "should register the version loaded from `puppet/string/{name}` as `:current`" do + subject.expects(:require).with do |file| + @strings[:huzzah]['2.0.1'] = :huzzah_string + file == 'puppet/string/huzzah' + end + subject.string?("huzzah", '2.0.1') + @strings[:huzzah][:current].should == :huzzah_string + end + + it "should not register the version loaded from `{name}@{version}` as `:current`" do + subject.expects(:require).with('puppet/string/huzzah').raises(LoadError) + subject.expects(:require).with do |file| + @strings[:huzzah]['0.0.1'] = true + file == 'huzzah@0.0.1/puppet/string/huzzah' + end + subject.string?("huzzah", '0.0.1') + @strings[:huzzah].should_not have_key(:current) + end end describe "::register" do diff --git a/spec/unit/string_spec.rb b/spec/unit/string_spec.rb index 73d1f2177..64d4f12f8 100755 --- a/spec/unit/string_spec.rb +++ b/spec/unit/string_spec.rb @@ -75,7 +75,8 @@ describe Puppet::String do end it "should try to require strings that are not known" do - Puppet::String::StringCollection.expects(:require).with "puppet/string/v0.0.1/foo" + Puppet::String::StringCollection.expects(:require).with "puppet/string/foo" + Puppet::String::StringCollection.expects(:require).with "foo@0.0.1/puppet/string/foo" Puppet::String[:foo, '0.0.1'] end -- cgit From f7f1e5822f3921336872956fe07c4da4406ce8eb Mon Sep 17 00:00:00 2001 From: Pieter van de Bruggen Date: Tue, 29 Mar 2011 13:29:01 -0700 Subject: (#6770) Fix Puppet::String#load_actions. Reviewed-By: Nick Lewis --- lib/puppet/string.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/puppet/string.rb b/lib/puppet/string.rb index 9a223a40c..783b6afe0 100644 --- a/lib/puppet/string.rb +++ b/lib/puppet/string.rb @@ -87,7 +87,7 @@ class Puppet::String end loaded << aname Puppet.debug "Loading action '#{aname}' for '#{name}' from '#{fdir}/#{file}'" - require "#{path}/#{aname}" + require "#{Dir.pwd}/#{aname}" end end end -- cgit