Module: AugeasProviders::Provider

Defined in:
lib/augeasproviders/provider.rb

Overview

Mixin to add Augeas-related helpers to Puppet providers.

Handles basics such as opening, accessing and saving changes via an Augeas handle, plus standard configuration from a Puppet resource (e.g. the target parameter).

To use, include in the provider:

Puppet::Type.type(:example).provide(:augeas) do
  include AugeasProviders::Provider
  # [..]
end

Defined Under Namespace

Modules: ClassMethods

Class Attribute Summary (collapse)

Instance Method Summary (collapse)

Class Attribute Details

+ (Object) loadpath

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.

Override Augeas' loadpath, usually for testing with a separate Augeas checkout.



28
29
30
# File 'lib/augeasproviders/provider.rb', line 28

def loadpath
  @loadpath
end

Instance Method Details

- (Augeas) aug_handler

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 an Augeas handler.

On Puppet >= 3.4, stores and returns a shared Augeas handler for all instances of the class

Returns:

  • (Augeas)

    Augeas shared Augeas handle



815
816
817
# File 'lib/augeasproviders/provider.rb', line 815

def aug_handler
  self.class.aug_handler
end

- (String) aug_version

Returns the Augeas version used

Returns:

  • (String)

    Augeas version in use



724
725
726
# File 'lib/augeasproviders/provider.rb', line 724

def aug_version
  self.class.aug_version
end

- (Object) augclose!(aug)

Close the shared Augeas handler.

Parameters:

  • aug (Augeas)

    open Augeas handle



804
805
806
# File 'lib/augeasproviders/provider.rb', line 804

def augclose!(aug)
  self.class.augclose!(aug)
end

- (Augeas) augopen(yield_resource = false, *yield_params) {|aug, resource, *yield_params| ... }

Opens Augeas and returns a handle to use. It loads only the file for the current Puppet resource using AugeasProviders::Provider::ClassMethods#lens.

If called with a block, this will be yielded to and the Augeas handle closed after the block has executed (on Puppet < 3.4.0). Otherwise, the handle will be returned and not closed automatically. On Puppet >= 3.4, the handle will be closed by post_resource_eval. On older versions, the caller is responsible for closing it to free resources.

If yield_resource is set to true, the supplied resource will be passed as a yieldparam to the block, after the aug handle. Any arguments passed after yield_resource will be added as yieldparams to the block.

Yields:

  • (aug, resource, *yield_params)

    block that uses the Augeas handle

Yield Parameters:

  • aug (Augeas)

    open Augeas handle

  • resource (Puppet::Resource)

    the supplied Puppet resource, passed if yield_resource is set to true

  • *yield_params (Splat)

    a splat of additional arguments sent to the block, if yield_resource is set to true

Returns:

  • (Augeas)

    Augeas handle if no block is given

Raises:

  • (Puppet::Error)

    if Augeas did not load the file



763
764
765
# File 'lib/augeasproviders/provider.rb', line 763

def augopen(yield_resource = false, *yield_params, &block)
  self.class.augopen(self.resource, yield_resource, *yield_params, &block)
end

- (Augeas) augopen!(yield_resource = false, *yield_params) {|aug, resource, *yield_params| ... }

Opens Augeas and returns a handle to use. It loads only the file for the current Puppet resource using AugeasProviders::Provider::ClassMethods#lens.

augsave! is called after the block is evaluated.

If called with a block, this will be yielded to and the Augeas handle closed after the block has executed (on Puppet < 3.4.0). Otherwise, the handle will be returned and not closed automatically. On Puppet >= 3.4, the handle will be closed by post_resource_eval. On older versions, the caller is responsible for closing it to free resources.

Yields:

  • (aug, resource, *yield_params)

    block that uses the Augeas handle

Yield Parameters:

  • aug (Augeas)

    open Augeas handle

  • resource (Puppet::Resource)

    the supplied Puppet resource, passed if yield_resource is set to true

  • *yield_params (Splat)

    a splat of additional arguments sent to the block, if yield_resource is set to true

Returns:

  • (Augeas)

    Augeas handle if no block is given

Raises:

  • (Puppet::Error)

    if Augeas did not load the file



785
786
787
# File 'lib/augeasproviders/provider.rb', line 785

def augopen!(yield_resource = false, *yield_params, &block)
  self.class.augopen!(self.resource, yield_resource, *yield_params, &block)
