Skip to content

Commit cbd43f2

Browse files
Upgrade puppetcore* msi from artifacts-puppetcore.puppet.com
When using the puppetcore collection on Windows, if we detect the installed version does not match, then upgrade the MSI. Due to a puppet bug, we cannot pass credentials in the `source` parameter. And `curl.exe` is not present in our puppet-agent packages. So use powershell to download. Co-authored-by: Kevin <114269618+klab-systems@users.noreply.github.com>
1 parent b501b48 commit cbd43f2

File tree

5 files changed

+104
-14
lines changed

5 files changed

+104
-14
lines changed

REFERENCE.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,7 @@ working with a remote https repository.
624624
The following parameters are available in the `puppet_agent::prepare::package` class:
625625

626626
* [`source`](#-puppet_agent--prepare--package--source)
627+
* [`package_file_name`](#-puppet_agent--prepare--package--package_file_name)
627628

628629
##### <a name="-puppet_agent--prepare--package--source"></a>`source`
629630

@@ -632,6 +633,16 @@ Data type: `Variant[String, Array]`
632633
The source file for the puppet-agent package. Can use any of the data types
633634
and protocols that the File resource's source attribute can.
634635

636+
##### <a name="-puppet_agent--prepare--package--package_file_name"></a>`package_file_name`
637+
638+
Data type: `Optional[String]`
639+
640+
The destination file name for the puppet-agent package. If no destination
641+
is given, then the basename component of the source will be used as the
642+
destination filename.
643+
644+
Default value: `undef`
645+
635646
### <a name="puppet_agent--prepare--puppet_config"></a>`puppet_agent::prepare::puppet_config`
636647

637648
Private class called from puppet_agent::prepare class.
@@ -993,6 +1004,18 @@ Data type: `Optional[Integer]`
9931004

9941005
The number of retries in case of network connectivity failures
9951006

1007+
##### `username`
1008+
1009+
Data type: `Optional[String]`
1010+
1011+
The username to use when downloading from a source location requiring authentication
1012+
1013+
##### `password`
1014+
1015+
Data type: `Optional[String]`
1016+
1017+
The password to use when downloading from a source location requiring authentication
1018+
9961019
### <a name="install_shell"></a>`install_shell`
9971020

9981021
Install the Puppet agent package

manifests/osfamily/windows.pp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,22 @@
2323
} else {
2424
if $puppet_agent::collection == 'PC1' {
2525
$source = "${puppet_agent::windows_source}/windows/${puppet_agent::package_name}-${puppet_agent::prepare::package_version}-${puppet_agent::arch}.msi"
26+
} elsif $puppet_agent::collection =~ /core/ {
27+
$source = 'https://artifacts-puppetcore.puppet.com/v1/download'
2628
} else {
2729
$source = "${puppet_agent::windows_source}/windows/${puppet_agent::collection}/${puppet_agent::package_name}-${puppet_agent::prepare::package_version}-${puppet_agent::arch}.msi"
2830
}
2931
}
3032

33+
if $puppet_agent::collection and $puppet_agent::collection =~ /core/ {
34+
$destination_name = "${puppet_agent::package_name}-${puppet_agent::prepare::package_version}-${puppet_agent::arch}.msi"
35+
} else {
36+
$destination_name = undef
37+
}
38+
3139
class { 'puppet_agent::prepare::package':
32-
source => $source,
40+
source => $source,
41+
destination_name => $destination_name,
3342
}
3443

3544
contain puppet_agent::prepare::package

manifests/prepare/package.pp

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,31 @@
55
# @param source
66
# The source file for the puppet-agent package. Can use any of the data types
77
# and protocols that the File resource's source attribute can.
8+
# @param destination_name
9+
# The destination file name for the puppet-agent package. If no destination
10+
# is given, then the basename component of the source will be used as the
11+
# destination name.
812
class puppet_agent::prepare::package (
913
Variant[String, Array] $source,
14+
Optional[String] $destination_name = undef
1015
) {
1116
assert_private()
1217

1318
file { $puppet_agent::params::local_packages_dir:
1419
ensure => directory,
1520
}
1621

17-
# In order for the 'basename' function to work correctly we need to change
18-
# any \s to /s (even for windows UNC paths) so that it will correctly pull off
19-
# the filename. Since this operation is only grabbing the base filename and not
20-
# any part of the path this should be safe, since the source will simply remain
21-
# what it was before and we can still pull off the filename.
22-
$package_file_name = basename(regsubst($source, "\\\\", '/', 'G'))
22+
if $destination_name {
23+
$package_file_name = $destination_name
24+
} else {
25+
# In order for the 'basename' function to work correctly we need to change
26+
# any \s to /s (even for windows UNC paths) so that it will correctly pull off
27+
# the filename. Since this operation is only grabbing the base filename and not
28+
# any part of the path this should be safe, since the source will simply remain
29+
# what it was before and we can still pull off the filename.
30+
$package_file_name = basename(regsubst($source, "\\\\", '/', 'G'))
31+
}
32+
2333
if $facts['os']['family'] =~ /windows/ {
2434
$local_package_file_path = windows_native_path("${puppet_agent::params::local_packages_dir}/${package_file_name}")
2535
$mode = undef
@@ -28,12 +38,37 @@
2838
$mode = '0644'
2939
}
3040

31-
file { $local_package_file_path:
32-
ensure => file,
33-
owner => $puppet_agent::params::user,
34-
group => $puppet_agent::params::group,
35-
mode => $mode,
36-
source => $source,
37-
require => File[$puppet_agent::params::local_packages_dir],
41+
if $puppet_agent::collection =~ /core/ and $facts['os']['family'] =~ /windows/ {
42+
$download_username = getvar('puppet_agent::username', 'forge-key')
43+
$download_password = unwrap(getvar('puppet_agent::password'))
44+
45+
$_download_puppet = windows_native_path("${facts['env_temp_variable']}/download_puppet.ps1")
46+
file { $_download_puppet:
47+
ensure => file,
48+
content => Sensitive(epp('puppet_agent/download_puppet.ps1.epp')),
49+
}
50+
51+
exec { 'Download Puppet Agent':
52+
command => [
53+
"${facts['os']['windows']['system32']}\\WindowsPowerShell\\v1.0\\powershell.exe",
54+
"-ExecutionPolicy",
55+
"Bypass",
56+
"-NoProfile",
57+
"-NoLogo",
58+
"-NonInteractive"
59+
${_download_puppet}"
60+
],
61+
creates => $local_package_file_path,
62+
require => File[$puppet_agent::params::local_packages_dir],
63+
}
64+
} else {
65+
file { $local_package_file_path:
66+
ensure => file,
67+
owner => $puppet_agent::params::user,
68+
group => $puppet_agent::params::group,
69+
mode => $mode,
70+
source => $source,
71+
require => File[$puppet_agent::params::local_packages_dir],
72+
}
3873
}
3974
}

metadata.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
{
2424
"name": "puppetlabs-facts",
2525
"version_requirement": ">= 0.5.0 < 2.0.0"
26+
},
27+
{
28+
"name": "puppetlabs-powershell",
29+
"version_requirement": ">= 6.0.2 < 7.0.0"
2630
}
2731
],
2832
"operatingsystem_support": [

templates/download_puppet.ps1.epp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
$body = @{
2+
"version" = "<%= $puppet_agent::prepare::package_version %>"
3+
"os_name" = "<%= $facts['os']['family'] %>"
4+
"os_version" = "<%= $facts['os']['release']['major'] %>"
5+
"os_arch" = "<%= $facts['os']['architecture'] %>"
6+
"fips" = "<%= $facts['fips_enabled'] %>"
7+
}
8+
$username = "<%= $puppet_agent::prepare::package::download_username %>"
9+
$password = ConvertTo-SecureString "<%= $puppet_agent::prepare::package::download_password %>" -AsPlainText -Force
10+
$credential = New-Object System.Management.Automation.PSCredential($username, $password)
11+
try {
12+
Invoke-WebRequest -Uri "<%= $puppet_agent::prepare::package::source %>" `
13+
-Body $body `
14+
-Credential $credential `
15+
-OutFile "<%= $puppet_agent::prepare::package::local_package_file_path %>"
16+
} catch [System.Net.WebException] {
17+
Write-Host "Network-related error: $($_.Exception.Message)"
18+
exit 1
19+
}

0 commit comments

Comments
 (0)