Skip to content

Commit

Permalink
Fix container running check to work for windows hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
florindragos committed Apr 9, 2019
1 parent 043395e commit d860415
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
7 changes: 5 additions & 2 deletions manifests/run.pp
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@
$cidfile = "${::docker_user_temp_path}/${service_prefix}${sanitised_title}.cid"
# lint:ignore:140chars
$restart_check = "${docker_command} inspect ${sanitised_title} -f '{{ if eq \\\"unhealthy\\\" .State.Health.Status }} {{ .Name }}{{ end }}' | findstr ${sanitised_title}"
$container_running_check = "\$state = ${docker_command} inspect ${sanitised_title} -f \"{{ .State.Running }}\"; if (\$state -ieq \"true\") { Exit 0 } else { Exit 1 }"

# lint:endignore
} else {
$exec_environment = 'HOME=/root'
Expand All @@ -249,6 +251,7 @@
$cidfile = "/var/run/${service_prefix}${sanitised_title}.cid"
# lint:ignore:140chars
$restart_check = "${docker_command} inspect ${sanitised_title} -f '{{ if eq \"unhealthy\" .State.Health.Status }} {{ .Name }}{{ end }}' | grep ${sanitised_title}"
$container_running_check = "${docker_command} inspect ${sanitised_title} -f \"{{ .State.Running }}\" | grep true"
# lint:endignore
}

Expand Down Expand Up @@ -311,7 +314,7 @@
if $running == false {
exec { "stop ${title} with docker":
command => "${docker_command} stop --time=${stop_wait_time} ${sanitised_title}",
unless => "${docker_command} inspect ${sanitised_title} -f \"{{ .State.Running }}\" | grep true",
onlyif => $container_running_check,
environment => $exec_environment,
path => $exec_path,
provider => $exec_provider,
Expand All @@ -320,7 +323,7 @@
} else {
exec { "start ${title} with docker":
command => "${docker_command} start ${sanitised_title}",
onlyif => "${docker_command} inspect ${sanitised_title} -f \"{{ .State.Running }}\" | grep false",
unless => $container_running_check,
environment => $exec_environment,
path => $exec_path,
provider => $exec_provider,
Expand Down
7 changes: 7 additions & 0 deletions spec/defines/run_windows_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
it { should contain_exec('run sample with docker').with_command(/--restart="always"/) }
it { should contain_exec('run sample with docker').with_command(/base command/) }
it { should contain_exec('run sample with docker').with_timeout(3000) }
it { should contain_exec('start sample with docker').with_command(/docker start sample/) }
end

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

context 'with ensure present and running false' do
let(:params) { {'ensure' => 'present', 'image' => 'base', 'restart' => 'always', 'running' => false} }
it { should compile.with_all_deps }
it { should contain_exec("stop sample with docker").with_command('docker stop --time=0 sample') }
end

context 'with ensure present and no restart policy' do
let(:params) { {'ensure' => 'present', 'image' => 'base'} }
it do
Expand Down

0 comments on commit d860415

Please sign in to comment.