Skip to content

Commit 33cd0f6

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 <[email protected]>
1 parent 8e6b2c8 commit 33cd0f6

File tree

4 files changed

+101
-14
lines changed

4 files changed

+101
-14
lines changed

REFERENCE.md

+23
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

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
class puppet_agent::osfamily::windows {
33
assert_private()
44

5+
$destination_name = undef
6+
57
if $puppet_agent::absolute_source {
68
$source = $puppet_agent::absolute_source
79
} elsif $puppet_agent::source {
@@ -23,13 +25,17 @@
2325
} else {
2426
if $puppet_agent::collection == 'PC1' {
2527
$source = "${puppet_agent::windows_source}/windows/${puppet_agent::package_name}-${puppet_agent::prepare::package_version}-${puppet_agent::arch}.msi"
28+
} elsif $puppet_agent::collection =~ /core/ {
29+
$source = 'https://artifacts-puppetcore.puppet.com/v1/download'
30+
$destination_name = "${puppet_agent::package_name}-${puppet_agent::prepare::package_version}-${puppet_agent::arch}.msi"
2631
} else {
2732
$source = "${puppet_agent::windows_source}/windows/${puppet_agent::collection}/${puppet_agent::package_name}-${puppet_agent::prepare::package_version}-${puppet_agent::arch}.msi"
2833
}
2934
}
3035

3136
class { 'puppet_agent::prepare::package':
32-
source => $source,
37+
source => $source,
38+
destination_name => $destination_name,
3339
}
3440

3541
contain puppet_agent::prepare::package

manifests/prepare/package.pp

+52-13
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,41 @@
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+
# REMIND: redhat/suse with absolute_source
42+
# REMIND: debian with absolute_source
43+
# REMIND: solaris 10
44+
# REMIND: solaris 11 with manage_repo
45+
# REMIND: aix
46+
# REMIND: darwin
47+
# REMIND: suse 11 and PE
48+
if $puppet_agent::collection =~ /core/ and $facts['os']['family'] =~ /windows/ {
49+
$download_username = getvar('puppet_agent::username', 'forge-key')
50+
$download_password = unwrap(getvar('puppet_agent::password'))
51+
52+
$_download_puppet = windows_native_path("${facts['env_temp_variable']}/download_puppet.ps1")
53+
file { $_download_puppet:
54+
ensure => file,
55+
content => Sensitive(epp('puppet_agent/download_puppet.ps1.epp')),
56+
}
57+
58+
exec { 'Download Puppet Agent':
59+
command => "${facts['os']['windows']['system32']}\\WindowsPowerShell\\v1.0\\powershell.exe \
60+
-ExecutionPolicy Bypass \
61+
-NoProfile \
62+
-NoLogo \
63+
-NonInteractive \
64+
${_download_puppet}",
65+
creates => $local_package_file_path,
66+
provider => powershell,
67+
}
68+
} else {
69+
file { $local_package_file_path:
70+
ensure => file,
71+
owner => $puppet_agent::params::user,
72+
group => $puppet_agent::params::group,
73+
mode => $mode,
74+
source => $source,
75+
require => File[$puppet_agent::params::local_packages_dir],
76+
}
3877
}
3978
}

templates/download_puppet.ps1.epp

+19
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)