diff options
author | Luke Kanies <luke@madstop.com> | 2007-11-13 11:24:22 -0600 |
---|---|---|
committer | Luke Kanies <luke@madstop.com> | 2007-11-13 11:24:22 -0600 |
commit | ffb4c2dbc7314b364d25e4f7be599ef05b767b44 (patch) | |
tree | 819cdb29053d004cfab70a22ea95a576b1608df7 | |
parent | b65fb8316e10fb31c4ac9d0af75bb3e7f452d312 (diff) | |
download | puppet-ffb4c2dbc7314b364d25e4f7be599ef05b767b44.tar.gz puppet-ffb4c2dbc7314b364d25e4f7be599ef05b767b44.tar.xz puppet-ffb4c2dbc7314b364d25e4f7be599ef05b767b44.zip |
This commit is the first run at removing all global
references to resources. It deprecates the class-level
[] and []= methods, used for so long to provide closure
behaviour but now unnecessary with the node configuration's
ability to function as a resource container.
All of the spec tests pass, but there is much to do to make
the test/ tests pass, I expect.
-rw-r--r-- | lib/puppet/metatype/instances.rb | 37 | ||||
-rw-r--r-- | lib/puppet/metatype/metaparams.rb | 11 | ||||
-rw-r--r-- | lib/puppet/metatype/relationships.rb | 6 | ||||
-rw-r--r-- | lib/puppet/network/client/master.rb | 25 | ||||
-rw-r--r-- | lib/puppet/node/configuration.rb | 5 | ||||
-rw-r--r-- | lib/puppet/transportable.rb | 15 | ||||
-rw-r--r-- | lib/puppet/type.rb | 6 | ||||
-rw-r--r-- | lib/puppet/type/component.rb | 8 | ||||
-rw-r--r-- | lib/puppet/type/pfile.rb | 11 | ||||
-rwxr-xr-x | lib/puppet/type/pfilebucket.rb | 8 | ||||
-rwxr-xr-x | lib/puppet/type/schedule.rb | 14 | ||||
-rwxr-xr-x | spec/unit/network/client/master.rb | 79 | ||||
-rwxr-xr-x | spec/unit/node/configuration.rb | 1 |
13 files changed, 132 insertions, 94 deletions
diff --git a/lib/puppet/metatype/instances.rb b/lib/puppet/metatype/instances.rb index 8cc648e8f..8fc25d0bb 100644 --- a/lib/puppet/metatype/instances.rb +++ b/lib/puppet/metatype/instances.rb @@ -8,11 +8,13 @@ class Puppet::Type # retrieve a named instance of the current type def self.[](name) + raise "DEPRECATED [], yo" @objects[name] || @aliases[name] end # add an instance by name to the class list of instances def self.[]=(name,object) + raise "DEPRECATED []=, yo" newobj = nil if object.is_a?(Puppet::Type) newobj = object @@ -128,36 +130,6 @@ class Puppet::Type #Puppet.debug "Creating %s[%s]" % [self.name, title] - # if the object already exists - if self.isomorphic? and retobj = self[title] - # if only one of our objects is implicit, then it's easy to see - # who wins -- the non-implicit one. - if retobj.implicit? and ! implicit - Puppet.notice "Removing implicit %s" % retobj.title - # Remove all of the objects, but do not remove their subscriptions. - retobj.remove(false) - - # now pass through and create the new object - elsif implicit - Puppet.debug "Ignoring implicit %s[%s]" % [self.name, title] - return nil - else - # If only one of the objects is being managed, then merge them - if retobj.managed? - raise Puppet::Error, "%s '%s' is already being managed" % - [self.name, title] - else - retobj.merge(hash) - return retobj - end - # We will probably want to support merging of some kind in - # the future, but for now, just throw an error. - #retobj.merge(hash) - - #return retobj - end - end - # create it anew # if there's a failure, destroy the object if it got that far, but raise # the error. @@ -167,8 +139,6 @@ class Puppet::Type Puppet.err "Could not create %s: %s" % [title, detail.to_s] if obj obj.remove(true) - elsif obj = self[title] - obj.remove(true) end raise end @@ -177,9 +147,6 @@ class Puppet::Type obj.implicit = true end - # Store the object by title - self[obj.title] = obj - return obj end diff --git a/lib/puppet/metatype/metaparams.rb b/lib/puppet/metatype/metaparams.rb index eb158a47d..8ac2ead9f 100644 --- a/lib/puppet/metatype/metaparams.rb +++ b/lib/puppet/metatype/metaparams.rb @@ -272,16 +272,11 @@ class Puppet::Type # which resource is applied first and which resource is considered # to be the event generator. def to_edges - @value.collect do |value| + raise(Puppet::DevError, "Cannot create dependencies without a configuration") unless resource.configuration + @value.collect do |tname, name| # we just have a name and a type, and we need to convert it # to an object... - tname, name = value - object = nil - if type = Puppet::Type.type(tname) - object = type[name] - else # try to treat it as a component - object = Puppet::Type::Component["#{tname}[#{name}]"] - end + object = resource.configuration.resource(tname, name) # Either of the two retrieval attempts could have returned # nil. diff --git a/lib/puppet/metatype/relationships.rb b/lib/puppet/metatype/relationships.rb index 4fb78ae56..332702a25 100644 --- a/lib/puppet/metatype/relationships.rb +++ b/lib/puppet/metatype/relationships.rb @@ -18,11 +18,9 @@ class Puppet::Type # Figure out of there are any objects we can automatically add as # dependencies. def autorequire + raise(ArgumentError, "Cannot autorequire resources without a configuration") unless configuration reqs = [] self.class.eachautorequire { |type, block| - # Ignore any types we can't find, although that would be a bit odd. - next unless typeobj = Puppet.type(type) - # Retrieve the list of names from the block. next unless list = self.instance_eval(&block) unless list.is_a?(Array) @@ -35,7 +33,7 @@ class Puppet::Type # Support them passing objects directly, to save some effort. unless dep.is_a? Puppet::Type # Skip autorequires that we aren't managing - unless dep = typeobj[dep] + unless dep = configuration.resource(type, dep) next end end diff --git a/lib/puppet/network/client/master.rb b/lib/puppet/network/client/master.rb index ea351ddc3..2b0c99d33 100644 --- a/lib/puppet/network/client/master.rb +++ b/lib/puppet/network/client/master.rb @@ -49,6 +49,15 @@ class Puppet::Network::Client::Master < Puppet::Network::Client Puppet.settings[:dynamicfacts].split(/\s*,\s*/).collect { |fact| fact.downcase } end + # Add our default resources to the configuration. + def add_default_resources(configuration) + # First create the default scheduling objects + Puppet::Type.type(:schedule).add_default_schedules(configuration) + + # And filebuckets + Puppet::Type.type(:filebucket).add_default_filebucket(configuration) + end + # Cache the config def cache(text) Puppet.info "Caching configuration at %s" % self.cachefile @@ -69,7 +78,6 @@ class Puppet::Network::Client::Master < Puppet::Network::Client def clear @configuration.clear(true) if @configuration Puppet::Type.allclear - mkdefault_objects @configuration = nil end @@ -190,6 +198,8 @@ class Puppet::Network::Client::Master < Puppet::Network::Client # Keep the state database up to date. @configuration.host_config = true + + add_default_resources(@configuration) end # A simple proxy method, so it's easy to test. @@ -204,17 +214,6 @@ class Puppet::Network::Client::Master < Puppet::Network::Client self.class.instance = self @running = false - - mkdefault_objects - end - - # Make the default objects necessary for function. - def mkdefault_objects - # First create the default scheduling objects - Puppet::Type.type(:schedule).mkdefaultschedules - - # And filebuckets - Puppet::Type.type(:filebucket).mkdefaultbucket end # Mark that we should restart. The Puppet module checks whether we're running, @@ -582,6 +581,8 @@ class Puppet::Network::Client::Master < Puppet::Network::Client clear return false end + + add_default_resources(@configuration) return true end end diff --git a/lib/puppet/node/configuration.rb b/lib/puppet/node/configuration.rb index 804f357d1..061e83f4b 100644 --- a/lib/puppet/node/configuration.rb +++ b/lib/puppet/node/configuration.rb @@ -370,6 +370,11 @@ class Puppet::Node::Configuration < Puppet::PGraph end end + # Return an array of the currently-defined resources. + def resources + @resource_table.keys + end + # Add a tag. def tag(*names) names.each do |name| diff --git a/lib/puppet/transportable.rb b/lib/puppet/transportable.rb index 6a573489c..8f8c32b8d 100644 --- a/lib/puppet/transportable.rb +++ b/lib/puppet/transportable.rb @@ -52,13 +52,10 @@ module Puppet 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 + tmpname = "%s[%s]" % [type_capitalized, self.name] + trans = TransObject.new(tmpname, :component) + @params.each { |param,value| next unless Puppet::Type::Component.validattr?(param) Puppet.debug "Defining %s on %s of type %s" % [param,@name,@type] @@ -91,11 +88,7 @@ module Puppet def to_ref unless defined? @res_ref - if self.type and self.name - @res_ref = "%s[%s]" % [type_capitalized, self.name] - else - @res_ref = nil - end + @res_ref = "%s[%s]" % [type_capitalized, self.name] end @res_ref end diff --git a/lib/puppet/type.rb b/lib/puppet/type.rb index f5dd0f8dd..02e04653c 100644 --- a/lib/puppet/type.rb +++ b/lib/puppet/type.rb @@ -229,14 +229,14 @@ class Type end # If the name and title differ, set up an alias - if self.name != self.title - if obj = self.class[self.name] + if self.configuration and (self.name != self.title) + if obj = self.configuration.resource(self.class.name, self.name) if self.class.isomorphic? raise Puppet::Error, "%s already exists with name %s" % [obj.title, self.name] end else - self.class.alias(self.name, self) + self.configuration.alias(self, self.name) end end diff --git a/lib/puppet/type/component.rb b/lib/puppet/type/component.rb index 7aa24a302..0a7c182cf 100644 --- a/lib/puppet/type/component.rb +++ b/lib/puppet/type/component.rb @@ -20,7 +20,7 @@ Puppet::Type.newtype(:component) do desc "The type that this component maps to. Generally some kind of class from the language." - defaultto "component" + defaultto "class" end # Remove a child from the component. @@ -97,10 +97,8 @@ Puppet::Type.newtype(:component) do @children = [] super - # If the title isn't a full resource reference, assume - # we're a class and make an alias for that. - unless @title.to_s.include?("[") - self.class.alias("class[%s]" % @title, self) + unless @title.include?("[") + @title = "%s[%s]" % [self[:type].capitalize, @title] end end diff --git a/lib/puppet/type/pfile.rb b/lib/puppet/type/pfile.rb index 73c60bd14..723fecde2 100644 --- a/lib/puppet/type/pfile.rb +++ b/lib/puppet/type/pfile.rb @@ -73,10 +73,13 @@ module Puppet " defaultto do - # Make sure the default file bucket exists. - obj = Puppet::Type.type(:filebucket)["puppet"] || - Puppet::Type.type(:filebucket).create(:name => "puppet") - obj.bucket + if resource.configuration + # Make sure the default file bucket exists. + obj = resource.configuration.resource(:filebucket, "puppet") || resource.configuration.create_resource(:filebucket, :name => "puppet") + obj.bucket + else + nil + end end munge do |value| diff --git a/lib/puppet/type/pfilebucket.rb b/lib/puppet/type/pfilebucket.rb index cf4e5aac3..065d07fcb 100755 --- a/lib/puppet/type/pfilebucket.rb +++ b/lib/puppet/type/pfilebucket.rb @@ -64,11 +64,11 @@ module Puppet end # Create a default filebucket. - def self.mkdefaultbucket - unless default = self["puppet"] - default = self.create :name => "puppet", :path => Puppet[:clientbucketdir] + def self.add_default_bucket(configuration) + unless configuration.resource(:filebucket, "puppet") + Puppet.debug "Creating default local filebucket" + configuration.create :filebucket, :name => "puppet", :path => Puppet[:clientbucketdir] end - default end def self.instances diff --git a/lib/puppet/type/schedule.rb b/lib/puppet/type/schedule.rb index 46cff10f5..9772762bb 100755 --- a/lib/puppet/type/schedule.rb +++ b/lib/puppet/type/schedule.rb @@ -311,11 +311,12 @@ module Puppet [] end - def self.mkdefaultschedules + def self.add_default_schedules(configuration) + Puppet.debug "Creating default schedules" + resources = [] # Create our default schedule - unless self["puppet"] - Puppet.debug "Creating default schedules" - self.create( + unless configuration.resource(:schedule, "puppet") + configuration.create(:schedule, :name => "puppet", :period => :hourly, :repeat => "2" @@ -324,8 +325,8 @@ module Puppet # And then one for every period @parameters.find { |p| p.name == :period }.values.each { |value| - unless self[value.to_s] - self.create( + unless configuration.resource(:schedule, value.to_s) + configuraiton.create(:schedule, :name => value.to_s, :period => value ) @@ -334,7 +335,6 @@ module Puppet end def match?(previous = nil, now = nil) - # If we've got a value, then convert it to a Time instance if previous previous = Time.at(previous) diff --git a/spec/unit/network/client/master.rb b/spec/unit/network/client/master.rb index dca923994..68bb21d5e 100755 --- a/spec/unit/network/client/master.rb +++ b/spec/unit/network/client/master.rb @@ -72,6 +72,7 @@ describe Puppet::Network::Client::Master, " when retrieving the configuration" d @client.stubs(:fresh?).with(@facts).returns true @client.stubs(:use_cached_config).returns(true) @client.class.stubs(:facts).returns(@facts) + @client.stubs(:add_default_resources) @client.getconfig end @@ -97,6 +98,7 @@ describe Puppet::Network::Client::Master, " when retrieving the configuration" d config.stubs(:to_configuration).returns(config) config.stubs(:host_config=) config.stubs(:from_cache).returns(true) + @client.stubs(:add_default_resources) @client.getconfig end @@ -127,6 +129,7 @@ describe Puppet::Network::Client::Master, " when retrieving the configuration" d config.stubs(:to_configuration).returns(config) config.stubs(:host_config=) config.stubs(:from_cache).returns(true) + @client.stubs(:add_default_resources) @client.getconfig end @@ -145,9 +148,9 @@ describe Puppet::Network::Client::Master, " when retrieving the configuration" d yamlconfig.stubs(:classes) yamlconfig.expects(:to_configuration).returns(config) - config.stubs(:host_config=) config.stubs(:from_cache).returns(true) + @client.stubs(:add_default_resources) @client.getconfig end @@ -210,6 +213,7 @@ describe Puppet::Network::Client::Master, " when retrieving the configuration" d yamlconfig.expects(:to_configuration).returns(config) config.stubs(:host_config=) + @client.stubs(:add_default_resources) config.expects(:from_cache).returns(false) @@ -236,6 +240,31 @@ describe Puppet::Network::Client::Master, " when retrieving the configuration" d config.stubs(:from_cache).returns(true) config.expects(:host_config=).with(true) + @client.stubs(:add_default_resources) + + @client.getconfig + end + + it "should add the default resources to the configuration" do + @client.stubs(:dostorage) + @client.class.stubs(:facts).returns(@facts) + @master.stubs(:getconfig).returns("myconfig") + + yamlconfig = mock 'yaml config' + YAML.stubs(:load).returns(yamlconfig) + + @client.stubs(:setclasses) + + config = mock 'config' + + yamlconfig.stubs(:classes) + yamlconfig.stubs(:to_configuration).returns(config) + + config.stubs(:from_cache).returns(true) + + config.stubs(:host_config=).with(true) + + @client.expects(:add_default_resources).with(config) @client.getconfig end @@ -307,6 +336,7 @@ describe Puppet::Network::Client::Master, " when using the cached configuration" config.expects(:to_configuration).returns(ral_config) @client.stubs(:retrievecache).returns("whatever") + @client.stubs(:add_default_resources) Puppet::Network::Client::Master.publicize_methods :use_cached_config do @client.use_cached_config().should be_true end @@ -322,6 +352,7 @@ describe Puppet::Network::Client::Master, " when using the cached configuration" config.expects(:to_configuration).returns(ral_config) @client.stubs(:retrievecache).returns("whatever") + @client.stubs(:add_default_resources) Puppet::Network::Client::Master.publicize_methods :use_cached_config do @client.use_cached_config() end @@ -339,6 +370,7 @@ describe Puppet::Network::Client::Master, " when using the cached configuration" config.expects(:to_configuration).returns(ral_config) @client.stubs(:retrievecache).returns("whatever") + @client.stubs(:add_default_resources) Puppet::Network::Client::Master.publicize_methods :use_cached_config do @client.use_cached_config() end @@ -356,10 +388,55 @@ describe Puppet::Network::Client::Master, " when using the cached configuration" config.expects(:to_configuration).returns(ral_config) @client.stubs(:retrievecache).returns("whatever") + @client.stubs(:add_default_resources) Puppet::Network::Client::Master.publicize_methods :use_cached_config do @client.use_cached_config() end @client.configuration.should equal(ral_config) end + + it "should add the default resources to the configuration" do + config = mock 'config' + YAML.stubs(:load).returns(config) + + ral_config = mock 'ral config' + ral_config.expects(:from_cache=).with(true) + ral_config.stubs(:host_config=) + config.stubs(:to_configuration).returns(ral_config) + + @client.stubs(:retrievecache).returns("whatever") + @client.expects(:add_default_resources).with(ral_config) + Puppet::Network::Client::Master.publicize_methods :use_cached_config do + @client.use_cached_config() + end + end +end + +describe Puppet::Network::Client::Master, " when adding default resources" do + before do + @master = mock 'master' + @client = Puppet::Network::Client.master.new( + :Master => @master + ) + @facts = {"one" => "two", "three" => "four"} + end + + it "should add the default schedules" do + config = mock 'config' + Puppet::Type.type(:schedule).expects(:add_default_schedules).with(config) + Puppet::Type.type(:filebucket).stubs(:add_default_filebucket) + Puppet::Network::Client::Master.publicize_methods :add_default_resources do + @client.add_default_resources(config) + end + end + + it "should add the default filebucket" do + config = mock 'config' + Puppet::Type.type(:schedule).stubs(:add_default_schedules) + Puppet::Type.type(:filebucket).expects(:add_default_filebucket).with(config) + Puppet::Network::Client::Master.publicize_methods :add_default_resources do + @client.add_default_resources(config) + end + end end diff --git a/spec/unit/node/configuration.rb b/spec/unit/node/configuration.rb index 5780d4fbb..7fda4e9a8 100755 --- a/spec/unit/node/configuration.rb +++ b/spec/unit/node/configuration.rb @@ -540,6 +540,7 @@ describe Puppet::Node::Configuration, " when creating a relationship graph" do @config = Puppet::Node::Configuration.new("host") @compone = Puppet::Type::Component.create :name => "one" @comptwo = Puppet::Type::Component.create :name => "two", :require => ["class", "one"] + @file = Puppet::Type.type(:file) @one = @file.create :path => "/one" @two = @file.create :path => "/two" |