Class: Puppet::Transaction Private

Inherits:
Object show all
Includes:
Util, Util::Tagging
Defined in:
lib/puppet/transaction.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

the class that actually walks our resource/property tree, collects the changes, and performs them

Defined Under Namespace

Classes: AdditionalResourceGenerator, Event, EventManager, Persistence, Report, ResourceHarness

Constant Summary

Constants included from Util::Tagging

Util::Tagging::ValidTagRegex

Constants included from Util

Util::AbsolutePathPosix, Util::AbsolutePathWindows, Util::DEFAULT_POSIX_MODE, Util::DEFAULT_WINDOWS_MODE, Util::RFC_3986_URI_REGEX

Constants included from Util::SymbolicFileMode

Util::SymbolicFileMode::SetGIDBit, Util::SymbolicFileMode::SetUIDBit, Util::SymbolicFileMode::StickyBit, Util::SymbolicFileMode::SymbolicMode, Util::SymbolicFileMode::SymbolicSpecialToBit

Constants included from Util::POSIX

Util::POSIX::LOCALE_ENV_VARS, Util::POSIX::USER_ENV_VARS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util::Tagging

#merge_into, #merge_tags_from, #raw_tagged?, #set_tags, #tag, #tag_if_valid, #tagged?, #tags=, #valid_tag?

Methods included from Util

absolute_path?, benchmark, chuser, clear_environment, default_env, deterministic_rand, deterministic_rand_int, exit_on_fail, get_env, get_environment, logmethods, merge_environment, path_to_uri, pretty_backtrace, replace_file, safe_posix_fork, set_env, symbolizehash, thinmark, uri_encode, uri_query_encode, uri_to_path, which, withenv, withumask

Methods included from Util::SymbolicFileMode

#normalize_symbolic_mode, #symbolic_mode_to_int, #valid_symbolic_mode?

Methods included from Util::POSIX

#get_posix_field, #gid, groups_of, #idfield, #methodbyid, #methodbyname, #search_posix_field, #uid

Constructor Details

#initialize(catalog, report, prioritizer) ⇒ Transaction

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Transaction


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/puppet/transaction.rb', line 41

def initialize(catalog, report, prioritizer)
  @catalog = catalog

  @persistence = Puppet::Transaction::Persistence.new

  @report = report || Puppet::Transaction::Report.new(catalog.version, catalog.environment)

  @prioritizer = prioritizer

  @report.add_times(:config_retrieval, @catalog.retrieval_duration || 0)

  @event_manager = Puppet::Transaction::EventManager.new(self)

  @resource_harness = Puppet::Transaction::ResourceHarness.new(self)

  @prefetched_providers = Hash.new { |h,k| h[k] = {} }

  @prefetch_failed_providers = Hash.new { |h,k| h[k] = {} }

  @failed_dependencies_already_notified = Set.new()
end

Instance Attribute Details

#catalogObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


20
21
22
# File 'lib/puppet/transaction.rb', line 20

def catalog
  @catalog
end

#event_managerObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Routes and stores any events and subscriptions.


26
27
28
# File 'lib/puppet/transaction.rb', line 26

def event_manager
  @event_manager
end

#for_network_deviceObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


20
21
22
# File 'lib/puppet/transaction.rb', line 20

def for_network_device
  @for_network_device
end

#ignoreschedulesObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


20
21
22
# File 'lib/puppet/transaction.rb', line 20

def ignoreschedules
  @ignoreschedules
end

#persistenceObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


36
37
38
# File 'lib/puppet/transaction.rb', line 36

def persistence
  @persistence
end

#prefetch_failed_providersObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


31
32
33
# File 'lib/puppet/transaction.rb', line 31

def prefetch_failed_providers
  @prefetch_failed_providers
end

#prefetched_providersObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


31
32
33
# File 'lib/puppet/transaction.rb', line 31

def prefetched_providers
  @prefetched_providers
end

#reportObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The report, once generated.


23
24
25
# File 'lib/puppet/transaction.rb', line 23

def report
  @report
end

#resource_harnessObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Handles most of the actual interacting with resources


29
30
31
# File 'lib/puppet/transaction.rb', line 29

def resource_harness
  @resource_harness
end

Instance Method Details

#any_failed?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Are there any failed resources in this transaction?


210
211
212
213
214
# File 'lib/puppet/transaction.rb', line 210

