Module: AugeasProviders::Provider

Included in:
Mounttab::Fstab, Mounttab::Vfstab
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

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.loadpathObject

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

Class Method Details

.included(base) ⇒ Object



31
32
33
# File 'lib/augeasproviders/provider.rb', line 31

def self.included(base)
  base.send(:extend, ClassMethods)
end

Instance Method Details

#aug_handlerAugeas

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

#aug_versionString

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

#augclose!(aug) ⇒ Object

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

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

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

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

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

#augsave!(aug, reload = false) ⇒ Object

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

#destroyObject

Default method to destroy a resource can be overridden if necessary



922
923
924
925
926
# File 'lib/augeasproviders/provider.rb', line 922

def destroy
  augopen! do |aug|
    aug.rm('$resource')
  end
end

#exists?Boolean

Default method to determine the existence of a resource can be overridden if necessary

Returns:

  • (Boolean)


914
915
916
917
918
# File 'lib/augeasproviders/provider.rb', line 914

def exists?
  augopen do |aug|
    not aug.match('$resource').empty?
  end
end

#flushObject

Default method to flush a resource

On Puppet >= 3.4, this takes care of saving the tree for the shared Augeas handler.

This method can be overridden in your provider but you should make sure to call ‘super` to ensure that the tree will be saved in Puppet >= 3.4



936
937
938
# File 'lib/augeasproviders/provider.rb', line 936

def flush
  augsave!(aug_handler, true) if supported?(:post_resource_eval)
end

#parsed_as?(text, path, lens = nil) ⇒ Boolean

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

#path_label(aug, path) ⇒ String

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

#quoteit(value, oldvalue = nil) ⇒ String

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

#readquote(value) ⇒ Symbol

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

#resource_pathString

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

#setvars(aug) ⇒ Object

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

#supported?(feature) ⇒ Boolean

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

#targetString

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

#unquoteit(value) ⇒ String

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