diff options
| author | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2006-03-11 22:18:59 +0000 |
|---|---|---|
| committer | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2006-03-11 22:18:59 +0000 |
| commit | fa9aab6749bcb76004b567e228c5af8f7a4bfee8 (patch) | |
| tree | 15aa928fd00bcdec80ccb88642f93139e794d9d5 | |
| parent | 414d364a6f337cc8e542dc107d8e51f625375db4 (diff) | |
| download | puppet-fa9aab6749bcb76004b567e228c5af8f7a4bfee8.tar.gz puppet-fa9aab6749bcb76004b567e228c5af8f7a4bfee8.tar.xz puppet-fa9aab6749bcb76004b567e228c5af8f7a4bfee8.zip | |
Fixing #82. You can now specify comma-separated tags to get run in puppet or puppetd: puppetd --onetime --tags "enhost, facter" -v. You cannot specify classes explicitly, but tags map well to classes and have the benefit of being more generic.
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@1005 980ebf18-57e1-0310-9a29-db15c13687c0
| -rw-r--r-- | lib/puppet/config.rb | 2 | ||||
| -rw-r--r-- | lib/puppet/transaction.rb | 19 | ||||
| -rw-r--r-- | lib/puppet/type.rb | 33 | ||||
| -rw-r--r-- | test/tagging/tagging.rb | 68 |
4 files changed, 118 insertions, 4 deletions
diff --git a/lib/puppet/config.rb b/lib/puppet/config.rb index b0aa93839..ff1494ea8 100644 --- a/lib/puppet/config.rb +++ b/lib/puppet/config.rb @@ -263,7 +263,7 @@ class Config when String, Integer, Float: # nothing klass = CElement else - raise Puppet::Error, "Invalid value '%s' for %s" % [value, hash[:name]] + raise Puppet::Error, "Invalid value '%s' for %s" % [value.inspect, hash[:name]] end element = klass.new(hash) element.parent = self diff --git a/lib/puppet/transaction.rb b/lib/puppet/transaction.rb index 182401cb5..d9bea92ba 100644 --- a/lib/puppet/transaction.rb +++ b/lib/puppet/transaction.rb @@ -8,6 +8,13 @@ module Puppet class Transaction attr_accessor :toplevel, :component, :objects + + Puppet.config.setdefaults(:transaction, + :tags => ["", "Tags to use to find objects. If this is set, then + only objects tagged with the specified tags will be applied. Values must + be comma-separated."] + ) + # a bit of a gross hack; a global list of objects that have failed to sync, # so that we can verify during later syncs that our dependencies haven't # failed @@ -35,7 +42,19 @@ class Transaction count = 0 now = Time.now + tags = Puppet[:tags] + if tags == "" + tags = nil + else + tags = tags.split(/\s*,\s*/) + end events = @objects.find_all { |child| + if tags + child.tagged?(tags) + else + true # match everything when there are no tags + end + }.find_all { |child| child.scheduled? }.collect { |child| # these children are all Puppet::Type instances diff --git a/lib/puppet/type.rb b/lib/puppet/type.rb index 1d2eea54c..eec397670 100644 --- a/lib/puppet/type.rb +++ b/lib/puppet/type.rb @@ -19,7 +19,8 @@ class Type < Puppet::Element # that it is clear whether it operates on all attributes (thus has 'attr' in # the method name, or whether it operates on a specific type of attributes. attr_accessor :children, :parent - attr_accessor :file, :line, :tags + attr_accessor :file, :line + attr_reader :tags attr_writer :implicit def implicit? @@ -1466,8 +1467,36 @@ class Type < Puppet::Element return schedule.match?(self.cached(:checked).to_i) end + # Add a new tag. def tag(tag) - @tags << tag + tag = tag.intern if tag.is_a? String + unless @tags.include? tag + @tags << tag + end + end + + # Define the initial list of tags. + def tags=(list) + list = [list] unless list.is_a? Array + + @tags = list.collect do |t| + case t + when String: t.intern + when Symbol: t + else + self.warning "Ignoring tag %s of type %s" % [tag.inspect, tag.class] + end + end + end + + # Figure out of any of the specified tags apply to this object. This is an + # OR operation. + def tagged?(tags) + tags = [tags] unless tags.is_a? Array + + tags = tags.collect { |t| t.intern } + + return tags.find { |tag| @tags.include? tag } end # Is the specified parameter set? diff --git a/test/tagging/tagging.rb b/test/tagging/tagging.rb index ed3bd9fbb..3f1f15add 100644 --- a/test/tagging/tagging.rb +++ b/test/tagging/tagging.rb @@ -71,10 +71,76 @@ class TestTagging < Test::Unit::TestCase object = objects.shift assert_nothing_raised { - assert_equal(%w{solaris}, object.tags, + assert_equal([:solaris], object.tags, "Incorrect tags") } end + + # Make sure that specifying tags results in only those objects getting + # run. + def test_tagspecs + a = tempfile() + b = tempfile() + + afile = Puppet.type(:file).create( + :path => a, + :ensure => :file + ) + afile.tag("a") + + bfile = Puppet.type(:file).create( + :path => b, + :ensure => :file + ) + bfile.tag(:b) + + # First, make sure they get created when no spec'ed tags + assert_events([:file_created,:file_created], afile, bfile) + assert(FileTest.exists?(a), "A did not get created") + assert(FileTest.exists?(b), "B did not get created") + File.unlink(a) + File.unlink(b) + + # Set the tags to a + assert_nothing_raised { + Puppet[:tags] = "a" + } + + assert_events([:file_created], afile, bfile) + assert(FileTest.exists?(a), "A did not get created") + assert(!FileTest.exists?(b), "B got created") + File.unlink(a) + + # Set the tags to b + assert_nothing_raised { + Puppet[:tags] = "b" + } + + assert_events([:file_created], afile, bfile) + assert(!FileTest.exists?(a), "A got created") + assert(FileTest.exists?(b), "B did not get created") + File.unlink(b) + + # Set the tags to something else + assert_nothing_raised { + Puppet[:tags] = "c" + } + + assert_events([], afile, bfile) + assert(!FileTest.exists?(a), "A got created") + assert(!FileTest.exists?(b), "B got created") + + # Now set both tags + assert_nothing_raised { + Puppet[:tags] = "b, a" + } + + assert_events([:file_created, :file_created], afile, bfile) + assert(FileTest.exists?(a), "A did not get created") + assert(FileTest.exists?(b), "B did not get created") + File.unlink(a) + + end end # $Id$ |