def any_failed?
  report.resource_statuses.values.detect { |status|
    status.failed? || status.failed_to_restart?
  }
end

#changed?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Find all of the changed resources.


217
218
219
# File 'lib/puppet/transaction.rb', line 217

def changed?
  report.resource_statuses.values.find_all { |status| status.changed }.collect { |status| catalog.resource(status.resource) }
end

#evaluate(&block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method does all the actual work of running a transaction. It collects all of the changes, executes them, and responds to any necessary events.


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
# File 'lib/puppet/transaction.rb', line 93

def evaluate(&block)
  block ||= method(:eval_resource)
  generator = AdditionalResourceGenerator.new(@catalog, nil, @prioritizer)
  @catalog.vertices.each { |resource| generator.generate_additional_resources(resource) }

  perform_pre_run_checks

  persistence.load if persistence.enabled?(catalog)

  Puppet.info _("Applying configuration version '%{version}'") % { version: catalog.version } if catalog.version

  continue_while = lambda { !stop_processing? }

  post_evalable_providers = Set.new
  pre_process = lambda do |resource|
    prov_class = resource.provider.class
    post_evalable_providers << prov_class if prov_class.respond_to?(:post_resource_eval)

    prefetch_if_necessary(resource)

    # If we generated resources, we don't know what they are now
    # blocking, so we opt to recompute it, rather than try to track every
    # change that would affect the number.
    relationship_graph.clear_blockers if generator.eval_generate(resource)
  end

  providerless_types = []
  overly_deferred_resource_handler = lambda do |resource|
    # We don't automatically assign unsuitable providers, so if there
    # is one, it must have been selected by the user.
    return if missing_tags?(resource)
    if resource.provider
      resource.err _("Provider %{name} is not functional on this host") % { name: resource.provider.class.name }
    else
      providerless_types << resource.type
    end

    resource_status(resource).failed = true
  end

  canceled_resource_handler = lambda do |resource|
    resource_status(resource).skipped = true
    resource.debug "Transaction canceled, skipping"
  end

  teardown = lambda do
    # Just once per type. No need to punish the user.
    providerless_types.uniq.each do |type|
      Puppet.err _("Could not find a suitable provider for %{type}") % { type: type }
    end

    post_evalable_providers.each do |provider|
      begin
        provider.post_resource_eval
      rescue => detail
        Puppet.log_exception(detail, _("post_resource_eval failed for provider %{provider}") % { provider: provider })
      end
    end

    persistence.save if persistence.enabled?(catalog)
  end

  # Graph cycles are returned as an array of arrays
  # - outer array is an array of cycles
  # - each inner array is an array of resources involved in a cycle
  # Short circuit resource evaluation if we detect cycle(s) in the graph. Mark
  # each corresponding resource as failed in the report before we fail to
  # ensure accurate reporting.
  graph_cycle_handler = lambda do |cycles|
    cycles.flatten.uniq.each do |resource|
      # We add a failed resource event to the status to ensure accurate
      # reporting through the event manager.
      resource_status(resource).fail_with_event(_('resource is part of a dependency cycle'))
    end
    raise Puppet::Error, _('One or more resource dependency cycles detected in graph')
  end

  # Generate the relationship graph, set up our generator to use it
  # for eval_generate, then kick off our traversal.
  generator.relationship_graph = relationship_graph
  progress = 0
  relationship_graph.traverse(:while => continue_while,
                              :pre_process => pre_process,
                              :overly_deferred_resource_handler => overly_deferred_resource_handler,
                              :canceled_resource_handler => canceled_resource_handler,
                              :graph_cycle_handler => graph_cycle_handler,
                              :teardown => teardown) do |resource|
    progress += 1
    if resource.is_a?(Puppet::Type::Component)
      Puppet.warning _("Somehow left a component in the relationship graph")
    else
      if Puppet[:evaltrace] && @catalog.host_config?
        resource.info _("Starting to evaluate the resource (%{progress} of %{total})") % { progress: progress, total: relationship_graph.size }
      end
      seconds = thinmark { block.call(resource) }
      resource.info _("Evaluated in %{seconds} seconds") % { seconds: "%0.2f" % seconds } if Puppet[:evaltrace] && @catalog.host_config?
    end
  end

  # if one or more resources has attempted and failed to generate resources,
  # report it
  if generator.resources_failed_to_generate
    report.resources_failed_to_generate = true
  end

  # mark the end of transaction evaluate.
  report.transaction_completed = true

  Puppet.debug "Finishing transaction #{object_id}"
