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.
Files changed (200) hide show
  1. data/CHANGELOG +253 -0
  2. data/CONTRIBUTING.md +65 -0
  3. data/Gemfile +33 -0
  4. data/LICENSE +19 -0
  5. data/Modulefile +11 -0
  6. data/README.markdown +1173 -0
  7. data/README_DEVELOPER.markdown +35 -0
  8. data/RELEASE_PROCESS.markdown +24 -0
  9. data/Rakefile +2 -0
  10. data/lib/facter/pe_version.rb +53 -0
  11. data/lib/facter/puppet_vardir.rb +26 -0
  12. data/lib/facter/root_home.rb +19 -0
  13. data/lib/facter/util/puppet_settings.rb +21 -0
  14. data/lib/puppet/parser/functions/abs.rb +36 -0
  15. data/lib/puppet/parser/functions/any2array.rb +33 -0
  16. data/lib/puppet/parser/functions/bool2num.rb +49 -0
  17. data/lib/puppet/parser/functions/capitalize.rb +34 -0
  18. data/lib/puppet/parser/functions/chomp.rb +35 -0
  19. data/lib/puppet/parser/functions/chop.rb +37 -0
  20. data/lib/puppet/parser/functions/concat.rb +37 -0
  21. data/lib/puppet/parser/functions/count.rb +22 -0
  22. data/lib/puppet/parser/functions/defined_with_params.rb +35 -0
  23. data/lib/puppet/parser/functions/delete.rb +46 -0
  24. data/lib/puppet/parser/functions/delete_at.rb +49 -0
  25. data/lib/puppet/parser/functions/downcase.rb +33 -0
  26. data/lib/puppet/parser/functions/empty.rb +28 -0
  27. data/lib/puppet/parser/functions/ensure_packages.rb +24 -0
  28. data/lib/puppet/parser/functions/ensure_resource.rb +35 -0
  29. data/lib/puppet/parser/functions/flatten.rb +33 -0
  30. data/lib/puppet/parser/functions/floor.rb +20 -0
  31. data/lib/puppet/parser/functions/fqdn_rotate.rb +46 -0
  32. data/lib/puppet/parser/functions/get_module_path.rb +17 -0
  33. data/lib/puppet/parser/functions/getparam.rb +35 -0
  34. data/lib/puppet/parser/functions/getvar.rb +26 -0
  35. data/lib/puppet/parser/functions/grep.rb +33 -0
  36. data/lib/puppet/parser/functions/has_interface_with.rb +52 -0
  37. data/lib/puppet/parser/functions/has_ip_address.rb +25 -0
  38. data/lib/puppet/parser/functions/has_ip_network.rb +25 -0
  39. data/lib/puppet/parser/functions/has_key.rb +28 -0
  40. data/lib/puppet/parser/functions/hash.rb +41 -0
  41. data/lib/puppet/parser/functions/is_array.rb +22 -0
  42. data/lib/puppet/parser/functions/is_domain_name.rb +47 -0
  43. data/lib/puppet/parser/functions/is_float.rb +27 -0
  44. data/lib/puppet/parser/functions/is_function_available.rb +23 -0
  45. data/lib/puppet/parser/functions/is_hash.rb +22 -0
  46. data/lib/puppet/parser/functions/is_integer.rb +27 -0
  47. data/lib/puppet/parser/functions/is_ip_address.rb +32 -0
  48. data/lib/puppet/parser/functions/is_mac_address.rb +27 -0
  49. data/lib/puppet/parser/functions/is_numeric.rb +27 -0
  50. data/lib/puppet/parser/functions/is_string.rb +26 -0
  51. data/lib/puppet/parser/functions/join.rb +41 -0
  52. data/lib/puppet/parser/functions/join_keys_to_values.rb +47 -0
  53. data/lib/puppet/parser/functions/keys.rb +26 -0
  54. data/lib/puppet/parser/functions/loadyaml.rb +20 -0
  55. data/lib/puppet/parser/functions/lstrip.rb +33 -0
  56. data/lib/puppet/parser/functions/max.rb +21 -0
  57. data/lib/puppet/parser/functions/member.rb +44 -0
  58. data/lib/puppet/parser/functions/merge.rb +33 -0
  59. data/lib/puppet/parser/functions/min.rb +21 -0
  60. data/lib/puppet/parser/functions/num2bool.rb +43 -0
  61. data/lib/puppet/parser/functions/parsejson.rb +24 -0
  62. data/lib/puppet/parser/functions/parseyaml.rb +24 -0
  63. data/lib/puppet/parser/functions/pick.rb +29 -0
  64. data/lib/puppet/parser/functions/prefix.rb +45 -0
  65. data/lib/puppet/parser/functions/range.rb +80 -0
  66. data/lib/puppet/parser/functions/reject.rb +31 -0
  67. data/lib/puppet/parser/functions/reverse.rb +28 -0
  68. data/lib/puppet/parser/functions/rstrip.rb +32 -0
  69. data/lib/puppet/parser/functions/shuffle.rb +46 -0
  70. data/lib/puppet/parser/functions/size.rb +48 -0
  71. data/lib/puppet/parser/functions/sort.rb +27 -0
  72. data/lib/puppet/parser/functions/squeeze.rb +36 -0
  73. data/lib/puppet/parser/functions/str2bool.rb +46 -0
  74. data/lib/puppet/parser/functions/str2saltedsha512.rb +32 -0
  75. data/lib/puppet/parser/functions/strftime.rb +107 -0
  76. data/lib/puppet/parser/functions/strip.rb +39 -0
  77. data/lib/puppet/parser/functions/suffix.rb +45 -0
  78. data/lib/puppet/parser/functions/swapcase.rb +39 -0
  79. data/lib/puppet/parser/functions/time.rb +49 -0
  80. data/lib/puppet/parser/functions/to_bytes.rb +28 -0
  81. data/lib/puppet/parser/functions/type.rb +50 -0
  82. data/lib/puppet/parser/functions/unique.rb +51 -0
  83. data/lib/puppet/parser/functions/upcase.rb +41 -0
  84. data/lib/puppet/parser/functions/uriescape.rb +36 -0
  85. data/lib/puppet/parser/functions/validate_absolute_path.rb +56 -0
  86. data/lib/puppet/parser/functions/validate_array.rb +33 -0
  87. data/lib/puppet/parser/functions/validate_augeas.rb +81 -0
  88. data/lib/puppet/parser/functions/validate_bool.rb +34 -0
  89. data/lib/puppet/parser/functions/validate_cmd.rb +47 -0
  90. data/lib/puppet/parser/functions/validate_hash.rb +33 -0
  91. data/lib/puppet/parser/functions/validate_re.rb +40 -0
  92. data/lib/puppet/parser/functions/validate_slength.rb +52 -0
  93. data/lib/puppet/parser/functions/validate_string.rb +33 -0
  94. data/lib/puppet/parser/functions/values.rb +39 -0
  95. data/lib/puppet/parser/functions/values_at.rb +98 -0
  96. data/lib/puppet/parser/functions/zip.rb +65 -0
  97. data/lib/puppet/provider/file_line/ruby.rb +59 -0
  98. data/lib/puppet/type/anchor.rb +41 -0
  99. data/lib/puppet/type/file_line.rb +70 -0
  100. data/manifests/init.pp +20 -0
  101. data/manifests/stages.pp +43 -0
  102. data/spec/functions/defined_with_params_spec.rb +37 -0
  103. data/spec/functions/ensure_packages_spec.rb +42 -0
  104. data/spec/functions/ensure_resource_spec.rb +40 -0
  105. data/spec/functions/getparam_spec.rb +34 -0
  106. data/spec/monkey_patches/alias_should_to_must.rb +8 -0
  107. data/spec/monkey_patches/publicize_methods.rb +10 -0
  108. data/spec/spec.opts +6 -0
  109. data/spec/spec_helper.rb +28 -0
  110. data/spec/unit/facter/pe_version_spec.rb +76 -0
  111. data/spec/unit/facter/root_home_spec.rb +40 -0
  112. data/spec/unit/facter/util/puppet_settings_spec.rb +35 -0
  113. data/spec/unit/puppet/parser/functions/abs_spec.rb +25 -0
  114. data/spec/unit/puppet/parser/functions/any2array_spec.rb +55 -0
  115. data/spec/unit/puppet/parser/functions/bool2num_spec.rb +24 -0
  116. data/spec/unit/puppet/parser/functions/capitalize_spec.rb +19 -0
  117. data/spec/unit/puppet/parser/functions/chomp_spec.rb +19 -0
  118. data/spec/unit/puppet/parser/functions/chop_spec.rb +19 -0
  119. data/spec/unit/puppet/parser/functions/concat_spec.rb +15 -0
  120. data/spec/unit/puppet/parser/functions/count_spec.rb +31 -0
  121. data/spec/unit/puppet/parser/functions/delete_at_spec.rb +19 -0
  122. data/spec/unit/puppet/parser/functions/delete_spec.rb +38 -0
  123. data/spec/unit/puppet/parser/functions/downcase_spec.rb +24 -0
  124. data/spec/unit/puppet/parser/functions/empty_spec.rb +23 -0
  125. data/spec/unit/puppet/parser/functions/flatten_spec.rb +27 -0
  126. data/spec/unit/puppet/parser/functions/floor_spec.rb +39 -0
  127. data/spec/unit/puppet/parser/functions/fqdn_rotate_spec.rb +33 -0
  128. data/spec/unit/puppet/parser/functions/get_module_path_spec.rb +46 -0
  129. data/spec/unit/puppet/parser/functions/getvar_spec.rb +37 -0
  130. data/spec/unit/puppet/parser/functions/grep_spec.rb +19 -0
  131. data/spec/unit/puppet/parser/functions/has_interface_with_spec.rb +64 -0
  132. data/spec/unit/puppet/parser/functions/has_ip_address_spec.rb +39 -0
  133. data/spec/unit/puppet/parser/functions/has_ip_network_spec.rb +36 -0
  134. data/spec/unit/puppet/parser/functions/has_key_spec.rb +42 -0
  135. data/spec/unit/puppet/parser/functions/hash_spec.rb +19 -0
  136. data/spec/unit/puppet/parser/functions/is_array_spec.rb +29 -0
  137. data/spec/unit/puppet/parser/functions/is_domain_name_spec.rb +64 -0
  138. data/spec/unit/puppet/parser/functions/is_float_spec.rb +33 -0
  139. data/spec/unit/puppet/parser/functions/is_function_available.rb +31 -0
  140. data/spec/unit/puppet/parser/functions/is_hash_spec.rb +29 -0
  141. data/spec/unit/puppet/parser/functions/is_integer_spec.rb +34 -0
  142. data/spec/unit/puppet/parser/functions/is_ip_address_spec.rb +39 -0
  143. data/spec/unit/puppet/parser/functions/is_mac_address_spec.rb +29 -0
  144. data/spec/unit/puppet/parser/functions/is_numeric_spec.rb +39 -0
  145. data/spec/unit/puppet/parser/functions/is_string_spec.rb +34 -0
  146. data/spec/unit/puppet/parser/functions/join_keys_to_values_spec.rb +40 -0
  147. data/spec/unit/puppet/parser/functions/join_spec.rb +19 -0
  148. data/spec/unit/puppet/parser/functions/keys_spec.rb +21 -0
  149. data/spec/unit/puppet/parser/functions/lstrip_spec.rb +19 -0
  150. data/spec/unit/puppet/parser/functions/max_spec.rb +27 -0
  151. data/spec/unit/puppet/parser/functions/member_spec.rb +24 -0
  152. data/spec/unit/puppet/parser/functions/merge_spec.rb +47 -0
  153. data/spec/unit/puppet/parser/functions/min_spec.rb +27 -0
  154. data/spec/unit/puppet/parser/functions/num2bool_spec.rb +67 -0
  155. data/spec/unit/puppet/parser/functions/parsejson_spec.rb +22 -0
  156. data/spec/unit/puppet/parser/functions/parseyaml_spec.rb +24 -0
  157. data/spec/unit/puppet/parser/functions/pick_spec.rb +34 -0
  158. data/spec/unit/puppet/parser/functions/prefix_spec.rb +19 -0
  159. data/spec/unit/puppet/parser/functions/range_spec.rb +34 -0
  160. data/spec/unit/puppet/parser/functions/reject_spec.rb +20 -0
  161. data/spec/unit/puppet/parser/functions/reverse_spec.rb +19 -0
  162. data/spec/unit/puppet/parser/functions/rstrip_spec.rb +24 -0
  163. data/spec/unit/puppet/parser/functions/shuffle_spec.rb +24 -0
  164. data/spec/unit/puppet/parser/functions/size_spec.rb +24 -0
  165. data/spec/unit/puppet/parser/functions/sort_spec.rb +24 -0
  166. data/spec/unit/puppet/parser/functions/squeeze_spec.rb +24 -0
  167. data/spec/unit/puppet/parser/functions/str2bool_spec.rb +31 -0
  168. data/spec/unit/puppet/parser/functions/str2saltedsha512_spec.rb +45 -0
  169. data/spec/unit/puppet/parser/functions/strftime_spec.rb +29 -0
  170. data/spec/unit/puppet/parser/functions/strip_spec.rb +18 -0
  171. data/spec/unit/puppet/parser/functions/suffix_spec.rb +19 -0
  172. data/spec/unit/puppet/parser/functions/swapcase_spec.rb +19 -0
  173. data/spec/unit/puppet/parser/functions/time_spec.rb +29 -0
  174. data/spec/unit/puppet/parser/functions/to_bytes_spec.rb +58 -0
  175. data/spec/unit/puppet/parser/functions/type_spec.rb +43 -0
  176. data/spec/unit/puppet/parser/functions/unique_spec.rb +24 -0
  177. data/spec/unit/puppet/parser/functions/upcase_spec.rb +24 -0
  178. data/spec/unit/puppet/parser/functions/uriescape_spec.rb +24 -0
  179. data/spec/unit/puppet/parser/functions/validate_absolute_path_spec.rb +83 -0
  180. data/spec/unit/puppet/parser/functions/validate_array_spec.rb +38 -0
  181. data/spec/unit/puppet/parser/functions/validate_augeas_spec.rb +102 -0
  182. data/spec/unit/puppet/parser/functions/validate_bool_spec.rb +51 -0
  183. data/spec/unit/puppet/parser/functions/validate_cmd_spec.rb +81 -0
  184. data/spec/unit/puppet/parser/functions/validate_hash_spec.rb +43 -0
  185. data/spec/unit/puppet/parser/functions/validate_re_spec.rb +76 -0
  186. data/spec/unit/puppet/parser/functions/validate_slength_spec.rb +48 -0
  187. data/spec/unit/puppet/parser/functions/validate_string_spec.rb +60 -0
  188. data/spec/unit/puppet/parser/functions/values_at_spec.rb +38 -0
  189. data/spec/unit/puppet/parser/functions/values_spec.rb +31 -0
  190. data/spec/unit/puppet/parser/functions/zip_spec.rb +15 -0
  191. data/spec/unit/puppet/provider/file_line/ruby_spec.rb +127 -0
  192. data/spec/unit/puppet/type/anchor_spec.rb +11 -0
  193. data/spec/unit/puppet/type/file_line_spec.rb +69 -0
  194. data/spec/watchr.rb +86 -0
  195. data/tests/file_line.pp +9 -0
  196. data/tests/has_interface_with.pp +10 -0
  197. data/tests/has_ip_address.pp +3 -0
  198. data/tests/has_ip_network.pp +4 -0
  199. data/tests/init.pp +1 -0
  200. metadata +252 -0
