summaryrefslogtreecommitdiff
path: root/manifests/defines/concatenated_file.pp
blob: e7014698157c87abd2029a952b88976c1878dd78 (plain)
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
# common/manifests/defines/concatenated_file.pp -- create a file from snippets
# stored in a directory
#
# Copyright (C) 2007 David Schmitt <david@schmitt.edv-bus.at>
# See LICENSE for the full license granted to you.

# TODO:
# * get rid of the $dir parameter
# * create the directory in _part too

# Usage:
# concatenated_file { "/etc/some.conf":
# 	dir => "/etc/some.conf.d",
# }
# Use Exec["concat_$name"] as Semaphor
define concatenated_file (
	# where the snippets are located
	$dir,
	# a file with content to prepend
	$header = '',
	# a file with content to append
	$footer = '',
	$mode = 0644, $owner = root, $group = root
	)
{
	file {
		$dir:
			source => "puppet://$servername/common/empty",
			checksum => mtime,
			recurse => true, purge => true, force => true,
			mode => $mode, owner => $owner, group => $group,
			notify => Exec["concat_${name}"];
		$name:
			ensure => present, checksum => md5,
			mode => $mode, owner => $owner, group => $group;
	}

	# if there is a header or footer file, add it
	$additional_cmd = "$header$footer" ? { '' => '', default => "| cat '${header}' - '${footer}' " }

	# use >| to force clobbering the target file
	exec { "/usr/bin/find ${dir} -maxdepth 1 -type f ! -name '*puppettmp' -print0 | sort -z | xargs -0 cat ${header_cmd} >| ${name}.puppettmp":
		refreshonly => true,
		subscribe => File[$dir],
		before => File[$name],
		alias => [ "concat_${name}", "concat_${dir}"] ,
	}
}


# Add a snippet called $name to the concatenated_file at $dir.
# The file can be referenced as File["cf_part_${name}"]
define concatenated_file_part (
	$dir, $content = '', $ensure = present,
	$mode = 0644, $owner = root, $group = root 
	)
{

	file { "${dir}/${name}":
		ensure => $ensure, content => $content,
		mode => $mode, owner => $owner, group => $group,
		alias => "cf_part_${name}",
		notify => Exec["concat_${dir}"],
	}

}