Skip to content

Commit fdb3460

Browse files
authored
Merge pull request #295 from mihaibuzgau/master
[CLOUD-2041] Windows service idempotence using powershell script
2 parents 1dc3490 + 8f4d6a1 commit fdb3460

File tree

3 files changed

+59
-21
lines changed

3 files changed

+59
-21
lines changed

manifests/install.pp

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -88,24 +88,18 @@
8888
name => $docker::docker_package_name,
8989
}))
9090
} else {
91-
$install_script_path = 'C:/Windows/Temp/install_powershell_provider.ps1'
92-
file{ $install_script_path:
93-
ensure => present,
94-
force => true,
95-
content => template('docker/windows/install_powershell_provider.ps1.erb'),
96-
notify => Exec['install-docker-package']
97-
}
9891
exec { 'install-docker-package':
99-
command => "& ${install_script_path}",
100-
provider => powershell,
101-
refreshonly => true,
102-
logoutput => true,
103-
notify => Exec['service-restart-on-failure'],
92+
command => template('docker/windows/install_powershell_provider.ps1.erb'),
93+
provider => powershell,
94+
unless => template('docker/windows/check_powershell_provider.ps1.erb'),
95+
logoutput => true,
96+
notify => Exec['service-restart-on-failure'],
10497
}
10598
exec { 'service-restart-on-failure':
106-
command => 'cmd /c "SC failure Docker reset= 432000 actions= restart/30000/restart/60000/restart/60000"',
99+
command => 'SC.exe failure Docker reset= 432000 actions= restart/30000/restart/60000/restart/60000',
107100
refreshonly => true,
108-
path => 'c:/Windows/System32/'
101+
logoutput => true,
102+
provider => powershell,
109103
}
110104
}
111105
}

spec/classes/docker_windows_spec.rb

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,16 @@
1313
:os => { :family => 'windows', :name => 'windows', :release => { :major => '2016', :full => '2016' } }
1414
} }
1515
service_config_file = 'C:/ProgramData/docker/config/daemon.json'
16-
service_install_file = 'C:/Windows/Temp/install_powershell_provider.ps1'
1716
let(:params) {{ 'docker_ee' => true }}
1817

1918
it { should compile.with_all_deps }
2019
it { should contain_file('C:/ProgramData/docker/').with({
2120
'ensure' => 'directory'
2221
} ) }
23-
it { should contain_file(service_install_file).with({
24-
'ensure' => 'present'
25-
})}
2622
it { should contain_file('C:/ProgramData/docker/config/')}
2723
it { should contain_exec('service-restart-on-failure') }
28-
it { should contain_exec('install-docker-package').with_command("& #{service_install_file}") }
24+
it { should contain_exec('install-docker-package').with_command(/Install-PackageProvider NuGet -Force/) }
25+
it { should contain_exec('install-docker-package').with_command(/Install-Module \$dockerProviderName -Force/) }
2926
it { should contain_class('docker::repos').that_comes_before('Class[docker::install]') }
3027
it { should contain_class('docker::install').that_comes_before('Class[docker::config]') }
3128
it { should contain_class('docker::config').that_comes_before('Class[docker::service]') }
@@ -267,15 +264,15 @@
267264
let(:params) { {
268265
'docker_ee' => true
269266
} }
270-
it { should contain_file(service_install_file).with_content(/ Docker /)}
267+
it { should contain_exec('install-docker-package').with_command(/ Docker /) }
271268
end
272269

273270
context 'with custom package name' do
274271
let(:params) { {
275272
'docker_ee_package_name'=> "mydockerpackage",
276273
'docker_ee' => true
277274
} }
278-
it { should contain_file(service_install_file).with_content(/ mydockerpackage /)}
275+
it { should contain_exec('install-docker-package').with_command(/ mydockerpackage /) }
279276
end
280277

281278
context 'without docker_ee' do
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# this file checks the status of the Windows Docker package using the DockerMsftProvider powershell provider
2+
$dockerProviderName="DockerMsftProvider"
3+
4+
Write-Information "Checking Package Provider"
5+
$module = Get-PackageProvider NuGet
6+
If ($module -eq $null) {
7+
Write-Error "NuGet PackagePrivider is not installed."
8+
Exit 1
9+
}
10+
<% if @nuget_package_provider_version -%>
11+
Write-Information "Checking Package provider version"
12+
if ($module.Version.ToString() -ne "<%= @nuget_package_provider_version %>" ) {
13+
Write-Error "Incorrect Microsoft Nuget provider version installed"
14+
Exit 1
15+
}
16+
<% end -%>
17+
18+
19+
Write-Information "Checking Docker Provider"
20+
$provider = Get-Module -ListAvailable -Name $dockerProviderName
21+
If ($provider -eq $null) {
22+
Write-Error "Docker Microsoft Docker provider is not installed."
23+
Exit 1
24+
}
25+
26+
<% if @docker_msft_provider_version -%>
27+
Write-Information "Checking Docker provider version"
28+
if ($provider.Version.ToString() -ne "<%= @docker_msft_provider_version %>" ) {
29+
Write-Error "Incorrect Microsoft Docker Provider version installed."
30+
Exit 1
31+
}
32+
<% end -%>
33+
34+
Write-Information "Checking Docker package."
35+
$package=Get-Package <%= @docker_ee_package_name %> -ProviderName $dockerProviderName
36+
If ($package -eq $null) {
37+
Write-Error "Docker package is not installed."
38+
Exit 1
39+
}
40+
41+
<% if @version -%>
42+
Write-Information "Checking Docker package version"
43+
if ($package.Version.ToString() -ne "<%= @version %>"){
44+
Write-Error "Incorrect Docker package version installed."
45+
Exit 1
46+
}
47+
<% end %>

0 commit comments

Comments
 (0)