-
Notifications
You must be signed in to change notification settings - Fork 321
/
Copy pathrun.pp
executable file
·466 lines (442 loc) · 18.4 KB
/
run.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# == Define: docker:run
#
# A define which manages a running docker container.
#
# == Parameters
#
# [*restart*]
# Sets a restart policy on the docker run.
# Note: If set, puppet will NOT setup an init script to manage, instead
# it will do a raw docker run command using a CID file to track the container
# ID.
#
# If you want a normal named container with an init script and a restart policy
# you must use the extra_parameters feature and pass it in like this:
#
# extra_parameters => ['--restart=always']
#
# However, if your system is using sytemd this restart policy will be
# ineffective because the ExecStop commands will run which will cause
# docker to stop restarting it. In this case you should use the
# systemd_restart option to specify the policy you want.
#
# This will allow the docker container to be restarted if it dies, without
# puppet help.
#
# [*service_prefix*]
# (optional) The name to prefix the startup script with and the Puppet
# service resource title with. Default: 'docker-'
#
# [*restart_service*]
# (optional) Whether or not to restart the service if the the generated init
# script changes. Default: true
#
# [*restart_service_on_docker_refresh*]
# Whether or not to restart the service if the docker service is restarted.
# Only has effect if the docker_service parameter is set.
# Default: true
#
# [*manage_service*]
# (optional) Whether or not to create a puppet Service resource for the init
# script. Disabling this may be useful if integrating with existing modules.
# Default: true
#
# [*docker_service*]
# (optional) If (and how) the Docker service itself is managed by Puppet
# true -> Service['docker']
# false -> no Service dependency
# anything else -> Service[docker_service]
# Default: false
#
# [*health_check_cmd*]
# (optional) Specifies the command to execute to check that the container is healthy using the docker health check functionality.
# Default: undef
#
# [*restart_on_unhealthy*]
# (optional) Checks the health status of Docker container and if it is unhealthy the service will be restarted.
# The health_check_cmd parameter must be set to true to use this functionality.
# Default: undef
#
# [*extra_parameters*]
# An array of additional command line arguments to pass to the `docker run`
# command. Useful for adding additional new or experimental options that the
# module does not yet support.
#
# [*systemd_restart*]
# (optional) If the container is to be managed by a systemd unit file set the
# Restart option on the unit file. Can be any valid value for this systemd
# configuration. Most commonly used are on-failure or always.
# Default: on-failure
#
define docker::run(
Optional[Pattern[/^[\S]*$/]] $image,
Optional[Pattern[/^present$|^absent$/]] $ensure = 'present',
Optional[String] $command = undef,
Optional[Pattern[/^[\d]*(b|k|m|g)$/]] $memory_limit = '0b',
Variant[String,Array,Undef] $cpuset = [],
Variant[String,Array,Undef] $ports = [],
Variant[String,Array,Undef] $labels = [],
Variant[String,Array,Undef] $expose = [],
Variant[String,Array,Undef] $volumes = [],
Variant[String,Array,Undef] $links = [],
Optional[Boolean] $use_name = false,
Optional[Boolean] $running = true,
Variant[String,Array,Undef] $volumes_from = [],
Optional[String] $net = 'bridge',
Variant[String,Boolean] $username = false,
Variant[String,Boolean] $hostname = false,
Variant[String,Array,Undef] $env = [],
Variant[String,Array,Undef] $env_file = [],
Variant[String,Array,Undef] $dns = [],
Variant[String,Array,Undef] $dns_search = [],
Variant[String,Array,Undef] $lxc_conf = [],
Optional[String] $service_prefix = 'docker-',
Optional[Boolean] $restart_service = true,
Optional[Boolean] $restart_service_on_docker_refresh = true,
Optional[Boolean] $manage_service = true,
Variant[String,Boolean] $docker_service = false,
Optional[Boolean] $disable_network = false,
Optional[Boolean] $privileged = false,
Optional[Boolean] $detach = undef,
Variant[String,Array[String],Undef] $extra_parameters = undef,
Optional[String] $systemd_restart = 'on-failure',
Variant[String,Hash,Undef] $extra_systemd_parameters = {},
Optional[Boolean] $pull_on_start = false,
Variant[String,Array,Undef] $after = [],
Variant[String,Array,Undef] $after_service = [],
Variant[String,Array,Undef] $depends = [],
Variant[String,Array,Undef] $depend_services = [],
Optional[Boolean] $tty = false,
Variant[String,Array,Undef] $socket_connect = [],
Variant[String,Array,Undef] $hostentries = [],
Optional[String] $restart = undef,
Variant[String,Boolean] $before_start = false,
Variant[String,Boolean] $before_stop = false,
Optional[Boolean] $remove_container_on_start = true,
Optional[Boolean] $remove_container_on_stop = true,
Optional[Boolean] $remove_volume_on_start = false,
Optional[Boolean] $remove_volume_on_stop = false,
Optional[Integer] $stop_wait_time = 0,
Optional[String] $syslog_identifier = undef,
Optional[Boolean] $read_only = false,
Optional[String] $health_check_cmd = undef,
Optional[Boolean] $restart_on_unhealthy = false,
) {
include docker::params
if ($socket_connect != []) {
$sockopts = join(any2array($socket_connect), ',')
$docker_command = "${docker::params::docker_command} -H ${sockopts}"
}else {
$docker_command = $docker::params::docker_command
}
$service_name = $docker::params::service_name
$docker_group = $docker::params::docker_group
if $restart {
assert_type(Pattern[/^(no|always|unless-stopped|on-failure)|^on-failure:[\d]+$/], $restart)
}
if ($remove_volume_on_start and !$remove_container_on_start) {
fail translate(("In order to remove the volume on start for ${title} you need to also remove the container"))
}
if ($remove_volume_on_stop and !$remove_container_on_stop) {
fail translate(("In order to remove the volume on stop for ${title} you need to also remove the container"))
}
if $use_name {
notify { "docker use_name warning: ${title}":
message => 'The use_name parameter is no-longer required and will be removed in a future release',
withpath => true,
}
}
if $systemd_restart {
assert_type(Pattern[/^(no|always|on-success|on-failure|on-abnormal|on-abort|on-watchdog)$/], $systemd_restart)
}
if $detach == undef {
$valid_detach = $docker::params::detach_service_in_init
} else {
$valid_detach = $detach
}
$extra_parameters_array = any2array($extra_parameters)
$after_array = any2array($after)
$depends_array = any2array($depends)
$depend_services_array = any2array($depend_services)
$docker_run_flags = docker_run_flags({
cpuset => any2array($cpuset),
detach => $valid_detach,
disable_network => $disable_network,
dns => any2array($dns),
dns_search => any2array($dns_search),
env => any2array($env),
env_file => any2array($env_file),
expose => any2array($expose),
extra_params => any2array($extra_parameters),
hostentries => any2array($hostentries),
hostname => $hostname,
links => any2array($links),
lxc_conf => any2array($lxc_conf),
memory_limit => $memory_limit,
net => $net,
ports => any2array($ports),
labels => any2array($labels),
privileged => $privileged,
socket_connect => any2array($socket_connect),
tty => $tty,
username => $username,
volumes => any2array($volumes),
volumes_from => any2array($volumes_from),
read_only => $read_only,
health_check_cmd => $health_check_cmd,
restart_on_unhealthy => $restart_on_unhealthy,
})
$sanitised_title = regsubst($title, '[^0-9A-Za-z.\-_]', '-', 'G')
if empty($depends_array) {
$sanitised_depends_array = []
}
else {
$sanitised_depends_array = regsubst($depends_array, '[^0-9A-Za-z.\-_]', '-', 'G')
}
if empty($after_array) {
$sanitised_after_array = []
}
else {
$sanitised_after_array = regsubst($after_array, '[^0-9A-Za-z.\-_]', '-', 'G')
}
if $::osfamily == 'windows' {
$exec_environment = 'PATH=C:/Program Files/Docker/;C:/Windows/System32/'
$exec_timeout = 3000
$exec_path = ['c:/Windows/Temp/', 'C:/Program Files/Docker/']
$exec_provider = 'powershell'
$cidfile = "c:/Windows/Temp/${service_prefix}${sanitised_title}.cid"
$restart_check = "${docker_command} inspect ${sanitised_title} -f '{{ if eq \\\"unhealthy\\\" .State.Health.Status }} {{ .Name }}{{ end }}' | findstr ${sanitised_title}"
} else {
$exec_environment = 'HOME=/root'
$exec_path = ['/bin', '/usr/bin']
$exec_timeout = 0
$exec_provider = undef
$cidfile = "/var/run/${service_prefix}${sanitised_title}.cid"
$restart_check = "${docker_command} inspect ${sanitised_title} -f '{{ if eq \"unhealthy\" .State.Health.Status }} {{ .Name }}{{ end }}' | grep ${sanitised_title}"
}
if $restart_on_unhealthy {
exec { "Restart unhealthy container ${title} with docker":
command => "${docker_command} restart ${sanitised_title}",
onlyif => $restart_check,
environment => $exec_environment,
path => $exec_path,
provider => $exec_provider,
timeout => $exec_timeout
}
}
if $restart {
if $ensure == 'absent' {
exec { "stop ${title} with docker":
command => "${docker_command} stop --time=${stop_wait_time} ${sanitised_title}",
onlyif => "${docker_command} inspect ${sanitised_title}",
environment => $exec_environment,
path => $exec_path,
provider => $exec_provider,
timeout => $exec_timeout
}
exec { "remove ${title} with docker":
command => "${docker_command} rm -v ${sanitised_title}",
onlyif => "${docker_command} inspect ${sanitised_title}",
environment => $exec_environment,
path => $exec_path,
provider => $exec_provider,
timeout => $exec_timeout
}
file { $cidfile:
ensure => absent,
}
}
else {
$run_with_docker_command = [
"${docker_command} run -d ${docker_run_flags}",
"--name ${sanitised_title} --cidfile=${cidfile}",
"--restart=\"${restart}\" ${image} ${command}",
]
exec { "run ${title} with docker":
command => join($run_with_docker_command, ' '),
unless => "${docker_command} inspect ${sanitised_title}",
environment => $exec_environment,
path => $exec_path,
provider => $exec_provider,
timeout => $exec_timeout
}
if $running == false {
exec { "stop ${title} with docker":
command => "${docker_command} stop --time=${stop_wait_time} ${sanitised_title}",
unless => "${docker_command} inspect ${sanitised_title} -f \"{{ if (.State.Running) }} {{ nil }}{{ end }}\"",
environment => $exec_environment,
path => $exec_path,
provider => $exec_provider,
timeout => $exec_timeout
}
} else {
exec { "start ${title} with docker":
command => "${docker_command} start ${sanitised_title}",
onlyif => "${docker_command} inspect ${sanitised_title} -f \"{{ if (.State.Running) }} {{ nil }}{{ end }}\"",
environment => $exec_environment,
path => $exec_path,
provider => $exec_provider,
timeout => $exec_timeout
}
}
}
} else {
case $docker::params::service_provider {
'systemd': {
$initscript = "/etc/systemd/system/${service_prefix}${sanitised_title}.service"
$runscript = "/usr/local/bin/docker-run-${sanitised_title}.sh"
$run_template = 'docker/usr/local/bin/docker-run.sh.erb'
$init_template = 'docker/etc/systemd/system/docker-run.erb'
$mode = '0640'
}
'upstart': {
$initscript = "/etc/init.d/${service_prefix}${sanitised_title}"
$init_template = 'docker/etc/init.d/docker-run.erb'
$mode = '0750'
$runscript = undef
$run_template = undef
}
default: {
if $::osfamily != 'windows' {
fail translate(('Docker needs a Debian or RedHat based system.'))
}
elsif $ensure == 'present' {
fail translate(('Restart parameter is required for Windows'))
}
}
}
if $syslog_identifier {
$_syslog_identifier = $syslog_identifier
} else {
$_syslog_identifier = "${service_prefix}${sanitised_title}"
}
if $ensure == 'absent' {
if $::osfamily == 'windows'{
exec {
"stop container ${service_prefix}${sanitised_title}":
command => "${docker_command} stop --time=${stop_wait_time} ${sanitised_title}",
onlyif => "${docker_command} inspect ${sanitised_title}",
environment => $exec_environment,
path => $exec_path,
provider => $exec_provider,
timeout => $exec_timeout,
notify => Exec["remove container ${service_prefix}${sanitised_title}"]
}
}
else {
service { "${service_prefix}${sanitised_title}":
ensure => false,
enable => false,
hasstatus => $docker::params::service_hasstatus,
}
}
exec {
"remove container ${service_prefix}${sanitised_title}":
command => "${docker_command} rm -v ${sanitised_title}",
onlyif => "${docker_command} inspect ${sanitised_title}",
environment => $exec_environment,
path => $exec_path,
refreshonly => true,
provider => $exec_provider,
timeout => $exec_timeout
}
if $::osfamily != 'windows' {
file { "/etc/systemd/system/${service_prefix}${sanitised_title}.service":
ensure => absent,
path => "/etc/systemd/system/${service_prefix}${sanitised_title}.service",
}
}
else {
file { $cidfile:
ensure => absent,
}
}
}
else {
if ($runscript) {
file { $runscript:
ensure => present,
content => template($run_template),
owner => 'root',
group => $docker_group,
mode => '0770'
}
}
file { $initscript:
ensure => present,
content => template($init_template),
owner => 'root',
group => $docker_group,
mode => $mode,
}
if $manage_service {
if $running == false {
service { "${service_prefix}${sanitised_title}":
ensure => $running,
enable => false,
hasstatus => $docker::params::service_hasstatus,
require => File[$initscript],
}
}
else {
# Transition help from moving from CID based container detection to
# Name-based container detection. See #222 for context.
# This code should be considered temporary until most people have
# transitioned. - 2015-04-15
if $initscript == "/etc/init.d/${service_prefix}${sanitised_title}" {
# This exec sequence will ensure the old-style CID container is stopped
# before we replace the init script with the new-style.
$transition_onlyif = [
"/usr/bin/test -f /var/run/docker-${sanitised_title}.cid &&",
"/usr/bin/test -f /etc/init.d/${service_prefix}${sanitised_title}",
]
exec { "/bin/sh /etc/init.d/${service_prefix}${sanitised_title} stop":
onlyif => join($transition_onlyif, ' '),
require => [],
}
-> file { "/var/run/${service_prefix}${sanitised_title}.cid":
ensure => absent,
}
-> File[$initscript]
}
service { "${service_prefix}${sanitised_title}":
ensure => $running,
enable => true,
provider => $docker::params::service_provider,
hasstatus => $docker::params::service_hasstatus,
require => File[$initscript],
}
}
if $docker_service {
if $docker_service == true {
Service['docker'] -> Service["${service_prefix}${sanitised_title}"]
if $restart_service_on_docker_refresh == true {
Service['docker'] ~> Service["${service_prefix}${sanitised_title}"]
}
} else {
Service[$docker_service] -> Service["${service_prefix}${sanitised_title}"]
if $restart_service_on_docker_refresh == true {
Service[$docker_service] ~> Service["${service_prefix}${sanitised_title}"]
}
}
}
}
if $docker::params::service_provider == 'systemd' {
exec { "docker-${sanitised_title}-systemd-reload":
path => ['/bin/', '/sbin/', '/usr/bin/', '/usr/sbin/'],
command => 'systemctl daemon-reload',
refreshonly => true,
require => [File[$initscript],File[$runscript]],
subscribe => [File[$initscript],File[$runscript]]
}
Exec["docker-${sanitised_title}-systemd-reload"] -> Service<| title == "${service_prefix}${sanitised_title}" |>
}
if $restart_service {
[File[$initscript],File[$runscript]] ~> Service<| title == "${service_prefix}${sanitised_title}" |>
}
else {
[File[$initscript],File[$runscript]] -> Service<| title == "${service_prefix}${sanitised_title}" |>
}
}
}
}