Class: Puppet::Provider::ParsedFile

Inherits:
Puppet::Provider show all
Defined in:
lib/puppet/provider/parsedfile.rb

Overview

This provider can be used as the parent class for a provider that parses and generates files. Its content must be loaded via the 'prefetch' method, and the file will be written when 'flush' is called on the provider instance. At this point, the file is written once for every provider instance.

Once the provider prefetches the data, it's the resource's job to copy that data over to the @is variables.

Constant Summary

Class Method Summary (collapse)

Methods included from Util

exit_on_fail, #exit_on_fail, which, #which

Methods inherited from Puppet::Provider

command, #command, commands, create_class_and_instance_method, default_match, execfail, execpipe, execute, has_command, make_command_methods, optional_commands, post_resource_eval

Methods included from Confiner

#confine, #confine_collection, #suitable?

Class Method Details

+ (Object) drop_native_header

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 is abstract.

Providers based on ParsedFile that make use of the support for third party headers may override this method to return +true+. When this is done, headers that are matched by the native_header_regex are not written back to disk.

How to handle third party headers.



136
137
138
# File 'lib/puppet/provider/parsedfile.rb', line 136

def self.drop_native_header
  false
end

+ (Object) match_providers_with_resources(resources)

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.

Match a list of catalog resources with provider instances



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/puppet/provider/parsedfile.rb', line 219

def self.match_providers_with_resources(resources)
  return unless resources
  matchers = resources.dup
  @records.each do |record|
    # Skip things like comments and blank lines
    next if skip_record?(record)

    if (resource = resource_for_record(record, resources))
      resource.provider = new(record)
    elsif respond_to?(:match)
      if resource = match(record, matchers)
        matchers.delete(resource.title)
        record[:name] = resource[:name]
        resource.provider = new(record)
      end
    end
  end
end

+ (Object) native_header_regex

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 is abstract.

Providers based on ParsedFile may implement this to make it possible to identify a header maintained by a third party tool. The provider can then allow that header to remain near the top of the written file, or remove it after composing the file content. If implemented, the function must return a Regexp object. The expression must be tailored to match exactly one third party header.

Note:

When specifying regular expressions in multiline mode, avoid greedy repititions such as '.' (use .? instead). Otherwise, the provider may drop file content between sparse headers.

An optional regular expression matched by third party headers.

For example, this can be used to filter the vixie cron headers as erronously exported by older cron versions.

See Also:



125
126
127
# File 'lib/puppet/provider/parsedfile.rb', line 125

def self.native_header_regex
  nil
end

+ (Puppet::Resource?) resource_for_record(record, resources)

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.

Look up a resource based on a parsed file record



246
247
248
249
250
251
# File 'lib/puppet/provider/parsedfile.rb', line 246

def self.resource_for_record(record, resources)
  name = record[:name]
  if name
    resources[name]
  end
end

+ (Object) to_file(records)

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.

Compose file contents from the set of records.

If self.native_header_regex is not nil, possible vendor headers are identified by matching the return value against the expression. If one (or several consecutive) such headers, are found, they are either moved in front of the self.header if self.drop_native_header is false (this is the default), or removed from the return value otherwise.



380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'lib/puppet/provider/parsedfile.rb', line 380

def self.to_file(records)
  text = super
  if native_header_regex and (match = text.match(native_header_regex))
    if drop_native_header
      # concatenate the text in front of and after the native header
      text = match.pre_match + match.post_match
    else
      native_header = match[0]
      return native_header + header + match.pre_match + match.post_match
    end
  end
  header + text
end