|
48 | 48 | # anything else -> Service[docker_service] |
49 | 49 | # Default: false |
50 | 50 | # |
| 51 | +# [*health_check_cmd*] |
| 52 | +# (optional) Specifies the command to execute to check that the container is healthy using the docker health check functionality. |
| 53 | +# Default: undef |
| 54 | +# |
| 55 | +# [*restart_on_unhealthy*] |
| 56 | +# (optional) Checks the health status of Docker container and if it is unhealthy the service will be restarted. |
| 57 | +# The health_check_cmd parameter must be set to true to use this functionality. |
| 58 | +# Default: undef |
| 59 | +# |
51 | 60 | # [*extra_parameters*] |
52 | 61 | # An array of additional command line arguments to pass to the `docker run` |
53 | 62 | # command. Useful for adding additional new or experimental options that the |
|
108 | 117 | Optional[Boolean] $remove_volume_on_start = false, |
109 | 118 | Optional[Boolean] $remove_volume_on_stop = false, |
110 | 119 | Optional[Integer] $stop_wait_time = 0, |
111 | | - Optional[String] $syslog_identifier = undef, |
| 120 | + Optional[String] $syslog_identifier = undef, |
112 | 121 | Optional[Boolean] $read_only = false, |
| 122 | + Optional[String] $health_check_cmd = undef, |
| 123 | + Optional[Boolean] $restart_on_unhealthy = false, |
113 | 124 | ) { |
114 | | - include docker |
| 125 | + include docker::params |
115 | 126 | if ($socket_connect != []) { |
116 | 127 | $sockopts = join(any2array($socket_connect), ',') |
117 | | - $docker_command = "${docker::docker_command} -H ${sockopts}" |
| 128 | + $docker_command = "${docker::params::docker_command} -H ${sockopts}" |
118 | 129 | }else { |
119 | | - $docker_command = $docker::docker_command |
| 130 | + $docker_command = $docker::params::docker_command |
120 | 131 | } |
121 | | - $service_name = $docker::service_name |
122 | | - $docker_group = $docker::docker_group |
| 132 | + $service_name = $docker::params::service_name |
| 133 | + $docker_group = $docker::params::docker_group |
123 | 134 |
|
124 | 135 | if $restart { |
125 | 136 | assert_type(Pattern[/^(no|always|unless-stopped|on-failure)|^on-failure:[\d]+$/], $restart) |
|
156 | 167 | $depend_services_array = any2array($depend_services) |
157 | 168 |
|
158 | 169 | $docker_run_flags = docker_run_flags({ |
159 | | - cpuset => any2array($cpuset), |
160 | | - detach => $valid_detach, |
161 | | - disable_network => $disable_network, |
162 | | - dns => any2array($dns), |
163 | | - dns_search => any2array($dns_search), |
164 | | - env => any2array($env), |
165 | | - env_file => any2array($env_file), |
166 | | - expose => any2array($expose), |
167 | | - extra_params => any2array($extra_parameters), |
168 | | - hostentries => any2array($hostentries), |
169 | | - hostname => $hostname, |
170 | | - links => any2array($links), |
171 | | - lxc_conf => any2array($lxc_conf), |
172 | | - memory_limit => $memory_limit, |
173 | | - net => $net, |
174 | | - ports => any2array($ports), |
175 | | - labels => any2array($labels), |
176 | | - privileged => $privileged, |
177 | | - socket_connect => any2array($socket_connect), |
178 | | - tty => $tty, |
179 | | - username => $username, |
180 | | - volumes => any2array($volumes), |
181 | | - volumes_from => any2array($volumes_from), |
182 | | - read_only => $read_only, |
| 170 | + cpuset => any2array($cpuset), |
| 171 | + detach => $valid_detach, |
| 172 | + disable_network => $disable_network, |
| 173 | + dns => any2array($dns), |
| 174 | + dns_search => any2array($dns_search), |
| 175 | + env => any2array($env), |
| 176 | + env_file => any2array($env_file), |
| 177 | + expose => any2array($expose), |
| 178 | + extra_params => any2array($extra_parameters), |
| 179 | + hostentries => any2array($hostentries), |
| 180 | + hostname => $hostname, |
| 181 | + links => any2array($links), |
| 182 | + lxc_conf => any2array($lxc_conf), |
| 183 | + memory_limit => $memory_limit, |
| 184 | + net => $net, |
| 185 | + ports => any2array($ports), |
| 186 | + labels => any2array($labels), |
| 187 | + privileged => $privileged, |
| 188 | + socket_connect => any2array($socket_connect), |
| 189 | + tty => $tty, |
| 190 | + username => $username, |
| 191 | + volumes => any2array($volumes), |
| 192 | + volumes_from => any2array($volumes_from), |
| 193 | + read_only => $read_only, |
| 194 | + health_check_cmd => $health_check_cmd, |
| 195 | + restart_on_unhealthy => $restart_on_unhealthy, |
183 | 196 | }) |
184 | 197 |
|
185 | 198 | $sanitised_title = regsubst($title, '[^0-9A-Za-z.\-_]', '-', 'G') |
|
197 | 210 | $sanitised_after_array = regsubst($after_array, '[^0-9A-Za-z.\-_]', '-', 'G') |
198 | 211 | } |
199 | 212 |
|
| 213 | + if $::osfamily == 'windows' { |
| 214 | + $exec_environment = 'PATH=C:/Program Files/Docker/' |
| 215 | + $exec_timeout = 3000 |
| 216 | + $exec_path = ['c:/Windows/Temp/', 'C:/Program Files/Docker/'] |
| 217 | + $exec_provider = 'powershell' |
| 218 | + $cidfile = "c:/Windows/Temp/${service_prefix}${sanitised_title}.cid" |
| 219 | + } else { |
| 220 | + $exec_environment = 'HOME=/root' |
| 221 | + $exec_path = ['/bin', '/usr/bin'] |
| 222 | + $exec_timeout = 0 |
| 223 | + $exec_provider = undef |
| 224 | + $cidfile = "/var/run/${service_prefix}${sanitised_title}.cid" |
| 225 | + $restart_check = "${docker_command} inspect ${sanitised_title} -f '{{ if eq \"unhealthy\" .State.Health.Status }} {{ .Name }}{{ end }}' | grep ${sanitised_title}" |
| 226 | + } |
| 227 | + |
| 228 | + if $restart_on_unhealthy { |
| 229 | + exec { "Restart unhealthy container ${title} with docker": |
| 230 | + command => "${docker_command} restart ${sanitised_title}", |
| 231 | + onlyif => $restart_check, |
| 232 | + environment => $exec_environment, |
| 233 | + path => $exec_path, |
| 234 | + provider => $exec_provider, |
| 235 | + timeout => $exec_timeout |
| 236 | + } |
| 237 | + } |
| 238 | + |
200 | 239 | if $restart { |
| 240 | + if $ensure == 'absent' { |
| 241 | + exec { "stop ${title} with docker": |
| 242 | + command => "${docker_command} stop --time=${stop_wait_time} ${sanitised_title}", |
| 243 | + onlyif => "${docker_command} inspect ${sanitised_title}", |
| 244 | + environment => $exec_environment, |
| 245 | + path => $exec_path, |
| 246 | + provider => $exec_provider, |
| 247 | + timeout => $exec_timeout |
| 248 | + } |
201 | 249 |
|
202 | | - $cidfile = "/var/run/${service_prefix}${sanitised_title}.cid" |
| 250 | + exec { "remove ${title} with docker": |
| 251 | + command => "${docker_command} rm -v ${sanitised_title}", |
| 252 | + onlyif => "${docker_command} inspect ${sanitised_title}", |
| 253 | + environment => $exec_environment, |
| 254 | + path => $exec_path, |
| 255 | + provider => $exec_provider, |
| 256 | + timeout => $exec_timeout |
| 257 | + } |
| 258 | + |
| 259 | + file { $cidfile: |
| 260 | + ensure => absent, |
| 261 | + } |
| 262 | + } |
| 263 | + else { |
| 264 | + $run_with_docker_command = [ |
| 265 | + "${docker_command} run -d ${docker_run_flags}", |
| 266 | + "--name ${sanitised_title} --cidfile=${cidfile}", |
| 267 | + "--restart=\"${restart}\" ${image} ${command}", |
| 268 | + ] |
| 269 | + exec { "run ${title} with docker": |
| 270 | + command => join($run_with_docker_command, ' '), |
| 271 | + unless => "${docker_command} inspect ${sanitised_title}", |
| 272 | + environment => $exec_environment, |
| 273 | + path => $exec_path, |
| 274 | + provider => $exec_provider, |
| 275 | + timeout => $exec_timeout |
| 276 | + } |
203 | 277 |
|
204 | | - $run_with_docker_command = [ |
205 | | - "${docker_command} run -d ${docker_run_flags}", |
206 | | - "--name ${sanitised_title} --cidfile=${cidfile}", |
207 | | - "--restart=\"${restart}\" ${image} ${command}", |
208 | | - ] |
209 | | - exec { "run ${title} with docker": |
210 | | - command => join($run_with_docker_command, ' '), |
211 | | - unless => "${docker_command} ps --no-trunc -a | grep `cat ${cidfile}`", |
212 | | - environment => 'HOME=/root', |
213 | | - path => ['/bin', '/usr/bin'], |
214 | | - timeout => 0 |
| 278 | + if $running == false { |
| 279 | + exec { "stop ${title} with docker": |
| 280 | + command => "${docker_command} stop --time=${stop_wait_time} ${sanitised_title}", |
| 281 | + unless => "${docker_command} inspect ${sanitised_title} -f \"{{ if (.State.Running) }} {{ nil }}{{ end }}\"", |
| 282 | + environment => $exec_environment, |
| 283 | + path => $exec_path, |
| 284 | + provider => $exec_provider, |
| 285 | + timeout => $exec_timeout |
| 286 | + } |
| 287 | + } else { |
| 288 | + exec { "start ${title} with docker": |
| 289 | + command => "${docker_command} start ${sanitised_title}", |
| 290 | + onlyif => "${docker_command} inspect ${sanitised_title} -f \"{{ if (.State.Running) }} {{ nil }}{{ end }}\"", |
| 291 | + environment => $exec_environment, |
| 292 | + path => $exec_path, |
| 293 | + provider => $exec_provider, |
| 294 | + timeout => $exec_timeout |
| 295 | + } |
| 296 | + } |
215 | 297 | } |
216 | 298 | } else { |
217 | 299 |
|
|
227 | 309 | $mode = '0750' |
228 | 310 | } |
229 | 311 | default: { |
230 | | - fail translate(('Docker needs a Debian or RedHat based system.')) |
| 312 | + if $::osfamily != 'windows' { |
| 313 | + fail translate(('Docker needs a Debian or RedHat based system.')) |
| 314 | + } |
| 315 | + elsif $ensure == 'present' { |
| 316 | + fail translate(('Restart parameter is required for Windows')) |
| 317 | + } |
231 | 318 | } |
232 | 319 | } |
233 | 320 |
|
|
237 | 324 | $_syslog_identifier = "${service_prefix}${sanitised_title}" |
238 | 325 | } |
239 | 326 |
|
240 | | - |
241 | 327 | if $ensure == 'absent' { |
| 328 | + if $::osfamily == 'windows'{ |
| 329 | + exec { |
| 330 | + "stop container ${service_prefix}${sanitised_title}": |
| 331 | + command => "${docker_command} stop --time=${stop_wait_time} ${sanitised_title}", |
| 332 | + onlyif => "${docker_command} inspect ${sanitised_title}", |
| 333 | + environment => $exec_environment, |
| 334 | + path => $exec_path, |
| 335 | + provider => $exec_provider, |
| 336 | + timeout => $exec_timeout, |
| 337 | + notify => Exec["remove container ${service_prefix}${sanitised_title}"] |
| 338 | + } |
| 339 | + } |
| 340 | + else { |
242 | 341 | service { "${service_prefix}${sanitised_title}": |
243 | 342 | ensure => false, |
244 | 343 | enable => false, |
245 | 344 | hasstatus => $docker::params::service_hasstatus, |
246 | 345 | } |
247 | | - |
248 | | - exec { |
249 | | - "remove container ${service_prefix}${sanitised_title}": |
250 | | - command => "${docker_command} rm -v ${sanitised_title}", |
251 | | - refreshonly => true, |
252 | | - onlyif => "${docker_command} ps --no-trunc -a --format='table {{.Names}}' | grep '^${sanitised_title}$'", |
253 | | - path => ['/bin', '/usr/bin'], |
254 | | - environment => 'HOME=/root', |
255 | | - timeout => 0 |
256 | | - } |
257 | | - |
| 346 | + } |
| 347 | + exec { |
| 348 | + "remove container ${service_prefix}${sanitised_title}": |
| 349 | + command => "${docker_command} rm -v ${sanitised_title}", |
| 350 | + onlyif => "${docker_command} inspect ${sanitised_title}", |
| 351 | + environment => $exec_environment, |
| 352 | + path => $exec_path, |
| 353 | + refreshonly => true, |
| 354 | + provider => $exec_provider, |
| 355 | + timeout => $exec_timeout |
| 356 | + } |
| 357 | + if $::osfamily != 'windows' { |
258 | 358 | file { "/etc/systemd/system/${service_prefix}${sanitised_title}.service": |
259 | 359 | ensure => absent, |
260 | 360 | path => "/etc/systemd/system/${service_prefix}${sanitised_title}.service", |
261 | 361 | } |
| 362 | + } |
| 363 | + else { |
| 364 | + file { $cidfile: |
| 365 | + ensure => absent, |
| 366 | + } |
| 367 | + } |
262 | 368 | } |
263 | 369 | else { |
264 | 370 | file { $initscript: |
|
0 commit comments