init.pp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. # == Define: concat
  2. #
  3. # Sets up so that you can use fragments to build a final config file,
  4. #
  5. # === Options:
  6. #
  7. # [*ensure*]
  8. # Present/Absent
  9. # [*path*]
  10. # The path to the final file. Use this in case you want to differentiate
  11. # between the name of a resource and the file path. Note: Use the name you
  12. # provided in the target of your fragments.
  13. # [*owner*]
  14. # Who will own the file
  15. # [*group*]
  16. # Who will own the file
  17. # [*mode*]
  18. # The mode of the final file
  19. # [*force*]
  20. # Enables creating empty files if no fragments are present
  21. # [*warn*]
  22. # Adds a normal shell style comment top of the file indicating that it is
  23. # built by puppet
  24. # [*force*]
  25. # [*backup*]
  26. # Controls the filebucketing behavior of the final file and see File type
  27. # reference for its use. Defaults to 'puppet'
  28. # [*replace*]
  29. # Whether to replace a file that already exists on the local system
  30. # [*order*]
  31. # [*ensure_newline*]
  32. # [*gnu*]
  33. # Deprecated
  34. #
  35. # === Actions:
  36. # * Creates fragment directories if it didn't exist already
  37. # * Executes the concatfragments.sh script to build the final file, this
  38. # script will create directory/fragments.concat. Execution happens only
  39. # when:
  40. # * The directory changes
  41. # * fragments.concat != final destination, this means rebuilds will happen
  42. # whenever someone changes or deletes the final file. Checking is done
  43. # using /usr/bin/cmp.
  44. # * The Exec gets notified by something else - like the concat::fragment
  45. # define
  46. # * Copies the file over to the final destination using a file resource
  47. #
  48. # === Aliases:
  49. #
  50. # * The exec can notified using Exec["concat_/path/to/file"] or
  51. # Exec["concat_/path/to/directory"]
  52. # * The final file can be referenced as File["/path/to/file"] or
  53. # File["concat_/path/to/file"]
  54. #
  55. define concat(
  56. $ensure = 'present',
  57. $path = $name,
  58. $owner = undef,
  59. $group = undef,
  60. $mode = '0644',
  61. $warn = false,
  62. $force = false,
  63. $backup = 'puppet',
  64. $replace = true,
  65. $order = 'alpha',
  66. $ensure_newline = false,
  67. $gnu = undef
  68. ) {
  69. validate_re($ensure, '^present$|^absent$')
  70. validate_absolute_path($path)
  71. validate_string($owner)
  72. validate_string($group)
  73. validate_string($mode)
  74. if ! (is_string($warn) or $warn == true or $warn == false) {
  75. fail('$warn is not a string or boolean')
  76. }
  77. validate_bool($force)
  78. if ! concat_is_bool($backup) and ! is_string($backup) {
  79. fail('$backup must be string or bool!')
  80. }
  81. validate_bool($replace)
  82. validate_re($order, '^alpha$|^numeric$')
  83. validate_bool($ensure_newline)
  84. if $gnu {
  85. warning('The $gnu parameter to concat is deprecated and has no effect')
  86. }
  87. include concat::setup
  88. $safe_name = regsubst($name, '[/:]', '_', 'G')
  89. $concatdir = $concat::setup::concatdir
  90. $fragdir = "${concatdir}/${safe_name}"
  91. $concat_name = 'fragments.concat.out'
  92. $script_command = $concat::setup::script_command
  93. $default_warn_message = '# This file is managed by Puppet. DO NOT EDIT.'
  94. $bool_warn_message = 'Using stringified boolean values (\'true\', \'yes\', \'on\', \'false\', \'no\', \'off\') to represent boolean true/false as the $warn parameter to concat is deprecated and will be treated as the warning message in a future release'
  95. case $warn {
  96. true: {
  97. $warn_message = $default_warn_message
  98. }
  99. 'true', 'yes', 'on': {
  100. warning($bool_warn_message)
  101. $warn_message = $default_warn_message
  102. }
  103. false: {
  104. $warn_message = ''
  105. }
  106. 'false', 'no', 'off': {
  107. warning($bool_warn_message)
  108. $warn_message = ''
  109. }
  110. default: {
  111. $warn_message = $warn
  112. }
  113. }
  114. $warnmsg_escaped = regsubst($warn_message, '\'', '\'\\\'\'', 'G')
  115. $warnflag = $warnmsg_escaped ? {
  116. '' => '',
  117. default => "-w '${warnmsg_escaped}'"
  118. }
  119. $forceflag = $force ? {
  120. true => '-f',
  121. false => '',
  122. }
  123. $orderflag = $order ? {
  124. 'numeric' => '-n',
  125. 'alpha' => '',
  126. }
  127. $newlineflag = $ensure_newline ? {
  128. true => '-l',
  129. false => '',
  130. }
  131. File {
  132. backup => false,
  133. }
  134. # reset poisoned Exec defaults
  135. Exec {
  136. user => undef,
  137. group => undef,
  138. }
  139. if $ensure == 'present' {
  140. file { $fragdir:
  141. ensure => directory,
  142. mode => '0750',
  143. }
  144. file { "${fragdir}/fragments":
  145. ensure => directory,
  146. mode => '0750',
  147. force => true,
  148. ignore => ['.svn', '.git', '.gitignore'],
  149. notify => Exec["concat_${name}"],
  150. purge => true,
  151. recurse => true,
  152. }
  153. file { "${fragdir}/fragments.concat":
  154. ensure => present,
  155. mode => '0640',
  156. }
  157. file { "${fragdir}/${concat_name}":
  158. ensure => present,
  159. mode => '0640',
  160. }
  161. file { $name:
  162. ensure => present,
  163. owner => $owner,
  164. group => $group,
  165. mode => $mode,
  166. replace => $replace,
  167. path => $path,
  168. alias => "concat_${name}",
  169. source => "${fragdir}/${concat_name}",
  170. backup => $backup,
  171. }
  172. # remove extra whitespace from string interpolation to make testing easier
  173. $command = strip(regsubst("${script_command} -o \"${fragdir}/${concat_name}\" -d \"${fragdir}\" ${warnflag} ${forceflag} ${orderflag} ${newlineflag}", '\s+', ' ', 'G'))
  174. # if puppet is running as root, this exec should also run as root to allow
  175. # the concatfragments.sh script to potentially be installed in path that
  176. # may not be accessible by a target non-root owner.
  177. exec { "concat_${name}":
  178. alias => "concat_${fragdir}",
  179. command => $command,
  180. notify => File[$name],
  181. subscribe => File[$fragdir],
  182. unless => "${command} -t",
  183. path => $::path,
  184. require => [
  185. File[$fragdir],
  186. File["${fragdir}/fragments"],
  187. File["${fragdir}/fragments.concat"],
  188. ],
  189. }
  190. } else {
  191. file { [
  192. $fragdir,
  193. "${fragdir}/fragments",
  194. "${fragdir}/fragments.concat",
  195. "${fragdir}/${concat_name}"
  196. ]:
  197. ensure => absent,
  198. force => true,
  199. }
  200. file { $path:
  201. ensure => absent,
  202. backup => $backup,
  203. }
  204. $absent_exec_command = $::kernel ? {
  205. 'windows' => 'cmd.exe /c exit 0',
  206. default => 'true',
  207. }
  208. $absent_exec_path = $::kernel ? {
  209. 'windows' => $::path,
  210. default => '/bin:/usr/bin',
  211. }
  212. # Need to have an unless here for idempotency.
  213. exec { "concat_${name}":
  214. alias => "concat_${fragdir}",
  215. command => $absent_exec_command,
  216. unless => $absent_exec_command,
  217. path => $absent_exec_path,
  218. }
  219. }
  220. }
  221. # vim:sw=2:ts=2:expandtab:textwidth=79