end

#missing_tags?(resource) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Is this resource tagged appropriately?


426
427
428
429
430
431
# File 'lib/puppet/transaction.rb', line 426

def missing_tags?(resource)
  return false if ignore_tags?
  return false if tags.empty?

  not resource.tagged?(*tags)
end

#perform_pre_run_checksvoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Invoke the pre_run_check hook in every resource in the catalog. This should (only) be called by Transaction#evaluate before applying the catalog.

Raises:

See Also:


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/puppet/transaction.rb', line 71

def perform_pre_run_checks
  prerun_errors = {}

  @catalog.vertices.each do |res|
    begin
      res.pre_run_check
    rescue Puppet::Error => detail
      prerun_errors[res] = detail
    end
  end

  unless prerun_errors.empty?
    prerun_errors.each do |res, detail|
      res.log_exception(detail)
    end
    raise Puppet::Error, _("Some pre-run checks failed")
  end
end

#prefetch_if_necessary(resource) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/puppet/transaction.rb', line 240

def prefetch_if_necessary(resource)
  provider_class = resource.provider.class
  if !provider_class.respond_to?(:prefetch) or
      prefetched_providers[resource.type][provider_class.name] or
      prefetch_failed_providers[resource.type][provider_class.name]
    return
  end

  resources = resources_by_provider(resource.type, provider_class.name)

  if provider_class == resource.class.defaultprovider
    providerless_resources = resources_by_provider(resource.type, nil)
    providerless_resources.values.each {|res| res.provider = provider_class.name}
    resources.merge! providerless_resources
  end

  prefetch(provider_class, resources)
end

#relationship_graphObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


221
222
223
# File 'lib/puppet/transaction.rb', line 221

def relationship_graph
  catalog.relationship_graph(@prioritizer)
end

#resource_status(resource) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


225
226
227
# File 'lib/puppet/transaction.rb', line 225

def resource_status(resource)
  report.resource_statuses[resource.to_s] || add_resource_status(Puppet::Resource::Status.new(resource))
end

#skip?(resource) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Should this resource be skipped?


390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# File 'lib/puppet/transaction.rb', line 390

def skip?(resource)
  if skip_tags?(resource)
    resource.debug "Skipping with skip tags #{skip_tags.join(", ")}"
  elsif missing_tags?(resource)
    resource.debug "Not tagged with #{tags.join(", ")}"
  elsif ! scheduled?(resource)
    resource.debug "Not scheduled"
  elsif failed_dependencies?(resource)
    # When we introduced the :whit into the graph, to reduce the combinatorial
    # explosion of edges, we also ended up reporting failures for containers
    # like class and stage.  This is undesirable; while just skipping the
    # output isn't perfect, it is RC-safe. --daniel 2011-06-07
    unless resource.class == Puppet::Type.type(:whit) then
      resource.warning _("Skipping because of failed dependencies")
    end
  elsif resource_status(resource).failed? &&
      @prefetch_failed_providers[resource.type][resource.provider.class.name]
    #Do not try to evaluate a resource with a known failed provider
    resource.warning _("Skipping because provider prefetch failed")
  elsif resource.virtual?
    resource.debug "Skipping because virtual"
  elsif !host_and_device_resource?(resource) && resource.appliable_to_host? && for_network_device
    resource.debug "Skipping host resources because running on a device"
  elsif !host_and_device_resource?(resource) && resource.appliable_to_device? && !for_network_device
    resource.debug "Skipping device resources because running on a posix host"
  else
    return false
  end
  true
end

#skip_tagsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


236
237
238
# File 'lib/puppet/transaction.rb', line 236

def skip_tags
  @skip_tags ||= Puppet::Util::SkipTags.new(Puppet[:skip_tags]).tags
end

#stop_processing?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Wraps application run state check to flag need to interrupt processing


205
206
207
# File 'lib/puppet/transaction.rb', line 205

def stop_processing?
  Puppet::Application.stop_requested? && catalog.host_config?
end

#tagsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The tags we should be checking.


230
231
232
233
234
# File 'lib/puppet/transaction.rb', line 230

def tags
  self.tags = Puppet[:tags] unless defined?(@tags)

  super
end