-
Notifications
You must be signed in to change notification settings - Fork 583
/
Copy pathmanage.pp
118 lines (117 loc) · 3.83 KB
/
manage.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# @summary A simple place to define trivial resources
#
# Sometimes your systems require a single simple resource.
# It can feel unnecessary to create a module for a single
# resource. There are a number of possible patterns to
# generate trivial resource definitions. This is an attempt
# to create a single clear method for uncomplicated resources.
# There is __limited__ support for `before`, `require`, `notify`,
# and `subscribe`.
#
# @param create_resources
# A hash of resources to create
# NOTE: functions, such as `template` or `epp`, are not directly evaluated
# but processed as Puppet code based on epp and erb hash keys.
#
# @example
# class { 'stdlib::manage':
# 'create_resources' => {
# 'file' => {
# '/etc/motd.d/hello' => {
# 'content' => 'I say Hi',
# 'notify' => 'Service[sshd]',
# },
# '/etc/motd' => {
# 'ensure' => 'file',
# 'epp' => {
# 'template' => 'profile/motd.epp',
# }
# },
# '/etc/information' => {
# 'ensure' => 'file',
# 'erb' => {
# 'template' => 'profile/informaiton.erb',
# }
# }
# },
# 'package' => {
# 'example' => {
# 'ensure' => 'installed',
# 'subscribe' => ['Service[sshd]', 'Exec[something]'],
# }
# }
# }
# }
#
# @example
# stdlib::manage::create_resources:
# file:
# '/etc/motd.d/hello':
# content: I say Hi
# notify: 'Service[sshd]'
# '/etc/motd':
# ensure: 'file'
# epp:
# template: 'profile/motd.epp'
# context: {}
# '/etc/information':
# ensure: 'file'
# erb:
# template: 'profile/information.erb'
# package:
# example:
# ensure: installed
# subscribe:
# - 'Service[sshd]'
# - 'Exec[something]'
class stdlib::manage (
Hash[String, Hash] $create_resources = {}
) {
$create_resources.each |$type, $resources| {
$resources.each |$title, $attributes| {
case $type {
'file', 'concat::fragment': {
# sanity checks
# epp, erb and content are exclusive
if 'epp' in $attributes and 'content' in $attributes {
fail("You can not set 'epp' and 'content' for ${type} ${title}")
}
if 'erb' in $attributes and 'content' in $attributes {
fail("You can not set 'erb' and 'content' for ${type} ${title}")
}
if 'erb' in $attributes and 'epp' in $attributes {
fail("You can not set 'erb' and 'epp' for ${type} ${title}")
}
if 'epp' in $attributes {
if 'template' in $attributes['epp'] {
if 'context' in $attributes['epp'] {
$content = epp($attributes['epp']['template'], $attributes['epp']['context'])
} else {
$content = epp($attributes['epp']['template'])
}
} else {
fail("No template configured for epp for ${type} ${title}")
}
} elsif 'erb' in $attributes {
if 'template' in $attributes['erb'] {
$content = template($attributes['erb']['template'])
} else {
fail("No template configured for erb for ${type} ${title}")
}
} elsif 'content' in $attributes {
$content = $attributes['content']
} else {
$content = undef
}
$type { $title:
* => $attributes - 'erb' - 'epp' - 'content',
content => $content,
}
}
default: {
create_resources($type, { $title => $attributes })
}
}
}
}
}