summaryrefslogtreecommitdiffstats
path: root/lib/puppet/transaction/resource_harness.rb
blob: 669b0ae98689dd43fc11f4b4e73812569014da1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
require 'puppet/resource/status'

class Puppet::Transaction::ResourceHarness
    extend Forwardable
    def_delegators :@transaction, :relationship_graph

    attr_reader :transaction

    def allow_changes?(resource)
        return true unless resource.purging? and resource.deleting?
        return true unless deps = relationship_graph.dependents(resource) and ! deps.empty? and deps.detect { |d| ! d.deleting? }

        deplabel = deps.collect { |r| r.ref }.join(",")
        plurality = deps.length > 1 ? "":"s"
        resource.warning "#{deplabel} still depend#{plurality} on me -- not purging"
        return false
    end

    def apply_changes(status, changes)
        changes.each do |change|
            status << change.apply
        end
        status.changed = true
    end

    def changes_to_perform(status, resource)
        current = resource.retrieve

        resource.cache :checked, Time.now

        if param = resource.parameter(:ensure)
            return [] if absent_and_not_being_created?(current, param)
            return [Puppet::Transaction::Change.new(param, current[:ensure])] unless ensure_is_insync?(current, param)
            return [] if ensure_should_be_absent?(current, param)
        end

        resource.properties.reject { |p| p.name == :ensure }.find_all do |param|
            param_is_not_insync?(current, param)
        end.collect do |param|
            Puppet::Transaction::Change.new(param, current[param.name])
        end
    end

    def evaluate(resource)
        status = Puppet::Resource::Status.new(resource)

        if changes = changes_to_perform(status, resource) and ! changes.empty?
            status.out_of_sync = true
            apply_changes(status, changes)
            resource.cache(:synced, Time.now)
            resource.flush if resource.respond_to?(:flush)
        end
        return status
    rescue => detail
        resource.fail "Could not create resource status: #{detail}" unless status
        puts detail.backtrace if Puppet[:trace]
        resource.err "Could not evaluate: #{detail}"
        status.failed = true
        return status
    end

    def initialize(transaction)
        @transaction = transaction
    end

    private

    def absent_and_not_being_created?(current, param)
        current[:ensure] == :absent and param.should.nil?
    end

    def ensure_is_insync?(current, param)
        param.insync?(current[:ensure])
    end

    def ensure_should_be_absent?(current, param)
        param.should == :absent
    end

    def param_is_not_insync?(current, param)
        ! param.insync?(current[param.name] || :absent)
    end
end