-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathcopy_file.pp
70 lines (70 loc) · 2.29 KB
/
copy_file.pp
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# @summary Copies a file from source to destination
#
# This defines copied a file from a source to a destination
# using an exec resource. It does not overwrite the destination
# file if it exists (and overwrite is set to true) and can be
# used as alternative to a file resource to avoid duplicated
# resources.
#
# @param source The source of the file to copy (must be a valid path)
# @param ensure If to create (present) or remove (absent)the target file
# @param path The path of the file to create. Default $title
# @param owner The owner of the created file
# @param group The group of the created file
# @param mode The mode of the created file
# @param overwrite If to overwrite the target file when already existing
define tp::copy_file (
Stdlib::AbsolutePath $source,
Enum['present','absent'] $ensure = 'present',
Stdlib::AbsolutePath $path = $title,
Optional[String] $owner = undef,
Optional[String] $group = undef,
Optional[String] $mode = undef,
Boolean $overwrite = false,
) {
if $overwrite {
$exec_creates = undef
$exec_unless = "diff ${source} ${path} >/dev/null"
$cp_force = '-f'
} else {
$exec_creates = $path
$exec_unless = undef
$cp_force = ''
}
if $ensure == 'present' {
exec { "tp::copy_file ${title}":
command => "cp ${cp_force} ${source} ${path}",
path => $facts['path'],
creates => $exec_creates,
unless => $exec_unless,
}
if $owner {
exec { "chown ${owner} ${title}":
command => "chown ${owner} ${path}",
path => $facts['path'],
onlyif => "[ $(ls -ld ${path} | awk '{ print \$3 }') != ${owner} ]",
}
}
if $group {
exec { "chgrp ${group} ${title}":
command => "chgrp ${group} ${path}",
path => $facts['path'],
onlyif => "[ $(ls -ld ${path} | awk '{ print \$4 }') != ${group} ]",
}
}
if $mode {
exec { "chmod ${mode} ${title}":
command => "chmod ${mode} ${path}",
path => $facts['path'],
subscribe => Exec["tp::copy_file ${title}"],
refreshonly => true,
}
}
} else {
exec { "tp::copy_file ${title}":
command => "rm -f ${path}",
path => $facts['path'],
onlyif => "test -f ${path}",
}
}
}