diff --git a/manifests/run.pp b/manifests/run.pp index bbda5db75..a3bbef7d0 100755 --- a/manifests/run.pp +++ b/manifests/run.pp @@ -36,6 +36,11 @@ # script. Disabling this may be useful if integrating with existing modules. # Default: true # +# [*enable*] +# (optional) Whether or not to enable puppet Service. If left unset enable in +# Service resource will be set to the value of [*running*]. +# Default: undef +# # [*docker_service*] # (optional) If (and how) the Docker service itself is managed by Puppet # true -> Service['docker'] @@ -74,6 +79,7 @@ $restart_service = true, $restart_service_on_docker_refresh = true, $manage_service = true, + $enable = undef, $docker_service = false, $disable_network = false, $privileged = false, @@ -264,23 +270,31 @@ } + $real_enable = $enable ? { undef => $running, default => $enable } + if $ensure == 'absent' { - service { "${service_prefix}${sanitised_title}": - ensure => false, - enable => false, - hasstatus => $hasstatus, - } + service { "${service_prefix}${sanitised_title}": + ensure => false, + enable => false, + hasstatus => $hasstatus, + } - exec { - "remove container ${service_prefix}${sanitised_title}": - command => "${docker_command} rm -v ${sanitised_title}", - refreshonly => true, - onlyif => "${docker_command} ps --no-trunc -a --format='table {{.Names}}' | grep '^${sanitised_title}$'", - path => ['/bin', '/usr/bin'], - environment => 'HOME=/root', - timeout => 0 - } + exec { + "remove container ${service_prefix}${sanitised_title}": + command => "${docker_command} rm -v ${sanitised_title}", + refreshonly => true, + onlyif => "${docker_command} ps --no-trunc -a --format='table {{.Names}}' | grep '^${sanitised_title}$'", + path => ['/bin', '/usr/bin'], + environment => 'HOME=/root', + timeout => 0 + } + unless $uses_systemd { + file { "/etc/init.d/${service_prefix}${sanitised_title}.override": + ensure => present, + content => 'manual', + } + } } else { file { $initscript: @@ -293,7 +307,7 @@ if $running == false { service { "${service_prefix}${sanitised_title}": ensure => $running, - enable => false, + enable => $real_enable, hasstatus => $hasstatus, require => File[$initscript], } @@ -319,12 +333,12 @@ if $uses_systemd { $provider = 'systemd' } else { - $provider = undef + $provider = 'upstart' } service { "${service_prefix}${sanitised_title}": ensure => $running, - enable => true, + enable => $real_enable, provider => $provider, hasstatus => $hasstatus, require => File[$initscript], diff --git a/spec/acceptance/docker_full_spec.rb b/spec/acceptance/docker_full_spec.rb index 7700a21d3..af9a32d5f 100644 --- a/spec/acceptance/docker_full_spec.rb +++ b/spec/acceptance/docker_full_spec.rb @@ -92,7 +92,7 @@ class { 'docker': end describe 'docker::image' do - + it 'should successfully download an image from the Docker Hub' do pp=<<-EOS class { 'docker':} @@ -484,7 +484,7 @@ class { 'docker':} require => Class['docker'], } - docker::run { 'container_3_6': + docker::run { 'container_3_7': image => 'ubuntu', command => 'init', require => Docker::Image['ubuntu'], @@ -498,7 +498,7 @@ class { 'docker':} require => Class['docker'], } - docker::run { 'container_3_6': + docker::run { 'container_3_7': ensure => 'absent', image => 'ubuntu', require => Docker::Image['ubuntu'], @@ -511,7 +511,7 @@ class { 'docker':} # A sleep to give docker time to execute properly sleep 4 - shell('docker inspect container-3-6', :acceptable_exit_codes => [0]) + shell('docker inspect container-3-7', :acceptable_exit_codes => [0]) apply_manifest(pp2, :catch_failures => true) apply_manifest(pp2, :catch_changes => true) unless fact('selinux') == 'true' @@ -519,7 +519,48 @@ class { 'docker':} # A sleep to give docker time to execute properly sleep 4 - shell('docker inspect container-3-6', :acceptable_exit_codes => [1]) + shell('docker inspect container-3-7', :acceptable_exit_codes => [1]) + + if fact('osfamily') == 'RedHat' + shell('systemctl is-enabled docker-container-3-7', :acceptable_exit_codes => [1]) do |r| + expect(r.stdout).to match('disabled') + end + else + shell('ls /etc/init.d/docker-container-3-7.override | wc -l') do |r| + expect(r.stdout).to match(/^1$/) + end + end + end + + it 'should allow enabling a container but not starting it' do + pp=<<-EOS + class { 'docker':} + + docker::image { 'ubuntu': + require => Class['docker'], + } + + docker::run { 'container_3_8': + image => 'ubuntu', + command => 'init', + running => false, + enable => true, + require => Docker::Image['ubuntu'], + } + EOS + + apply_manifest(pp, :catch_failures => true) + apply_manifest(pp, :catch_changes => true) unless fact('selinux') == 'true' + + shell('docker ps | wc -l') do |r| + expect(r.stdout).to match(/^1$/) + end + + if fact('osfamily') == 'RedHat' + shell('systemctl is-enabled docker-container-3-8') do |r| + expect(r.stdout).to match('enabled') + end + end end end @@ -569,4 +610,3 @@ class { 'docker':} end end end - diff --git a/spec/defines/run_spec.rb b/spec/defines/run_spec.rb index a6a6b7359..4111959f8 100755 --- a/spec/defines/run_spec.rb +++ b/spec/defines/run_spec.rb @@ -249,6 +249,19 @@ context 'when stopping the service' do let(:params) { {'command' => 'command', 'image' => 'base', 'running' => false} } it { should contain_service('docker-sample').with_ensure(false) } + it { should contain_service('docker-sample').with_enable(false) } + end + + context 'when stopping the service and it needs to remain enabled' do + let(:params) { {'command' => 'command', 'image' => 'base', 'running' => false, 'enable' => true} } + it { should contain_service('docker-sample').with_ensure(false) } + it { should contain_service('docker-sample').with_enable(true) } + end + + context 'when disabling the service' do + # running defaults to true + let(:params) {{ 'command' => 'command', 'image' => 'base', 'enable' => false} } + it { should contain_service('docker-sample').with_enable(false) } end context 'when passing a memory limit in bytes' do