From 3ef7526eccdf2faddfae197b8dbcaae2de8cc3cc Mon Sep 17 00:00:00 2001 From: Ben Roberts Date: Sun, 1 Jul 2018 23:59:15 +0100 Subject: [PATCH] Allow overriding service_provider on docker::run Its possible to override the `service_provider` when managing the docker daemon in case the defaults in params.pp aren't correct, but not when defining a container using `docker::run`. This change adds a new `service_provider` parameter to the run defined type, so that containers can also be managed in these situations. Fixes garethr/garethr-docker#755 --- manifests/run.pp | 68 ++++++++++++++++++---------------------- spec/defines/run_spec.rb | 5 +++ 2 files changed, 35 insertions(+), 38 deletions(-) diff --git a/manifests/run.pp b/manifests/run.pp index 74ddbae17..8b7d49078 100755 --- a/manifests/run.pp +++ b/manifests/run.pp @@ -81,6 +81,7 @@ $dns = [], $dns_search = [], $lxc_conf = [], + $service_provider = undef, $service_prefix = 'docker-', $restart_service = true, $restart_service_on_docker_refresh = true, @@ -140,6 +141,13 @@ if $hostname { validate_string($hostname) } + if $service_provider == undef { + $valid_service_provider = $docker::params::service_provider + } else { + validate_string($service_provider) + $valid_service_provider = $service_provider + } + validate_bool($running) validate_bool($disable_network) validate_bool($privileged) @@ -245,54 +253,38 @@ } } else { - case $::osfamily { - 'Debian': { - $deprecated_initscript = "/etc/init/${service_prefix}${sanitised_title}.conf" - $hasstatus = true - if ($::operatingsystem == 'Debian' and versioncmp($::operatingsystemmajrelease, '8') >= 0) or - ($::operatingsystem == 'Ubuntu' and versioncmp($::operatingsystemrelease, '15.04') >= 0) { - $initscript = "/etc/systemd/system/${service_prefix}${sanitised_title}.service" - $init_template = 'docker/etc/systemd/system/docker-run.erb' - $uses_systemd = true - $mode = '0640' - } else { - $uses_systemd = false + if $valid_service_provider == 'systemd' { + $initscript = "/etc/systemd/system/${service_prefix}${sanitised_title}.service" + $init_template = 'docker/etc/systemd/system/docker-run.erb' + $hasstatus = true + $mode = '0640' + $uses_systemd = true + } else { + $uses_systemd = false + + case $::osfamily { + 'Debian': { + $deprecated_initscript = "/etc/init/${service_prefix}${sanitised_title}.conf" $initscript = "/etc/init.d/${service_prefix}${sanitised_title}" $init_template = 'docker/etc/init.d/docker-run.erb' + $hasstatus = true $mode = '0750' } - } - 'RedHat': { - if ($::operatingsystem == 'Amazon') or (versioncmp($::operatingsystemrelease, '7.0') < 0) { + 'RedHat': { $initscript = "/etc/init.d/${service_prefix}${sanitised_title}" $init_template = 'docker/etc/init.d/docker-run.erb' $hasstatus = undef $mode = '0750' - $uses_systemd = false - } else { - $initscript = "/etc/systemd/system/${service_prefix}${sanitised_title}.service" - $init_template = 'docker/etc/systemd/system/docker-run.erb' + } + 'Gentoo': { + $initscript = "/etc/init.d/${service_prefix}${sanitised_title}" + $init_template = 'docker/etc/init.d/docker-run.gentoo.erb' $hasstatus = true - $mode = '0640' - $uses_systemd = true + $mode = '0770' + } + default: { + fail('Docker needs a Debian, RedHat, Gentoo or other systemd-based system.') } - } - 'Archlinux': { - $initscript = "/etc/systemd/system/${service_prefix}${sanitised_title}.service" - $init_template = 'docker/etc/systemd/system/docker-run.erb' - $hasstatus = true - $mode = '0640' - $uses_systemd = true - } - 'Gentoo': { - $initscript = "/etc/init.d/${service_prefix}${sanitised_title}" - $init_template = 'docker/etc/init.d/docker-run.gentoo.erb' - $hasstatus = true - $mode = '0770' - $uses_systemd = false - } - default: { - fail('Docker needs a Debian, RedHat, Archlinux or Gentoo based system.') } } diff --git a/spec/defines/run_spec.rb b/spec/defines/run_spec.rb index d6c53c5f5..2b8a44884 100755 --- a/spec/defines/run_spec.rb +++ b/spec/defines/run_spec.rb @@ -145,6 +145,11 @@ end end + context 'when passing `service_provider`' do + let(:params) { {'command' => 'command', 'image' => 'base', 'service_provider' => 'systemd'} } + it { should contain_file('/etc/systemd/system/docker-sample.service') } + end + context 'removing containers and volumes' do context 'when trying to remove the volume and not the container on stop' do let(:params) {{