puppetmodule-stdlib 4.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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,22 @@
|
|
|
1
|
+
module Puppet::Parser::Functions
|
|
2
|
+
newfunction(:count, :type => :rvalue, :arity => -2, :doc => <<-EOS
|
|
3
|
+
Takes an array as first argument and an optional second argument.
|
|
4
|
+
Count the number of elements in array that matches second argument.
|
|
5
|
+
If called with only an array it counts the number of elements that are not nil/undef.
|
|
6
|
+
EOS
|
|
7
|
+
) do |args|
|
|
8
|
+
|
|
9
|
+
if (args.size > 2) then
|
|
10
|
+
raise(ArgumentError, "count(): Wrong number of arguments "+
|
|
11
|
+
"given #{args.size} for 1 or 2.")
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
collection, item = args
|
|
15
|
+
|
|
16
|
+
if item then
|
|
17
|
+
collection.count item
|
|
18
|
+
else
|
|
19
|
+
collection.count { |obj| obj != nil && obj != :undef && obj != '' }
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# Test whether a given class or definition is defined
|
|
2
|
+
require 'puppet/parser/functions'
|
|
3
|
+
|
|
4
|
+
Puppet::Parser::Functions.newfunction(:defined_with_params,
|
|
5
|
+
:type => :rvalue,
|
|
6
|
+
:doc => <<-'ENDOFDOC'
|
|
7
|
+
Takes a resource reference and an optional hash of attributes.
|
|
8
|
+
|
|
9
|
+
Returns true if a resource with the specified attributes has already been added
|
|
10
|
+
to the catalog, and false otherwise.
|
|
11
|
+
|
|
12
|
+
user { 'dan':
|
|
13
|
+
ensure => present,
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if ! defined_with_params(User[dan], {'ensure' => 'present' }) {
|
|
17
|
+
user { 'dan': ensure => present, }
|
|
18
|
+
}
|
|
19
|
+
ENDOFDOC
|
|
20
|
+
) do |vals|
|
|
21
|
+
reference, params = vals
|
|
22
|
+
raise(ArgumentError, 'Must specify a reference') unless reference
|
|
23
|
+
if (! params) || params == ''
|
|
24
|
+
params = {}
|
|
25
|
+
end
|
|
26
|
+
ret = false
|
|
27
|
+
if resource = findresource(reference.to_s)
|
|
28
|
+
matches = params.collect do |key, value|
|
|
29
|
+
resource[key] == value
|
|
30
|
+
end
|
|
31
|
+
ret = params.empty? || !matches.include?(false)
|
|
32
|
+
end
|
|
33
|
+
Puppet.debug("Resource #{reference} was not determined to be defined")
|
|
34
|
+
ret
|
|
35
|
+
end
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
#
|
|
2
|
+
# delete.rb
|
|
3
|
+
#
|
|
4
|
+
|
|
5
|
+
# TODO(Krzysztof Wilczynski): We need to add support for regular expression ...
|
|
6
|
+
|
|
7
|
+
module Puppet::Parser::Functions
|
|
8
|
+
newfunction(:delete, :type => :rvalue, :doc => <<-EOS
|
|
9
|
+
Deletes all instances of a given element from an array, substring from a
|
|
10
|
+
string, or key from a hash.
|
|
11
|
+
|
|
12
|
+
*Examples:*
|
|
13
|
+
|
|
14
|
+
delete(['a','b','c','b'], 'b')
|
|
15
|
+
Would return: ['a','c']
|
|
16
|
+
|
|
17
|
+
delete({'a'=>1,'b'=>2,'c'=>3}, 'b')
|
|
18
|
+
Would return: {'a'=>1,'c'=>3}
|
|
19
|
+
|
|
20
|
+
delete('abracadabra', 'bra')
|
|
21
|
+
Would return: 'acada'
|
|
22
|
+
EOS
|
|
23
|
+
) do |arguments|
|
|
24
|
+
|
|
25
|
+
if (arguments.size != 2) then
|
|
26
|
+
raise(Puppet::ParseError, "delete(): Wrong number of arguments "+
|
|
27
|
+
"given #{arguments.size} for 2.")
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
collection = arguments[0]
|
|
31
|
+
item = arguments[1]
|
|
32
|
+
|
|
33
|
+
case collection
|
|
34
|
+
when Array, Hash
|
|
35
|
+
collection.delete item
|
|
36
|
+
when String
|
|
37
|
+
collection.gsub! item, ''
|
|
38
|
+
else
|
|
39
|
+
raise(TypeError, "delete(): First argument must be an Array, " +
|
|
40
|
+
"String, or Hash. Given an argument of class #{collection.class}.")
|
|
41
|
+
end
|
|
42
|
+
collection
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# vim: set ts=2 sw=2 et :
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
#
|
|
2
|
+
# delete_at.rb
|
|
3
|
+
#
|
|
4
|
+
|
|
5
|
+
module Puppet::Parser::Functions
|
|
6
|
+
newfunction(:delete_at, :type => :rvalue, :doc => <<-EOS
|
|
7
|
+
Deletes a determined indexed value from an array.
|
|
8
|
+
|
|
9
|
+
*Examples:*
|
|
10
|
+
|
|
11
|
+
delete_at(['a','b','c'], 1)
|
|
12
|
+
|
|
13
|
+
Would return: ['a','c']
|
|
14
|
+
EOS
|
|
15
|
+
) do |arguments|
|
|
16
|
+
|
|
17
|
+
raise(Puppet::ParseError, "delete_at(): Wrong number of arguments " +
|
|
18
|
+
"given (#{arguments.size} for 2)") if arguments.size < 2
|
|
19
|
+
|
|
20
|
+
array = arguments[0]
|
|
21
|
+
|
|
22
|
+
unless array.is_a?(Array)
|
|
23
|
+
raise(Puppet::ParseError, 'delete_at(): Requires array to work with')
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
index = arguments[1]
|
|
27
|
+
|
|
28
|
+
if index.is_a?(String) and not index.match(/^\d+$/)
|
|
29
|
+
raise(Puppet::ParseError, 'delete_at(): You must provide ' +
|
|
30
|
+
'non-negative numeric index')
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
result = array.clone
|
|
34
|
+
|
|
35
|
+
# Numbers in Puppet are often string-encoded which is troublesome ...
|
|
36
|
+
index = index.to_i
|
|
37
|
+
|
|
38
|
+
if index > result.size - 1 # First element is at index 0 is it not?
|
|
39
|
+
raise(Puppet::ParseError, 'delete_at(): Given index ' +
|
|
40
|
+
'exceeds size of array given')
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
result.delete_at(index) # We ignore the element that got deleted ...
|
|
44
|
+
|
|
45
|
+
return result
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# vim: set ts=2 sw=2 et :
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
#
|
|
2
|
+
# downcase.rb
|
|
3
|
+
#
|
|
4
|
+
|
|
5
|
+
module Puppet::Parser::Functions
|
|
6
|
+
newfunction(:downcase, :type => :rvalue, :doc => <<-EOS
|
|
7
|
+
Converts the case of a string or all strings in an array to lower case.
|
|
8
|
+
EOS
|
|
9
|
+
) do |arguments|
|
|
10
|
+
|
|
11
|
+
raise(Puppet::ParseError, "downcase(): 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, 'downcase(): 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.downcase : i }
|
|
25
|
+
else
|
|
26
|
+
result = value.downcase
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
return result
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# vim: set ts=2 sw=2 et :
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
#
|
|
2
|
+
# empty.rb
|
|
3
|
+
#
|
|
4
|
+
|
|
5
|
+
module Puppet::Parser::Functions
|
|
6
|
+
newfunction(:empty, :type => :rvalue, :doc => <<-EOS
|
|
7
|
+
Returns true if the variable is empty.
|
|
8
|
+
EOS
|
|
9
|
+
) do |arguments|
|
|
10
|
+
|
|
11
|
+
raise(Puppet::ParseError, "empty(): 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, Hash, String].include?(klass)
|
|
18
|
+
raise(Puppet::ParseError, 'empty(): Requires either ' +
|
|
19
|
+
'array, hash or string to work with')
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
result = value.empty?
|
|
23
|
+
|
|
24
|
+
return result
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# vim: set ts=2 sw=2 et :
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
#
|
|
2
|
+
# ensure_packages.rb
|
|
3
|
+
#
|
|
4
|
+
require 'puppet/parser/functions'
|
|
5
|
+
|
|
6
|
+
module Puppet::Parser::Functions
|
|
7
|
+
newfunction(:ensure_packages, :type => :statement, :doc => <<-EOS
|
|
8
|
+
Takes a list of packages and only installs them if they don't already exist.
|
|
9
|
+
EOS
|
|
10
|
+
) do |arguments|
|
|
11
|
+
|
|
12
|
+
raise(Puppet::ParseError, "ensure_packages(): Wrong number of arguments " +
|
|
13
|
+
"given (#{arguments.size} for 1)") if arguments.size != 1
|
|
14
|
+
raise(Puppet::ParseError, "ensure_packages(): Requires array " +
|
|
15
|
+
"given (#{arguments[0].class})") if !arguments[0].kind_of?(Array)
|
|
16
|
+
|
|
17
|
+
Puppet::Parser::Functions.function(:ensure_resource)
|
|
18
|
+
arguments[0].each { |package_name|
|
|
19
|
+
function_ensure_resource(['package', package_name, {'ensure' => 'present' } ])
|
|
20
|
+
}
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# vim: set ts=2 sw=2 et :
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# Test whether a given class or definition is defined
|
|
2
|
+
require 'puppet/parser/functions'
|
|
3
|
+
|
|
4
|
+
Puppet::Parser::Functions.newfunction(:ensure_resource,
|
|
5
|
+
:type => :statement,
|
|
6
|
+
:doc => <<-'ENDOFDOC'
|
|
7
|
+
Takes a resource type, title, and a list of attributes that describe a
|
|
8
|
+
resource.
|
|
9
|
+
|
|
10
|
+
user { 'dan':
|
|
11
|
+
ensure => present,
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
This example only creates the resource if it does not already exist:
|
|
15
|
+
|
|
16
|
+
ensure_resource('user, 'dan', {'ensure' => 'present' })
|
|
17
|
+
|
|
18
|
+
If the resource already exists but does not match the specified parameters,
|
|
19
|
+
this function will attempt to recreate the resource leading to a duplicate
|
|
20
|
+
resource definition error.
|
|
21
|
+
|
|
22
|
+
ENDOFDOC
|
|
23
|
+
) do |vals|
|
|
24
|
+
type, title, params = vals
|
|
25
|
+
raise(ArgumentError, 'Must specify a type') unless type
|
|
26
|
+
raise(ArgumentError, 'Must specify a title') unless title
|
|
27
|
+
params ||= {}
|
|
28
|
+
Puppet::Parser::Functions.function(:defined_with_params)
|
|
29
|
+
if function_defined_with_params(["#{type}[#{title}]", params])
|
|
30
|
+
Puppet.debug("Resource #{type}[#{title}] not created b/c it already exists")
|
|
31
|
+
else
|
|
32
|
+
Puppet::Parser::Functions.function(:create_resources)
|
|
33
|
+
function_create_resources([type.capitalize, { title => params }])
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
#
|
|
2
|
+
# flatten.rb
|
|
3
|
+
#
|
|
4
|
+
|
|
5
|
+
module Puppet::Parser::Functions
|
|
6
|
+
newfunction(:flatten, :type => :rvalue, :doc => <<-EOS
|
|
7
|
+
This function flattens any deeply nested arrays and returns a single flat array
|
|
8
|
+
as a result.
|
|
9
|
+
|
|
10
|
+
*Examples:*
|
|
11
|
+
|
|
12
|
+
flatten(['a', ['b', ['c']]])
|
|
13
|
+
|
|
14
|
+
Would return: ['a','b','c']
|
|
15
|
+
EOS
|
|
16
|
+
) do |arguments|
|
|
17
|
+
|
|
18
|
+
raise(Puppet::ParseError, "flatten(): 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, 'flatten(): Requires array to work with')
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
result = array.flatten
|
|
28
|
+
|
|
29
|
+
return result
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# vim: set ts=2 sw=2 et :
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
module Puppet::Parser::Functions
|
|
2
|
+
newfunction(:floor, :type => :rvalue, :doc => <<-EOS
|
|
3
|
+
Returns the largest integer less or equal to the argument.
|
|
4
|
+
Takes a single numeric value as an argument.
|
|
5
|
+
EOS
|
|
6
|
+
) do |arguments|
|
|
7
|
+
|
|
8
|
+
raise(Puppet::ParseError, "floor(): Wrong number of arguments " +
|
|
9
|
+
"given (#{arguments.size} for 1)") if arguments.size != 1
|
|
10
|
+
|
|
11
|
+
arg = arguments[0]
|
|
12
|
+
|
|
13
|
+
raise(Puppet::ParseError, "floor(): Wrong argument type " +
|
|
14
|
+
"given (#{arg.class} for Numeric)") if arg.is_a?(Numeric) == false
|
|
15
|
+
|
|
16
|
+
arg.floor
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# vim: set ts=2 sw=2 et :
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
#
|
|
2
|
+
# fqdn_rotate.rb
|
|
3
|
+
#
|
|
4
|
+
|
|
5
|
+
module Puppet::Parser::Functions
|
|
6
|
+
newfunction(:fqdn_rotate, :type => :rvalue, :doc => <<-EOS
|
|
7
|
+
Rotates an array a random number of times based on a nodes fqdn.
|
|
8
|
+
EOS
|
|
9
|
+
) do |arguments|
|
|
10
|
+
|
|
11
|
+
raise(Puppet::ParseError, "fqdn_rotate(): Wrong number of arguments " +
|
|
12
|
+
"given (#{arguments.size} for 1)") if arguments.size < 1
|
|
13
|
+
|
|
14
|
+
value = arguments[0]
|
|
15
|
+
klass = value.class
|
|
16
|
+
require 'digest/md5'
|
|
17
|
+
|
|
18
|
+
unless [Array, String].include?(klass)
|
|
19
|
+
raise(Puppet::ParseError, 'fqdn_rotate(): Requires either ' +
|
|
20
|
+
'array or string to work with')
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
result = value.clone
|
|
24
|
+
|
|
25
|
+
string = value.is_a?(String) ? true : false
|
|
26
|
+
|
|
27
|
+
# Check whether it makes sense to rotate ...
|
|
28
|
+
return result if result.size <= 1
|
|
29
|
+
|
|
30
|
+
# We turn any string value into an array to be able to rotate ...
|
|
31
|
+
result = string ? result.split('') : result
|
|
32
|
+
|
|
33
|
+
elements = result.size
|
|
34
|
+
|
|
35
|
+
srand(Digest::MD5.hexdigest([lookupvar('::fqdn'),arguments].join(':')).hex)
|
|
36
|
+
rand(elements).times {
|
|
37
|
+
result.push result.shift
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
result = string ? result.join : result
|
|
41
|
+
|
|
42
|
+
return result
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# vim: set ts=2 sw=2 et :
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
module Puppet::Parser::Functions
|
|
2
|
+
newfunction(:get_module_path, :type =>:rvalue, :doc => <<-EOT
|
|
3
|
+
Returns the absolute path of the specified module for the current
|
|
4
|
+
environment.
|
|
5
|
+
|
|
6
|
+
Example:
|
|
7
|
+
$module_path = get_module_path('stdlib')
|
|
8
|
+
EOT
|
|
9
|
+
) do |args|
|
|
10
|
+
raise(Puppet::ParseError, "get_module_path(): Wrong number of arguments, expects one") unless args.size == 1
|
|
11
|
+
if module_path = Puppet::Module.find(args[0], compiler.environment.to_s)
|
|
12
|
+
module_path.path
|
|
13
|
+
else
|
|
14
|
+
raise(Puppet::ParseError, "Could not find module #{args[0]} in environment #{compiler.environment}")
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# Test whether a given class or definition is defined
|
|
2
|
+
require 'puppet/parser/functions'
|
|
3
|
+
|
|
4
|
+
Puppet::Parser::Functions.newfunction(:getparam,
|
|
5
|
+
:type => :rvalue,
|
|
6
|
+
:doc => <<-'ENDOFDOC'
|
|
7
|
+
Takes a resource reference and name of the parameter and
|
|
8
|
+
returns value of resource's parameter.
|
|
9
|
+
|
|
10
|
+
*Examples:*
|
|
11
|
+
|
|
12
|
+
define example_resource($param) {
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
example_resource { "example_resource_instance":
|
|
16
|
+
param => "param_value"
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
getparam(Example_resource["example_resource_instance"], "param")
|
|
20
|
+
|
|
21
|
+
Would return: param_value
|
|
22
|
+
ENDOFDOC
|
|
23
|
+
) do |vals|
|
|
24
|
+
reference, param = vals
|
|
25
|
+
raise(ArgumentError, 'Must specify a reference') unless reference
|
|
26
|
+
raise(ArgumentError, 'Must specify name of a parameter') unless param and param.instance_of? String
|
|
27
|
+
|
|
28
|
+
return '' if param.empty?
|
|
29
|
+
|
|
30
|
+
if resource = findresource(reference.to_s)
|
|
31
|
+
return resource[param] if resource[param]
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
return ''
|
|
35
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
module Puppet::Parser::Functions
|
|
2
|
+
|
|
3
|
+
newfunction(:getvar, :type => :rvalue, :doc => <<-'ENDHEREDOC') do |args|
|
|
4
|
+
Lookup a variable in a remote namespace.
|
|
5
|
+
|
|
6
|
+
For example:
|
|
7
|
+
|
|
8
|
+
$foo = getvar('site::data::foo')
|
|
9
|
+
# Equivalent to $foo = $site::data::foo
|
|
10
|
+
|
|
11
|
+
This is useful if the namespace itself is stored in a string:
|
|
12
|
+
|
|
13
|
+
$datalocation = 'site::data'
|
|
14
|
+
$bar = getvar("${datalocation}::bar")
|
|
15
|
+
# Equivalent to $bar = $site::data::bar
|
|
16
|
+
ENDHEREDOC
|
|
17
|
+
|
|
18
|
+
unless args.length == 1
|
|
19
|
+
raise Puppet::ParseError, ("getvar(): wrong number of arguments (#{args.length}; must be 1)")
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
self.lookupvar("#{args[0]}")
|
|
23
|
+
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
end
|