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,32 @@
|
|
1
|
+
#
|
2
|
+
# str2saltedsha512.rb
|
3
|
+
#
|
4
|
+
|
5
|
+
module Puppet::Parser::Functions
|
6
|
+
newfunction(:str2saltedsha512, :type => :rvalue, :doc => <<-EOS
|
7
|
+
This converts a string to a salted-SHA512 password hash (which is used for
|
8
|
+
OS X versions >= 10.7). Given any simple string, you will get a hex version
|
9
|
+
of a salted-SHA512 password hash that can be inserted into your Puppet
|
10
|
+
manifests as a valid password attribute.
|
11
|
+
EOS
|
12
|
+
) do |arguments|
|
13
|
+
require 'digest/sha2'
|
14
|
+
|
15
|
+
raise(Puppet::ParseError, "str2saltedsha512(): Wrong number of arguments " +
|
16
|
+
"passed (#{arguments.size} but we require 1)") if arguments.size != 1
|
17
|
+
|
18
|
+
password = arguments[0]
|
19
|
+
|
20
|
+
unless password.is_a?(String)
|
21
|
+
raise(Puppet::ParseError, 'str2saltedsha512(): Requires a ' +
|
22
|
+
"String argument, you passed: #{password.class}")
|
23
|
+
end
|
24
|
+
|
25
|
+
seedint = rand(2**31 - 1)
|
26
|
+
seedstring = Array(seedint).pack("L")
|
27
|
+
saltedpass = Digest::SHA512.digest(seedstring + password)
|
28
|
+
(seedstring + saltedpass).unpack('H*')[0]
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# vim: set ts=2 sw=2 et :
|
@@ -0,0 +1,107 @@
|
|
1
|
+
#
|
2
|
+
# strftime.rb
|
3
|
+
#
|
4
|
+
|
5
|
+
module Puppet::Parser::Functions
|
6
|
+
newfunction(:strftime, :type => :rvalue, :doc => <<-EOS
|
7
|
+
This function returns formatted time.
|
8
|
+
|
9
|
+
*Examples:*
|
10
|
+
|
11
|
+
To return the time since epoch:
|
12
|
+
|
13
|
+
strftime("%s")
|
14
|
+
|
15
|
+
To return the date:
|
16
|
+
|
17
|
+
strftime("%Y-%m-%d")
|
18
|
+
|
19
|
+
*Format meaning:*
|
20
|
+
|
21
|
+
%a - The abbreviated weekday name (``Sun'')
|
22
|
+
%A - The full weekday name (``Sunday'')
|
23
|
+
%b - The abbreviated month name (``Jan'')
|
24
|
+
%B - The full month name (``January'')
|
25
|
+
%c - The preferred local date and time representation
|
26
|
+
%C - Century (20 in 2009)
|
27
|
+
%d - Day of the month (01..31)
|
28
|
+
%D - Date (%m/%d/%y)
|
29
|
+
%e - Day of the month, blank-padded ( 1..31)
|
30
|
+
%F - Equivalent to %Y-%m-%d (the ISO 8601 date format)
|
31
|
+
%h - Equivalent to %b
|
32
|
+
%H - Hour of the day, 24-hour clock (00..23)
|
33
|
+
%I - Hour of the day, 12-hour clock (01..12)
|
34
|
+
%j - Day of the year (001..366)
|
35
|
+
%k - hour, 24-hour clock, blank-padded ( 0..23)
|
36
|
+
%l - hour, 12-hour clock, blank-padded ( 0..12)
|
37
|
+
%L - Millisecond of the second (000..999)
|
38
|
+
%m - Month of the year (01..12)
|
39
|
+
%M - Minute of the hour (00..59)
|
40
|
+
%n - Newline (\n)
|
41
|
+
%N - Fractional seconds digits, default is 9 digits (nanosecond)
|
42
|
+
%3N millisecond (3 digits)
|
43
|
+
%6N microsecond (6 digits)
|
44
|
+
%9N nanosecond (9 digits)
|
45
|
+
%p - Meridian indicator (``AM'' or ``PM'')
|
46
|
+
%P - Meridian indicator (``am'' or ``pm'')
|
47
|
+
%r - time, 12-hour (same as %I:%M:%S %p)
|
48
|
+
%R - time, 24-hour (%H:%M)
|
49
|
+
%s - Number of seconds since 1970-01-01 00:00:00 UTC.
|
50
|
+
%S - Second of the minute (00..60)
|
51
|
+
%t - Tab character (\t)
|
52
|
+
%T - time, 24-hour (%H:%M:%S)
|
53
|
+
%u - Day of the week as a decimal, Monday being 1. (1..7)
|
54
|
+
%U - Week number of the current year,
|
55
|
+
starting with the first Sunday as the first
|
56
|
+
day of the first week (00..53)
|
57
|
+
%v - VMS date (%e-%b-%Y)
|
58
|
+
%V - Week number of year according to ISO 8601 (01..53)
|
59
|
+
%W - Week number of the current year,
|
60
|
+
starting with the first Monday as the first
|
61
|
+
day of the first week (00..53)
|
62
|
+
%w - Day of the week (Sunday is 0, 0..6)
|
63
|
+
%x - Preferred representation for the date alone, no time
|
64
|
+
%X - Preferred representation for the time alone, no date
|
65
|
+
%y - Year without a century (00..99)
|
66
|
+
%Y - Year with century
|
67
|
+
%z - Time zone as hour offset from UTC (e.g. +0900)
|
68
|
+
%Z - Time zone name
|
69
|
+
%% - Literal ``%'' character
|
70
|
+
EOS
|
71
|
+
) do |arguments|
|
72
|
+
|
73
|
+
# Technically we support two arguments but only first is mandatory ...
|
74
|
+
raise(Puppet::ParseError, "strftime(): Wrong number of arguments " +
|
75
|
+
"given (#{arguments.size} for 1)") if arguments.size < 1
|
76
|
+
|
77
|
+
format = arguments[0]
|
78
|
+
|
79
|
+
raise(Puppet::ParseError, 'strftime(): You must provide ' +
|
80
|
+
'format for evaluation') if format.empty?
|
81
|
+
|
82
|
+
# The Time Zone argument is optional ...
|
83
|
+
time_zone = arguments[1] if arguments[1]
|
84
|
+
|
85
|
+
time = Time.new
|
86
|
+
|
87
|
+
# There is probably a better way to handle Time Zone ...
|
88
|
+
if time_zone and not time_zone.empty?
|
89
|
+
original_zone = ENV['TZ']
|
90
|
+
|
91
|
+
local_time = time.clone
|
92
|
+
local_time = local_time.utc
|
93
|
+
|
94
|
+
ENV['TZ'] = time_zone
|
95
|
+
|
96
|
+
time = local_time.localtime
|
97
|
+
|
98
|
+
ENV['TZ'] = original_zone
|
99
|
+
end
|
100
|
+
|
101
|
+
result = time.strftime(format)
|
102
|
+
|
103
|
+
return result
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
# vim: set ts=2 sw=2 et :
|
@@ -0,0 +1,39 @@
|
|
1
|
+
#
|
2
|
+
# strip.rb
|
3
|
+
#
|
4
|
+
|
5
|
+
module Puppet::Parser::Functions
|
6
|
+
newfunction(:strip, :type => :rvalue, :doc => <<-EOS
|
7
|
+
This function removes leading and trailing whitespace from a string or from
|
8
|
+
every string inside an array.
|
9
|
+
|
10
|
+
*Examples:*
|
11
|
+
|
12
|
+
strip(" aaa ")
|
13
|
+
|
14
|
+
Would result in: "aaa"
|
15
|
+
EOS
|
16
|
+
) do |arguments|
|
17
|
+
|
18
|
+
raise(Puppet::ParseError, "strip(): Wrong number of arguments " +
|
19
|
+
"given (#{arguments.size} for 1)") if arguments.size < 1
|
20
|
+
|
21
|
+
value = arguments[0]
|
22
|
+
klass = value.class
|
23
|
+
|
24
|
+
unless [Array, String].include?(klass)
|
25
|
+
raise(Puppet::ParseError, 'strip(): Requires either ' +
|
26
|
+
'array or string to work with')
|
27
|
+
end
|
28
|
+
|
29
|
+
if value.is_a?(Array)
|
30
|
+
result = value.collect { |i| i.is_a?(String) ? i.strip : i }
|
31
|
+
else
|
32
|
+
result = value.strip
|
33
|
+
end
|
34
|
+
|
35
|
+
return result
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# vim: set ts=2 sw=2 et :
|
@@ -0,0 +1,45 @@
|
|
1
|
+
#
|
2
|
+
# suffix.rb
|
3
|
+
#
|
4
|
+
|
5
|
+
module Puppet::Parser::Functions
|
6
|
+
newfunction(:suffix, :type => :rvalue, :doc => <<-EOS
|
7
|
+
This function applies a suffix to all elements in an array.
|
8
|
+
|
9
|
+
*Examples:*
|
10
|
+
|
11
|
+
suffix(['a','b','c'], 'p')
|
12
|
+
|
13
|
+
Will return: ['ap','bp','cp']
|
14
|
+
EOS
|
15
|
+
) do |arguments|
|
16
|
+
|
17
|
+
# Technically we support two arguments but only first is mandatory ...
|
18
|
+
raise(Puppet::ParseError, "suffix(): 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, "suffix(): expected first argument to be an Array, got #{array.inspect}"
|
25
|
+
end
|
26
|
+
|
27
|
+
suffix = arguments[1] if arguments[1]
|
28
|
+
|
29
|
+
if suffix
|
30
|
+
unless suffix.is_a? String
|
31
|
+
raise Puppet::ParseError, "suffix(): expected second argument to be a String, got #{suffix.inspect}"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# Turn everything into string same as join would do ...
|
36
|
+
result = array.collect do |i|
|
37
|
+
i = i.to_s
|
38
|
+
suffix ? i + suffix : i
|
39
|
+
end
|
40
|
+
|
41
|
+
return result
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
# vim: set ts=2 sw=2 et :
|
@@ -0,0 +1,39 @@
|
|
1
|
+
#
|
2
|
+
# swapcase.rb
|
3
|
+
#
|
4
|
+
|
5
|
+
module Puppet::Parser::Functions
|
6
|
+
newfunction(:swapcase, :type => :rvalue, :doc => <<-EOS
|
7
|
+
This function will swap the existing case of a string.
|
8
|
+
|
9
|
+
*Examples:*
|
10
|
+
|
11
|
+
swapcase("aBcD")
|
12
|
+
|
13
|
+
Would result in: "AbCd"
|
14
|
+
EOS
|
15
|
+
) do |arguments|
|
16
|
+
|
17
|
+
raise(Puppet::ParseError, "swapcase(): Wrong number of arguments " +
|
18
|
+
"given (#{arguments.size} for 1)") if arguments.size < 1
|
19
|
+
|
20
|
+
value = arguments[0]
|
21
|
+
klass = value.class
|
22
|
+
|
23
|
+
unless [Array, String].include?(klass)
|
24
|
+
raise(Puppet::ParseError, 'swapcase(): Requires either ' +
|
25
|
+
'array or string to work with')
|
26
|
+
end
|
27
|
+
|
28
|
+
if value.is_a?(Array)
|
29
|
+
# Numbers in Puppet are often string-encoded which is troublesome ...
|
30
|
+
result = value.collect { |i| i.is_a?(String) ? i.swapcase : i }
|
31
|
+
else
|
32
|
+
result = value.swapcase
|
33
|
+
end
|
34
|
+
|
35
|
+
return result
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# vim: set ts=2 sw=2 et :
|
@@ -0,0 +1,49 @@
|
|
1
|
+
#
|
2
|
+
# time.rb
|
3
|
+
#
|
4
|
+
|
5
|
+
module Puppet::Parser::Functions
|
6
|
+
newfunction(:time, :type => :rvalue, :doc => <<-EOS
|
7
|
+
This function will return the current time since epoch as an integer.
|
8
|
+
|
9
|
+
*Examples:*
|
10
|
+
|
11
|
+
time()
|
12
|
+
|
13
|
+
Will return something like: 1311972653
|
14
|
+
EOS
|
15
|
+
) do |arguments|
|
16
|
+
|
17
|
+
# The Time Zone argument is optional ...
|
18
|
+
time_zone = arguments[0] if arguments[0]
|
19
|
+
|
20
|
+
if (arguments.size != 0) and (arguments.size != 1) then
|
21
|
+
raise(Puppet::ParseError, "time(): Wrong number of arguments "+
|
22
|
+
"given #{arguments.size} for 0 or 1")
|
23
|
+
end
|
24
|
+
|
25
|
+
time = Time.new
|
26
|
+
|
27
|
+
# There is probably a better way to handle Time Zone ...
|
28
|
+
if time_zone and not time_zone.empty?
|
29
|
+
original_zone = ENV['TZ']
|
30
|
+
|
31
|
+
local_time = time.clone
|
32
|
+
local_time = local_time.utc
|
33
|
+
|
34
|
+
ENV['TZ'] = time_zone
|
35
|
+
|
36
|
+
time = local_time.localtime
|
37
|
+
|
38
|
+
ENV['TZ'] = original_zone
|
39
|
+
end
|
40
|
+
|
41
|
+
# Calling Time#to_i on a receiver changes it. Trust me I am the Doctor.
|
42
|
+
result = time.strftime('%s')
|
43
|
+
result = result.to_i
|
44
|
+
|
45
|
+
return result
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# vim: set ts=2 sw=2 et :
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Puppet::Parser::Functions
|
2
|
+
newfunction(:to_bytes, :type => :rvalue, :doc => <<-EOS
|
3
|
+
Converts the argument into bytes, for example 4 kB becomes 4096.
|
4
|
+
Takes a single string value as an argument.
|
5
|
+
EOS
|
6
|
+
) do |arguments|
|
7
|
+
|
8
|
+
raise(Puppet::ParseError, "to_bytes(): Wrong number of arguments " +
|
9
|
+
"given (#{arguments.size} for 1)") if arguments.size != 1
|
10
|
+
|
11
|
+
arg = arguments[0]
|
12
|
+
|
13
|
+
return arg if arg.is_a? Numeric
|
14
|
+
|
15
|
+
value,prefix = */([0-9.e+-]*)\s*([^bB]?)/.match(arg)[1,2]
|
16
|
+
|
17
|
+
value = value.to_f
|
18
|
+
case prefix
|
19
|
+
when '' then return value.to_i
|
20
|
+
when 'k' then return (value*(1<<10)).to_i
|
21
|
+
when 'M' then return (value*(1<<20)).to_i
|
22
|
+
when 'G' then return (value*(1<<30)).to_i
|
23
|
+
when 'T' then return (value*(1<<40)).to_i
|
24
|
+
when 'E' then return (value*(1<<50)).to_i
|
25
|
+
else raise Puppet::ParseError, "to_bytes(): Unknown prefix #{prefix}"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
#
|
2
|
+
# type.rb
|
3
|
+
#
|
4
|
+
|
5
|
+
module Puppet::Parser::Functions
|
6
|
+
newfunction(:type, :type => :rvalue, :doc => <<-EOS
|
7
|
+
Returns the type when passed a variable. Type can be one of:
|
8
|
+
|
9
|
+
* string
|
10
|
+
* array
|
11
|
+
* hash
|
12
|
+
* float
|
13
|
+
* integer
|
14
|
+
* boolean
|
15
|
+
EOS
|
16
|
+
) do |arguments|
|
17
|
+
|
18
|
+
raise(Puppet::ParseError, "type(): Wrong number of arguments " +
|
19
|
+
"given (#{arguments.size} for 1)") if arguments.size < 1
|
20
|
+
|
21
|
+
value = arguments[0]
|
22
|
+
|
23
|
+
klass = value.class
|
24
|
+
|
25
|
+
if not [TrueClass, FalseClass, Array, Bignum, Fixnum, Float, Hash, String].include?(klass)
|
26
|
+
raise(Puppet::ParseError, 'type(): Unknown type')
|
27
|
+
end
|
28
|
+
|
29
|
+
klass = klass.to_s # Ugly ...
|
30
|
+
|
31
|
+
# We note that Integer is the parent to Bignum and Fixnum ...
|
32
|
+
result = case klass
|
33
|
+
when /^(?:Big|Fix)num$/ then 'integer'
|
34
|
+
when /^(?:True|False)Class$/ then 'boolean'
|
35
|
+
else klass
|
36
|
+
end
|
37
|
+
|
38
|
+
if result == "String" then
|
39
|
+
if value == value.to_i.to_s then
|
40
|
+
result = "Integer"
|
41
|
+
elsif value == value.to_f.to_s then
|
42
|
+
result = "Float"
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
return result.downcase
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
# vim: set ts=2 sw=2 et :
|
@@ -0,0 +1,51 @@
|
|
1
|
+
#
|
2
|
+
# unique.rb
|
3
|
+
#
|
4
|
+
|
5
|
+
module Puppet::Parser::Functions
|
6
|
+
newfunction(:unique, :type => :rvalue, :doc => <<-EOS
|
7
|
+
This function will remove duplicates from strings and arrays.
|
8
|
+
|
9
|
+
*Examples:*
|
10
|
+
|
11
|
+
unique("aabbcc")
|
12
|
+
|
13
|
+
Will return:
|
14
|
+
|
15
|
+
abc
|
16
|
+
|
17
|
+
You can also use this with arrays:
|
18
|
+
|
19
|
+
unique(["a","a","b","b","c","c"])
|
20
|
+
|
21
|
+
This returns:
|
22
|
+
|
23
|
+
["a","b","c"]
|
24
|
+
EOS
|
25
|
+
) do |arguments|
|
26
|
+
|
27
|
+
raise(Puppet::ParseError, "unique(): Wrong number of arguments " +
|
28
|
+
"given (#{arguments.size} for 1)") if arguments.size < 1
|
29
|
+
|
30
|
+
value = arguments[0]
|
31
|
+
klass = value.class
|
32
|
+
|
33
|
+
unless [Array, String].include?(klass)
|
34
|
+
raise(Puppet::ParseError, 'unique(): Requires either ' +
|
35
|
+
'array or string to work with')
|
36
|
+
end
|
37
|
+
|
38
|
+
result = value.clone
|
39
|
+
|
40
|
+
string = value.is_a?(String) ? true : false
|
41
|
+
|
42
|
+
# We turn any string value into an array to be able to shuffle ...
|
43
|
+
result = string ? result.split('') : result
|
44
|
+
result = result.uniq # Remove duplicates ...
|
45
|
+
result = string ? result.join : result
|
46
|
+
|
47
|
+
return result
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# vim: set ts=2 sw=2 et :
|