puppetmodule-stdlib 4.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +253 -0
- data/CONTRIBUTING.md +65 -0
- data/Gemfile +33 -0
- data/LICENSE +19 -0
- data/Modulefile +11 -0
- data/README.markdown +1173 -0
- data/README_DEVELOPER.markdown +35 -0
- data/RELEASE_PROCESS.markdown +24 -0
- data/Rakefile +2 -0
- data/lib/facter/pe_version.rb +53 -0
- data/lib/facter/puppet_vardir.rb +26 -0
- data/lib/facter/root_home.rb +19 -0
- data/lib/facter/util/puppet_settings.rb +21 -0
- data/lib/puppet/parser/functions/abs.rb +36 -0
- data/lib/puppet/parser/functions/any2array.rb +33 -0
- data/lib/puppet/parser/functions/bool2num.rb +49 -0
- data/lib/puppet/parser/functions/capitalize.rb +34 -0
- data/lib/puppet/parser/functions/chomp.rb +35 -0
- data/lib/puppet/parser/functions/chop.rb +37 -0
- data/lib/puppet/parser/functions/concat.rb +37 -0
- data/lib/puppet/parser/functions/count.rb +22 -0
- data/lib/puppet/parser/functions/defined_with_params.rb +35 -0
- data/lib/puppet/parser/functions/delete.rb +46 -0
- data/lib/puppet/parser/functions/delete_at.rb +49 -0
- data/lib/puppet/parser/functions/downcase.rb +33 -0
- data/lib/puppet/parser/functions/empty.rb +28 -0
- data/lib/puppet/parser/functions/ensure_packages.rb +24 -0
- data/lib/puppet/parser/functions/ensure_resource.rb +35 -0
- data/lib/puppet/parser/functions/flatten.rb +33 -0
- data/lib/puppet/parser/functions/floor.rb +20 -0
- data/lib/puppet/parser/functions/fqdn_rotate.rb +46 -0
- data/lib/puppet/parser/functions/get_module_path.rb +17 -0
- data/lib/puppet/parser/functions/getparam.rb +35 -0
- data/lib/puppet/parser/functions/getvar.rb +26 -0
- data/lib/puppet/parser/functions/grep.rb +33 -0
- data/lib/puppet/parser/functions/has_interface_with.rb +52 -0
- data/lib/puppet/parser/functions/has_ip_address.rb +25 -0
- data/lib/puppet/parser/functions/has_ip_network.rb +25 -0
- data/lib/puppet/parser/functions/has_key.rb +28 -0
- data/lib/puppet/parser/functions/hash.rb +41 -0
- data/lib/puppet/parser/functions/is_array.rb +22 -0
- data/lib/puppet/parser/functions/is_domain_name.rb +47 -0
- data/lib/puppet/parser/functions/is_float.rb +27 -0
- data/lib/puppet/parser/functions/is_function_available.rb +23 -0
- data/lib/puppet/parser/functions/is_hash.rb +22 -0
- data/lib/puppet/parser/functions/is_integer.rb +27 -0
- data/lib/puppet/parser/functions/is_ip_address.rb +32 -0
- data/lib/puppet/parser/functions/is_mac_address.rb +27 -0
- data/lib/puppet/parser/functions/is_numeric.rb +27 -0
- data/lib/puppet/parser/functions/is_string.rb +26 -0
- data/lib/puppet/parser/functions/join.rb +41 -0
- data/lib/puppet/parser/functions/join_keys_to_values.rb +47 -0
- data/lib/puppet/parser/functions/keys.rb +26 -0
- data/lib/puppet/parser/functions/loadyaml.rb +20 -0
- data/lib/puppet/parser/functions/lstrip.rb +33 -0
- data/lib/puppet/parser/functions/max.rb +21 -0
- data/lib/puppet/parser/functions/member.rb +44 -0
- data/lib/puppet/parser/functions/merge.rb +33 -0
- data/lib/puppet/parser/functions/min.rb +21 -0
- data/lib/puppet/parser/functions/num2bool.rb +43 -0
- data/lib/puppet/parser/functions/parsejson.rb +24 -0
- data/lib/puppet/parser/functions/parseyaml.rb +24 -0
- data/lib/puppet/parser/functions/pick.rb +29 -0
- data/lib/puppet/parser/functions/prefix.rb +45 -0
- data/lib/puppet/parser/functions/range.rb +80 -0
- data/lib/puppet/parser/functions/reject.rb +31 -0
- data/lib/puppet/parser/functions/reverse.rb +28 -0
- data/lib/puppet/parser/functions/rstrip.rb +32 -0
- data/lib/puppet/parser/functions/shuffle.rb +46 -0
- data/lib/puppet/parser/functions/size.rb +48 -0
- data/lib/puppet/parser/functions/sort.rb +27 -0
- data/lib/puppet/parser/functions/squeeze.rb +36 -0
- data/lib/puppet/parser/functions/str2bool.rb +46 -0
- data/lib/puppet/parser/functions/str2saltedsha512.rb +32 -0
- data/lib/puppet/parser/functions/strftime.rb +107 -0
- data/lib/puppet/parser/functions/strip.rb +39 -0
- data/lib/puppet/parser/functions/suffix.rb +45 -0
- data/lib/puppet/parser/functions/swapcase.rb +39 -0
- data/lib/puppet/parser/functions/time.rb +49 -0
- data/lib/puppet/parser/functions/to_bytes.rb +28 -0
- data/lib/puppet/parser/functions/type.rb +50 -0
- data/lib/puppet/parser/functions/unique.rb +51 -0
- data/lib/puppet/parser/functions/upcase.rb +41 -0
- data/lib/puppet/parser/functions/uriescape.rb +36 -0
- data/lib/puppet/parser/functions/validate_absolute_path.rb +56 -0
- data/lib/puppet/parser/functions/validate_array.rb +33 -0
- data/lib/puppet/parser/functions/validate_augeas.rb +81 -0
- data/lib/puppet/parser/functions/validate_bool.rb +34 -0
- data/lib/puppet/parser/functions/validate_cmd.rb +47 -0
- data/lib/puppet/parser/functions/validate_hash.rb +33 -0
- data/lib/puppet/parser/functions/validate_re.rb +40 -0
- data/lib/puppet/parser/functions/validate_slength.rb +52 -0
- data/lib/puppet/parser/functions/validate_string.rb +33 -0
- data/lib/puppet/parser/functions/values.rb +39 -0
- data/lib/puppet/parser/functions/values_at.rb +98 -0
- data/lib/puppet/parser/functions/zip.rb +65 -0
- data/lib/puppet/provider/file_line/ruby.rb +59 -0
- data/lib/puppet/type/anchor.rb +41 -0
- data/lib/puppet/type/file_line.rb +70 -0
- data/manifests/init.pp +20 -0
- data/manifests/stages.pp +43 -0
- data/spec/functions/defined_with_params_spec.rb +37 -0
- data/spec/functions/ensure_packages_spec.rb +42 -0
- data/spec/functions/ensure_resource_spec.rb +40 -0
- data/spec/functions/getparam_spec.rb +34 -0
- data/spec/monkey_patches/alias_should_to_must.rb +8 -0
- data/spec/monkey_patches/publicize_methods.rb +10 -0
- data/spec/spec.opts +6 -0
- data/spec/spec_helper.rb +28 -0
- data/spec/unit/facter/pe_version_spec.rb +76 -0
- data/spec/unit/facter/root_home_spec.rb +40 -0
- data/spec/unit/facter/util/puppet_settings_spec.rb +35 -0
- data/spec/unit/puppet/parser/functions/abs_spec.rb +25 -0
- data/spec/unit/puppet/parser/functions/any2array_spec.rb +55 -0
- data/spec/unit/puppet/parser/functions/bool2num_spec.rb +24 -0
- data/spec/unit/puppet/parser/functions/capitalize_spec.rb +19 -0
- data/spec/unit/puppet/parser/functions/chomp_spec.rb +19 -0
- data/spec/unit/puppet/parser/functions/chop_spec.rb +19 -0
- data/spec/unit/puppet/parser/functions/concat_spec.rb +15 -0
- data/spec/unit/puppet/parser/functions/count_spec.rb +31 -0
- data/spec/unit/puppet/parser/functions/delete_at_spec.rb +19 -0
- data/spec/unit/puppet/parser/functions/delete_spec.rb +38 -0
- data/spec/unit/puppet/parser/functions/downcase_spec.rb +24 -0
- data/spec/unit/puppet/parser/functions/empty_spec.rb +23 -0
- data/spec/unit/puppet/parser/functions/flatten_spec.rb +27 -0
- data/spec/unit/puppet/parser/functions/floor_spec.rb +39 -0
- data/spec/unit/puppet/parser/functions/fqdn_rotate_spec.rb +33 -0
- data/spec/unit/puppet/parser/functions/get_module_path_spec.rb +46 -0
- data/spec/unit/puppet/parser/functions/getvar_spec.rb +37 -0
- data/spec/unit/puppet/parser/functions/grep_spec.rb +19 -0
- data/spec/unit/puppet/parser/functions/has_interface_with_spec.rb +64 -0
- data/spec/unit/puppet/parser/functions/has_ip_address_spec.rb +39 -0
- data/spec/unit/puppet/parser/functions/has_ip_network_spec.rb +36 -0
- data/spec/unit/puppet/parser/functions/has_key_spec.rb +42 -0
- data/spec/unit/puppet/parser/functions/hash_spec.rb +19 -0
- data/spec/unit/puppet/parser/functions/is_array_spec.rb +29 -0
- data/spec/unit/puppet/parser/functions/is_domain_name_spec.rb +64 -0
- data/spec/unit/puppet/parser/functions/is_float_spec.rb +33 -0
- data/spec/unit/puppet/parser/functions/is_function_available.rb +31 -0
- data/spec/unit/puppet/parser/functions/is_hash_spec.rb +29 -0
- data/spec/unit/puppet/parser/functions/is_integer_spec.rb +34 -0
- data/spec/unit/puppet/parser/functions/is_ip_address_spec.rb +39 -0
- data/spec/unit/puppet/parser/functions/is_mac_address_spec.rb +29 -0
- data/spec/unit/puppet/parser/functions/is_numeric_spec.rb +39 -0
- data/spec/unit/puppet/parser/functions/is_string_spec.rb +34 -0
- data/spec/unit/puppet/parser/functions/join_keys_to_values_spec.rb +40 -0
- data/spec/unit/puppet/parser/functions/join_spec.rb +19 -0
- data/spec/unit/puppet/parser/functions/keys_spec.rb +21 -0
- data/spec/unit/puppet/parser/functions/lstrip_spec.rb +19 -0
- data/spec/unit/puppet/parser/functions/max_spec.rb +27 -0
- data/spec/unit/puppet/parser/functions/member_spec.rb +24 -0
- data/spec/unit/puppet/parser/functions/merge_spec.rb +47 -0
- data/spec/unit/puppet/parser/functions/min_spec.rb +27 -0
- data/spec/unit/puppet/parser/functions/num2bool_spec.rb +67 -0
- data/spec/unit/puppet/parser/functions/parsejson_spec.rb +22 -0
- data/spec/unit/puppet/parser/functions/parseyaml_spec.rb +24 -0
- data/spec/unit/puppet/parser/functions/pick_spec.rb +34 -0
- data/spec/unit/puppet/parser/functions/prefix_spec.rb +19 -0
- data/spec/unit/puppet/parser/functions/range_spec.rb +34 -0
- data/spec/unit/puppet/parser/functions/reject_spec.rb +20 -0
- data/spec/unit/puppet/parser/functions/reverse_spec.rb +19 -0
- data/spec/unit/puppet/parser/functions/rstrip_spec.rb +24 -0
- data/spec/unit/puppet/parser/functions/shuffle_spec.rb +24 -0
- data/spec/unit/puppet/parser/functions/size_spec.rb +24 -0
- data/spec/unit/puppet/parser/functions/sort_spec.rb +24 -0
- data/spec/unit/puppet/parser/functions/squeeze_spec.rb +24 -0
- data/spec/unit/puppet/parser/functions/str2bool_spec.rb +31 -0
- data/spec/unit/puppet/parser/functions/str2saltedsha512_spec.rb +45 -0
- data/spec/unit/puppet/parser/functions/strftime_spec.rb +29 -0
- data/spec/unit/puppet/parser/functions/strip_spec.rb +18 -0
- data/spec/unit/puppet/parser/functions/suffix_spec.rb +19 -0
- data/spec/unit/puppet/parser/functions/swapcase_spec.rb +19 -0
- data/spec/unit/puppet/parser/functions/time_spec.rb +29 -0
- data/spec/unit/puppet/parser/functions/to_bytes_spec.rb +58 -0
- data/spec/unit/puppet/parser/functions/type_spec.rb +43 -0
- data/spec/unit/puppet/parser/functions/unique_spec.rb +24 -0
- data/spec/unit/puppet/parser/functions/upcase_spec.rb +24 -0
- data/spec/unit/puppet/parser/functions/uriescape_spec.rb +24 -0
- data/spec/unit/puppet/parser/functions/validate_absolute_path_spec.rb +83 -0
- data/spec/unit/puppet/parser/functions/validate_array_spec.rb +38 -0
- data/spec/unit/puppet/parser/functions/validate_augeas_spec.rb +102 -0
- data/spec/unit/puppet/parser/functions/validate_bool_spec.rb +51 -0
- data/spec/unit/puppet/parser/functions/validate_cmd_spec.rb +81 -0
- data/spec/unit/puppet/parser/functions/validate_hash_spec.rb +43 -0
- data/spec/unit/puppet/parser/functions/validate_re_spec.rb +76 -0
- data/spec/unit/puppet/parser/functions/validate_slength_spec.rb +48 -0
- data/spec/unit/puppet/parser/functions/validate_string_spec.rb +60 -0
- data/spec/unit/puppet/parser/functions/values_at_spec.rb +38 -0
- data/spec/unit/puppet/parser/functions/values_spec.rb +31 -0
- data/spec/unit/puppet/parser/functions/zip_spec.rb +15 -0
- data/spec/unit/puppet/provider/file_line/ruby_spec.rb +127 -0
- data/spec/unit/puppet/type/anchor_spec.rb +11 -0
- data/spec/unit/puppet/type/file_line_spec.rb +69 -0
- data/spec/watchr.rb +86 -0
- data/tests/file_line.pp +9 -0
- data/tests/has_interface_with.pp +10 -0
- data/tests/has_ip_address.pp +3 -0
- data/tests/has_ip_network.pp +4 -0
- data/tests/init.pp +1 -0
- metadata +252 -0
@@ -0,0 +1,35 @@
|
|
1
|
+
Puppet Specific Facts
|
2
|
+
=====================
|
3
|
+
|
4
|
+
Facter is meant to stand alone and apart from Puppet. However, Facter often
|
5
|
+
runs inside Puppet and all custom facts included in the stdlib module will
|
6
|
+
almost always be evaluated in the context of Puppet and Facter working
|
7
|
+
together.
|
8
|
+
|
9
|
+
Still, we don't want to write custom facts that blow up in the users face if
|
10
|
+
Puppet is not loaded in memory. This is often the case if the user runs
|
11
|
+
`facter` without also supplying the `--puppet` flag.
|
12
|
+
|
13
|
+
Ah! But Jeff, the custom fact won't be in the `$LOAD_PATH` unless the user
|
14
|
+
supplies `--facter`! You might say...
|
15
|
+
|
16
|
+
Not (always) true I say! If the user happens to have a CWD of
|
17
|
+
`<modulepath>/stdlib/lib` then the facts will automatically be evaluated and
|
18
|
+
blow up.
|
19
|
+
|
20
|
+
In any event, it's pretty easy to write a fact that has no value if Puppet is
|
21
|
+
not loaded. Simply do it like this:
|
22
|
+
|
23
|
+
Facter.add(:node_vardir) do
|
24
|
+
setcode do
|
25
|
+
# This will be nil if Puppet is not available.
|
26
|
+
Facter::Util::PuppetSettings.with_puppet do
|
27
|
+
Puppet[:vardir]
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
The `Facter::Util::PuppetSettings.with_puppet` method accepts a block and
|
33
|
+
yields to it only if the Puppet library is loaded. If the Puppet library is
|
34
|
+
not loaded, then the method silently returns `nil` which Facter interprets as
|
35
|
+
an undefined fact value. The net effect is that the fact won't be set.
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# Contributing to this module #
|
2
|
+
|
3
|
+
* Work in a topic branch
|
4
|
+
* Submit a github pull request
|
5
|
+
* Address any comments / feeback
|
6
|
+
* Merge into master using --no-ff
|
7
|
+
|
8
|
+
# Releasing this module #
|
9
|
+
|
10
|
+
* This module adheres to http://semver.org/
|
11
|
+
* Look for API breaking changes using git diff vX.Y.Z..master
|
12
|
+
* If no API breaking changes, the minor version may be bumped.
|
13
|
+
* If there are API breaking changes, the major version must be bumped.
|
14
|
+
* If there are only small minor changes, the patch version may be bumped.
|
15
|
+
* Update the CHANGELOG
|
16
|
+
* Update the Modulefile
|
17
|
+
* Commit these changes with a message along the lines of "Update CHANGELOG and
|
18
|
+
Modulefile for release"
|
19
|
+
* Create an annotated tag with git tag -a vX.Y.Z -m 'version X.Y.Z' (NOTE the
|
20
|
+
leading v as per semver.org)
|
21
|
+
* Push the tag with git push origin --tags
|
22
|
+
* Build a new package with puppet-module or the rake build task if it exists
|
23
|
+
* Publish the new package to the forge
|
24
|
+
* Bonus points for an announcement to puppet-users.
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# Fact: is_pe, pe_version, pe_major_version, pe_minor_version, pe_patch_version
|
2
|
+
#
|
3
|
+
# Purpose: Return various facts about the PE state of the system
|
4
|
+
#
|
5
|
+
# Resolution: Uses a regex match against puppetversion to determine whether the
|
6
|
+
# machine has Puppet Enterprise installed, and what version (overall, major,
|
7
|
+
# minor, patch) is installed.
|
8
|
+
#
|
9
|
+
# Caveats:
|
10
|
+
#
|
11
|
+
Facter.add("pe_version") do
|
12
|
+
setcode do
|
13
|
+
pe_ver = Facter.value("puppetversion").match(/Puppet Enterprise (\d+\.\d+\.\d+)/)
|
14
|
+
pe_ver[1] if pe_ver
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
Facter.add("is_pe") do
|
19
|
+
setcode do
|
20
|
+
if Facter.value(:pe_version).to_s.empty? then
|
21
|
+
false
|
22
|
+
else
|
23
|
+
true
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
Facter.add("pe_major_version") do
|
29
|
+
confine :is_pe => true
|
30
|
+
setcode do
|
31
|
+
if pe_version = Facter.value(:pe_version)
|
32
|
+
pe_version.to_s.split('.')[0]
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
Facter.add("pe_minor_version") do
|
38
|
+
confine :is_pe => true
|
39
|
+
setcode do
|
40
|
+
if pe_version = Facter.value(:pe_version)
|
41
|
+
pe_version.to_s.split('.')[1]
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
Facter.add("pe_patch_version") do
|
47
|
+
confine :is_pe => true
|
48
|
+
setcode do
|
49
|
+
if pe_version = Facter.value(:pe_version)
|
50
|
+
pe_version.to_s.split('.')[2]
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# This facter fact returns the value of the Puppet vardir setting for the node
|
2
|
+
# running puppet or puppet agent. The intent is to enable Puppet modules to
|
3
|
+
# automatically have insight into a place where they can place variable data,
|
4
|
+
# regardless of the node's platform.
|
5
|
+
#
|
6
|
+
# The value should be directly usable in a File resource path attribute.
|
7
|
+
|
8
|
+
|
9
|
+
begin
|
10
|
+
require 'facter/util/puppet_settings'
|
11
|
+
rescue LoadError => e
|
12
|
+
# puppet apply does not add module lib directories to the $LOAD_PATH (See
|
13
|
+
# #4248). It should (in the future) but for the time being we need to be
|
14
|
+
# defensive which is what this rescue block is doing.
|
15
|
+
rb_file = File.join(File.dirname(__FILE__), 'util', 'puppet_settings.rb')
|
16
|
+
load rb_file if File.exists?(rb_file) or raise e
|
17
|
+
end
|
18
|
+
|
19
|
+
Facter.add(:puppet_vardir) do
|
20
|
+
setcode do
|
21
|
+
# This will be nil if Puppet is not available.
|
22
|
+
Facter::Util::PuppetSettings.with_puppet do
|
23
|
+
Puppet[:vardir]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# A facter fact to determine the root home directory.
|
2
|
+
# This varies on PE supported platforms and may be
|
3
|
+
# reconfigured by the end user.
|
4
|
+
|
5
|
+
module Facter::Util::RootHome
|
6
|
+
class << self
|
7
|
+
def get_root_home
|
8
|
+
root_ent = Facter::Util::Resolution.exec("getent passwd root")
|
9
|
+
# The home directory is the sixth element in the passwd entry
|
10
|
+
# If the platform doesn't have getent, root_ent will be nil and we should
|
11
|
+
# return it straight away.
|
12
|
+
root_ent && root_ent.split(":")[5]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
Facter.add(:root_home) do
|
18
|
+
setcode { Facter::Util::RootHome.get_root_home }
|
19
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Facter
|
2
|
+
module Util
|
3
|
+
module PuppetSettings
|
4
|
+
# This method is intended to provide a convenient way to evaluate a
|
5
|
+
# Facter code block only if Puppet is loaded. This is to account for the
|
6
|
+
# situation where the fact happens to be in the load path, but Puppet is
|
7
|
+
# not loaded for whatever reason. Perhaps the user is simply running
|
8
|
+
# facter without the --puppet flag and they happen to be working in a lib
|
9
|
+
# directory of a module.
|
10
|
+
def self.with_puppet
|
11
|
+
begin
|
12
|
+
Module.const_get("Puppet")
|
13
|
+
rescue NameError
|
14
|
+
nil
|
15
|
+
else
|
16
|
+
yield
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
#
|
2
|
+
# abs.rb
|
3
|
+
#
|
4
|
+
|
5
|
+
module Puppet::Parser::Functions
|
6
|
+
newfunction(:abs, :type => :rvalue, :doc => <<-EOS
|
7
|
+
Returns the absolute value of a number, for example -34.56 becomes
|
8
|
+
34.56. Takes a single integer and float value as an argument.
|
9
|
+
EOS
|
10
|
+
) do |arguments|
|
11
|
+
|
12
|
+
raise(Puppet::ParseError, "abs(): Wrong number of arguments " +
|
13
|
+
"given (#{arguments.size} for 1)") if arguments.size < 1
|
14
|
+
|
15
|
+
value = arguments[0]
|
16
|
+
|
17
|
+
# Numbers in Puppet are often string-encoded which is troublesome ...
|
18
|
+
if value.is_a?(String)
|
19
|
+
if value.match(/^-?(?:\d+)(?:\.\d+){1}$/)
|
20
|
+
value = value.to_f
|
21
|
+
elsif value.match(/^-?\d+$/)
|
22
|
+
value = value.to_i
|
23
|
+
else
|
24
|
+
raise(Puppet::ParseError, 'abs(): Requires float or ' +
|
25
|
+
'integer to work with')
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# We have numeric value to handle ...
|
30
|
+
result = value.abs
|
31
|
+
|
32
|
+
return result
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
# vim: set ts=2 sw=2 et :
|
@@ -0,0 +1,33 @@
|
|
1
|
+
#
|
2
|
+
# any2array.rb
|
3
|
+
#
|
4
|
+
|
5
|
+
module Puppet::Parser::Functions
|
6
|
+
newfunction(:any2array, :type => :rvalue, :doc => <<-EOS
|
7
|
+
This converts any object to an array containing that object. Empty argument
|
8
|
+
lists are converted to an empty array. Arrays are left untouched. Hashes are
|
9
|
+
converted to arrays of alternating keys and values.
|
10
|
+
EOS
|
11
|
+
) do |arguments|
|
12
|
+
|
13
|
+
if arguments.empty?
|
14
|
+
return []
|
15
|
+
end
|
16
|
+
|
17
|
+
if arguments.length == 1
|
18
|
+
if arguments[0].kind_of?(Array)
|
19
|
+
return arguments[0]
|
20
|
+
elsif arguments[0].kind_of?(Hash)
|
21
|
+
result = []
|
22
|
+
arguments[0].each do |key, value|
|
23
|
+
result << key << value
|
24
|
+
end
|
25
|
+
return result
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
return arguments
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
# vim: set ts=2 sw=2 et :
|
@@ -0,0 +1,49 @@
|
|
1
|
+
#
|
2
|
+
# bool2num.rb
|
3
|
+
#
|
4
|
+
|
5
|
+
module Puppet::Parser::Functions
|
6
|
+
newfunction(:bool2num, :type => :rvalue, :doc => <<-EOS
|
7
|
+
Converts a boolean to a number. Converts the values:
|
8
|
+
false, f, 0, n, and no to 0
|
9
|
+
true, t, 1, y, and yes to 1
|
10
|
+
Requires a single boolean or string as an input.
|
11
|
+
EOS
|
12
|
+
) do |arguments|
|
13
|
+
|
14
|
+
raise(Puppet::ParseError, "bool2num(): Wrong number of arguments " +
|
15
|
+
"given (#{arguments.size} for 1)") if arguments.size < 1
|
16
|
+
|
17
|
+
value = arguments[0]
|
18
|
+
klass = value.class
|
19
|
+
|
20
|
+
# We can have either true or false, or string which resembles boolean ...
|
21
|
+
unless [FalseClass, TrueClass, String].include?(klass)
|
22
|
+
raise(Puppet::ParseError, 'bool2num(): Requires either ' +
|
23
|
+
'boolean or string to work with')
|
24
|
+
end
|
25
|
+
|
26
|
+
if value.is_a?(String)
|
27
|
+
# We consider all the yes, no, y, n and so on too ...
|
28
|
+
value = case value
|
29
|
+
#
|
30
|
+
# This is how undef looks like in Puppet ...
|
31
|
+
# We yield 0 (or false if you wish) in this case.
|
32
|
+
#
|
33
|
+
when /^$/, '' then false # Empty string will be false ...
|
34
|
+
when /^(1|t|y|true|yes)$/ then true
|
35
|
+
when /^(0|f|n|false|no)$/ then false
|
36
|
+
when /^(undef|undefined)$/ then false # This is not likely to happen ...
|
37
|
+
else
|
38
|
+
raise(Puppet::ParseError, 'bool2num(): Unknown type of boolean given')
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# We have real boolean values as well ...
|
43
|
+
result = value ? 1 : 0
|
44
|
+
|
45
|
+
return result
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# vim: set ts=2 sw=2 et :
|
@@ -0,0 +1,34 @@
|
|
1
|
+
#
|
2
|
+
# capitalize.rb
|
3
|
+
#
|
4
|
+
|
5
|
+
module Puppet::Parser::Functions
|
6
|
+
newfunction(:capitalize, :type => :rvalue, :doc => <<-EOS
|
7
|
+
Capitalizes the first letter of a string or array of strings.
|
8
|
+
Requires either a single string or an array as an input.
|
9
|
+
EOS
|
10
|
+
) do |arguments|
|
11
|
+
|
12
|
+
raise(Puppet::ParseError, "capitalize(): Wrong number of arguments " +
|
13
|
+
"given (#{arguments.size} for 1)") if arguments.size < 1
|
14
|
+
|
15
|
+
value = arguments[0]
|
16
|
+
klass = value.class
|
17
|
+
|
18
|
+
unless [Array, String].include?(klass)
|
19
|
+
raise(Puppet::ParseError, 'capitalize(): Requires either ' +
|
20
|
+
'array or string to work with')
|
21
|
+
end
|
22
|
+
|
23
|
+
if value.is_a?(Array)
|
24
|
+
# Numbers in Puppet are often string-encoded which is troublesome ...
|
25
|
+
result = value.collect { |i| i.is_a?(String) ? i.capitalize : i }
|
26
|
+
else
|
27
|
+
result = value.capitalize
|
28
|
+
end
|
29
|
+
|
30
|
+
return result
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
# vim: set ts=2 sw=2 et :
|
@@ -0,0 +1,35 @@
|
|
1
|
+
#
|
2
|
+
# chomp.rb
|
3
|
+
#
|
4
|
+
|
5
|
+
module Puppet::Parser::Functions
|
6
|
+
newfunction(:chomp, :type => :rvalue, :doc => <<-'EOS'
|
7
|
+
Removes the record separator from the end of a string or an array of
|
8
|
+
strings, for example `hello\n` becomes `hello`.
|
9
|
+
Requires a single string or array as an input.
|
10
|
+
EOS
|
11
|
+
) do |arguments|
|
12
|
+
|
13
|
+
raise(Puppet::ParseError, "chomp(): Wrong number of arguments " +
|
14
|
+
"given (#{arguments.size} for 1)") if arguments.size < 1
|
15
|
+
|
16
|
+
value = arguments[0]
|
17
|
+
klass = value.class
|
18
|
+
|
19
|
+
unless [Array, String].include?(klass)
|
20
|
+
raise(Puppet::ParseError, 'chomp(): Requires either ' +
|
21
|
+
'array or string to work with')
|
22
|
+
end
|
23
|
+
|
24
|
+
if value.is_a?(Array)
|
25
|
+
# Numbers in Puppet are often string-encoded which is troublesome ...
|
26
|
+
result = value.collect { |i| i.is_a?(String) ? i.chomp : i }
|
27
|
+
else
|
28
|
+
result = value.chomp
|
29
|
+
end
|
30
|
+
|
31
|
+
return result
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# vim: set ts=2 sw=2 et :
|
@@ -0,0 +1,37 @@
|
|
1
|
+
#
|
2
|
+
# chop.rb
|
3
|
+
#
|
4
|
+
|
5
|
+
module Puppet::Parser::Functions
|
6
|
+
newfunction(:chop, :type => :rvalue, :doc => <<-'EOS'
|
7
|
+
Returns a new string with the last character removed. If the string ends
|
8
|
+
with `\r\n`, both characters are removed. Applying chop to an empty
|
9
|
+
string returns an empty string. If you wish to merely remove record
|
10
|
+
separators then you should use the `chomp` function.
|
11
|
+
Requires a string or array of strings as input.
|
12
|
+
EOS
|
13
|
+
) do |arguments|
|
14
|
+
|
15
|
+
raise(Puppet::ParseError, "chop(): Wrong number of arguments " +
|
16
|
+
"given (#{arguments.size} for 1)") if arguments.size < 1
|
17
|
+
|
18
|
+
value = arguments[0]
|
19
|
+
klass = value.class
|
20
|
+
|
21
|
+
unless [Array, String].include?(klass)
|
22
|
+
raise(Puppet::ParseError, 'chop(): Requires either an ' +
|
23
|
+
'array or string to work with')
|
24
|
+
end
|
25
|
+
|
26
|
+
if value.is_a?(Array)
|
27
|
+
# Numbers in Puppet are often string-encoded which is troublesome ...
|
28
|
+
result = value.collect { |i| i.is_a?(String) ? i.chop : i }
|
29
|
+
else
|
30
|
+
result = value.chop
|
31
|
+
end
|
32
|
+
|
33
|
+
return result
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
# vim: set ts=2 sw=2 et :
|
@@ -0,0 +1,37 @@
|
|
1
|
+
#
|
2
|
+
# concat.rb
|
3
|
+
#
|
4
|
+
|
5
|
+
module Puppet::Parser::Functions
|
6
|
+
newfunction(:concat, :type => :rvalue, :doc => <<-EOS
|
7
|
+
Appends the contents of array 2 onto array 1.
|
8
|
+
|
9
|
+
*Example:*
|
10
|
+
|
11
|
+
concat(['1','2','3'],['4','5','6'])
|
12
|
+
|
13
|
+
Would result in:
|
14
|
+
|
15
|
+
['1','2','3','4','5','6']
|
16
|
+
EOS
|
17
|
+
) do |arguments|
|
18
|
+
|
19
|
+
# Check that 2 arguments have been given ...
|
20
|
+
raise(Puppet::ParseError, "concat(): Wrong number of arguments " +
|
21
|
+
"given (#{arguments.size} for 2)") if arguments.size != 2
|
22
|
+
|
23
|
+
a = arguments[0]
|
24
|
+
b = arguments[1]
|
25
|
+
|
26
|
+
# Check that both args are arrays.
|
27
|
+
unless a.is_a?(Array) and b.is_a?(Array)
|
28
|
+
raise(Puppet::ParseError, 'concat(): Requires array to work with')
|
29
|
+
end
|
30
|
+
|
31
|
+
result = a.concat(b)
|
32
|
+
|
33
|
+
return result
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
# vim: set ts=2 sw=2 et :
|