Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create shared start/stop scripts for better extensibility #367

Merged
merged 9 commits into from
Nov 28, 2018
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,6 @@ This is equivalent to running the `docker run -d base /bin/sh -c "while true; d
```puppet
docker::run { 'helloworld':
image => 'base',
detach => true,
service_prefix => 'docker-',
command => '/bin/sh -c "while true; do echo hello world; sleep 1; done"',
ports => ['4444', '4555'],
Expand Down
4 changes: 0 additions & 4 deletions lib/puppet/parser/functions/docker_run_flags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ module Puppet::Parser::Functions
flags << '--privileged'
end

if opts['detach']
flags << '--detach=true'
end

if opts['health_check_cmd'] && opts['health_check_cmd'].to_s != 'undef'
flags << "--health-cmd='#{opts['health_check_cmd']}'"
end
Expand Down
63 changes: 42 additions & 21 deletions manifests/run.pp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@
# (optional) Specify an additional unless for the Docker run command when using restart.
# Default: undef
#
# [*after_create*]
# (optional) Specifies the command to execute after container is created but before it is started.
# Default: undef
#
define docker::run(
Optional[Pattern[/^[\S]*$/]] $image,
Optional[Pattern[/^present$|^absent$/]] $ensure = 'present',
Expand Down Expand Up @@ -105,7 +109,6 @@
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 = {},
Expand All @@ -120,6 +123,7 @@
Optional[String] $restart = undef,
Variant[String,Boolean] $before_start = false,
Variant[String,Boolean] $before_stop = false,
Optional[String] $after_create = undef,
Optional[Boolean] $remove_container_on_start = true,
Optional[Boolean] $remove_container_on_stop = true,
Optional[Boolean] $remove_volume_on_start = false,
Expand Down Expand Up @@ -165,20 +169,13 @@
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),
Expand Down Expand Up @@ -320,20 +317,25 @@
}
} else {

$docker_run_inline_start = template('docker/docker-run-start.erb')
$docker_run_inline_stop = template('docker/docker-run-stop.erb')

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'
$startscript = "/usr/local/bin/docker-run-${sanitised_title}-start.sh"
$stopscript = "/usr/local/bin/docker-run-${sanitised_title}-stop.sh"
$startstop_template = 'docker/usr/local/bin/docker-run.sh.epp'
$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
$startscript = undef
$stopscript = undef
$starstop_template = undef
}
default: {
if $::osfamily != 'windows' {
Expand Down Expand Up @@ -386,6 +388,16 @@
ensure => absent,
path => "/etc/systemd/system/${service_prefix}${sanitised_title}.service",
}
if ($startscript) {
file { $startscript:
ensure => absent
}
}
if ($stopscript) {
file { $stopscript:
ensure => absent
}
}
}
else {
file { $cidfile:
Expand All @@ -394,10 +406,19 @@
}
}
else {
if ($runscript) {
file { $runscript:
if ($startscript) {
file { $startscript:
ensure => present,
content => epp($startstop_template, {'script' => $docker_run_inline_start}),
owner => 'root',
group => $docker_group,
mode => '0770'
}
}
if ($stopscript) {
file { $stopscript:
ensure => present,
content => template($run_template),
content => epp($startstop_template, {'script' => $docker_run_inline_stop}),
owner => 'root',
group => $docker_group,
mode => '0770'
Expand Down Expand Up @@ -471,23 +492,23 @@
path => ['/bin/', '/sbin/', '/usr/bin/', '/usr/sbin/'],
command => 'systemctl daemon-reload',
refreshonly => true,
require => [File[$initscript],File[$runscript]],
subscribe => [File[$initscript],File[$runscript]]
require => [File[$initscript],File[$startscript],File[$stopscript]],
subscribe => [File[$initscript],File[$startscript],File[$stopscript]]
}
Exec["docker-${sanitised_title}-systemd-reload"] -> Service<| title == "${service_prefix}${sanitised_title}" |>
}

if $restart_service {
if $runscript {
[File[$initscript],File[$runscript]] ~> Service<| title == "${service_prefix}${sanitised_title}" |>
if $startscript or $stopscript {
[File[$initscript],File[$startscript],File[$stopscript]] ~> Service<| title == "${service_prefix}${sanitised_title}" |>
}
else {
[File[$initscript]] ~> Service<| title == "${service_prefix}${sanitised_title}" |>
}
}
else {
if $runscript {
[File[$initscript],File[$runscript]] -> Service<| title == "${service_prefix}${sanitised_title}" |>
if $startscript or $stopscript {
[File[$initscript],File[$startscript],File[$stopscript]] -> Service<| title == "${service_prefix}${sanitised_title}" |>
}
else {
[File[$initscript]] -> Service<| title == "${service_prefix}${sanitised_title}" |>
Expand Down
Loading