Skip to content

Commit 8e6b2c8

Browse files
committed
Add puppetcore support for Windows install tasks
Now possible to run the install task specifying puppetcore collection: ``` /opt/puppetlabs/bolt/bin/bolt task run puppet_agent::install \ collection=puppetcore8 \ version=8.11.0 \ username=forge-key \ password=${PUPPET_FORGE_TOKEN} \ --targets 'winrm://HOST' \ --user Administrator \ --password ... ``` If the `windows_source` class parameter is explicitly given, then the task will use that. Also add additional logging as to where we are downloading the MSI from and the exception message if downloading fails.
1 parent 14b1844 commit 8e6b2c8

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

tasks/install_powershell.json

+8
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@
4242
"description": "The number of retries in case of network connectivity failures",
4343
"type": "Optional[Integer]",
4444
"default": 5
45+
},
46+
"username": {
47+
"description": "The username to use when downloading from a source location requiring authentication",
48+
"type": "Optional[String]"
49+
},
50+
"password": {
51+
"description": "The password to use when downloading from a source location requiring authentication",
52+
"type": "Optional[String]"
4553
}
4654
},
4755
"supports_noop": true

tasks/install_powershell.ps1

+34-5
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,21 @@ Param(
77
[String]$install_options = 'REINSTALLMODE="amus"',
88
[Bool]$stop_service = $False,
99
[Int]$retry = 5,
10-
[Bool]$_noop = $False
10+
[Bool]$_noop = $False,
11+
[String]$username = 'forge-key',
12+
[String]$password
1113
)
1214
# If an error is encountered, the script will stop instead of the default of "Continue"
1315
$ErrorActionPreference = "Stop"
1416

17+
try {
18+
$os_version = (Get-WmiObject Win32_OperatingSystem).Version
19+
}
20+
catch [System.Management.Automation.CommandNotFoundException] {
21+
$os_version = (Get-CimInstance -ClassName win32_OperatingSystem).Version
22+
}
23+
$major_os_version = ($os_version -split '\.')[0]
24+
1525
try {
1626
if ((Get-WmiObject Win32_OperatingSystem).OSArchitecture -match '^32') {
1727
$arch = "x86"
@@ -27,9 +37,19 @@ catch [System.Management.Automation.CommandNotFoundException] {
2737
}
2838
}
2939

40+
$fips = 'false'
41+
try {
42+
if ((Get-ItemPropertyValue -Path 'HKLM:\System\CurrentControlSet\Control\Lsa\FipsAlgorithmPolicy' -Name Enabled) -ne 0) {
43+
$fips = 'true'
44+
}
45+
}
46+
catch {
47+
Write-Output "Failed to lookup FIPS mode, assuming it is disabled"
48+
}
49+
3050
function Test-PuppetInstalled {
3151
$rootPath = 'HKLM:\SOFTWARE\Puppet Labs\Puppet'
32-
try {
52+
try {
3353
if (Get-ItemProperty -Path $rootPath) { RETURN $true }
3454
}
3555
catch {
@@ -98,12 +118,16 @@ if (Test-RunningServices) {
98118
# Change windows_source only if the collection is a nightly build, and the source was not explicitly specified.
99119
if (($collection -like '*nightly*') -And -Not ($PSBoundParameters.ContainsKey('windows_source'))) {
100120
$windows_source = 'https://nightlies.puppet.com/downloads'
121+
} elseif (($collection -like '*puppetcore*') -And -Not ($PSBoundParameters.ContainsKey('windows_source'))) {
122+
$windows_source = 'https://artifacts-puppetcore.puppet.com/v1/download'
101123
}
102124

103125
if ($absolute_source) {
104126
$msi_source = "$absolute_source"
105127
}
106-
else {
128+
elseif ($collection -like '*puppetcore*') {
129+
$msi_source = "${windows_source}?version=${version}&os_name=windows&os_version=${major_os_version}&os_arch=${arch}&fips=${fips}"
130+
} else {
107131
$msi_source = "$windows_source/windows/${collection}/${msi_name}"
108132
}
109133

@@ -125,22 +149,27 @@ function Set-Tls12 {
125149
}
126150

127151
function DownloadPuppet {
128-
Write-Output "Downloading the Puppet Agent installer on $env:COMPUTERNAME..."
152+
Write-Output "Downloading the Puppet Agent installer on $env:COMPUTERNAME from ${msi_source}"
129153
Set-Tls12
130154

131155
$webclient = New-Object system.net.webclient
132-
156+
if ($password) {
157+
$credentials = [Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes("${username}:${password}"))
158+
$webclient.Headers.Add("Authorization", "Basic ${credentials}")
159+
}
133160
try {
134161
$webclient.DownloadFile($msi_source,$msi_dest)
135162
}
136163
catch [System.Net.WebException] {
164+
Write-Host "Download exception: $($_.Exception.Message)"
137165
For ($attempt_number = 1; $attempt_number -le $retry; $attempt_number++) {
138166
try {
139167
Write-Output "Retrying... [$attempt_number/$retry]"
140168
$webclient.DownloadFile($msi_source,$msi_dest)
141169
break
142170
}
143171
catch [System.Net.WebException] {
172+
Write-Host "Download exception: $($_.Exception.Message)"
144173
if($attempt_number -eq $retry) {
145174
# If we can't find the msi, then we may not be configured correctly
146175
if($_.Exception.Response.StatusCode -eq [system.net.httpstatuscode]::NotFound) {

0 commit comments

Comments
 (0)