@@ -0,0 +1,41 @@
1
+ #
2
+ # upcase.rb
3
+ #
4
+
5
+ module Puppet::Parser::Functions
6
+ newfunction(:upcase, :type => :rvalue, :doc => <<-EOS
7
+ Converts a string or an array of strings to uppercase.
8
+
9
+ *Examples:*
10
+
11
+ upcase("abcd")
12
+
13
+ Will return:
14
+
15
+ ASDF
16
+ EOS
17
+ ) do |arguments|
18
+
19
+ raise(Puppet::ParseError, "upcase(): Wrong number of arguments " +
20
+ "given (#{arguments.size} for 1)") if arguments.size < 1
21
+
22
+ value = arguments[0]
23
+ klass = value.class
24
+
25
+ unless [Array, String].include?(klass)
26
+ raise(Puppet::ParseError, 'upcase(): Requires either ' +
27
+ 'array or string to work with')
28
+ end
29
+
30
+ if value.is_a?(Array)
31
+ # Numbers in Puppet are often string-encoded which is troublesome ...
32
+ result = value.collect { |i| i.is_a?(String) ? i.upcase : i }
33
+ else
34
+ result = value.upcase
35
+ end
36
+
37
+ return result
38
+ end
39
+ end
40
+
41
+ # vim: set ts=2 sw=2 et :
@@ -0,0 +1,36 @@
1
+ #
2
+ # uriescape.rb
3
+ #
4
+ require 'uri'
5
+
6
+ module Puppet::Parser::Functions
7
+ newfunction(:uriescape, :type => :rvalue, :doc => <<-EOS
8
+ Urlencodes a string or array of strings.
9
+ Requires either a single string or an array as an input.
10
+ EOS
11
+ ) do |arguments|
12
+
13
+ raise(Puppet::ParseError, "uriescape(): Wrong number of arguments " +
14
+ "given (#{arguments.size} for 1)") if arguments.size < 1
15
+
16
+ value = arguments[0]
17
+ klass = value.class
18
+ unsafe = ":/?#[]@!$&'()*+,;= "
19
+
20
+ unless [Array, String].include?(klass)
21
+ raise(Puppet::ParseError, 'uriescape(): Requires either ' +
22
+ 'array or string to work with')
23
+ end
24
+
25
+ if value.is_a?(Array)
26
+ # Numbers in Puppet are often string-encoded which is troublesome ...
27
+ result = value.collect { |i| i.is_a?(String) ? URI.escape(i,unsafe) : i }
28
+ else
29
+ result = URI.escape(value,unsafe)
30
+ end
31
+
32
+ return result
33
+ end
34
+ end
35
+
36
+ # vim: set ts=2 sw=2 et :
@@ -0,0 +1,56 @@
1
+ module Puppet::Parser::Functions
2
+ newfunction(:validate_absolute_path, :doc => <<-'ENDHEREDOC') do |args|
3
+ Validate the string represents an absolute path in the filesystem. This function works
4
+ for windows and unix style paths.
5
+
6
+ The following values will pass:
7
+
8
+ $my_path = "C:/Program Files (x86)/Puppet Labs/Puppet"
9
+ validate_absolute_path($my_path)
10
+ $my_path2 = "/var/lib/puppet"
11
+ validate_absolute_path($my_path2)
12
+
13
+
14
+ The following values will fail, causing compilation to abort:
15
+
16
+ validate_absolute_path(true)
17
+ validate_absolute_path([ 'var/lib/puppet', '/var/foo' ])
18
+ validate_absolute_path([ '/var/lib/puppet', 'var/foo' ])
19
+ $undefined = undef
20
+ validate_absolute_path($undefined)
21
+
22
+ ENDHEREDOC
23
+
24
+ require 'puppet/util'
25
+
26
+ unless args.length > 0 then
27
+ raise Puppet::ParseError, ("validate_absolute_path(): wrong number of arguments (#{args.length}; must be > 0)")
28
+ end
29
+
30
+ args.each do |arg|
31
+ # This logic was borrowed from
32
+ # [lib/puppet/file_serving/base.rb](https://github.com/puppetlabs/puppet/blob/master/lib/puppet/file_serving/base.rb)
33
+
34
+ # Puppet 2.7 and beyond will have Puppet::Util.absolute_path? Fall back to a back-ported implementation otherwise.
35
+ if Puppet::Util.respond_to?(:absolute_path?) then
36
+ unless Puppet::Util.absolute_path?(arg, :posix) or Puppet::Util.absolute_path?(arg, :windows)
37
+ raise Puppet::ParseError, ("#{arg.inspect} is not an absolute path.")
38
+ end
39
+ else
40
+ # This code back-ported from 2.7.x's lib/puppet/util.rb Puppet::Util.absolute_path?
41
+ # Determine in a platform-specific way whether a path is absolute. This
42
+ # defaults to the local platform if none is specified.
43
+ # Escape once for the string literal, and once for the regex.
44
+ slash = '[\\\\/]'
45
+ name = '[^\\\\/]+'
46
+ regexes = {
47
+ :windows => %r!^(([A-Z]:#{slash})|(#{slash}#{slash}#{name}#{slash}#{name})|(#{slash}#{slash}\?#{slash}#{name}))!i,
48
+ :posix => %r!^/!,
49
+ }
50
+
51
+ rval = (!!(arg =~ regexes[:posix])) || (!!(arg =~ regexes[:windows]))
52
+ rval or raise Puppet::ParseError, ("#{arg.inspect} is not an absolute path.")
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,33 @@
1
+ module Puppet::Parser::Functions
2
+
3
+ newfunction(:validate_array, :doc => <<-'ENDHEREDOC') do |args|
4
+ Validate that all passed values are array data structures. Abort catalog
5
+ compilation if any value fails this check.
6
+
7
+ The following values will pass:
8
+
9
+ $my_array = [ 'one', 'two' ]
10
+ validate_array($my_array)
11
+
12
+ The following values will fail, causing compilation to abort:
13
+
14
+ validate_array(true)
15
+ validate_array('some_string')
16
+ $undefined = undef
17
+ validate_array($undefined)
18
+
19
+ ENDHEREDOC
20
+
21
+ unless args.length > 0 then
22
+ raise Puppet::ParseError, ("validate_array(): wrong number of arguments (#{args.length}; must be > 0)")
23
+ end
24
+
25
+ args.each do |arg|
26
+ unless arg.is_a?(Array)
27
+ raise Puppet::ParseError, ("#{arg.inspect} is not an Array. It looks to be a #{arg.class}")
28
+ end
29
+ end
30
+
31
+ end
32
+
33
+ end
@@ -0,0 +1,81 @@
1
+ module Puppet::Parser::Functions
2
+ newfunction(:validate_augeas, :doc => <<-'ENDHEREDOC') do |args|
3
+ Perform validation of a string using an Augeas lens
4
+ The first argument of this function should be a string to
5
+ test, and the second argument should be the name of the Augeas lens to use.
6
+ If Augeas fails to parse the string with the lens, the compilation will
7
+ abort with a parse error.
8
+
9
+ A third argument can be specified, listing paths which should
10
+ not be found in the file. The `$file` variable points to the location
11
+ of the temporary file being tested in the Augeas tree.
12
+
13
+ For example, if you want to make sure your passwd content never contains
14
+ a user `foo`, you could write:
15
+
16
+ validate_augeas($passwdcontent, 'Passwd.lns', ['$file/foo'])
17
+
18
+ Or if you wanted to ensure that no users used the '/bin/barsh' shell,
19
+ you could use:
20
+
21
+ validate_augeas($passwdcontent, 'Passwd.lns', ['$file/*[shell="/bin/barsh"]']
22
+
23
+ If a fourth argument is specified, this will be the error message raised and
24
+ seen by the user.
25
+
26
+ A helpful error message can be returned like this:
27
+
28
+ validate_augeas($sudoerscontent, 'Sudoers.lns', [], 'Failed to validate sudoers content with Augeas')
29
+
30
+ ENDHEREDOC
31
+ unless Puppet.features.augeas?
32
+ raise Puppet::ParseError, ("validate_augeas(): this function requires the augeas feature. See http://projects.puppetlabs.com/projects/puppet/wiki/Puppet_Augeas#Pre-requisites for how to activate it.")
33
+ end
34
+
35
+ if (args.length < 2) or (args.length > 4) then
36
+ raise Puppet::ParseError, ("validate_augeas(): wrong number of arguments (#{args.length}; must be 2, 3, or 4)")
37
+ end
38
+
39
+ msg = args[3] || "validate_augeas(): Failed to validate content against #{args[1].inspect}"
40
+
41
+ require 'augeas'
42
+ aug = Augeas::open(nil, nil, Augeas::NO_MODL_AUTOLOAD)
43
+ begin
44
+ content = args[0]
45
+
46
+ # Test content in a temporary file
47
+ tmpfile = Tempfile.new("validate_augeas")
48
+ begin
49
+ tmpfile.write(content)
50
+ ensure
51
+ tmpfile.close
52
+ end
53
+
54
+ # Check for syntax
55
+ lens = args[1]
56
+ aug.transform(
57
+ :lens => lens,
58
+ :name => 'Validate_augeas',
59
+ :incl => tmpfile.path
60
+ )
61
+ aug.load!
62
+
63
+ unless aug.match("/augeas/files#{tmpfile.path}//error").empty?
64
+ error = aug.get("/augeas/files#{tmpfile.path}//error/message")
65
+ msg += " with error: #{error}"
66
+ raise Puppet::ParseError, (msg)
67
+ end
68
+
69
+ # Launch unit tests
70
+ tests = args[2] || []
71
+ aug.defvar('file', "/files#{tmpfile.path}")
72
+ tests.each do |t|
73
+ msg += " testing path #{t}"
74
+ raise Puppet::ParseError, (msg) unless aug.match(t).empty?
75
+ end
76
+ ensure
77
+ aug.close
78
+ tmpfile.unlink
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,34 @@
1
+ module Puppet::Parser::Functions
2
+
3
+ newfunction(:validate_bool, :doc => <<-'ENDHEREDOC') do |args|
4
+ Validate that all passed values are either true or false. Abort catalog
5
+ compilation if any value fails this check.
6
+
7
+ The following values will pass:
8
+
9
+ $iamtrue = true
10
+ validate_bool(true)
11
+ validate_bool(true, true, false, $iamtrue)
12
+
13
+ The following values will fail, causing compilation to abort:
14
+
15
+ $some_array = [ true ]
16
+ validate_bool("false")
17
+ validate_bool("true")
18
+ validate_bool($some_array)
19
+
20
+ ENDHEREDOC
21
+
22
+ unless args.length > 0 then
23
+ raise Puppet::ParseError, ("validate_bool(): wrong number of arguments (#{args.length}; must be > 0)")
24
+ end
25
+
26
+ args.each do |arg|
27
+ unless (arg.is_a?(TrueClass) || arg.is_a?(FalseClass))
28
+ raise Puppet::ParseError, ("#{arg.inspect} is not a boolean. It looks to be a #{arg.class}")
29
+ end
30
+ end
31
+
32
+ end
33
+
34
+ end
@@ -0,0 +1,47 @@
1
+ require 'puppet/util/execution'
2
+
3
+ module Puppet::Parser::Functions
4
+ newfunction(:validate_cmd, :doc => <<-'ENDHEREDOC') do |args|
5
+ Perform validation of a string with an external command.
6
+ The first argument of this function should be a string to
7
+ test, and the second argument should be a path to a test command
8
+ taking a file as last argument. If the command, launched against
9
+ a tempfile containing the passed string, returns a non-null value,
10
+ compilation will abort with a parse error.
11
+
12
+ If a third argument is specified, this will be the error message raised and
13
+ seen by the user.
14
+
15
+ A helpful error message can be returned like this:
16
+
17
+ Example:
18
+
19
+ validate_cmd($sudoerscontent, '/usr/sbin/visudo -c -f', 'Visudo failed to validate sudoers content')
20
+
21
+ ENDHEREDOC
22
+ if (args.length < 2) or (args.length > 3) then
23
+ raise Puppet::ParseError, ("validate_cmd(): wrong number of arguments (#{args.length}; must be 2 or 3)")
24
+ end
25
+
26
+ msg = args[2] || "validate_cmd(): failed to validate content with command #{args[1].inspect}"
27
+
28
+ content = args[0]
29
+ checkscript = args[1]
30
+
31
+ # Test content in a temporary file
32
+ tmpfile = Tempfile.new("validate_cmd")
33
+ begin
34
+ tmpfile.write(content)
35
+ if Puppet::Util::Execution.respond_to?('execute')
36
+ Puppet::Util::Execution.execute("#{checkscript} #{tmpfile.path}")
37
+ else
38
+ Puppet::Util.execute("#{checkscript} #{tmpfile.path}")
39
+ end
40
+ rescue Puppet::ExecutionFailure => detail
41
+ msg += "\n#{detail}"
42
+ raise Puppet::ParseError, msg
43
+ ensure
44
+ tmpfile.unlink
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,33 @@
1
+ module Puppet::Parser::Functions
2
+
3
+ newfunction(:validate_hash, :doc => <<-'ENDHEREDOC') do |args|
4
+ Validate that all passed values are hash data structures. Abort catalog
5
+ compilation if any value fails this check.
6
+
7
+ The following values will pass:
8
+
9
+ $my_hash = { 'one' => 'two' }
10
+ validate_hash($my_hash)
11
+
12
+ The following values will fail, causing compilation to abort:
13
+
14
+ validate_hash(true)
15
+ validate_hash('some_string')
16
+ $undefined = undef
17
+ validate_hash($undefined)
18
+
19
+ ENDHEREDOC
20
+
21
+ unless args.length > 0 then
22
+ raise Puppet::ParseError, ("validate_hash(): wrong number of arguments (#{args.length}; must be > 0)")
23
+ end
24
+
25
+ args.each do |arg|
26
+ unless arg.is_a?(Hash)
27
+ raise Puppet::ParseError, ("#{arg.inspect} is not a Hash. It looks to be a #{arg.class}")
28
+ end
29
+ end
30
+
31
+ end
32
+
33
+ end
@@ -0,0 +1,40 @@
1
+ module Puppet::Parser::Functions
2
+ newfunction(:validate_re, :doc => <<-'ENDHEREDOC') do |args|
3
+ Perform simple validation of a string against one or more regular
4
+ expressions. The first argument of this function should be a string to
5
+ test, and the second argument should be a stringified regular expression
6
+ (without the // delimiters) or an array of regular expressions. If none
7
+ of the regular expressions match the string passed in, compilation will
8
+ abort with a parse error.
9
+
10
+ If a third argument is specified, this will be the error message raised and
11
+ seen by the user.
12
+
13
+ The following strings will validate against the regular expressions:
14
+
15
+ validate_re('one', '^one$')
16
+ validate_re('one', [ '^one', '^two' ])
17
+
18
+ The following strings will fail to validate, causing compilation to abort:
19
+
20
+ validate_re('one', [ '^two', '^three' ])
21
+
22
+ A helpful error message can be returned like this:
23
+
24
+ validate_re($::puppetversion, '^2.7', 'The $puppetversion fact value does not match 2.7')
25
+
26
+ ENDHEREDOC
27
+ if (args.length < 2) or (args.length > 3) then
28
+ raise Puppet::ParseError, ("validate_re(): wrong number of arguments (#{args.length}; must be 2 or 3)")
29
+ end
30
+
31
+ msg = args[2] || "validate_re(): #{args[0].inspect} does not match #{args[1].inspect}"
32
+
33
+ # We're using a flattened array here because we can't call String#any? in
34
+ # Ruby 1.9 like we can in Ruby 1.8
35
+ raise Puppet::ParseError, (msg) unless [args[1]].flatten.any? do |re_str|
36
+ args[0] =~ Regexp.compile(re_str)
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,52 @@
1
+ module Puppet::Parser::Functions
2
+
3
+ newfunction(:validate_slength, :doc => <<-'ENDHEREDOC') do |args|
4
+ Validate that the first argument is a string (or an array of strings), and
5
+ less/equal to than the length of the second argument. It fails if the first
6
+ argument is not a string or array of strings, and if arg 2 is not convertable
7
+ to a number.
8
+
9
+ The following values will pass:
10
+
11
+ validate_slength("discombobulate",17)
12
+ validate_slength(["discombobulate","moo"],17)
13
+
14
+ The following valueis will not:
15
+
16
+ validate_slength("discombobulate",1)
17
+ validate_slength(["discombobulate","thermometer"],5)
18
+
19
+ ENDHEREDOC
20
+
21
+ raise Puppet::ParseError, ("validate_slength(): Wrong number of arguments (#{args.length}; must be = 2)") unless args.length == 2
22
+
23
+ unless (args[0].is_a?(String) or args[0].is_a?(Array))
24
+ raise Puppet::ParseError, ("validate_slength(): please pass a string, or an array of strings - what you passed didn't work for me at all - #{args[0].class}")
25
+ end
26
+
27
+ begin
28
+ max_length = args[1].to_i
29
+ rescue NoMethodError => e
30
+ raise Puppet::ParseError, ("validate_slength(): Couldn't convert whatever you passed as the length parameter to an integer - sorry: " + e.message )
31
+ end
32
+
33
+ raise Puppet::ParseError, ("validate_slength(): please pass a positive number as max_length") unless max_length > 0
34
+
35
+ case args[0]
36
+ when String
37
+ raise Puppet::ParseError, ("validate_slength(): #{args[0].inspect} is #{args[0].length} characters. It should have been less than or equal to #{max_length} characters") unless args[0].length <= max_length
38
+ when Array
39
+ args[0].each do |arg|
40
+ if arg.is_a?(String)
41
+ unless ( arg.is_a?(String) and arg.length <= max_length )
42
+ raise Puppet::ParseError, ("validate_slength(): #{arg.inspect} is #{arg.length} characters. It should have been less than or equal to #{max_length} characters")
43
+ end
44
+ else
45
+ raise Puppet::ParseError, ("validate_slength(): #{arg.inspect} is not a string, it's a #{arg.class}")
46
+ end
47
+ end
48
+ else
49
+ raise Puppet::ParseError, ("validate_slength(): please pass a string, or an array of strings - what you passed didn't work for me at all - #{args[0].class}")
50
+ end
51
+ end
52
+ end