fragment.pp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # == Define: concat::fragment
  2. #
  3. # Puts a file fragment into a directory previous setup using concat
  4. #
  5. # === Options:
  6. #
  7. # [*target*]
  8. # The file that these fragments belong to
  9. # [*content*]
  10. # If present puts the content into the file
  11. # [*source*]
  12. # If content was not specified, use the source
  13. # [*order*]
  14. # By default all files gets a 10_ prefix in the directory you can set it to
  15. # anything else using this to influence the order of the content in the file
  16. # [*ensure*]
  17. # Present/Absent or destination to a file to include another file
  18. # [*mode*]
  19. # Deprecated
  20. # [*owner*]
  21. # Deprecated
  22. # [*group*]
  23. # Deprecated
  24. # [*backup*]
  25. # Deprecated
  26. #
  27. define concat::fragment(
  28. $target,
  29. $content = undef,
  30. $source = undef,
  31. $order = '10',
  32. $ensure = undef,
  33. $mode = undef,
  34. $owner = undef,
  35. $group = undef,
  36. $backup = undef
  37. ) {
  38. validate_string($target)
  39. validate_string($content)
  40. if !(is_string($source) or is_array($source)) {
  41. fail('$source is not a string or an Array.')
  42. }
  43. if !(is_string($order) or is_integer($order)) {
  44. fail('$order is not a string or integer.')
  45. }
  46. if $mode {
  47. warning('The $mode parameter to concat::fragment is deprecated and has no effect')
  48. }
  49. if $owner {
  50. warning('The $owner parameter to concat::fragment is deprecated and has no effect')
  51. }
  52. if $group {
  53. warning('The $group parameter to concat::fragment is deprecated and has no effect')
  54. }
  55. if $backup {
  56. warning('The $backup parameter to concat::fragment is deprecated and has no effect')
  57. }
  58. if $ensure == undef {
  59. $my_ensure = concat_getparam(Concat[$target], 'ensure')
  60. } else {
  61. if ! ($ensure in [ 'present', 'absent' ]) {
  62. warning('Passing a value other than \'present\' or \'absent\' as the $ensure parameter to concat::fragment is deprecated. If you want to use the content of a file as a fragment please use the $source parameter.')
  63. }
  64. $my_ensure = $ensure
  65. }
  66. include concat::setup
  67. $safe_name = regsubst($name, '[/:\n]', '_', 'GM')
  68. $safe_target_name = regsubst($target, '[/:\n]', '_', 'GM')
  69. $concatdir = $concat::setup::concatdir
  70. $fragdir = "${concatdir}/${safe_target_name}"
  71. $fragowner = $concat::setup::fragment_owner
  72. $fragmode = $concat::setup::fragment_mode
  73. # The file type's semantics are problematic in that ensure => present will
  74. # not over write a pre-existing symlink. We are attempting to provide
  75. # backwards compatiblity with previous concat::fragment versions that
  76. # supported the file type's ensure => /target syntax
  77. # be paranoid and only allow the fragment's file resource's ensure param to
  78. # be file, absent, or a file target
  79. $safe_ensure = $my_ensure ? {
  80. '' => 'file',
  81. undef => 'file',
  82. 'file' => 'file',
  83. 'present' => 'file',
  84. 'absent' => 'absent',
  85. default => $my_ensure,
  86. }
  87. # if it looks line ensure => /target syntax was used, fish that out
  88. if ! ($my_ensure in ['', 'present', 'absent', 'file' ]) {
  89. $ensure_target = $my_ensure
  90. } else {
  91. $ensure_target = undef
  92. }
  93. # the file type's semantics only allows one of: ensure => /target, content,
  94. # or source
  95. if ($ensure_target and $source) or
  96. ($ensure_target and $content) or
  97. ($source and $content) {
  98. fail('You cannot specify more than one of $content, $source, $ensure => /target')
  99. }
  100. if ! ($content or $source or $ensure_target) {
  101. crit('No content, source or symlink specified')
  102. }
  103. # punt on group ownership until some point in the distant future when $::gid
  104. # can be relied on to be present
  105. file { "${fragdir}/fragments/${order}_${safe_name}":
  106. ensure => $safe_ensure,
  107. owner => $fragowner,
  108. mode => $fragmode,
  109. source => $source,
  110. content => $content,
  111. backup => false,
  112. replace => true,
  113. alias => "concat_fragment_${name}",
  114. notify => Exec["concat_${target}"]
  115. }
  116. }