Skip to content

Commit fc67e28

Browse files
glorpendavejrt
authored andcommitted
Create shared start/stop scripts for better extensibility (#367)
* move docker-run script logic from upstart template to standalone one * convert $variables to commands in docker-run scripts * use inline docker-run scripts with systemd service * updated tests for start/stop scripts * made docker-run-start.sh compatible with systemd service logic * always run container with docker create and docker start * added new parameter to docker:run: after_create * remove start/stop scripts when ensure=absent * removed $::docker::run::detach parameter
1 parent 10bdd1e commit fc67e28

File tree

10 files changed

+166
-191
lines changed

10 files changed

+166
-191
lines changed

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,6 @@ This is equivalent to running the `docker run -d base /bin/sh -c "while true; d
324324
```puppet
325325
docker::run { 'helloworld':
326326
image => 'base',
327-
detach => true,
328327
service_prefix => 'docker-',
329328
command => '/bin/sh -c "while true; do echo hello world; sleep 1; done"',
330329
ports => ['4444', '4555'],

lib/puppet/parser/functions/docker_run_flags.rb

-4
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,6 @@ module Puppet::Parser::Functions
4242
flags << '--privileged'
4343
end
4444

45-
if opts['detach']
46-
flags << '--detach=true'
47-
end
48-
4945
if opts['health_check_cmd'] && opts['health_check_cmd'].to_s != 'undef'
5046
flags << "--health-cmd='#{opts['health_check_cmd']}'"
5147
end

manifests/run.pp

+42-21
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@
7676
# (optional) Specify an additional unless for the Docker run command when using restart.
7777
# Default: undef
7878
#
79+
# [*after_create*]
80+
# (optional) Specifies the command to execute after container is created but before it is started.
81+
# Default: undef
82+
#
7983
define docker::run(
8084
Optional[Pattern[/^[\S]*$/]] $image,
8185
Optional[Pattern[/^present$|^absent$/]] $ensure = 'present',
@@ -105,7 +109,6 @@
105109
Variant[String,Boolean] $docker_service = false,
106110
Optional[Boolean] $disable_network = false,
107111
Optional[Boolean] $privileged = false,
108-
Optional[Boolean] $detach = undef,
109112
Variant[String,Array[String],Undef] $extra_parameters = undef,
110113
Optional[String] $systemd_restart = 'on-failure',
111114
Variant[String,Hash,Undef] $extra_systemd_parameters = {},
@@ -120,6 +123,7 @@
120123
Optional[String] $restart = undef,
121124
Variant[String,Boolean] $before_start = false,
122125
Variant[String,Boolean] $before_stop = false,
126+
Optional[String] $after_create = undef,
123127
Optional[Boolean] $remove_container_on_start = true,
124128
Optional[Boolean] $remove_container_on_stop = true,
125129
Optional[Boolean] $remove_volume_on_start = false,
@@ -165,20 +169,13 @@
165169
assert_type(Pattern[/^(no|always|on-success|on-failure|on-abnormal|on-abort|on-watchdog)$/], $systemd_restart)
166170
}
167171

168-
if $detach == undef {
169-
$valid_detach = $docker::params::detach_service_in_init
170-
} else {
171-
$valid_detach = $detach
172-
}
173-
174172
$extra_parameters_array = any2array($extra_parameters)
175173
$after_array = any2array($after)
176174
$depends_array = any2array($depends)
177175
$depend_services_array = any2array($depend_services)
178176

179177
$docker_run_flags = docker_run_flags({
180178
cpuset => any2array($cpuset),
181-
detach => $valid_detach,
182179
disable_network => $disable_network,
183180
dns => any2array($dns),
184181
dns_search => any2array($dns_search),
@@ -320,20 +317,25 @@
320317
}
321318
} else {
322319

320+
$docker_run_inline_start = template('docker/docker-run-start.erb')
321+
$docker_run_inline_stop = template('docker/docker-run-stop.erb')
322+
323323
case $docker::params::service_provider {
324324
'systemd': {
325325
$initscript = "/etc/systemd/system/${service_prefix}${sanitised_title}.service"
326-
$runscript = "/usr/local/bin/docker-run-${sanitised_title}.sh"
327-
$run_template = 'docker/usr/local/bin/docker-run.sh.erb'
326+
$startscript = "/usr/local/bin/docker-run-${sanitised_title}-start.sh"
327+
$stopscript = "/usr/local/bin/docker-run-${sanitised_title}-stop.sh"
328+
$startstop_template = 'docker/usr/local/bin/docker-run.sh.epp'
328329
$init_template = 'docker/etc/systemd/system/docker-run.erb'
329330
$mode = '0640'
330331
}
331332
'upstart': {
332333
$initscript = "/etc/init.d/${service_prefix}${sanitised_title}"
333334
$init_template = 'docker/etc/init.d/docker-run.erb'
334335
$mode = '0750'
335-
$runscript = undef
336-
$run_template = undef
336+
$startscript = undef
337+
$stopscript = undef
338+
$starstop_template = undef
337339
}
338340
default: {
339341
if $::osfamily != 'windows' {
@@ -386,6 +388,16 @@
386388
ensure => absent,
387389
path => "/etc/systemd/system/${service_prefix}${sanitised_title}.service",
388390
}
391+
if ($startscript) {
392+
file { $startscript:
393+
ensure => absent
394+
}
395+
}
396+
if ($stopscript) {
397+
file { $stopscript:
398+
ensure => absent
399+
}
400+
}
389401
}
390402
else {
391403
file { $cidfile:
@@ -394,10 +406,19 @@
394406
}
395407
}
396408
else {
397-
if ($runscript) {
398-
file { $runscript:
409+
if ($startscript) {
410+
file { $startscript:
411+
ensure => present,
412+
content => epp($startstop_template, {'script' => $docker_run_inline_start}),
413+
owner => 'root',
414+
group => $docker_group,
415+
mode => '0770'
416+
}
417+
}
418+
if ($stopscript) {
419+
file { $stopscript:
399420
ensure => present,
400-
content => template($run_template),
421+
content => epp($startstop_template, {'script' => $docker_run_inline_stop}),
401422
owner => 'root',
402423
group => $docker_group,
403424
mode => '0770'
@@ -471,23 +492,23 @@
471492
path => ['/bin/', '/sbin/', '/usr/bin/', '/usr/sbin/'],
472493
command => 'systemctl daemon-reload',
473494
refreshonly => true,
474-
require => [File[$initscript],File[$runscript]],
475-
subscribe => [File[$initscript],File[$runscript]]
495+
require => [File[$initscript],File[$startscript],File[$stopscript]],
496+
subscribe => [File[$initscript],File[$startscript],File[$stopscript]]
476497
}
477498
Exec["docker-${sanitised_title}-systemd-reload"] -> Service<| title == "${service_prefix}${sanitised_title}" |>
478499
}
479500

480501
if $restart_service {
481-
if $runscript {
482-
[File[$initscript],File[$runscript]] ~> Service<| title == "${service_prefix}${sanitised_title}" |>
502+
if $startscript or $stopscript {
503+
[File[$initscript],File[$startscript],File[$stopscript]] ~> Service<| title == "${service_prefix}${sanitised_title}" |>
483504
}
484505
else {
485506
[File[$initscript]] ~> Service<| title == "${service_prefix}${sanitised_title}" |>
486507
}
487508
}
488509
else {
489-
if $runscript {
490-
[File[$initscript],File[$runscript]] -> Service<| title == "${service_prefix}${sanitised_title}" |>
510+
if $startscript or $stopscript {
511+
[File[$initscript],File[$startscript],File[$stopscript]] -> Service<| title == "${service_prefix}${sanitised_title}" |>
491512
}
492513
else {
493514
[File[$initscript]] -> Service<| title == "${service_prefix}${sanitised_title}" |>

0 commit comments

Comments
 (0)