summaryrefslogtreecommitdiff
path: root/lib/chef/data_collector.rb
blob: c4afbfaa34a025adf46fbfe2f0e30ee18b5799fc (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#
# Author:: Adam Leff (<adamleff@chef.io>)
# Author:: Ryan Cragun (<ryan@chef.io>)
#
# Copyright:: Copyright 2012-2016, 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 "uri"
require "chef/event_dispatch/base"
require "chef/data_collector/resource_report"
require "chef/data_collector/serializers/node_update"
require "chef/data_collector/serializers/run_end"
require "chef/data_collector/serializers/run_start"

class Chef
  class DataCollector
    def self.register_reporter?
      Chef::Config[:data_collector_server_url] &&
        !Chef::Config[:why_run] &&
        self.reporter_enabled_for_current_mode?
    end

    def self.reporter_enabled_for_current_mode?
      if Chef::Config[:solo] || Chef::Config[:local_mode]
        acceptable_modes = [:solo, :both]
      else
        acceptable_modes = [:client, :both]
      end

      acceptable_modes.include?(Chef::Config[:data_collector_mode])
    end

    class Reporter < EventDispatch::Base
      attr_reader :updated_resources, :status, :exception, :error_descriptions,
                  :expanded_run_list, :run_status, :http, :resource_count,
                  :current_resource_report, :enabled

      def initialize
        @updated_resources       = []
        @resource_count          = 0
        @current_resource_loaded = nil
        @status                  = "success"
        @exception               = nil
        @error_descriptions      = {}
        @expanded_run_list       = {}
        @http                    = Chef::HTTP.new(data_collector_server_url)
        @enabled                 = true
      end

      def run_started(current_run_status)
        update_run_status(current_run_status)

        disable_reporter_on_error do
          send_to_data_collector(Serializers::RunStart.new(run_status))
        end
      end

      def run_completed(node)
        send_run_completion
      end

      def run_failed(exception)
        update_exception(exception)
        send_run_completion
      end

      def resource_current_state_loaded(new_resource, action, current_resource)
        return if nested_resource?(new_resource)
        update_current_resource_report(
          Chef::DataCollector::ResourceReport.new(
            new_resource,
            action,
            current_resource
          )
        )
      end

      def resource_up_to_date(new_resource, action)
        current_resource_report.up_to_date unless nested_resource?(new_resource)
        increment_resource_count
      end

      def resource_skipped(new_resource, action, conditional)
        increment_resource_count
        return if nested_resource?(new_resource)

        update_current_resource_report(
          Chef::DataCollector::ResourceReport.new(
            new_resource,
            action
          )
        )
        current_resource_report.skipped(conditional)
      end

      def resource_updated(new_resource, action)
        current_resource_report.updated unless nested_resource?(new_resource)
        increment_resource_count
      end

      def resource_failed(new_resource, action, exception)
        current_resource_report.failed(exception) unless nested_resource?(new_resource)
        increment_resource_count
        update_error_description(
          Formatters::ErrorMapper.resource_failed(
            new_resource,
            action,
            exception
          ).for_json
        )
      end

      def resource_completed(new_resource)
        if current_resource_report && !nested_resource?(new_resource)
          current_resource_report.finish
          add_updated_resource(current_resource_report)
          update_current_resource_report(nil)
        end
      end

      def run_list_expanded(run_list_expansion)
        @expanded_run_list = run_list_expansion
      end

      def run_list_expand_failed(node, exception)
        update_error_description(
          Formatters::ErrorMapper.run_list_expand_failed(
            node,
            exception
          ).for_json
        )
      end

      def cookbook_resolution_failed(expanded_run_list, exception)
        update_error_description(
          Formatters::ErrorMapper.cookbook_resolution_failed(
            expanded_run_list,
            exception
          ).for_json
        )
      end

      def cookbook_sync_failed(cookbooks, exception)
        update_error_description(
          Formatters::ErrorMapper.cookbook_sync_failed(
            cookbooks,
            exception
          ).for_json
        )
      end

      private

      def disable_reporter_on_error
        yield
      rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET,
             Errno::ECONNREFUSED, EOFError, Net::HTTPBadResponse,
             Net::HTTPHeaderSyntaxError, Net::ProtocolError, OpenSSL::SSL::SSLError => e
        disable_data_collector_reporter
        code = if e.respond_to?(:response) && e.response.code
                 e.response.code.to_s
               else
                 "Exception Code Empty"
               end

        msg = "Error while reporting run start to Data Collector. " \
              "URL: #{data_collector_server_url} " \
              "Exception: #{code} -- #{e.message} "

        if Chef::Config[:data_collector_raise_on_failure]
          Chef::Log.error(msg)
          raise
        else
          Chef::Log.warn(msg)
        end
      end

      def send_to_data_collector(message)
        return unless data_collector_accessible?

        Chef::Log.debug("data_collector_reporter: POSTing the following message to #{data_collector_server_url}: #{message.to_json}")
        http.post(nil, message.to_json, headers)
      end

      def send_run_completion
        # If run_status is nil we probably failed before the client triggered
        # the run_started callback. In this case we'll skip updating because
        # we have nothing to report.
        return unless run_status

        send_to_data_collector(Serializers::NodeUpdate.new(run_status))
        send_to_data_collector(
          Serializers::RunEnd.new(
            run_status: run_status,
            expanded_run_list: expanded_run_list,
            total_resource_count: resource_count,
            updated_resources: updated_resources,
            status: status,
            error_descriptions: error_descriptions
          )
        )
      end

      def headers(additional_headers = {})
        headers = { "Content-Type" => "application/json" }
        headers["x-data-collector-token"] = data_collector_token unless data_collector_token.nil?

        headers.merge(additional_headers)
      end

      def data_collector_server_url
        Chef::Config[:data_collector_server_url]
      end

      def data_collector_token
        Chef::Config[:data_collector_token]
      end

      def increment_resource_count
        @resource_count += 1
      end

      def add_updated_resource(resource_report)
        @updated_resources << resource_report
      end

      def disable_data_collector_reporter
        # TODO: should the event dispatcher support de-registering?
        @enabled = false
      end

      def data_collector_accessible?
        @enabled
      end

      def update_status(status)
        @status = status
      end

      def update_run_status(run_status)
        @run_status = run_status
      end

      def update_exception(ex)
        @exception = ex
        update_status("failure")
      end

      def update_current_resource_report(resource_report)
        @current_resource_report = resource_report
      end

      def update_error_description(discription_hash)
        @error_descriptions = discription_hash
      end

      # If we are getting messages about a resource while we are in the middle of
      # another resource's update, we assume that the nested resource is just the
      # implementation of a provider, and we want to hide it from the reporting
      # output.
      def nested_resource?(new_resource)
        @current_resource_report && @current_resource_report.new_resource != new_resource
      end
    end
  end
end