Skip to content

Commit

Permalink
Merge pull request garethr#350 from jae2/master
Browse files Browse the repository at this point in the history
Adding support for unless within a docker exec type
  • Loading branch information
garethr committed Oct 7, 2015
2 parents ac6dbe4 + 3982a07 commit 1af3711
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,15 +283,16 @@ docker::registry_auth::registries:
### Exec
Docker also supports running arbitrary comments within the context of a
Docker also supports running arbitrary commands within the context of a
running container. And now so does the Puppet module.
```puppet
docker::exec { 'helloworld-uptime':
detach => true,
container => 'helloworld',
command => 'uptime',
tty => true,
docker::exec { 'cron_allow_root':
detach => true,
container => 'mycontainer',
command => '/bin/echo root >> /usr/lib/cron/cron.allow',
tty => true,
unless => 'grep root /usr/lib/cron/cron.allow 2>/dev/null',
}
```

10 changes: 9 additions & 1 deletion manifests/exec.pp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# == Define: docker:exec

#
# A define which executes a command inside a container.
#
Expand All @@ -8,6 +8,7 @@
$tty = false,
$container = undef,
$command = undef,
$unless = undef,
$sanitise_name = true,
) {
include docker::params
Expand All @@ -17,6 +18,7 @@

validate_string($container)
validate_string($command)
validate_string($unless)
validate_bool($detach)
validate_bool($interactive)
validate_bool($tty)
Expand All @@ -34,10 +36,16 @@
$sanitised_container = $container
}
$exec = "${docker_command} exec ${docker_exec_flags} ${sanitised_container} ${command}"
$unless_command = $unless ? {
undef => undef,
'' => undef,
default => "${docker_command} exec ${docker_exec_flags} ${sanitised_container} ${unless}",
}

exec { $exec:
environment => 'HOME=/root',
path => ['/bin', '/usr/bin'],
timeout => 0,
unless => $unless_command,
}
}
10 changes: 10 additions & 0 deletions spec/defines/exec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,14 @@
let(:params) { {'command' => 'command', 'container' => 'container', 'interactive' => true} }
it { should contain_exec('docker exec --interactive=true container command') }
end

context 'when running with unless' do
let(:params) { {'command' => 'command', 'container' => 'container', 'interactive' => true, 'unless' => 'some_command arg1'} }
it { should contain_exec('docker exec --interactive=true container command').with_unless ('docker exec --interactive=true container some_command arg1') }
end

context 'when running without unless' do
let(:params) { {'command' => 'command', 'container' => 'container', 'interactive' => true,} }
it { should contain_exec('docker exec --interactive=true container command').with_unless (nil) }
end
end

0 comments on commit 1af3711

Please sign in to comment.