summaryrefslogtreecommitdiff
path: root/lib/chef/runner.rb
blob: a0ae61fe4c0be9614368ff38c50d7e937aedc3fc (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#--
# Author:: Adam Jacob (<adam@chef.io>)
# Author:: Christopher Walters (<cw@chef.io>)
# Author:: Tim Hinderliter (<tim@chef.io>)
# Copyright:: Copyright 2008-2019, Chef Software Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require_relative "exceptions"
require_relative "mixin/params_validate"
require_relative "node"
require_relative "resource_collection"

class Chef
  # == Chef::Runner
  # This class is responsible for executing the steps in a Chef run.
  class Runner

    attr_reader :run_context

    include Chef::Mixin::ParamsValidate

    def initialize(run_context)
      @run_context = run_context
      run_context.runner = self
    end

    def delayed_actions
      @run_context.delayed_actions
    end

    def events
      @run_context.events
    end

    # Determine the appropriate provider for the given resource, then
    # execute it.
    def run_action(resource, action, notification_type = nil, notifying_resource = nil)
      # If there are any before notifications, why-run the resource
      # and notify anyone who needs notifying
      before_notifications = run_context.before_notifications(resource) || []
      unless before_notifications.empty?
        forced_why_run do
          Chef::Log.info("#{resource} running why-run #{action} action to support before action")
          resource.run_action(action, notification_type, notifying_resource)
        end

        if resource.updated_by_last_action?
          before_notifications.each do |notification|
            Chef::Log.info("#{resource} sending #{notification.action} action to #{notification.resource} (before)")
            run_action(notification.resource, notification.action, :before, resource)
          end
          resource.updated_by_last_action(false)
        end
      end

      # Actually run the action for realsies
      resource.run_action(action, notification_type, notifying_resource)

      # Execute any immediate and queue up any delayed notifications
      # associated with the resource, but only if it was updated *this time*
      # we ran an action on it.
      if resource.updated_by_last_action?
        run_context.immediate_notifications(resource).each do |notification|
          Chef::Log.info("#{resource} sending #{notification.action} action to #{notification.resource} (immediate)")
          run_action(notification.resource, notification.action, :immediate, resource)
        end

        run_context.delayed_notifications(resource).each do |notification|
          # send the notification to the run_context of the receiving resource
          notification.resource.run_context.add_delayed_action(notification)
        end
      end
    end

    # Iterates over the +resource_collection+ in the +run_context+ calling
    # +run_action+ for each resource in turn.
    def converge
      # Resolve all lazy/forward references in notifications
      run_context.resource_collection.each(&:resolve_notification_references)

      # Execute each resource.
      run_context.resource_collection.execute_each_resource do |resource|
        begin
          Array(resource.action).each { |action| run_action(resource, action) }
        ensure
          resource.executed_by_runner = true
        end
      end

    rescue Exception => e
      Chef::Log.info "Running queued delayed notifications before re-raising exception"
      run_delayed_notifications(e)
    else
      run_delayed_notifications(nil)
      true
    end

    private

    # Run all our :delayed actions
    def run_delayed_notifications(error = nil)
      collected_failures = Exceptions::MultipleFailures.new
      collected_failures.client_run_failure(error) unless error.nil?
      delayed_actions.each do |notification|
        result = run_delayed_notification(notification)
        if result.is_a?(Exception)
          collected_failures.notification_failure(result)
        end
      end
      collected_failures.raise!
    end

    def run_delayed_notification(notification)
      Chef::Log.info( "#{notification.notifying_resource} sending #{notification.action}"\
                      " action to #{notification.resource} (delayed)")
      # Struct of resource/action to call
      run_action(notification.resource, notification.action, :delayed)
      true
    rescue Exception => e
      e
    end

    # helper to run a block of code with why_run forced to true and then restore it correctly
    def forced_why_run
      saved = Chef::Config[:why_run]
      Chef::Config[:why_run] = true
      yield
    ensure
      Chef::Config[:why_run] = saved
    end

  end
end