diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/puppet/indirector.rb | 4 | ||||
-rw-r--r-- | lib/puppet/indirector/code/configuration.rb | 10 | ||||
-rw-r--r-- | lib/puppet/indirector/indirection.rb | 24 | ||||
-rw-r--r-- | lib/puppet/indirector/terminus.rb | 39 | ||||
-rw-r--r-- | lib/puppet/indirector/yaml.rb | 18 | ||||
-rw-r--r-- | lib/puppet/indirector/yaml/configuration.rb | 18 | ||||
-rw-r--r-- | lib/puppet/network/handler/configuration.rb | 10 | ||||
-rw-r--r-- | lib/puppet/node/configuration.rb | 57 | ||||
-rw-r--r-- | lib/puppet/pgraph.rb | 4 | ||||
-rw-r--r-- | lib/puppet/provider/package/portage.rb | 4 | ||||
-rwxr-xr-x | lib/puppet/provider/service/debian.rb | 2 | ||||
-rw-r--r-- | lib/puppet/transportable.rb | 68 | ||||
-rw-r--r-- | lib/puppet/type/component.rb | 10 |
13 files changed, 208 insertions, 60 deletions
diff --git a/lib/puppet/indirector.rb b/lib/puppet/indirector.rb index 7ceaa43e2..a2eb41763 100644 --- a/lib/puppet/indirector.rb +++ b/lib/puppet/indirector.rb @@ -62,6 +62,10 @@ module Puppet::Indirector end module InstanceMethods + # Make it easy for the model to set versions, + # which are used for caching and such. + attr_accessor :version + # these become instance methods def save(*args) self.class.indirection.save(self, *args) diff --git a/lib/puppet/indirector/code/configuration.rb b/lib/puppet/indirector/code/configuration.rb index 2d8fedcc8..949926a3c 100644 --- a/lib/puppet/indirector/code/configuration.rb +++ b/lib/puppet/indirector/code/configuration.rb @@ -21,9 +21,13 @@ class Puppet::Indirector::Code::Configuration < Puppet::Indirector::Code node = find_node(key) end - configuration = compile(node) - - return configuration + if configuration = compile(node) + return configuration.to_transportable + else + # This shouldn't actually happen; we should either return + # a config or raise an exception. + return nil + end end def initialize diff --git a/lib/puppet/indirector/indirection.rb b/lib/puppet/indirector/indirection.rb index 3a6284877..5f7baa3da 100644 --- a/lib/puppet/indirector/indirection.rb +++ b/lib/puppet/indirector/indirection.rb @@ -90,12 +90,21 @@ class Puppet::Indirector::Indirection end end - def find(*args) - terminus.find(*args) + def find(key, *args) + if cache? and cache.has_most_recent?(key, terminus.version(key)) + return cache.find(key, *args) + end + if result = terminus.find(key, *args) + result.version ||= Time.now.utc + if cache? + Puppet.info "Caching %s %s" % [self.name, key] + cache.save(result, *args) + end + return result + end end def destroy(*args) - cache.destroy(*args) if cache? terminus.destroy(*args) end @@ -104,9 +113,12 @@ class Puppet::Indirector::Indirection end # these become instance methods - def save(*args) - cache.save(*args) if cache? - terminus.save(*args) + def save(instance, *args) + instance.version ||= Time.now.utc + dest = cache? ? cache : terminus + return if dest.has_most_recent?(instance.name, instance.version) + cache.save(instance, *args) if cache? + terminus.save(instance, *args) end private diff --git a/lib/puppet/indirector/terminus.rb b/lib/puppet/indirector/terminus.rb index 3e0ea447c..37cfdc499 100644 --- a/lib/puppet/indirector/terminus.rb +++ b/lib/puppet/indirector/terminus.rb @@ -95,25 +95,52 @@ class Puppet::Indirector::Terminus end end + # Do we have an update for this object? This compares the provided version + # to our version, and returns true if our version is at least as high + # as the asked-about version. + def has_most_recent?(key, vers) + raise Puppet::DevError.new("Cannot check update status when no 'version' method is defined") unless respond_to?(:version) + + if existing_version = version(key) + #puts "%s fresh: %s (%s vs %s)" % [self.name, (existing_version.to_f >= vers.to_f).inspect, existing_version.to_f, vers.to_f] + existing_version.to_f >= vers.to_f + else + false + end + end + + def indirection + self.class.indirection + end + def initialize if self.class.abstract_terminus? raise Puppet::DevError, "Cannot create instances of abstract terminus types" end end - def terminus_type - self.class.terminus_type + def model + self.class.model end def name self.class.name end - def model - self.class.model + def terminus_type + self.class.terminus_type end - def indirection - self.class.indirection + # Provide a default method for retrieving an instance's version. + # By default, just find the resource and get its version. Individual + # terminus types can override this method to provide custom definitions of + # 'versions'. + def version(name) + raise Puppet::DevError.new("Cannot retrieve an instance's version without a :find method") unless respond_to?(:find) + if instance = find(name) + instance.version + else + nil + end end end diff --git a/lib/puppet/indirector/yaml.rb b/lib/puppet/indirector/yaml.rb index b9ea54f39..e11c88474 100644 --- a/lib/puppet/indirector/yaml.rb +++ b/lib/puppet/indirector/yaml.rb @@ -14,9 +14,9 @@ class Puppet::Indirector::Yaml < Puppet::Indirector::Terminus return nil unless FileTest.exist?(file) begin - return YAML.load(File.read(file)) + return from_yaml(File.read(file)) rescue => detail - raise Puppet::Error, "Could not read YAML data for %s(%s): %s" % [indirection.name, name, detail] + raise Puppet::Error, "Could not read YAML data for %s %s: %s" % [indirection.name, name, detail] end end @@ -33,11 +33,23 @@ class Puppet::Indirector::Yaml < Puppet::Indirector::Terminus Dir.mkdir(basedir) end - File.open(file, "w", 0660) { |f| f.print YAML.dump(object) } + begin + File.open(file, "w", 0660) { |f| f.print to_yaml(object) } + rescue TypeError => detail + Puppet.err "Could not save %s %s: %s" % [self.name, object.name, detail] + end end private + def from_yaml(text) + YAML.load(text) + end + + def to_yaml(object) + YAML.dump(object) + end + # Return the path to a given node's file. def path(name) File.join(Puppet[:yamldir], self.name.to_s, name.to_s + ".yaml") diff --git a/lib/puppet/indirector/yaml/configuration.rb b/lib/puppet/indirector/yaml/configuration.rb index 691f0e343..8e40ff9bf 100644 --- a/lib/puppet/indirector/yaml/configuration.rb +++ b/lib/puppet/indirector/yaml/configuration.rb @@ -2,4 +2,22 @@ require 'puppet/indirector/yaml' class Puppet::Indirector::Yaml::Configuration < Puppet::Indirector::Yaml desc "Store configurations as flat files, serialized using YAML." + + private + + # Override these, because yaml doesn't want to convert our self-referential + # objects. This is hackish, but eh. + def from_yaml(text) + if config = YAML.load(text) + # We can't yaml-dump classes. + #config.edgelist_class = Puppet::Relationship + return config + end + end + + def to_yaml(config) + # We can't yaml-dump classes. + #config.edgelist_class = nil + YAML.dump(config) + end end diff --git a/lib/puppet/network/handler/configuration.rb b/lib/puppet/network/handler/configuration.rb index 09c4b971a..680304e2a 100644 --- a/lib/puppet/network/handler/configuration.rb +++ b/lib/puppet/network/handler/configuration.rb @@ -13,7 +13,7 @@ class Puppet::Network::Handler include Puppet::Util - attr_accessor :local + attr_accessor :local, :classes @interface = XMLRPC::Service::Interface.new("configuration") { |iface| iface.add_method("string configuration(string)") @@ -43,16 +43,10 @@ class Puppet::Network::Handler end def initialize(options = {}) - if options[:Local] - @local = options[:Local] - else - @local = false - end - options.each do |param, value| case param when :Classes: @classes = value - when :Local: self.local = true + when :Local: self.local = value else raise ArgumentError, "Configuration handler does not accept %s" % param end diff --git a/lib/puppet/node/configuration.rb b/lib/puppet/node/configuration.rb index 084bdf880..da8dc3a9a 100644 --- a/lib/puppet/node/configuration.rb +++ b/lib/puppet/node/configuration.rb @@ -22,6 +22,10 @@ class Puppet::Node::Configuration < Puppet::PGraph # How we should extract the configuration for sending to the client. attr_reader :extraction_format + # We need the ability to set this externally, so we can yaml-dump the + # configuration. + attr_accessor :edgelist_class + # Whether this is a host configuration, which behaves very differently. # In particular, reports are sent, graphs are made, and state is # stored in the state database. If this is set incorrectly, then you often @@ -355,6 +359,16 @@ class Puppet::Node::Configuration < Puppet::PGraph @tags.dup end + # Convert our configuration into a RAL configuration. + def to_ral + to_configuration :to_type + end + + # Turn our parser configuration into a transportable configuration. + def to_transportable + to_configuration :to_transobject + end + # Produce the graph files if requested. def write_graph(name) # We only want to graph the main host configuration. @@ -370,6 +384,14 @@ class Puppet::Node::Configuration < Puppet::PGraph } end + # LAK:NOTE We cannot yaml-dump the class in the edgelist_class, because classes cannot be + # dumped by default, nor does yaml-dumping # the edge-labels work at this point (I don't + # know why). + # Neither of these matters right now, but I suppose it could at some point. + def to_yaml_properties + instance_variables.reject { |v| %w{@edgelist_class @edge_labels}.include?(v) } + end + private def cleanup @@ -379,4 +401,39 @@ class Puppet::Node::Configuration < Puppet::PGraph @relationship_graph = nil end end + + # An abstracted method for converting one configuration into another type of configuration. + # This pretty much just converts all of the resources from one class to another, using + # a conversion method. + def to_configuration(convert) + result = self.class.new(self.name) + + vertices.each do |resource| + next if resource.respond_to?(:virtual?) and resource.virtual? + + result.add_resource resource.send(convert) + end + + message = convert.to_s.gsub "_", " " + edges.each do |edge| + # Skip edges between virtual resources. + next if edge.source.respond_to?(:virtual?) and edge.source.virtual? + next if edge.target.respond_to?(:virtual?) and edge.target.virtual? + + unless source = result.resource(edge.source.ref) + raise Puppet::DevError, "Could not find vertex for %s when converting %s" % [edge.source.ref, message] + end + + unless target = result.resource(edge.target.ref) + raise Puppet::DevError, "Could not find vertex for %s when converting %s" % [edge.target.ref, message] + end + + result.add_edge!(source, target, edge.label) + end + + result.add_class *self.classes + result.tag(*self.tags) + + return result + end end diff --git a/lib/puppet/pgraph.rb b/lib/puppet/pgraph.rb index 2399d1651..ca45aa2b3 100644 --- a/lib/puppet/pgraph.rb +++ b/lib/puppet/pgraph.rb @@ -190,6 +190,10 @@ class Puppet::PGraph < GRATR::Digraph return result end + def to_yaml_properties + instance_variables + end + # A different way of walking a tree, and a much faster way than the # one that comes with GRATR. def tree_from_vertex2(start, direction = :out) diff --git a/lib/puppet/provider/package/portage.rb b/lib/puppet/provider/package/portage.rb index 866ec8730..6a42444ef 100644 --- a/lib/puppet/provider/package/portage.rb +++ b/lib/puppet/provider/package/portage.rb @@ -55,7 +55,7 @@ Puppet::Type.type(:package).provide :portage, :parent => Puppet::Provider::Packa # The common package name format. def package_name - "%s/%s" % [@resource[:category], @resource[:name]] + @resource[:category] ? "%s/%s" % [@resource[:category], @resource[:name]] : @resource[:name] end def uninstall @@ -71,7 +71,7 @@ Puppet::Type.type(:package).provide :portage, :parent => Puppet::Provider::Packa result_fields = [:category, :name, :ensure, :version_available, :slot, :vendor, :description] search_field = @resource[:category] ? "--category-name" : "--name" - search_value = @resource[:category] ? package_name : @resource[:name] + search_value = package_name search_format = "<category> <name> [<installedversionsshort>] [<best>] <homepage> <description>" begin diff --git a/lib/puppet/provider/service/debian.rb b/lib/puppet/provider/service/debian.rb index d810eac1b..0ba7e1a79 100755 --- a/lib/puppet/provider/service/debian.rb +++ b/lib/puppet/provider/service/debian.rb @@ -17,7 +17,7 @@ Puppet::Type.type(:service).provide :debian, :parent => :init do # If it's enabled, then it will print output showing removal of # links. - if output =~ /etc\/rc[\dS].d|Nothing to do\./ + if output =~ /etc\/rc[\dS].d|not installed/ return :true else return :false diff --git a/lib/puppet/transportable.rb b/lib/puppet/transportable.rb index ecd179ed7..2a600b50e 100644 --- a/lib/puppet/transportable.rb +++ b/lib/puppet/transportable.rb @@ -8,7 +8,7 @@ module Puppet # YAML. class TransObject include Enumerable - attr_accessor :type, :name, :file, :line, :collectable, :collected + attr_accessor :type, :name, :file, :line, :configuration attr_writer :tags @@ -25,7 +25,6 @@ module Puppet def initialize(name,type) @type = type @name = name - @collectable = false @params = {} @tags = [] end @@ -34,10 +33,44 @@ module Puppet return [@type,@name].join('--') end + def ref + unless defined? @ref + if @type == :component + @ref = @name + else + @ref = "%s[%s]" % [type_capitalized, name] + end + end + @ref + end + def tags return @tags end + # Convert a defined type into a component. + def to_component + tmpname = nil + + # Nodes have the same name and type + if self.name + tmpname = "%s[%s]" % [type_capitalized, self.name] + else + tmpname = @type + end + trans = TransObject.new(tmpname, :component) + if defined? @parameters + @parameters.each { |param,value| + Puppet.debug "Defining %s on %s of type %s" % + [param,@name,@type] + trans[param] = value + } + else + #Puppet.debug "%s[%s] has no parameters" % [@type, @name] + end + Puppet::Type::Component.create(trans) + end + def to_hash @params.dup end @@ -57,18 +90,18 @@ module Puppet end def to_yaml_properties - instance_variables + instance_variables.reject { |v| %w{@ref}.include?(v) } end def to_ref - unless defined? @ref + unless defined? @res_ref if self.type and self.name - @ref = "%s[%s]" % [self.type, self.name] + @res_ref = "%s[%s]" % [type_capitalized, self.name] else - @ref = nil + @res_ref = nil end end - @ref + @res_ref end def to_type @@ -85,11 +118,16 @@ module Puppet end end else - raise Puppet::Error.new("Could not find object type %s" % self.type) + return to_component end return retobj end + + # Return the type fully capitalized correctly. + def type_capitalized + type.split("::").collect { |s| s.capitalize }.join("::") + end end # Just a linear container for objects. Behaves mostly like an array, except @@ -107,20 +145,6 @@ module Puppet end } - # Remove all collectable objects from our tree, since the client - # should not see them. - def collectstrip! - @children.dup.each do |child| - if child.is_a? self.class - child.collectstrip! - else - if child.collectable and ! child.collected - @children.delete(child) - end - end - end - end - # Recursively yield everything. def delve(&block) @children.each do |obj| diff --git a/lib/puppet/type/component.rb b/lib/puppet/type/component.rb index c5842e0e7..4dc542a65 100644 --- a/lib/puppet/type/component.rb +++ b/lib/puppet/type/component.rb @@ -109,14 +109,6 @@ Puppet::Type.newtype(:component) do @children = [] end - def parent=(parent) - if self.parentof?(parent) - devfail "%s[%s] is already the parent of %s[%s]" % - [self.class.name, self.title, parent.class.name, parent.title] - end - @parent = parent - end - # Add a hook for testing for recursion. def parentof?(child) if super(child) @@ -133,7 +125,7 @@ Puppet::Type.newtype(:component) do def pathbuilder tmp = [] myname = "" - if self.title =~ /^class\[(.+)\]$/ + if self.title =~ /^class\[(.+)\]$/i # 'main' is the top class, so we want to see '//' instead of # its name. unless $1 == "main" |