28
|
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 |
} elsif (is_string($order) and $order =~ /[:\n\/]/) { |
|
46 |
fail("Order cannot contain '/', ':', or '\n'.") |
|
47 |
} |
|
48 |
if $mode { |
|
49 |
warning('The $mode parameter to concat::fragment is deprecated and has no effect') |
|
50 |
} |
|
51 |
if $owner { |
|
52 |
warning('The $owner parameter to concat::fragment is deprecated and has no effect') |
|
53 |
} |
|
54 |
if $group { |
|
55 |
warning('The $group parameter to concat::fragment is deprecated and has no effect') |
|
56 |
} |
|
57 |
if $backup { |
|
58 |
warning('The $backup parameter to concat::fragment is deprecated and has no effect') |
|
59 |
} |
|
60 |
|
|
61 |
$my_backup = concat_getparam(Concat[$target], 'backup') |
|
62 |
$_backup = $my_backup ? { |
|
63 |
'' => undef, |
|
64 |
default => $my_backup |
|
65 |
} |
|
66 |
|
|
67 |
if $ensure == undef { |
|
68 |
$my_ensure = concat_getparam(Concat[$target], 'ensure') |
|
69 |
} else { |
|
70 |
if ! ($ensure in [ 'present', 'absent' ]) { |
|
71 |
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.') |
|
72 |
} |
|
73 |
$my_ensure = $ensure |
|
74 |
} |
|
75 |
|
|
76 |
include concat::setup |
|
77 |
|
|
78 |
$safe_name = regsubst($name, '[/:\n]', '_', 'GM') |
|
79 |
$safe_target_name = regsubst($target, '[/:\n]', '_', 'GM') |
|
80 |
$concatdir = $concat::setup::concatdir |
|
81 |
$fragdir = "${concatdir}/${safe_target_name}" |
|
82 |
$fragowner = $concat::setup::fragment_owner |
|
83 |
$fraggroup = $concat::setup::fragment_group |
|
84 |
$fragmode = $concat::setup::fragment_mode |
|
85 |
|
|
86 |
# The file type's semantics are problematic in that ensure => present will |
|
87 |
# not over write a pre-existing symlink. We are attempting to provide |
|
88 |
# backwards compatiblity with previous concat::fragment versions that |
|
89 |
# supported the file type's ensure => /target syntax |
|
90 |
|
|
91 |
# be paranoid and only allow the fragment's file resource's ensure param to |
|
92 |
# be file, absent, or a file target |
|
93 |
$safe_ensure = $my_ensure ? { |
|
94 |
'' => 'file', |
|
95 |
undef => 'file', |
|
96 |
'file' => 'file', |
|
97 |
'present' => 'file', |
|
98 |
'absent' => 'absent', |
|
99 |
default => $my_ensure, |
|
100 |
} |
|
101 |
|
|
102 |
# if it looks line ensure => /target syntax was used, fish that out |
|
103 |
if ! ($my_ensure in ['', 'present', 'absent', 'file' ]) { |
|
104 |
$ensure_target = $my_ensure |
|
105 |
} else { |
|
106 |
$ensure_target = undef |
|
107 |
} |
|
108 |
|
|
109 |
# the file type's semantics only allows one of: ensure => /target, content, |
|
110 |
# or source |
|
111 |
if ($ensure_target and $source) or |
|
112 |
($ensure_target and $content) or |
|
113 |
($source and $content) { |
|
114 |
fail('You cannot specify more than one of $content, $source, $ensure => /target') |
|
115 |
} |
|
116 |
|
|
117 |
if ! ($content or $source or $ensure_target) { |
|
118 |
crit('No content, source or symlink specified') |
|
119 |
} |
|
120 |
|
|
121 |
file { "${fragdir}/fragments/${order}_${safe_name}": |
|
122 |
ensure => $safe_ensure, |
|
123 |
owner => $fragowner, |
|
124 |
group => $fraggroup, |
|
125 |
mode => $fragmode, |
|
126 |
source => $source, |
|
127 |
content => $content, |
|
128 |
backup => $_backup, |
|
129 |
replace => true, |
|
130 |
alias => "concat_fragment_${name}", |
|
131 |
notify => Exec["concat_${target}"] |
|
132 |
} |
|
133 |
} |