end

- (Object) augsave!(aug, reload = false)

Saves all changes made in the current Augeas handle and checks for any errors while doing so.

Parameters:

  • aug (Augeas)

    open Augeas handle

  • reload (Boolean) (defaults to: false)

    whether to reload the tree after saving

Raises:

  • (Augeas::Error)

    if saving fails



796
797
798
# File 'lib/augeasproviders/provider.rb', line 796

def augsave!(aug, reload = false)
  self.class.augsave!(aug, reload)
end

- (Boolean) parsed_as?(text, path, lens = nil)

Returns whether text is parsed as path using lens

Parameters:

  • text (String)

    the text to test

  • path (String)

    the relative path to test

  • lens (String) (defaults to: nil)

    the lens to use for parsing

Returns:

  • (Boolean)

    whether the path was found when parsing text with lens



907
908
909
910
# File 'lib/augeasproviders/provider.rb', line 907

def parsed_as?(text, path, lens = nil)
  lens ||= self.class.lens(self.resource)
  self.class.parsed_as?(text, path, lens)
end

- (String) path_label(aug, path)

Wrapper around Augeas#label for older versions of Augeas

Parameters:

  • aug (Augeas)

    Augeas handler

  • path (String)

    expression to get the label from

Returns:

  • (String)

    label of the given path



825
826
827
# File 'lib/augeasproviders/provider.rb', line 825

def path_label(aug, path)
  self.class.path_label(aug, path)
end

- (String) quoteit(value, oldvalue = nil)

Automatically quote a value

Parameters:

  • value (String)

    the value to quote

  • oldvalue (String) (defaults to: nil)

    the optional old value, used to auto-detect existing quoting

Returns:

  • (String)

    the quoted value



835
836
837
# File 'lib/augeasproviders/provider.rb', line 835

def quoteit(value, oldvalue = nil)
  self.class.quoteit(value, self.resource, oldvalue)
end

- (Symbol) readquote(value)

Detect what type of quoting a value uses

Parameters:

  • value (String)

    the value to be analyzed

Returns:

  • (Symbol)

    the type of quoting used (:double, :single or nil)



844
845
846
# File 'lib/augeasproviders/provider.rb', line 844

def readquote(value)
  self.class.readquote(value)
end

- (String) resource_path

Gets the Augeas path expression representing the individual resource inside the file, that represents the current Puppet resource.

If no block was set by the provider's class method, it returns the path expression representing the top-level of the file.

Returns:

  • (String)

    Augeas path expression to use, e.g. '/files/etc/hosts/1'

See Also:



858
859
860
# File 'lib/augeasproviders/provider.rb', line 858

def resource_path
  self.class.resource_path(self.resource)
end

- (Object) setvars(aug)

Sets useful Augeas variables for the session:

  • $target points to the root of the target file
  • $resource points to path defined by #resource_path

It also sets /augeas/context to the target file so relative paths can be used, before the variables are set.

If supplied with a resource, it will be used to determine the path to the used file.

Parameters:

  • aug (Augeas)

    Augeas handle

See Also:



876
877
878
# File 'lib/augeasproviders/provider.rb', line 876

def setvars(aug)
  self.class.setvars(aug, self.resource)
end

- (Boolean) supported?(feature)

Returns whether a feature is supported.

The following features are currently supported:

  • :regexpi: whether Augeas supports an 'i' flag in regexp expressions
  • :post_resource_eval: whether Puppet supports post_resource_eval hooks

Parameters:

  • feature (Symbol)

    the feature to check

Returns:

  • (Boolean)

    whether feature is supported



738
739
740
# File 'lib/augeasproviders/provider.rb', line 738

def supported?(feature)
  self.class.supported?(feature)
end

- (String) target

Gets the path expression representing the file being managed for the current Puppet resource.

Returns:

  • (String)

    path expression representing the file being managed

See Also:



887
888
889
# File 'lib/augeasproviders/provider.rb', line 887

def target
  self.class.target(self.resource)
end

- (String) unquoteit(value)

Automatically unquote a value

Parameters:

  • value (String)

    the value to unquote

Returns:

  • (String)

    the unquoted value



896
897
898
# File 'lib/augeasproviders/provider.rb', line 896

def unquoteit(value)
  self.class.unquoteit(value)
end