Skip to content

Commit 031e699

Browse files
authored
Merge pull request #580 from manheim/run-after-start-stop
Add after_start and after_stop options to docker::run define
2 parents 2a942ff + 0783f9f commit 031e699

File tree

5 files changed

+35
-1
lines changed

5 files changed

+35
-1
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,8 @@ docker::run { 'helloworld':
392392
pull_on_start => false,
393393
before_stop => 'echo "So Long, and Thanks for All the Fish"',
394394
before_start => 'echo "Run this on the host before starting the Docker container"',
395+
after_stop => 'echo "container has stopped"',
396+
after_start => 'echo "container has started"',
395397
after => [ 'container_b', 'mysql' ],
396398
depends => [ 'container_a', 'postgres' ],
397399
stop_wait_time => 0,
@@ -406,7 +408,7 @@ To pull the image before it starts, specify the `pull_on_start` parameter.
406408

407409
Use the `detach` param to run container a container without the `-a` flag. This is only required on systems without `systemd`. This default is set in the params.pp based on the OS. Only override if you understand the consuquences and have a specific use case.
408410

409-
To execute a command before the container stops, specify the `before_stop` parameter.
411+
To execute a command before the container starts or stops, specify the `before_start` or `before_stop` parameters, respectively. Similarly, you can specify the `after_start` or `after_stop` parameters to run a command after the container starts or stops.
410412

411413
Adding the container name to the `after` parameter to specify which containers start first, affects the generation of the `init.d/systemd` script.
412414

manifests/run.pp

+2
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@
137137
Optional[String] $restart = undef,
138138
Variant[String,Boolean] $before_start = false,
139139
Variant[String,Boolean] $before_stop = false,
140+
Variant[String,Boolean] $after_start = false,
141+
Variant[String,Boolean] $after_stop = false,
140142
Optional[String] $after_create = undef,
141143
Optional[Boolean] $remove_container_on_start = true,
142144
Optional[Boolean] $remove_container_on_stop = true,

spec/defines/run_spec.rb

+24
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,30 @@
550550
it { is_expected.not_to contain_file(stopscript_or_init).with_content(%r{before_stop}) }
551551
end
552552

553+
context 'when `after_start` is set' do
554+
let(:params) { params.merge('after_start' => 'echo after_start') }
555+
556+
it { is_expected.to contain_file(startscript_or_init).with_content(%r{after_start}) }
557+
end
558+
559+
context 'when `after_start` is not set' do
560+
let(:params) { params.merge('after_start' => false) }
561+
562+
it { is_expected.not_to contain_file(startscript_or_init).with_content(%r{after_start}) }
563+
end
564+
565+
context 'when `after_stop` is set' do
566+
let(:params) { params.merge('after_stop' => 'echo after_stop') }
567+
568+
it { is_expected.to contain_file(stopscript_or_init).with_content(%r{after_stop}) }
569+
end
570+
571+
context 'when `after_stop` is not set' do
572+
let(:params) { params.merge('after_stop' => false) }
573+
574+
it { is_expected.not_to contain_file(stopscript_or_init).with_content(%r{after_stop}) }
575+
end
576+
553577
context 'when `after_create` is set' do
554578
let(:params) { params.merge('after_create' => 'echo after_create') }
555579

templates/docker-run-start.erb

+3
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,6 @@
2323
<% end %>
2424

2525
/usr/bin/<%= @docker_command %> start <% if ! @valid_detach %>-a<% end %> <%= @sanitised_title %>
26+
<% if @after_start -%>
27+
<%= @after_start %>
28+
<% end -%>

templates/docker-run-stop.erb

+3
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@
55
<% if @remove_container_on_stop -%>
66
/usr/bin/<%= @docker_command %> rm <% if @remove_volume_on_stop %>-v<% end %> <%= @sanitised_title %>
77
<% end -%>
8+
<% if @after_stop -%>
9+
<%= @after_stop %>
10+
<% end -%>

0 commit comments

Comments
 (0)