Skip to content

Commit

Permalink
feat(docker::run): add the enable param
Browse files Browse the repository at this point in the history
  • Loading branch information
reppard committed Apr 27, 2016
1 parent dc123cf commit 3861829
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 23 deletions.
48 changes: 31 additions & 17 deletions manifests/run.pp
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand All @@ -293,7 +307,7 @@
if $running == false {
service { "${service_prefix}${sanitised_title}":
ensure => $running,
enable => false,
enable => $real_enable,
hasstatus => $hasstatus,
require => File[$initscript],
}
Expand All @@ -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],
Expand Down
52 changes: 46 additions & 6 deletions spec/acceptance/docker_full_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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':}
Expand Down Expand Up @@ -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'],
Expand All @@ -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'],
Expand All @@ -511,15 +511,56 @@ 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'

# 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

Expand Down Expand Up @@ -569,4 +610,3 @@ class { 'docker':}
end
end
end

13 changes: 13 additions & 0 deletions spec/defines/run_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3861829

Please sign in to comment.