Skip to content

Commit eeb02cd

Browse files
committed
(MODULES-10653) Failed to upgrade agent using puppet task
Before this commit, when trying to run the `puppet_agent::install_powershell` task, with a specific version as parameter, it was failing with below error message: Error: Timed out waiting for status response from <node_name> On the node, both `puppet agent` and `pxp-agent` services got stopped and the event log viewer showed: Application or service 'task_wrapper' could not be shut down. Due to Windows agents becoming unresponsive when running this task and as per MODULES-10633 discussions, the `puppet_agent::install_poweshell` task should not be used for upgrading Puppet Agents while either of `puppet agent` or `pxp-agent` services are still running. This Puppet Agent module task will now fail immediately and output a message if any of the two affected services are still running. The task will stop and output a message if the given version is already installed on the node, thus avoiding unnecessary msi download/installation. This behaviour is aligned with the existing implementation for no version given and Puppet Agent already installed on the node.
1 parent b83c5b9 commit eeb02cd

File tree

2 files changed

+81
-10
lines changed

2 files changed

+81
-10
lines changed

task_spec/spec/acceptance/init_spec.rb

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,29 +40,31 @@ def target_platform
4040
else
4141
'5.5.3'
4242
end
43-
# test the agent isn't already installed and that the version task works
43+
44+
# Test the agent isn't already installed and that the version task works
4445
results = run_task('puppet_agent::version', 'target', {})
4546
results.each do |res|
4647
expect(res).to include('status' => 'success')
4748
expect(res['result']['version']).to eq(nil)
4849
end
4950

50-
# Try to install an older version
51+
# Try to install an older puppet5 version
5152
results = run_task('puppet_agent::install', 'target', { 'collection' => 'puppet5',
5253
'version' => puppet_5_version,
5354
'stop_service' => true })
5455
results.each do |res|
5556
expect(res).to include('status' => 'success')
5657
end
5758

58-
# It installed a version older than latest
59+
# It installed a version older than latest puppet5
5960
results = run_task('puppet_agent::version', 'target', {})
6061
results.each do |res|
6162
expect(res).to include('status' => 'success')
6263
expect(res['result']['version']).to eq(puppet_5_version)
6364
expect(res['result']['source']).to be
6465
end
6566

67+
# Check that puppet agent service has been stopped due to 'stop_service' parameter set to true
6668
service = if target_platform =~ /win/
6769
run_command('c:/"program files"/"puppet labs"/puppet/bin/puppet resource service puppet', 'target')
6870
else
@@ -71,21 +73,23 @@ def target_platform
7173
output = service[0]['result']['stdout']
7274
expect(output).to include("ensure => 'stopped'")
7375

74-
# Run with no argument upgrades
76+
# Try to upgrade with no specific version given in parameter
77+
# Expect nothing to happen and receive a message regarding this
7578
results = run_task('puppet_agent::install', 'target', { 'collection' => 'puppet5' })
7679
results.each do |res|
7780
expect(res).to include('status' => 'success')
81+
expect(res['result']['_output']).to match(%r{Version parameter not defined and agent detected. Nothing to do.})
7882
end
7983

80-
# Verify that it did nothing
84+
# Verify that the version didn't change
8185
results = run_task('puppet_agent::version', 'target', {})
8286
results.each do |res|
8387
expect(res).to include('status' => 'success')
8488
expect(res['result']['version']).to eq(puppet_5_version)
8589
expect(res['result']['source']).to be
8690
end
8791

88-
# Run with latest upgrades
92+
# Upgrade to latest puppet5 version
8993
results = run_task('puppet_agent::install', 'target', { 'collection' => 'puppet5', 'version' => 'latest' })
9094
results.each do |res|
9195
expect(res).to include('status' => 'success')
@@ -100,19 +104,44 @@ def target_platform
100104
expect(res['result']['source']).to be
101105
end
102106

