-
-
Notifications
You must be signed in to change notification settings - Fork 241
/
Copy pathdaemon.pp
311 lines (302 loc) · 11 KB
/
daemon.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# @summary This define managed prometheus daemons that don't have their own class
# @param version
# The binary release version
# @param real_download_url
# Complete URL corresponding to the where the release binary archive can be downloaded
# @param notify_service
# The service to notify when something changes in this define
# @param user
# User which runs the service
# @param install_method
# Installation method: url or package
# @param download_extension
# Extension for the release binary archive
# @param os
# Operating system (linux is the only one supported)
# @param arch
# Architecture (amd64 or i386)
# @param bin_dir
# Directory where binaries are located
# @param bin_name
# The name of the binary to execute
# @param package_name
# The binary package name
# @param package_ensure
# If package, then use this for package ensure default 'installed'
# @param manage_user
# Whether to create user or rely on external code for that
# @param extra_groups
# Extra groups of which the user should be a part
# @param manage_group
# Whether to create a group for or rely on external code for that
# @param service_ensure
# State ensured for the service (default 'running')
# @param service_enable
# Whether to enable the service from puppet (default true)
# @param manage_service
# Should puppet manage the service? (default true)
# @param extract_command
# Custom command passed to the archive resource to extract the downloaded archive.
# @param extract_path
# Path where to find extracted binary
# @param archive_bin_path
# Path to the binary in the downloaded archive.
# @param init_style
# Service startup scripts style (e.g. rc, upstart or systemd).
# Can also be set to `none` when you don't want the class to create a startup script/unit_file for you.
# Typically this can be used when a package is already providing the file.
# @param proxy_server
# Optional proxy server, with port number if needed. ie: https://example.com:8080
# @param proxy_type
# Optional proxy server type (none|http|https|ftp)
define prometheus::daemon (
String[1] $version,
Prometheus::Uri $real_download_url,
$notify_service,
String[1] $user,
String[1] $group,
Prometheus::Install $install_method = $prometheus::install_method,
String $download_extension = $prometheus::download_extension,
String[1] $os = $prometheus::os,
String[1] $arch = $prometheus::real_arch,
Stdlib::Absolutepath $bin_dir = $prometheus::bin_dir,
String[1] $bin_name = $name,
Boolean $manage_bin_link = true,
Optional[String] $package_name = undef,
String[1] $package_ensure = 'installed',
Boolean $manage_user = true,
Array $extra_groups = [],
Boolean $manage_group = true,
Boolean $purge = true,
String $options = '', # lint:ignore:params_empty_string_assignment
Prometheus::Initstyle $init_style = $prometheus::init_style,
Stdlib::Ensure::Service $service_ensure = 'running',
Boolean $service_enable = true,
Boolean $manage_service = true,
Hash[String[1], Scalar] $env_vars = {},
Stdlib::Absolutepath $env_file_path = $prometheus::env_file_path,
Optional[String[1]] $extract_command = $prometheus::extract_command,
Stdlib::Absolutepath $extract_path = '/opt',
Stdlib::Absolutepath $archive_bin_path = "/opt/${name}-${version}.${os}-${arch}/${name}",
Boolean $export_scrape_job = false,
Stdlib::Host $scrape_host = $facts['networking']['fqdn'],
Optional[Stdlib::Port] $scrape_port = undef,
String[1] $scrape_job_name = $name,
Hash $scrape_job_labels = { 'alias' => $scrape_host },
Stdlib::Absolutepath $usershell = $prometheus::usershell,
Optional[String[1]] $proxy_server = undef,
Optional[Enum['none', 'http', 'https', 'ftp']] $proxy_type = undef,
) {
case $install_method {
'url': {
if $download_extension == '' {
file { "/opt/${name}-${version}.${os}-${arch}":
ensure => directory,
owner => 'root',
group => 0, # 0 instead of root because OS X uses "wheel".
mode => '0755',
}
-> archive { "/opt/${name}-${version}.${os}-${arch}/${name}":
ensure => present,
source => $real_download_url,
checksum_verify => false,
before => File["/opt/${name}-${version}.${os}-${arch}/${name}"],
proxy_server => $proxy_server,
proxy_type => $proxy_type,
}
} else {
archive { "/tmp/${name}-${version}.${download_extension}":
ensure => present,
extract => true,
extract_path => $extract_path,
source => $real_download_url,
checksum_verify => false,
creates => $archive_bin_path,
cleanup => true,
before => File[$archive_bin_path],
extract_command => $extract_command,
proxy_server => $proxy_server,
proxy_type => $proxy_type,
}
}
file { $archive_bin_path:
owner => 'root',
group => 0, # 0 instead of root because OS X uses "wheel".
mode => '0555',
}
if $manage_bin_link {
file { "${bin_dir}/${bin_name}":
ensure => link,
notify => $notify_service,
target => $archive_bin_path,
require => File[$archive_bin_path],
}
}
}
'package': {
package { $package_name:
ensure => $package_ensure,
notify => $notify_service,
}
if $manage_user {
User[$user] -> Package[$package_name]
}
}
'none': {}
default: {}
}
if $manage_user {
# if we manage the service, we need to reload it if our user changes
# important for cases where another group gets added
if $manage_service {
User[$user] ~> $notify_service
}
ensure_resource('user', [$user], {
ensure => 'present',
system => true,
groups => $extra_groups,
shell => $usershell,
})
if $manage_group {
Group[$group] -> User[$user]
}
}
if $manage_group {
ensure_resource('group', [$group], {
ensure => 'present',
system => true,
})
}
case $init_style { # lint:ignore:case_without_default
'upstart': {
file { "/etc/init/${name}.conf":
mode => '0444',
owner => 'root',
group => 'root',
content => template('prometheus/daemon.upstart.erb'),
notify => $notify_service,
}
file { "/etc/init.d/${name}":
ensure => link,
target => '/lib/init/upstart-job',
owner => 'root',
group => 'root',
mode => '0755',
}
}
'systemd': {
include 'systemd'
systemd::manage_unit { "${name}.service":
unit_entry => {
'Description' => "Prometheus ${name}",
'Wants' => 'network-online.target',
'After' => 'network-online.target',
},
service_entry => {
'User' => $user,
'Group' => $group,
'EnvironmentFile' => "-${env_file_path}/${name}",
'ExecStart' => sprintf('%s/%s %s', $bin_dir, $bin_name, $options),
'ExecReload' => '/bin/kill -HUP $MAINPID',
'KillMode' => 'process',
'Restart' => 'always',
},
install_entry => {
'WantedBy' => 'multi-user.target',
},
notify => $notify_service,
}
}
'sysv': {
file { "/etc/init.d/${name}":
mode => '0555',
owner => 'root',
group => 'root',
content => template('prometheus/daemon.sysv.erb'),
notify => $notify_service,
}
}
'sles': {
file { "/etc/init.d/${name}":
mode => '0555',
owner => 'root',
group => 'root',
content => template('prometheus/daemon.sles.erb'),
notify => $notify_service,
}
}
'launchd': {
file { "/Library/LaunchDaemons/io.${name}.daemon.plist":
mode => '0644',
owner => 'root',
group => 'wheel',
content => template('prometheus/daemon.launchd.erb'),
notify => $notify_service,
}
}
'none': {}
}
if $init_style == 'none' and $install_method == 'package' {
$env_vars_merged = $env_vars + {
'ARGS' => $options,
}
} else {
$env_vars_merged = $env_vars
}
if $install_method == 'package' and $package_ensure in ['absent', 'purged'] {
# purge the environment file if the package is removed
#
# this is to make sure we can garbage-collect the files created by
# this module, when purging it.
file { "${env_file_path}/${name}":
ensure => absent,
}
} elsif $install_method == 'package' or !$env_vars_merged.empty {
# manage the environment file if the package is installed *or*, in
# any other case, if there's something to add to it
#
# the logic here is that the package-managed .service files *need*
# those files to be present, even if empty, so it's critical that
# the file not get removed
file { "${env_file_path}/${name}":
mode => '0644',
owner => 'root',
group => '0', # Darwin uses wheel
content => epp(
'prometheus/daemon.env.epp',
{
'env_vars' => $env_vars_merged,
}
),
notify => $notify_service,
}
}
$init_selector = $init_style ? {
'launchd' => "io.${name}.daemon",
default => $name,
}
$real_provider = $init_style ? {
'sles' => 'redhat', # mimics puppet's default behaviour
'sysv' => 'redhat', # all currently used cases for 'sysv' are redhat-compatible
'none' => undef,
default => $init_style,
}
if $manage_service {
service { $name:
ensure => $service_ensure,
name => $init_selector,
enable => $service_enable,
provider => $real_provider,
}
}
if $export_scrape_job {
if $scrape_port == undef {
fail('must set $scrape_port on exported daemon')
}
@@prometheus::scrape_job { "${scrape_job_name}_${scrape_host}_${scrape_port}":
job_name => $scrape_job_name,
targets => ["${scrape_host}:${scrape_port}"],
labels => $scrape_job_labels,
}
}
}