#!/usr/local/bin/ruby -w # $Id$ # the class responsible for actually doing any work # enables no-op and logging/rollback module Puppet class StateChange attr_accessor :is, :should, :type, :path, :state, :transaction, :run #--------------------------------------------------------------- def initialize(state) @state = state @path = [state.path,"change"].flatten @is = state.is if state.is == state.should raise Puppet::Error.new( "Tried to create a change for in-sync state %s" % state.name ) end @should = state.should @run = false end #--------------------------------------------------------------- #--------------------------------------------------------------- def go if @state.noop #Puppet.debug "%s is noop" % @state return nil end if @state.is == @state.should raise Puppet::Error.new( "Tried to change in-sync state %s" % state.name ) end begin events = @state.sync if events.nil? return nil end unless events.is_a?(Array) events = [events] end @run = true return events.collect { |event| # default to a simple event type if ! event.is_a?(Symbol) Puppet.warning("State '%s' returned invalid event '%s'; resetting to default" % [@state.class,event]) event = @state.parent.class.name.id2name + "_changed" end # i should maybe include object type, but the event type # should basically point to that, right? #:state => @state, #:object => @state.parent, # FIXME this is where loglevel stuff should go Puppet.notice self.to_s Puppet::Event.new( :event => event, :change => self, :transaction => @transaction, :source => @state.parent, :message => self.to_s ) } rescue => detail #Puppet.err "%s failed: %s" % [self.to_s,detail] raise # there should be a way to ask the state what type of event # it would have generated, but... pname = @state.parent.class.name.id2name #if pname.is_a?(Symbol) # pname = pname.id2name #end #:state => @state, Puppet.notice "Failed: " + self.to_s return Puppet::Event.new( :event => pname + "_failed", :change => self, :source => @state.parent, :transaction => @transaction, :message => "Failed: " + self.to_s ) end end #--------------------------------------------------------------- #--------------------------------------------------------------- def forward #Puppet.debug "moving change forward" unless defined? @transaction raise "StateChange '%s' tried to be executed outside of transaction" % self end return self.go end #--------------------------------------------------------------- #--------------------------------------------------------------- def backward @state.should = @is @state.retrieve unless @state.insync? Puppet.notice "Rolling %s backward" % self return self.go else Puppet.debug "rollback is already in sync: %s vs. %s" % [@state.is.inspect, @state.should.inspect] return nil end #raise "Moving statechanges backward is currently unsupported" #@type.change(@path,@should,@is) end #--------------------------------------------------------------- #--------------------------------------------------------------- def noop return @state.noop end #--------------------------------------------------------------- #--------------------------------------------------------------- def to_s if @state.is == :notfound return "%s: defined '%s' as '%s'" % [@state.parent, @state.name, @state.should_to_s] elsif @state.should == :notfound return "%s: undefined %s from '%s'" % [@state.parent, @state.name, @state.is_to_s] else return "%s: %s changed '%s' to '%s'" % [@state.parent, @state.name, @state.is_to_s, @state.should_to_s] end end #--------------------------------------------------------------- end end