103-
# Upgrade from puppet5 to puppet6
107+
# Puppet Agent can't be upgraded on Windows nodes while 'puppet agent' service or 'pxp-agent' service are running
108+
if target_platform =~ /win/
109+
# Try to upgrade from puppet5 to puppet6 but fail due to puppet agent service already running
110+
results = run_task('puppet_agent::install', 'target', { 'collection' => 'puppet6', 'version' => 'latest' })
111+
results.each do |res|
112+
expect(res).to include('status' => 'failure')
113+
expect(res['result']['_error']['msg']).to match(%r{Puppet Agent upgrade cannot be done while Puppet services are still running.})
114+
end
115+
116+
# Manually stop the puppet agent service
117+
service = run_command('c:/"program files"/"puppet labs"/puppet/bin/puppet resource service puppet ensure=stopped', 'target')
118+
output = service[0]['result']['stdout']
119+
expect(output).to include("ensure => 'stopped'")
120+
end
121+
122+
# Succesfully upgrade from puppet5 to puppet6
104123
results = run_task('puppet_agent::install', 'target', { 'collection' => 'puppet6', 'version' => 'latest' })
105124
results.each do |res|
106125
expect(res).to include('status' => 'success')
107126
end
108127

109128
# Verify that it upgraded
129+
installed_version = nil
110130
results = run_task('puppet_agent::version', 'target', {})
111131
results.each do |res|
112132
expect(res).to include('status' => 'success')
113-
expect(res['result']['version']).not_to match(%r{^5\.\d+\.\d+})
114-
expect(res['result']['version']).to match(%r{^6\.\d+\.\d+})
133+
installed_version = res['result']['version']
134+
expect(installed_version).not_to match(%r{^5\.\d+\.\d+})
135+
expect(installed_version).to match(%r{^6\.\d+\.\d+})
115136
expect(res['result']['source']).to be
116137
end
138+
139+
# Try installing the same version again
140+
# Expect nothing to happen and receive a message regarding this
141+
results = run_task('puppet_agent::install', 'target', { 'collection' => 'puppet6', 'version' => installed_version })
142+
results.each do |res|
143+
expect(res).to include('status' => 'success')
144+
expect(res['result']['_output']).to match(%r{Puppet Agent #{installed_version} detected. Nothing to do.})
145+
end
117146
end
118147
end

tasks/install_powershell.ps1

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,45 @@ function Test-PuppetInstalled {
3434
}
3535
}
3636

37+
function Test-PuppetInstalledVersion {
38+
$rootPath = 'HKLM:\SOFTWARE\Puppet Labs\Puppet'
39+
40+
$reg = Get-ItemProperty -Path $rootPath -ErrorAction SilentlyContinue
41+
if ($null -ne $reg) {
42+
if ($null -ne $reg.RememberedInstallDir64) {
43+
$loc = $reg.RememberedInstallDir64+'VERSION'
44+
} elseif ($null -ne $reg.RememberedInstallDir) {
45+
$loc = $reg.RememberedInstallDir+'VERSION'
46+
}
47+
}
48+
49+
if ($null -ne $loc) {
50+
$installedVersion = Get-Content -Path $loc -ErrorAction SilentlyContinue
51+
if ($installedVersion -eq $version) {
52+
RETURN $true
53+
}
54+
}
55+
56+
RETURN $false
57+
}
58+
59+
function Test-RunningServices {
60+
$puppetAgentService = Get-Service -DisplayName 'Puppet Agent' -ErrorAction SilentlyContinue
61+
$pxpAgentService = Get-Service -DisplayName 'Puppet PXP Agent' -ErrorAction SilentlyContinue
62+
63+
if ($puppetAgentService.Status -eq 'Running' -or $pxpAgentService.Status -eq 'Running') {
64+
RETURN $true
65+
}
66+
67+
RETURN $false
68+
}
69+
3770
if ($version) {
71+
if (Test-PuppetInstalledVersion) {
72+
Write-Output "Puppet Agent ${version} detected. Nothing to do."
73+
Exit
74+
}
75+
3876
if ($version -eq "latest") {
3977
$msi_name = "puppet-agent-${arch}-latest.msi"
4078
} else {
@@ -43,13 +81,17 @@ if ($version) {
4381
}
4482
else {
4583
if (Test-PuppetInstalled) {
46-
Write-Output "Puppet Agent detected and no version specified. Nothing to do."
84+
Write-Output "Version parameter not defined and agent detected. Nothing to do."
4785
Exit
4886
}
4987

5088
$msi_name = "puppet-agent-${arch}-latest.msi"
5189
}
5290

91+
if (Test-RunningServices) {
92+
Write-Error "Puppet Agent upgrade cannot be done while Puppet services are still running."
93+
}
94+
5395
# Change windows_source only if the collection is a nightly build, and the source was not explicitly specified.
5496
if (($collection -like '*nightly*') -And -Not ($PSBoundParameters.ContainsKey('windows_source'))) {
5597
$windows_source = 'https://nightlies.puppet.com/downloads'

0 commit comments

Comments
 (0)