Skip to content

Commit 0506ec8

Browse files
committed
Fix container running check to work for windows hosts
1 parent 043395e commit 0506ec8

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

Diff for: manifests/run.pp

+4-2
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@
240240
$cidfile = "${::docker_user_temp_path}/${service_prefix}${sanitised_title}.cid"
241241
# lint:ignore:140chars
242242
$restart_check = "${docker_command} inspect ${sanitised_title} -f '{{ if eq \\\"unhealthy\\\" .State.Health.Status }} {{ .Name }}{{ end }}' | findstr ${sanitised_title}"
243+
$container_running_check = "\$state = ${docker_command} inspect ${sanitised_title} -f \"{{ .State.Running }}\"; if (\$state -ieq \"true\") { Exit 0 } else { Exit 1 }"
243244
# lint:endignore
244245
} else {
245246
$exec_environment = 'HOME=/root'
@@ -249,6 +250,7 @@
249250
$cidfile = "/var/run/${service_prefix}${sanitised_title}.cid"
250251
# lint:ignore:140chars
251252
$restart_check = "${docker_command} inspect ${sanitised_title} -f '{{ if eq \"unhealthy\" .State.Health.Status }} {{ .Name }}{{ end }}' | grep ${sanitised_title}"
253+
$container_running_check = "${docker_command} inspect ${sanitised_title} -f \"{{ .State.Running }}\" | grep true"
252254
# lint:endignore
253255
}
254256

@@ -311,7 +313,7 @@
311313
if $running == false {
312314
exec { "stop ${title} with docker":
313315
command => "${docker_command} stop --time=${stop_wait_time} ${sanitised_title}",
314-
unless => "${docker_command} inspect ${sanitised_title} -f \"{{ .State.Running }}\" | grep true",
316+
onlyif => $container_running_check,
315317
environment => $exec_environment,
316318
path => $exec_path,
317319
provider => $exec_provider,
@@ -320,7 +322,7 @@
320322
} else {
321323
exec { "start ${title} with docker":
322324
command => "${docker_command} start ${sanitised_title}",
323-
onlyif => "${docker_command} inspect ${sanitised_title} -f \"{{ .State.Running }}\" | grep false",
325+
unless => $container_running_check,
324326
environment => $exec_environment,
325327
path => $exec_path,
326328
provider => $exec_provider,

Diff for: spec/defines/run_windows_spec.rb

+7
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
it { should contain_exec('run sample with docker').with_command(/--restart="always"/) }
4141
it { should contain_exec('run sample with docker').with_command(/base command/) }
4242
it { should contain_exec('run sample with docker').with_timeout(3000) }
43+
it { should contain_exec('start sample with docker').with_command(/docker start sample/) }
4344
end
4445

4546
context 'with restart policy set to on-failure' do
@@ -82,6 +83,12 @@
8283
it { should_not contain_file('C:/Users/Administrator/AppData/Local/Temp/docker-sample.cid"')}
8384
end
8485

86+
context 'with ensure present and running false' do
87+
let(:params) { {'ensure' => 'present', 'image' => 'base', 'restart' => 'always', 'running' => false} }
88+
it { should compile.with_all_deps }
89+
it { should contain_exec("stop sample with docker").with_command('docker stop --time=0 sample') }
90+
end
91+
8592
context 'with ensure present and no restart policy' do
8693
let(:params) { {'ensure' => 'present', 'image' => 'base'} }
8794
it do

0 commit comments

Comments
 (0)