1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
#
# like built-in type "file", but gets gracefully ignored if the target does not exist or is undefined.
#
# /bin/true and /usr/bin/test are hardcoded to their paths in debian.
#
define try::file (
$ensure = undef,
$target = undef,
$restore = true) {
if $target != undef {
exec { "check_${name}":
command => "/bin/true",
onlyif => "/usr/bin/test -e '${target}'",
loglevel => info;
}
file { "$name":
ensure => $ensure,
target => $target,
require => Exec["check_${name}"],
loglevel => info;
}
}
#
# if the target does not exist (or is undef), and the file happens to be in a git repo,
# then restore the file to its original state.
#
if $target == undef or $restore {
$file_basename = basename($name)
$file_dirname = dirname($name)
$command = "git rev-parse && unlink '${name}'; git checkout -- '${file_basename}' && chown --reference='${file_dirname}' '${name}'; true"
debug($command)
if $target == undef {
exec { "restore_${name}":
command => $command,
cwd => $file_dirname,
loglevel => info;
}
} else {
exec { "restore_${name}":
unless => "/usr/bin/test -e '${target}'",
command => $command,
cwd => $file_dirname,
loglevel => info;
}
}
}
}
|