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,27 @@
|
|
1
|
+
#
|
2
|
+
# is_numeric.rb
|
3
|
+
#
|
4
|
+
|
5
|
+
module Puppet::Parser::Functions
|
6
|
+
newfunction(:is_numeric, :type => :rvalue, :doc => <<-EOS
|
7
|
+
Returns true if the variable passed to this function is a number.
|
8
|
+
EOS
|
9
|
+
) do |arguments|
|
10
|
+
|
11
|
+
if (arguments.size != 1) then
|
12
|
+
raise(Puppet::ParseError, "is_numeric(): Wrong number of arguments "+
|
13
|
+
"given #{arguments.size} for 1")
|
14
|
+
end
|
15
|
+
|
16
|
+
value = arguments[0]
|
17
|
+
|
18
|
+
if value == value.to_f.to_s or value == value.to_i.to_s or value.is_a? Numeric then
|
19
|
+
return true
|
20
|
+
else
|
21
|
+
return false
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
# vim: set ts=2 sw=2 et :
|
@@ -0,0 +1,26 @@
|
|
1
|
+
#
|
2
|
+
# is_string.rb
|
3
|
+
#
|
4
|
+
|
5
|
+
module Puppet::Parser::Functions
|
6
|
+
newfunction(:is_string, :type => :rvalue, :doc => <<-EOS
|
7
|
+
Returns true if the variable passed to this function is a string.
|
8
|
+
EOS
|
9
|
+
) do |arguments|
|
10
|
+
|
11
|
+
raise(Puppet::ParseError, "is_string(): Wrong number of arguments " +
|
12
|
+
"given (#{arguments.size} for 1)") if arguments.size < 1
|
13
|
+
|
14
|
+
type = arguments[0]
|
15
|
+
|
16
|
+
result = type.is_a?(String)
|
17
|
+
|
18
|
+
if result and (type == type.to_f.to_s or type == type.to_i.to_s) then
|
19
|
+
return false
|
20
|
+
end
|
21
|
+
|
22
|
+
return result
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# vim: set ts=2 sw=2 et :
|
@@ -0,0 +1,41 @@
|
|
1
|
+
#
|
2
|
+
# join.rb
|
3
|
+
#
|
4
|
+
|
5
|
+
module Puppet::Parser::Functions
|
6
|
+
newfunction(:join, :type => :rvalue, :doc => <<-EOS
|
7
|
+
This function joins an array into a string using a seperator.
|
8
|
+
|
9
|
+
*Examples:*
|
10
|
+
|
11
|
+
join(['a','b','c'], ",")
|
12
|
+
|
13
|
+
Would result in: "a,b,c"
|
14
|
+
EOS
|
15
|
+
) do |arguments|
|
16
|
+
|
17
|
+
# Technically we support two arguments but only first is mandatory ...
|
18
|
+
raise(Puppet::ParseError, "join(): Wrong number of arguments " +
|
19
|
+
"given (#{arguments.size} for 1)") if arguments.size < 1
|
20
|
+
|
21
|
+
array = arguments[0]
|
22
|
+
|
23
|
+
unless array.is_a?(Array)
|
24
|
+
raise(Puppet::ParseError, 'join(): Requires array to work with')
|
25
|
+
end
|
26
|
+
|
27
|
+
suffix = arguments[1] if arguments[1]
|
28
|
+
|
29
|
+
if suffix
|
30
|
+
unless suffix.is_a?(String)
|
31
|
+
raise(Puppet::ParseError, 'join(): Requires string to work with')
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
result = suffix ? array.join(suffix) : array.join
|
36
|
+
|
37
|
+
return result
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
# vim: set ts=2 sw=2 et :
|
@@ -0,0 +1,47 @@
|
|
1
|
+
#
|
2
|
+
# join.rb
|
3
|
+
#
|
4
|
+
|
5
|
+
module Puppet::Parser::Functions
|
6
|
+
newfunction(:join_keys_to_values, :type => :rvalue, :doc => <<-EOS
|
7
|
+
This function joins each key of a hash to that key's corresponding value with a
|
8
|
+
separator. Keys and values are cast to strings. The return value is an array in
|
9
|
+
which each element is one joined key/value pair.
|
10
|
+
|
11
|
+
*Examples:*
|
12
|
+
|
13
|
+
join_keys_to_values({'a'=>1,'b'=>2}, " is ")
|
14
|
+
|
15
|
+
Would result in: ["a is 1","b is 2"]
|
16
|
+
EOS
|
17
|
+
) do |arguments|
|
18
|
+
|
19
|
+
# Validate the number of arguments.
|
20
|
+
if arguments.size != 2
|
21
|
+
raise(Puppet::ParseError, "join_keys_to_values(): Takes exactly two " +
|
22
|
+
"arguments, but #{arguments.size} given.")
|
23
|
+
end
|
24
|
+
|
25
|
+
# Validate the first argument.
|
26
|
+
hash = arguments[0]
|
27
|
+
if not hash.is_a?(Hash)
|
28
|
+
raise(TypeError, "join_keys_to_values(): The first argument must be a " +
|
29
|
+
"hash, but a #{hash.class} was given.")
|
30
|
+
end
|
31
|
+
|
32
|
+
# Validate the second argument.
|
33
|
+
separator = arguments[1]
|
34
|
+
if not separator.is_a?(String)
|
35
|
+
raise(TypeError, "join_keys_to_values(): The second argument must be a " +
|
36
|
+
"string, but a #{separator.class} was given.")
|
37
|
+
end
|
38
|
+
|
39
|
+
# Join the keys to their values.
|
40
|
+
hash.map do |k,v|
|
41
|
+
String(k) + separator + String(v)
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# vim: set ts=2 sw=2 et :
|
@@ -0,0 +1,26 @@
|
|
1
|
+
#
|
2
|
+
# keys.rb
|
3
|
+
#
|
4
|
+
|
5
|
+
module Puppet::Parser::Functions
|
6
|
+
newfunction(:keys, :type => :rvalue, :doc => <<-EOS
|
7
|
+
Returns the keys of a hash as an array.
|
8
|
+
EOS
|
9
|
+
) do |arguments|
|
10
|
+
|
11
|
+
raise(Puppet::ParseError, "keys(): Wrong number of arguments " +
|
12
|
+
"given (#{arguments.size} for 1)") if arguments.size < 1
|
13
|
+
|
14
|
+
hash = arguments[0]
|
15
|
+
|
16
|
+
unless hash.is_a?(Hash)
|
17
|
+
raise(Puppet::ParseError, 'keys(): Requires hash to work with')
|
18
|
+
end
|
19
|
+
|
20
|
+
result = hash.keys
|
21
|
+
|
22
|
+
return result
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# vim: set ts=2 sw=2 et :
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Puppet::Parser::Functions
|
2
|
+
|
3
|
+
newfunction(:loadyaml, :type => :rvalue, :doc => <<-'ENDHEREDOC') do |args|
|
4
|
+
Load a YAML file containing an array, string, or hash, and return the data
|
5
|
+
in the corresponding native data type.
|
6
|
+
|
7
|
+
For example:
|
8
|
+
|
9
|
+
$myhash = loadyaml('/etc/puppet/data/myhash.yaml')
|
10
|
+
ENDHEREDOC
|
11
|
+
|
12
|
+
unless args.length == 1
|
13
|
+
raise Puppet::ParseError, ("loadyaml(): wrong number of arguments (#{args.length}; must be 1)")
|
14
|
+
end
|
15
|
+
|
16
|
+
YAML.load_file(args[0])
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
#
|
2
|
+
# lstrip.rb
|
3
|
+
#
|
4
|
+
|
5
|
+
module Puppet::Parser::Functions
|
6
|
+
newfunction(:lstrip, :type => :rvalue, :doc => <<-EOS
|
7
|
+
Strips leading spaces to the left of a string.
|
8
|
+
EOS
|
9
|
+
) do |arguments|
|
10
|
+
|
11
|
+
raise(Puppet::ParseError, "lstrip(): Wrong number of arguments " +
|
12
|
+
"given (#{arguments.size} for 1)") if arguments.size < 1
|
13
|
+
|
14
|
+
value = arguments[0]
|
15
|
+
klass = value.class
|
16
|
+
|
17
|
+
unless [Array, String].include?(klass)
|
18
|
+
raise(Puppet::ParseError, 'lstrip(): Requires either ' +
|
19
|
+
'array or string to work with')
|
20
|
+
end
|
21
|
+
|
22
|
+
if value.is_a?(Array)
|
23
|
+
# Numbers in Puppet are often string-encoded which is troublesome ...
|
24
|
+
result = value.collect { |i| i.is_a?(String) ? i.lstrip : i }
|
25
|
+
else
|
26
|
+
result = value.lstrip
|
27
|
+
end
|
28
|
+
|
29
|
+
return result
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
# vim: set ts=2 sw=2 et :
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Puppet::Parser::Functions
|
2
|
+
newfunction(:max, :type => :rvalue, :doc => <<-EOS
|
3
|
+
Returns the highest value of all arguments.
|
4
|
+
Requires at least one argument.
|
5
|
+
EOS
|
6
|
+
) do |args|
|
7
|
+
|
8
|
+
raise(Puppet::ParseError, "max(): Wrong number of arguments " +
|
9
|
+
"need at least one") if args.size == 0
|
10
|
+
|
11
|
+
# Sometimes we get numbers as numerics and sometimes as strings.
|
12
|
+
# We try to compare them as numbers when possible
|
13
|
+
return args.max do |a,b|
|
14
|
+
if a.to_s =~ /\A-?\d+(.\d+)?\z/ and b.to_s =~ /\A-?\d+(.\d+)?\z/ then
|
15
|
+
a.to_f <=> b.to_f
|
16
|
+
else
|
17
|
+
a.to_s <=> b.to_s
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
#
|
2
|
+
# member.rb
|
3
|
+
#
|
4
|
+
|
5
|
+
# TODO(Krzysztof Wilczynski): We need to add support for regular expression ...
|
6
|
+
# TODO(Krzysztof Wilczynski): Support for strings and hashes too ...
|
7
|
+
|
8
|
+
module Puppet::Parser::Functions
|
9
|
+
newfunction(:member, :type => :rvalue, :doc => <<-EOS
|
10
|
+
This function determines if a variable is a member of an array.
|
11
|
+
|
12
|
+
*Examples:*
|
13
|
+
|
14
|
+
member(['a','b'], 'b')
|
15
|
+
|
16
|
+
Would return: true
|
17
|
+
|
18
|
+
member(['a','b'], 'c')
|
19
|
+
|
20
|
+
Would return: false
|
21
|
+
EOS
|
22
|
+
) do |arguments|
|
23
|
+
|
24
|
+
raise(Puppet::ParseError, "member(): Wrong number of arguments " +
|
25
|
+
"given (#{arguments.size} for 2)") if arguments.size < 2
|
26
|
+
|
27
|
+
array = arguments[0]
|
28
|
+
|
29
|
+
unless array.is_a?(Array)
|
30
|
+
raise(Puppet::ParseError, 'member(): Requires array to work with')
|
31
|
+
end
|
32
|
+
|
33
|
+
item = arguments[1]
|
34
|
+
|
35
|
+
raise(Puppet::ParseError, 'member(): You must provide item ' +
|
36
|
+
'to search for within array given') if item.empty?
|
37
|
+
|
38
|
+
result = array.include?(item)
|
39
|
+
|
40
|
+
return result
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# vim: set ts=2 sw=2 et :
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Puppet::Parser::Functions
|
2
|
+
newfunction(:merge, :type => :rvalue, :doc => <<-'ENDHEREDOC') do |args|
|
3
|
+
Merges two or more hashes together and returns the resulting hash.
|
4
|
+
|
5
|
+
For example:
|
6
|
+
|
7
|
+
$hash1 = {'one' => 1, 'two', => 2}
|
8
|
+
$hash2 = {'two' => 'dos', 'three', => 'tres'}
|
9
|
+
$merged_hash = merge($hash1, $hash2)
|
10
|
+
# The resulting hash is equivalent to:
|
11
|
+
# $merged_hash = {'one' => 1, 'two' => 'dos', 'three' => 'tres'}
|
12
|
+
|
13
|
+
When there is a duplicate key, the key in the rightmost hash will "win."
|
14
|
+
|
15
|
+
ENDHEREDOC
|
16
|
+
|
17
|
+
if args.length < 2
|
18
|
+
raise Puppet::ParseError, ("merge(): wrong number of arguments (#{args.length}; must be at least 2)")
|
19
|
+
end
|
20
|
+
|
21
|
+
# The hash we accumulate into
|
22
|
+
accumulator = Hash.new
|
23
|
+
# Merge into the accumulator hash
|
24
|
+
args.each do |arg|
|
25
|
+
unless arg.is_a?(Hash)
|
26
|
+
raise Puppet::ParseError, "merge: unexpected argument type #{arg.class}, only expects hash arguments"
|
27
|
+
end
|
28
|
+
accumulator.merge!(arg)
|
29
|
+
end
|
30
|
+
# Return the fully merged hash
|
31
|
+
accumulator
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Puppet::Parser::Functions
|
2
|
+
newfunction(:min, :type => :rvalue, :doc => <<-EOS
|
3
|
+
Returns the lowest value of all arguments.
|
4
|
+
Requires at least one argument.
|
5
|
+
EOS
|
6
|
+
) do |args|
|
7
|
+
|
8
|
+
raise(Puppet::ParseError, "min(): Wrong number of arguments " +
|
9
|
+
"need at least one") if args.size == 0
|
10
|
+
|
11
|
+
# Sometimes we get numbers as numerics and sometimes as strings.
|
12
|
+
# We try to compare them as numbers when possible
|
13
|
+
return args.min do |a,b|
|
14
|
+
if a.to_s =~ /\A^-?\d+(.\d+)?\z/ and b.to_s =~ /\A-?\d+(.\d+)?\z/ then
|
15
|
+
a.to_f <=> b.to_f
|
16
|
+
else
|
17
|
+
a.to_s <=> b.to_s
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
#
|
2
|
+
# num2bool.rb
|
3
|
+
#
|
4
|
+
|
5
|
+
module Puppet::Parser::Functions
|
6
|
+
newfunction(:num2bool, :type => :rvalue, :doc => <<-EOS
|
7
|
+
This function converts a number or a string representation of a number into a
|
8
|
+
true boolean. Zero or anything non-numeric becomes false. Numbers higher then 0
|
9
|
+
become true.
|
10
|
+
EOS
|
11
|
+
) do |arguments|
|
12
|
+
|
13
|
+
raise(Puppet::ParseError, "num2bool(): Wrong number of arguments " +
|
14
|
+
"given (#{arguments.size} for 1)") if arguments.size != 1
|
15
|
+
|
16
|
+
number = arguments[0]
|
17
|
+
|
18
|
+
case number
|
19
|
+
when Numeric
|
20
|
+
# Yay, it's a number
|
21
|
+
when String
|
22
|
+
begin
|
23
|
+
number = Float(number)
|
24
|
+
rescue ArgumentError => ex
|
25
|
+
raise(Puppet::ParseError, "num2bool(): '#{number}' does not look like a number: #{ex.message}")
|
26
|
+
end
|
27
|
+
else
|
28
|
+
begin
|
29
|
+
number = number.to_s
|
30
|
+
rescue NoMethodError => ex
|
31
|
+
raise(Puppet::ParseError, "num2bool(): Unable to parse argument: #{ex.message}")
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# Truncate Floats
|
36
|
+
number = number.to_i
|
37
|
+
|
38
|
+
# Return true for any positive number and false otherwise
|
39
|
+
return number > 0
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
# vim: set ts=2 sw=2 et :
|
@@ -0,0 +1,24 @@
|
|
1
|
+
#
|
2
|
+
# parsejson.rb
|
3
|
+
#
|
4
|
+
|
5
|
+
module Puppet::Parser::Functions
|
6
|
+
newfunction(:parsejson, :type => :rvalue, :doc => <<-EOS
|
7
|
+
This function accepts JSON as a string and converts into the correct Puppet
|
8
|
+
structure.
|
9
|
+
EOS
|
10
|
+
) do |arguments|
|
11
|
+
|
12
|
+
if (arguments.size != 1) then
|
13
|
+
raise(Puppet::ParseError, "parsejson(): Wrong number of arguments "+
|
14
|
+
"given #{arguments.size} for 1")
|
15
|
+
end
|
16
|
+
|
17
|
+
json = arguments[0]
|
18
|
+
|
19
|
+
# PSON is natively available in puppet
|
20
|
+
PSON.load(json)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# vim: set ts=2 sw=2 et :
|
@@ -0,0 +1,24 @@
|
|
1
|
+
#
|
2
|
+
# parseyaml.rb
|
3
|
+
#
|
4
|
+
|
5
|
+
module Puppet::Parser::Functions
|
6
|
+
newfunction(:parseyaml, :type => :rvalue, :doc => <<-EOS
|
7
|
+
This function accepts YAML as a string and converts it into the correct
|
8
|
+
Puppet structure.
|
9
|
+
EOS
|
10
|
+
) do |arguments|
|
11
|
+
|
12
|
+
if (arguments.size != 1) then
|
13
|
+
raise(Puppet::ParseError, "parseyaml(): Wrong number of arguments "+
|
14
|
+
"given #{arguments.size} for 1")
|
15
|
+
end
|
16
|
+
|
17
|
+
require 'yaml'
|
18
|
+
|
19
|
+
YAML::load(arguments[0])
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# vim: set ts=2 sw=2 et :
|