# frozen_string_literal: true
# Fact: is_pe, pe_version, pe_major_version, pe_minor_version, pe_patch_version # Fact: is_pe, pe_version, pe_major_version, pe_minor_version, pe_patch_version
# #
# Purpose: Return various facts about the PE state of the system # Purpose: Return various facts about the PE state of the system
...@@ -10,24 +8,18 @@ ...@@ -10,24 +8,18 @@
# #
# Caveats: # Caveats:
# #
# Fact: pe_version
Facter.add('pe_version') do Facter.add('pe_version') do
setcode do setcode do
found_version = Facter.value('pe_build') puppet_ver = Facter.value('puppetversion')
if !puppet_ver.nil?
unless found_version pe_ver = puppet_ver.match(%r{Puppet Enterprise (\d+\.\d+\.\d+)})
puppet_ver = Facter.value('puppetversion') pe_ver[1] if pe_ver
unless puppet_ver.nil? else
pe_ver = puppet_ver.match(%r{Puppet Enterprise (\d+\.\d+\.\d+)}) nil
found_version = pe_ver[1] if pe_ver
end
end end
found_version
end end
end end
# Fact: is_pe
Facter.add('is_pe') do Facter.add('is_pe') do
setcode do setcode do
if Facter.value(:pe_version).to_s.empty? if Facter.value(:pe_version).to_s.empty?
...@@ -38,9 +30,8 @@ Facter.add('is_pe') do ...@@ -38,9 +30,8 @@ Facter.add('is_pe') do
end end
end end
# Fact: pe_major_version
Facter.add('pe_major_version') do Facter.add('pe_major_version') do
confine is_pe: true confine :is_pe => true
setcode do setcode do
pe_version = Facter.value(:pe_version) pe_version = Facter.value(:pe_version)
if pe_version if pe_version
...@@ -49,9 +40,8 @@ Facter.add('pe_major_version') do ...@@ -49,9 +40,8 @@ Facter.add('pe_major_version') do
end end
end end
# Fact: pe_minor_version
Facter.add('pe_minor_version') do Facter.add('pe_minor_version') do
confine is_pe: true confine :is_pe => true
setcode do setcode do
pe_version = Facter.value(:pe_version) pe_version = Facter.value(:pe_version)
if pe_version if pe_version
...@@ -60,9 +50,8 @@ Facter.add('pe_minor_version') do ...@@ -60,9 +50,8 @@ Facter.add('pe_minor_version') do
end end
end end
# Fact: pe_patch_version
Facter.add('pe_patch_version') do Facter.add('pe_patch_version') do
confine is_pe: true confine :is_pe => true
setcode do setcode do
pe_version = Facter.value(:pe_version) pe_version = Facter.value(:pe_version)
if pe_version if pe_version
......
# frozen_string_literal: true
# These facter facts return the value of the Puppet vardir and environment path # These facter facts return the value of the Puppet vardir and environment path
# settings for the node running puppet or puppet agent. The intent is to # settings for the node running puppet or puppet agent. The intent is to
# enable Puppet modules to automatically have insight into a place where they # enable Puppet modules to automatically have insight into a place where they
# can place variable data, or for modules running on the puppet server to know # can place variable data, or for modules running on the puppet master to know
# where environments are stored. # where environments are stored.
# #
# The values should be directly usable in a File resource path attribute. # The values should be directly usable in a File resource path attribute.
...@@ -18,7 +16,7 @@ rescue LoadError => e ...@@ -18,7 +16,7 @@ rescue LoadError => e
load rb_file if File.exist?(rb_file) || raise(e) load rb_file if File.exist?(rb_file) || raise(e)
end end
# Facter fact returns the value of the Puppet vardir # These will be nil if Puppet is not available.
Facter.add(:puppet_vardir) do Facter.add(:puppet_vardir) do
setcode do setcode do
Facter::Util::PuppetSettings.with_puppet do Facter::Util::PuppetSettings.with_puppet do
...@@ -27,7 +25,6 @@ Facter.add(:puppet_vardir) do ...@@ -27,7 +25,6 @@ Facter.add(:puppet_vardir) do
end end
end end
# Facter fact returns the value of the Puppet environment path
Facter.add(:puppet_environmentpath) do Facter.add(:puppet_environmentpath) do
setcode do setcode do
Facter::Util::PuppetSettings.with_puppet do Facter::Util::PuppetSettings.with_puppet do
...@@ -36,7 +33,6 @@ Facter.add(:puppet_environmentpath) do ...@@ -36,7 +33,6 @@ Facter.add(:puppet_environmentpath) do
end end
end end
# Facter fact returns the value of the Puppet server
Facter.add(:puppet_server) do Facter.add(:puppet_server) do
setcode do setcode do
Facter::Util::PuppetSettings.with_puppet do Facter::Util::PuppetSettings.with_puppet do
......
# frozen_string_literal: true # A facter fact to determine the root home directory.
# This varies on PE supported platforms and may be
# root_home.rb # reconfigured by the end user.
module Facter::Util::RootHome module Facter::Util::RootHome
# @summary
# A facter fact to determine the root home directory.
# This varies on PE supported platforms and may be
# reconfigured by the end user.
class << self class << self
# determines the root home directory def returnt_root_home
def returnt_root_home root_ent = Facter::Util::Resolution.exec('getent passwd root')
root_ent = Facter::Util::Resolution.exec('getent passwd root') # The home directory is the sixth element in the passwd entry
# The home directory is the sixth element in the passwd entry # If the platform doesn't have getent, root_ent will be nil and we should
# If the platform doesn't have getent, root_ent will be nil and we should # return it straight away.
# return it straight away. root_ent && root_ent.split(':')[5]
root_ent && root_ent.split(':')[5] end
end
end end
end end
...@@ -23,7 +18,7 @@ Facter.add(:root_home) do ...@@ -23,7 +18,7 @@ Facter.add(:root_home) do
end end
Facter.add(:root_home) do Facter.add(:root_home) do
confine kernel: :darwin confine :kernel => :darwin
setcode do setcode do
str = Facter::Util::Resolution.exec('dscacheutil -q user -a name root') str = Facter::Util::Resolution.exec('dscacheutil -q user -a name root')
hash = {} hash = {}
...@@ -36,12 +31,12 @@ Facter.add(:root_home) do ...@@ -36,12 +31,12 @@ Facter.add(:root_home) do
end end
Facter.add(:root_home) do Facter.add(:root_home) do
confine kernel: :aix confine :kernel => :aix
root_home = nil root_home = nil
setcode do setcode do
str = Facter::Util::Resolution.exec('lsuser -c -a home root') str = Facter::Util::Resolution.exec('lsuser -c -a home root')
str&.split("\n")&.each do |line| str && str.split("\n").each do |line|
next if %r{^#}.match?(line) next if line =~ %r{^#}
root_home = line.split(%r{:})[1] root_home = line.split(%r{:})[1]
end end
root_home root_home
......
# frozen_string_literal: true
# Fact: service_provider # Fact: service_provider
# #
# Purpose: Returns the default provider Puppet will choose to manage services # Purpose: Returns the default provider Puppet will choose to manage services
...@@ -14,6 +12,6 @@ require 'puppet/type/service' ...@@ -14,6 +12,6 @@ require 'puppet/type/service'
Facter.add(:service_provider) do Facter.add(:service_provider) do
setcode do setcode do
Puppet::Type.type(:service).newservice(name: 'dummy')[:provider].to_s Puppet::Type.type(:service).newservice(:name => 'dummy')[:provider].to_s
end end
end end
# frozen_string_literal: true
# A method to evaluate a Facter code block if puppet is loaded. # A method to evaluate a Facter code block if puppet is loaded.
module Facter::Util::PuppetSettings module Facter::Util::PuppetSettings
# This method is intended to provide a convenient way to evaluate a # This method is intended to provide a convenient way to evaluate a
......
# frozen_string_literal: true # Function to print deprecation warnings, Logs a warning once for a given key. The uniqueness key - can appear once.
# The msg is the message text including any positional information that is formatted by the user/caller of the method.
# Function to print deprecation warnings, Logs a warning once for a given key. # It is affected by the puppet setting 'strict', which can be set to :error (outputs as an error message),
# # :off (no message / error is displayed) and :warning (default, outputs a warning) *Type*: String, String.
# The uniqueness key - can appear once.
# The msg is the message text including any positional information that is formatted by the
# user/caller of the method.
# It is affected by the puppet setting 'strict', which can be set to :error
# (outputs as an error message), :off (no message / error is displayed) and :warning
# (default, outputs a warning) *Type*: String, String.
# #
Puppet::Functions.create_function(:deprecation) do Puppet::Functions.create_function(:deprecation) do
# @param key
# @param message
# @return deprecated warnings
dispatch :deprecation do dispatch :deprecation do
param 'String', :key param 'String', :key
param 'String', :message param 'String', :message
...@@ -27,7 +19,7 @@ Puppet::Functions.create_function(:deprecation) do ...@@ -27,7 +19,7 @@ Puppet::Functions.create_function(:deprecation) do
end end
# depending on configuration setting of strict # depending on configuration setting of strict
case Puppet.settings[:strict] case Puppet.settings[:strict]
when :off when :off # rubocop:disable Lint/EmptyWhen : Is required to prevent false errors
# do nothing # do nothing
when :error when :error
raise("deprecation. #{key}. #{message}") raise("deprecation. #{key}. #{message}")
......
# frozen_string_literal: true # Digs into the facts hash using dot-notation
# @summary
# Digs into the facts hash using dot-notation
# #
# Supports the use of dot-notation for referring to structured facts. If a fact requested # Example usage:
# does not exist, returns Undef.
# #
# @example Example usage:
# fact('osfamily') # fact('osfamily')
# fact('os.architecture') # fact('os.architecture')
# #
# @example Array indexing: # Array indexing:
#
# fact('mountpoints."/dev".options.1') # fact('mountpoints."/dev".options.1')
# #
# @example Fact containing a "." in the name: # Fact containing a "." in the name:
#
# fact('vmware."VRA.version"') # fact('vmware."VRA.version"')
# #
Puppet::Functions.create_function(:fact) do Puppet::Functions.create_function(:fact) do
# @param fact_name
# The name of the fact to check
#
# @return
# All information retrieved on the given fact_name
dispatch :fact do dispatch :fact do
param 'String', :fact_name param 'String', :fact_name
end end
......
# frozen_string_literal: true # Boolean check to determine whether a variable is of a given data type. This is equivalent to the `=~` type checks.
# @summary
# Boolean check to determine whether a variable is of a given data type.
# This is equivalent to the `=~` type checks.
# #
# @example Example Usage: # @example how to check a data type
# # check a data type # # check a data type
# foo = 3 # foo = 3
# $bar = [1,2,3] # $bar = [1,2,3]
# $baz = 'A string!' # $baz = 'A string!'
# #
# if $foo.is_a(Integer) { # if $foo.is_a(Integer) {
# notify { 'foo!': } # notify { 'foo!': }
# } # }
# if $bar.is_a(Array) { # if $bar.is_a(Array) {
# notify { 'bar!': } # notify { 'bar!': }
# } # }
# if $baz.is_a(String) { # if $baz.is_a(String) {
# notify { 'baz!': } # notify { 'baz!': }
# } # }
# #
# See the documentation for "The Puppet Type System" for more information about types. # See the documentation for "The Puppet Type System" for more information about types.
# See the `assert_type()` function for flexible ways to assert the type of a value. # See the `assert_type()` function for flexible ways to assert the type of a value.
# #
Puppet::Functions.create_function(:is_a) do Puppet::Functions.create_function(:is_a) do
# @param value
# The value to be checked
#
# @param type
# The expected type
#
# @return [Boolean]
# Return's `true` or `false`.
dispatch :is_a do dispatch :is_a do
param 'Any', :value param 'Any', :value
param 'Type', :type param 'Type', :type
end end
def is_a(value, type) # rubocop:disable Naming/PredicateName : Used in to many other places to rename at this time, attempting to refactor caused Rubocop to crash. def is_a(value, type) # rubocop:disable Style/PredicateName : Used in to many other places to rename at this time, attempting to refactor caused Rubocop to crash.
# See puppet's lib/puppet/pops/evaluator/evaluator_impl.rb eval_MatchExpression # See puppet's lib/puppet/pops/evaluator/evaluator_impl.rb eval_MatchExpression
Puppet::Pops::Types::TypeCalculator.instance?(type, value) Puppet::Pops::Types::TypeCalculator.instance?(type, value)
end end
......
# frozen_string_literal: true
# @summary
# Wrapper that calls the Puppet 3.x function of the same name.
Puppet::Functions.create_function(:is_absolute_path) do Puppet::Functions.create_function(:is_absolute_path) do
# @param scope
# The main value that will be passed to the wrapped method
#
# @param args
# Any additional values that are to be passed to the wrapped method
#
# @return [Boolea]
# A boolean value returned from the called 3.x function.
dispatch :deprecation_gen do dispatch :deprecation_gen do
param 'Any', :scope param 'Any', :scope
repeated_param 'Any', :args repeated_param 'Any', :args
......
# frozen_string_literal: true
# @summary
# Wrapper that calls the Puppet 3.x function of the same name.
Puppet::Functions.create_function(:is_array) do Puppet::Functions.create_function(:is_array) do
# @param scope
# The main value that will be passed to the wrapped method
#
# @param args
# Any additional values that are to be passed to the wrapped method
#
# @return [Boolea]
# A boolean value returned from the called 3.x function.
dispatch :deprecation_gen do dispatch :deprecation_gen do
param 'Any', :scope param 'Any', :scope
repeated_param 'Any', :args repeated_param 'Any', :args
......
# frozen_string_literal: true
# @summary
# Wrapper that calls the Puppet 3.x function of the same name.
Puppet::Functions.create_function(:is_bool) do Puppet::Functions.create_function(:is_bool) do
# @param scope
# The main value that will be passed to the wrapped method
#
# @param args
# Any additional values that are to be passed to the wrapped method
#
# @return [Boolea]
# A boolean value returned from the called 3.x function.
dispatch :deprecation_gen do dispatch :deprecation_gen do
param 'Any', :scope param 'Any', :scope
repeated_param 'Any', :args repeated_param 'Any', :args
......
# frozen_string_literal: true
# @summary
# Wrapper that calls the Puppet 3.x function of the same name.
Puppet::Functions.create_function(:is_float) do Puppet::Functions.create_function(:is_float) do
# @param scope
# The main value that will be passed to the wrapped method
#
# @param args
# Any additional values that are to be passed to the wrapped method
#
# @return [Boolea]
# A boolean value returned from the called 3.x function.
dispatch :deprecation_gen do dispatch :deprecation_gen do
param 'Any', :scope param 'Any', :scope
repeated_param 'Any', :args repeated_param 'Any', :args
......
# frozen_string_literal: true
# @summary
# Wrapper that calls the Puppet 3.x function of the same name.
Puppet::Functions.create_function(:is_ip_address) do Puppet::Functions.create_function(:is_ip_address) do
# @param scope
# The main value that will be passed to the wrapped method
#
# @param args
# Any additional values that are to be passed to the wrapped method
#
# @return [Boolea]
# A boolean value returned from the called 3.x function.
dispatch :deprecation_gen do dispatch :deprecation_gen do
param 'Any', :scope param 'Any', :scope
repeated_param 'Any', :args repeated_param 'Any', :args
......
# frozen_string_literal: true
# @summary
# Wrapper that calls the Puppet 3.x function of the same name.
Puppet::Functions.create_function(:is_ipv4_address) do Puppet::Functions.create_function(:is_ipv4_address) do
# @param scope
# The main value that will be passed to the wrapped method
#
# @param args
# Any additional values that are to be passed to the wrapped method
#
# @return [Boolea]
# A boolean value returned from the called 3.x function.
dispatch :deprecation_gen do dispatch :deprecation_gen do
param 'Any', :scope param 'Any', :scope
repeated_param 'Any', :args repeated_param 'Any', :args
......
# frozen_string_literal: true
# @summary
# Wrapper that calls the Puppet 3.x function of the same name.
Puppet::Functions.create_function(:is_ipv6_address) do Puppet::Functions.create_function(:is_ipv6_address) do
# @param scope
# The main value that will be passed to the wrapped method
#
# @param args
# Any additional values that are to be passed to the wrapped method
#
# @return [Boolea]
# A boolean value returned from the called 3.x function.
dispatch :deprecation_gen do dispatch :deprecation_gen do
param 'Any', :scope param 'Any', :scope
repeated_param 'Any', :args repeated_param 'Any', :args
......
# frozen_string_literal: true
# @summary
# Wrapper that calls the Puppet 3.x function of the same name.
Puppet::Functions.create_function(:is_numeric) do Puppet::Functions.create_function(:is_numeric) do
# @param scope
# The main value that will be passed to the wrapped method
#
# @param args
# Any additional values that are to be passed to the wrapped method
#
# @return [Boolea]
# A boolean value returned from the called 3.x function.
dispatch :deprecation_gen do dispatch :deprecation_gen do
param 'Any', :scope param 'Any', :scope
repeated_param 'Any', :args repeated_param 'Any', :args
......
# frozen_string_literal: true
# @summary
# Wrapper that calls the Puppet 3.x function of the same name.
Puppet::Functions.create_function(:is_string) do Puppet::Functions.create_function(:is_string) do
# @param scope
# The main value that will be passed to the wrapped method
#
# @param args
# Any additional values that are to be passed to the wrapped method
#
# @return [Boolean]
# A boolean value returned from the called 3.x function.
dispatch :deprecation_gen do dispatch :deprecation_gen do
param 'Any', :scope param 'Any', :scope
repeated_param 'Any', :args repeated_param 'Any', :args
......
# frozen_string_literal: true # A function to eventually replace the old size() function for stdlib
# The original size function did not handle Puppets new type capabilities, so this function is a Puppet 4 compatible solution.
# @summary
# **Deprecated:** A function to eventually replace the old size() function for stdlib
# #
# The original size() function did not handle Puppets new type capabilities, so this function # Note: from Puppet 6.0.0, the compatible function with the same name in Puppet core
# is a Puppet 4 compatible solution. # will be used instead of this function.
#
# > **Note:** **Deprecated** from Puppet 6.0.0, this function has been replaced with a
# built-in [`length`](https://puppet.com/docs/puppet/latest/function.html#length) function.
# #
Puppet::Functions.create_function(:length) do Puppet::Functions.create_function(:length) do
# @param value
# The value whose length is to be found
#
# @return [Integer]
# The length of the given object
dispatch :length do dispatch :length do
param 'Variant[String,Array,Hash]', :value param 'Variant[String,Array,Hash]', :value
end end
......
# frozen_string_literal: true
# @summary
# Merges two or more hashes together or hashes resulting from iteration, and returns
# the resulting hash.
#
# @example Using merge()
# $hash1 = {'one' => 1, 'two', => 2}
# $hash2 = {'two' => 'dos', 'three', => 'tres'}
# $merged_hash = merge($hash1, $hash2) # $merged_hash = {'one' => 1, 'two' => 'dos', 'three' => 'tres'}
#
# When there is a duplicate key, the key in the rightmost hash will "win."
#
# Note that since Puppet 4.0.0 the same merge can be achieved with the + operator.
# `$merged_hash = $hash1 + $hash2`
#
# If merge is given a single Iterable (Array, Hash, etc.) it will call a given block with
# up to three parameters, and merge each resulting Hash into the accumulated result. All other types
# of values returned from the block (typically undef) are skipped (not merged).
#
# The codeblock can take 2 or three parameters:
# * with two, it gets the current hash (as built to this point), and each value (for hash the value is a [key, value] tuple)
# * with three, it gets the current hash (as built to this point), the key/index of each value, and then the value
#
# If the iterable is empty, or no hash was returned from the given block, an empty hash is returned. In the given block, a call to `next()`
# will skip that entry, and a call to `break()` will end the iteration.
#
# @example counting occurrences of strings in an array
# ['a', 'b', 'c', 'c', 'd', 'b'].merge | $hsh, $v | { { $v => $hsh[$v].lest || { 0 } + 1 } } # results in { a => 1, b => 2, c => 2, d => 1 }
#
# @example skipping values for entries that are longer than 1 char
# ['a', 'b', 'c', 'c', 'd', 'b', 'blah', 'blah'].merge | $hsh, $v | { if $v =~ String[1,1] { { $v => $hsh[$v].lest || { 0 } + 1 } } } # results in { a => 1, b => 2, c => 2, d => 1 }
#
# The iterative `merge()` has an advantage over doing the same with a general `reduce()` in that the constructed hash
# does not have to be copied in each iteration and thus will perform much better with large inputs.
Puppet::Functions.create_function(:merge) do
# @param args
# Repeated Param - The hashes that are to be merged
#
# @return
# The merged hash
dispatch :merge2hashes do
repeated_param 'Variant[Hash, Undef, String[0,0]]', :args # this strange type is backwards compatible
return_type 'Hash'
end
# @param args
# Repeated Param - The hashes that are to be merged
#
# @param block
# A block placed on the repeatable param `args`
#
# @return
# The merged hash
dispatch :merge_iterable3 do
repeated_param 'Iterable', :args
block_param 'Callable[3,3]', :block
return_type 'Hash'
end
# @param args
# Repeated Param - The hashes that are to be merged
#
# @param block
# A block placed on the repeatable param `args`
#
# @return
# The merged hash
dispatch :merge_iterable2 do
repeated_param 'Iterable', :args
block_param 'Callable[2,2]', :block
return_type 'Hash'
end
def merge2hashes(*hashes)
accumulator = {}
hashes.each { |h| accumulator.merge!(h) if h.is_a?(Hash) }
accumulator
end
def merge_iterable2(iterable)
accumulator = {}
enum = Puppet::Pops::Types::Iterable.asserted_iterable(self, iterable)
enum.each do |v|
r = yield(accumulator, v)
accumulator.merge!(r) if r.is_a?(Hash)
end
accumulator
end
def merge_iterable3(iterable)
accumulator = {}
enum = Puppet::Pops::Types::Iterable.asserted_iterable(self, iterable)
if enum.hash_style?
enum.each do |entry|
r = yield(accumulator, *entry)
accumulator.merge!(r) if r.is_a?(Hash)
end
else
begin
index = 0
loop do
r = yield(accumulator, index, enum.next)
accumulator.merge!(r) if r.is_a?(Hash)
index += 1
end
rescue StopIteration
end
end
accumulator
end
end
# frozen_string_literal: true # Checks if the OS version is at least a certain version. Note that only the
# major version is taken into account.
# @summary #
# Checks if the OS version is at least a certain version. # Example usage:
# > *Note:*
# Only the major version is taken into account.
# #
# @example Example usage:#
# if os_version_gte('Debian', '9') { } # if os_version_gte('Debian', '9') { }
# if os_version_gte('Ubuntu', '18.04') { } # if os_version_gte('Ubuntu', '18.04') { }
Puppet::Functions.create_function(:os_version_gte) do Puppet::Functions.create_function(:os_version_gte) do
# @param os operating system
# @param version
#
# @return [Boolean] `true` or `false
dispatch :os_version_gte do dispatch :os_version_gte do
param 'String[1]', :os param 'String[1]', :os
param 'String[1]', :version param 'String[1]', :version
......