From 6124c693c98217bdb747f455eefd09d303b0b2a5 Mon Sep 17 00:00:00 2001 From: Luke Kanies Date: Thu, 3 Jul 2008 17:53:07 -0500 Subject: Renaming the Puppet::PropertyChange class to Puppet::Transaction::Change. Signed-off-by: Luke Kanies --- lib/puppet/metatype/evaluation.rb | 4 +- lib/puppet/property.rb | 1 - lib/puppet/propertychange.rb | 141 -------------------------------------- lib/puppet/transaction.rb | 2 +- lib/puppet/transaction/change.rb | 134 ++++++++++++++++++++++++++++++++++++ lib/puppet/type.rb | 1 - lib/puppet/type/yumrepo.rb | 1 - 7 files changed, 137 insertions(+), 147 deletions(-) delete mode 100644 lib/puppet/propertychange.rb create mode 100644 lib/puppet/transaction/change.rb (limited to 'lib') diff --git a/lib/puppet/metatype/evaluation.rb b/lib/puppet/metatype/evaluation.rb index ff1eddb55..18bbb812f 100644 --- a/lib/puppet/metatype/evaluation.rb +++ b/lib/puppet/metatype/evaluation.rb @@ -139,7 +139,7 @@ class Puppet::Type end if ensureparam and ! ensureparam.insync?(currentvalues[ensureparam]) - changes << Puppet::PropertyChange.new(ensureparam, currentvalues[ensureparam]) + changes << Puppet::Transaction::Change.new(ensureparam, currentvalues[ensureparam]) # Else, if the 'ensure' property is correctly absent, then do # nothing elsif ensureparam and currentvalues[ensureparam] == :absent @@ -149,7 +149,7 @@ class Puppet::Type currentvalues[property] ||= :absent ! property.insync?(currentvalues[property]) }.collect { |property| - Puppet::PropertyChange.new(property, currentvalues[property]) + Puppet::Transaction::Change.new(property, currentvalues[property]) } end diff --git a/lib/puppet/property.rb b/lib/puppet/property.rb index fcaa19d48..9e8bae7a4 100644 --- a/lib/puppet/property.rb +++ b/lib/puppet/property.rb @@ -2,7 +2,6 @@ # blocks for actually doing work on the system. require 'puppet' -require 'puppet/propertychange' require 'puppet/parameter' module Puppet diff --git a/lib/puppet/propertychange.rb b/lib/puppet/propertychange.rb deleted file mode 100644 index 35bbede1a..000000000 --- a/lib/puppet/propertychange.rb +++ /dev/null @@ -1,141 +0,0 @@ -# the class responsible for actually doing any work - -# enables no-op and logging/rollback - -module Puppet - # Handle all of the work around performing an actual change, - # including calling 'sync' on the properties and producing events. - class PropertyChange - attr_accessor :is, :should, :type, :path, :property, :transaction, :changed, :proxy - - # The log file generated when this object was changed. - attr_reader :report - - # Switch the goals of the property, thus running the change in reverse. - def backward - @property.should = @is - @is = @property.retrieve - - unless defined? @transaction - raise Puppet::Error, - "PropertyChange '%s' tried to be executed outside of transaction" % - self - end - unless @property.insync?(@is) - @property.info "Backing %s" % self - return self.go - else - @property.debug "rollback is already in sync: %s vs. %s" % - [@is, @property.should.inspect] - return nil - end - end - - def changed? - self.changed - end - - # Create our event object. - def event(name) - # default to a simple event type - unless name.is_a?(Symbol) - @property.warning("Property '%s' returned invalid event '%s'; resetting to default" % - [@property.class, name]) - - event = @property.resource.class.name.id2name + "_changed" - end - - Puppet::Event.new( - :event => name, - :transaction => @transaction, - :source => self.source - ) - end - - def initialize(property, currentvalue) - unless property.is_a?(Puppet::Property) - raise Puppet::DevError, "Got a %s instead of a property" % - property.class - end - @property = property - @path = [property.path,"change"].flatten - @is = currentvalue - - @should = property.should - - @changed = false - end - - # Perform the actual change. This method can go either forward or - # backward, and produces an event. - def go - if skip? - if self.noop - return [event(:noop)] - else - return nil - end - end - - # The transaction catches any exceptions here. - events = @property.sync - if events.nil? - return nil - end - - if events.is_a?(Array) - if events.empty? - return nil - end - else - events = [events] - end - - return events.collect { |name| - @report = @property.log(@property.change_to_s(@is, @should)) - event(name) - } - end - - def forward - #@property.debug "moving change forward" - - unless defined? @transaction - raise Puppet::Error, - "PropertyChange '%s' tried to be executed outside of transaction" % - self - end - - return self.go - end - - def noop - return @property.noop - end - - def skip? - if @property.insync?(@is) - @property.info "Already in sync" - return true - end - - if @property.noop - @property.log "is %s, should be %s (noop)" % - [property.is_to_s(@is), property.should_to_s(@should)] - #@property.debug "%s is noop" % @property - return true - end - return false - end - - def source - self.proxy || @property.resource - end - - def to_s - return "change %s.%s(%s)" % - [@transaction.object_id, self.object_id, @property.change_to_s(@is, @should)] - #return "change %s.%s" % [@transaction.object_id, self.object_id] - end - end -end diff --git a/lib/puppet/transaction.rb b/lib/puppet/transaction.rb index b191f8219..84a41d5b8 100644 --- a/lib/puppet/transaction.rb +++ b/lib/puppet/transaction.rb @@ -2,10 +2,10 @@ # and performs them require 'puppet' -require 'puppet/propertychange' module Puppet class Transaction + require 'puppet/transaction/change' attr_accessor :component, :catalog, :ignoreschedules attr_accessor :sorted_resources, :configurator diff --git a/lib/puppet/transaction/change.rb b/lib/puppet/transaction/change.rb new file mode 100644 index 000000000..ee92f2249 --- /dev/null +++ b/lib/puppet/transaction/change.rb @@ -0,0 +1,134 @@ +require 'puppet/transaction' + +# Handle all of the work around performing an actual change, +# including calling 'sync' on the properties and producing events. +class Puppet::Transaction::Change + attr_accessor :is, :should, :type, :path, :property, :transaction, :changed, :proxy + + # The log file generated when this object was changed. + attr_reader :report + + # Switch the goals of the property, thus running the change in reverse. + def backward + @property.should = @is + @is = @property.retrieve + + unless defined? @transaction + raise Puppet::Error, + "PropertyChange '%s' tried to be executed outside of transaction" % + self + end + unless @property.insync?(@is) + @property.info "Backing %s" % self + return self.go + else + @property.debug "rollback is already in sync: %s vs. %s" % + [@is, @property.should.inspect] + return nil + end + end + + def changed? + self.changed + end + + # Create our event object. + def event(name) + # default to a simple event type + unless name.is_a?(Symbol) + @property.warning("Property '%s' returned invalid event '%s'; resetting to default" % + [@property.class, name]) + + event = @property.resource.class.name.id2name + "_changed" + end + + Puppet::Event.new( + :event => name, + :transaction => @transaction, + :source => self.source + ) + end + + def initialize(property, currentvalue) + unless property.is_a?(Puppet::Property) + raise Puppet::DevError, "Got a %s instead of a property" % + property.class + end + @property = property + @path = [property.path,"change"].flatten + @is = currentvalue + + @should = property.should + + @changed = false + end + + # Perform the actual change. This method can go either forward or + # backward, and produces an event. + def go + if skip? + if self.noop + return [event(:noop)] + else + return nil + end + end + + # The transaction catches any exceptions here. + events = @property.sync + if events.nil? + return nil + end + + if events.is_a?(Array) + if events.empty? + return nil + end + else + events = [events] + end + + return events.collect { |name| + @report = @property.log(@property.change_to_s(@is, @should)) + event(name) + } + end + + def forward + unless defined? @transaction + raise Puppet::Error, + "PropertyChange '%s' tried to be executed outside of transaction" % + self + end + + return self.go + end + + def noop + return @property.noop + end + + def skip? + if @property.insync?(@is) + @property.info "Already in sync" + return true + end + + if @property.noop + @property.log "is %s, should be %s (noop)" % + [property.is_to_s(@is), property.should_to_s(@should)] + #@property.debug "%s is noop" % @property + return true + end + return false + end + + def source + self.proxy || @property.resource + end + + def to_s + return "change %s.%s(%s)" % + [@transaction.object_id, self.object_id, @property.change_to_s(@is, @should)] + end +end diff --git a/lib/puppet/type.rb b/lib/puppet/type.rb index f8949ec90..b7de11de5 100644 --- a/lib/puppet/type.rb +++ b/lib/puppet/type.rb @@ -415,7 +415,6 @@ class Type end # Puppet::Type end -require 'puppet/propertychange' require 'puppet/provider' # Always load these types. diff --git a/lib/puppet/type/yumrepo.rb b/lib/puppet/type/yumrepo.rb index acb3b9b83..d19b5a470 100644 --- a/lib/puppet/type/yumrepo.rb +++ b/lib/puppet/type/yumrepo.rb @@ -1,6 +1,5 @@ # Description of yum repositories -require 'puppet/propertychange' require 'puppet/util/inifile' module Puppet -- cgit From 73c06c05aec8834b6fdebac107fb289575779020 Mon Sep 17 00:00:00 2001 From: Luke Kanies Date: Thu, 3 Jul 2008 18:50:56 -0500 Subject: Renaming Puppet::Event to Puppet::Transaction::Event Signed-off-by: Luke Kanies --- lib/puppet/event.rb | 28 ---------------------------- lib/puppet/transaction.rb | 6 ++++-- lib/puppet/transaction/event.rb | 22 ++++++++++++++++++++++ lib/puppet/type.rb | 1 - 4 files changed, 26 insertions(+), 31 deletions(-) delete mode 100644 lib/puppet/event.rb create mode 100644 lib/puppet/transaction/event.rb (limited to 'lib') diff --git a/lib/puppet/event.rb b/lib/puppet/event.rb deleted file mode 100644 index c1928a354..000000000 --- a/lib/puppet/event.rb +++ /dev/null @@ -1,28 +0,0 @@ -require 'puppet' -require 'puppet/util/methodhelper' -require 'puppet/util/errors' - -module Puppet - # events are transient packets of information; they result in one or more (or none) - # subscriptions getting triggered, and then they get cleared - # eventually, these will be passed on to some central event system - class Event - include Puppet - include Puppet::Util::MethodHelper - include Puppet::Util::Errors - - attr_accessor :event, :source, :transaction - - @@events = [] - - def initialize(args) - set_options symbolize_options(args) - requiredopts(:event, :source) - end - - def to_s - @source.to_s + " -> " + self.event.to_s - end - end -end - diff --git a/lib/puppet/transaction.rb b/lib/puppet/transaction.rb index 84a41d5b8..78704bb13 100644 --- a/lib/puppet/transaction.rb +++ b/lib/puppet/transaction.rb @@ -6,6 +6,8 @@ require 'puppet' module Puppet class Transaction require 'puppet/transaction/change' + require 'puppet/transaction/event' + attr_accessor :component, :catalog, :ignoreschedules attr_accessor :sorted_resources, :configurator @@ -680,7 +682,7 @@ class Transaction [callback, subs.length] # And then add an event for it. - return [Puppet::Event.new( + return [Puppet::Transaction::Event.new( :event => :noop, :transaction => self, :source => resource @@ -712,7 +714,7 @@ class Transaction end # And then add an event for it. - trigged << Puppet::Event.new( + trigged << Puppet::Transaction::Event.new( :event => :triggered, :transaction => self, :source => resource diff --git a/lib/puppet/transaction/event.rb b/lib/puppet/transaction/event.rb new file mode 100644 index 000000000..418e70516 --- /dev/null +++ b/lib/puppet/transaction/event.rb @@ -0,0 +1,22 @@ +require 'puppet' +require 'puppet/util/methodhelper' +require 'puppet/util/errors' + +# events are transient packets of information; they result in one or more (or none) +# subscriptions getting triggered, and then they get cleared +# eventually, these will be passed on to some central event system +class Puppet::Transaction::Event + include Puppet::Util::MethodHelper + include Puppet::Util::Errors + + attr_accessor :event, :source, :transaction + + def initialize(args) + set_options symbolize_options(args) + requiredopts(:event, :source) + end + + def to_s + @source.to_s + " -> " + self.event.to_s + end +end diff --git a/lib/puppet/type.rb b/lib/puppet/type.rb index b7de11de5..45dd7f5b5 100644 --- a/lib/puppet/type.rb +++ b/lib/puppet/type.rb @@ -1,6 +1,5 @@ require 'puppet' require 'puppet/util/log' -require 'puppet/event' require 'puppet/util/metric' require 'puppet/property' require 'puppet/parameter' -- cgit From 31ffeabad92338d317c3193d50e8dd9a1a78977c Mon Sep 17 00:00:00 2001 From: Luke Kanies Date: Fri, 4 Jul 2008 15:05:29 -0500 Subject: Adding tests to the Transaction::Change class. There's a small amount of refactoring here, mostly removing code that appears to not be used at all. Signed-off-by: Luke Kanies --- lib/puppet/transaction/change.rb | 54 ++++++++++++++-------------------------- 1 file changed, 19 insertions(+), 35 deletions(-) (limited to 'lib') diff --git a/lib/puppet/transaction/change.rb b/lib/puppet/transaction/change.rb index ee92f2249..e1863ee13 100644 --- a/lib/puppet/transaction/change.rb +++ b/lib/puppet/transaction/change.rb @@ -1,4 +1,5 @@ require 'puppet/transaction' +require 'puppet/transaction/event' # Handle all of the work around performing an actual change, # including calling 'sync' on the properties and producing events. @@ -13,7 +14,7 @@ class Puppet::Transaction::Change @property.should = @is @is = @property.retrieve - unless defined? @transaction + unless transaction raise Puppet::Error, "PropertyChange '%s' tried to be executed outside of transaction" % self @@ -39,21 +40,17 @@ class Puppet::Transaction::Change @property.warning("Property '%s' returned invalid event '%s'; resetting to default" % [@property.class, name]) - event = @property.resource.class.name.id2name + "_changed" + name = @property.event(should) end - Puppet::Event.new( + Puppet::Transaction::Event.new( :event => name, - :transaction => @transaction, - :source => self.source + :transaction => transaction, + :source => self.resource ) end def initialize(property, currentvalue) - unless property.is_a?(Puppet::Property) - raise Puppet::DevError, "Got a %s instead of a property" % - property.class - end @property = property @path = [property.path,"change"].flatten @is = currentvalue @@ -66,12 +63,9 @@ class Puppet::Transaction::Change # Perform the actual change. This method can go either forward or # backward, and produces an event. def go - if skip? - if self.noop - return [event(:noop)] - else - return nil - end + if self.noop? + @property.log "is %s, should be %s (noop)" % [property.is_to_s(@is), property.should_to_s(@should)] + return [event(:noop)] end # The transaction catches any exceptions here. @@ -95,7 +89,7 @@ class Puppet::Transaction::Change end def forward - unless defined? @transaction + unless transaction raise Puppet::Error, "PropertyChange '%s' tried to be executed outside of transaction" % self @@ -104,31 +98,21 @@ class Puppet::Transaction::Change return self.go end - def noop + # Is our property noop? This is used for generating special events. + def noop? return @property.noop end - def skip? - if @property.insync?(@is) - @property.info "Already in sync" - return true - end - - if @property.noop - @property.log "is %s, should be %s (noop)" % - [property.is_to_s(@is), property.should_to_s(@should)] - #@property.debug "%s is noop" % @property - return true - end - return false - end - - def source + # The resource that generated this change. This is used for handling events, + # and the proxy resource is used for generated resources, since we can't + # send an event to a resource we don't have a direct relationship. If we + # have a proxy resource, then the events will be considered to be from + # that resource, rather than us, so the graph resolution will still work. + def resource self.proxy || @property.resource end def to_s - return "change %s.%s(%s)" % - [@transaction.object_id, self.object_id, @property.change_to_s(@is, @should)] + return "change %s.%s(%s)" % [transaction.object_id, self.object_id, @property.change_to_s(@is, @should)] end end -- cgit From 2863df288150da87a58ce4d938bbcf9a5d841f43 Mon Sep 17 00:00:00 2001 From: Luke Kanies Date: Fri, 4 Jul 2008 15:35:18 -0500 Subject: Refactoring the Transaction::Event class. The class had a 'transaction' accessor that was assigned but never used, and it is simple enough that it needed direct arguments rather than named arguments. The rest of the code is changing the other classes that use Events. Signed-off-by: Luke Kanies --- lib/puppet/pgraph.rb | 2 +- lib/puppet/transaction.rb | 16 ++++------------ lib/puppet/transaction/change.rb | 6 +----- lib/puppet/transaction/event.rb | 9 ++++----- 4 files changed, 10 insertions(+), 23 deletions(-) (limited to 'lib') diff --git a/lib/puppet/pgraph.rb b/lib/puppet/pgraph.rb index 3bcc2ced0..55ad7d2c1 100644 --- a/lib/puppet/pgraph.rb +++ b/lib/puppet/pgraph.rb @@ -58,7 +58,7 @@ class Puppet::PGraph < Puppet::SimpleGraph # to, which is the same thing as saying all edges directly below # This vertex in the graph. adjacent(source, :direction => :out, :type => :edges).find_all do |edge| - edge.match?(event.event) + edge.match?(event.name) end end.compact.flatten end diff --git a/lib/puppet/transaction.rb b/lib/puppet/transaction.rb index 78704bb13..695d0434c 100644 --- a/lib/puppet/transaction.rb +++ b/lib/puppet/transaction.rb @@ -98,7 +98,7 @@ class Transaction # Create an edge with this resource as both the source and # target. The triggering method treats these specially for # logging. - events = resourceevents.collect { |e| e.event } + events = resourceevents.collect { |e| e.name } set_trigger(Puppet::Relationship.new(resource, resource, :callback => :refresh, :event => events)) end end @@ -280,7 +280,7 @@ class Transaction # of course, bad. edge = orig_edge.class.new(orig_edge.source, orig_edge.target) label = orig_edge.label.dup - label[:event] = events.collect { |e| e.event } + label[:event] = events.collect { |e| e.name } edge.label = label set_trigger(edge) end @@ -682,11 +682,7 @@ class Transaction [callback, subs.length] # And then add an event for it. - return [Puppet::Transaction::Event.new( - :event => :noop, - :transaction => self, - :source => resource - )] + return [Puppet::Transaction::Event.new(:noop, resource)] end if subs.length == 1 and subs[0].source == resource @@ -714,11 +710,7 @@ class Transaction end # And then add an event for it. - trigged << Puppet::Transaction::Event.new( - :event => :triggered, - :transaction => self, - :source => resource - ) + trigged << Puppet::Transaction::Event.new(:triggered, resource) triggered(resource, callback) end diff --git a/lib/puppet/transaction/change.rb b/lib/puppet/transaction/change.rb index e1863ee13..cc7629376 100644 --- a/lib/puppet/transaction/change.rb +++ b/lib/puppet/transaction/change.rb @@ -43,11 +43,7 @@ class Puppet::Transaction::Change name = @property.event(should) end - Puppet::Transaction::Event.new( - :event => name, - :transaction => transaction, - :source => self.resource - ) + Puppet::Transaction::Event.new(name, self.resource) end def initialize(property, currentvalue) diff --git a/lib/puppet/transaction/event.rb b/lib/puppet/transaction/event.rb index 418e70516..f1a48b382 100644 --- a/lib/puppet/transaction/event.rb +++ b/lib/puppet/transaction/event.rb @@ -9,14 +9,13 @@ class Puppet::Transaction::Event include Puppet::Util::MethodHelper include Puppet::Util::Errors - attr_accessor :event, :source, :transaction + attr_reader :name, :source - def initialize(args) - set_options symbolize_options(args) - requiredopts(:event, :source) + def initialize(name, source) + @name, @source = name, source end def to_s - @source.to_s + " -> " + self.event.to_s + source.to_s + " -> " + name.to_s end end -- cgit From 61ec332144ad794fae80a16feac543afc014a5f9 Mon Sep 17 00:00:00 2001 From: Luke Kanies Date: Fri, 4 Jul 2008 16:06:02 -0500 Subject: Removing the Transaction::Change#transaction accessor. As with Events, this was never used (beyond being assigned), so I've gotten rid of it. Signed-off-by: Luke Kanies --- lib/puppet/transaction.rb | 1 - lib/puppet/transaction/change.rb | 15 ++------------- 2 files changed, 2 insertions(+), 14 deletions(-) (limited to 'lib') diff --git a/lib/puppet/transaction.rb b/lib/puppet/transaction.rb index 695d0434c..f3defb7a2 100644 --- a/lib/puppet/transaction.rb +++ b/lib/puppet/transaction.rb @@ -111,7 +111,6 @@ class Transaction changes.collect { |change| @changes << change @count += 1 - change.transaction = self events = nil begin # use an array, so that changes can return more than one diff --git a/lib/puppet/transaction/change.rb b/lib/puppet/transaction/change.rb index cc7629376..396b9233e 100644 --- a/lib/puppet/transaction/change.rb +++ b/lib/puppet/transaction/change.rb @@ -4,7 +4,7 @@ require 'puppet/transaction/event' # Handle all of the work around performing an actual change, # including calling 'sync' on the properties and producing events. class Puppet::Transaction::Change - attr_accessor :is, :should, :type, :path, :property, :transaction, :changed, :proxy + attr_accessor :is, :should, :type, :path, :property, :changed, :proxy # The log file generated when this object was changed. attr_reader :report @@ -14,11 +14,6 @@ class Puppet::Transaction::Change @property.should = @is @is = @property.retrieve - unless transaction - raise Puppet::Error, - "PropertyChange '%s' tried to be executed outside of transaction" % - self - end unless @property.insync?(@is) @property.info "Backing %s" % self return self.go @@ -85,12 +80,6 @@ class Puppet::Transaction::Change end def forward - unless transaction - raise Puppet::Error, - "PropertyChange '%s' tried to be executed outside of transaction" % - self - end - return self.go end @@ -109,6 +98,6 @@ class Puppet::Transaction::Change end def to_s - return "change %s.%s(%s)" % [transaction.object_id, self.object_id, @property.change_to_s(@is, @should)] + return "change %s" % @property.change_to_s(@is, @should) end end -- cgit From 9d69b3fd12f90ddead7b6a3392395fbe4e901d9b Mon Sep 17 00:00:00 2001 From: Luke Kanies Date: Fri, 4 Jul 2008 16:32:04 -0500 Subject: Testing and simplifying the Transaction::Change#backward method. Signed-off-by: Luke Kanies --- lib/puppet/transaction/change.rb | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) (limited to 'lib') diff --git a/lib/puppet/transaction/change.rb b/lib/puppet/transaction/change.rb index 396b9233e..e05c2592c 100644 --- a/lib/puppet/transaction/change.rb +++ b/lib/puppet/transaction/change.rb @@ -4,24 +4,15 @@ require 'puppet/transaction/event' # Handle all of the work around performing an actual change, # including calling 'sync' on the properties and producing events. class Puppet::Transaction::Change - attr_accessor :is, :should, :type, :path, :property, :changed, :proxy - - # The log file generated when this object was changed. - attr_reader :report + attr_accessor :is, :should, :path, :property, :changed, :proxy # Switch the goals of the property, thus running the change in reverse. def backward - @property.should = @is - @is = @property.retrieve + @is, @should = @should, @is + @property.should = @should - unless @property.insync?(@is) - @property.info "Backing %s" % self - return self.go - else - @property.debug "rollback is already in sync: %s vs. %s" % - [@is, @property.should.inspect] - return nil - end + @property.info "Reversing %s" % self + return self.go end def changed? -- cgit