From 7a9f83d9a6beb72b85da3da79bffb4216cb5b18f Mon Sep 17 00:00:00 2001 From: Darragh Bailey Date: Thu, 16 Mar 2017 11:31:55 +0000 Subject: [PATCH] Support refreshonly flag Allow for the refreshonly flag to be passed through to the underlying exec resource. This allows for Docker::Exec resources to be defined to only run when notified, which allows for construction of manifests that can check whether updated configuration made available to running containers (provided they are using directory mount binds) are valid before triggering a service restart. Use case for this is to test generated nginx configuration for the running nginx container using a Docker::Exec call that is notified by the file resource and in turn will notify the service resource to restart. --- README.md | 1 + manifests/exec.pp | 2 ++ spec/acceptance/docker_full_spec.rb | 49 +++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+) diff --git a/README.md b/README.md index df63d004..cf929ea9 100755 --- a/README.md +++ b/README.md @@ -751,6 +751,7 @@ docker::exec { 'cron_allow_root': command => '/bin/echo root >> /usr/lib/cron/cron.allow', tty => true, unless => 'grep root /usr/lib/cron/cron.allow 2>/dev/null', + refreshonly => true, } ``` diff --git a/manifests/exec.pp b/manifests/exec.pp index 1e70c59a..fa402675 100644 --- a/manifests/exec.pp +++ b/manifests/exec.pp @@ -10,6 +10,7 @@ Optional[String] $command = undef, Optional[String] $unless = undef, Optional[Boolean] $sanitise_name = true, + Optional[Boolean] $refreshonly = false, ) { include docker::params @@ -37,6 +38,7 @@ exec { $exec: environment => 'HOME=/root', path => ['/bin', '/usr/bin'], + refreshonly => $refreshonly, timeout => 0, unless => $unless_command, } diff --git a/spec/acceptance/docker_full_spec.rb b/spec/acceptance/docker_full_spec.rb index c1de13ac..0e3258c2 100644 --- a/spec/acceptance/docker_full_spec.rb +++ b/spec/acceptance/docker_full_spec.rb @@ -662,6 +662,55 @@ class { 'docker':} expect(r.stdout).to match(/test_command_file.txt/) end end + + it 'should only run if notified when refreshonly is true' do + container_name = 'container_4_2' + pp=<<-EOS + class { 'docker': } + + docker::image { 'ubuntu': } + + docker::run { '#{container_name}': + image => 'ubuntu', + command => 'init', + } + + docker::exec { 'test_command': + container => '#{container_name}', + command => 'touch /root/test_command_file.txt', + refreshonly => true, + } + EOS + + apply_manifest(pp, :catch_failures => true) + apply_manifest(pp, :catch_changes => true) unless fact('selinux') == 'true' + + # A sleep to give docker time to execute properly + sleep 4 + + shell("docker exec #{container_name} ls /root") do |r| + expect(r.stdout).to_not match(/test_command_file.txt/) + end + + pp_extra=<<-EOS + file { '/tmp/dummy_file': + ensure => 'present', + notify => Docker::Exec['test_command'], + } + EOS + + pp2 = pp + pp_extra + + 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 exec #{container_name} ls /root") do |r| + expect(r.stdout).to match(/test_command_file.txt/) + end + end end end end