Skip to content

New install script #363

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 41 additions & 6 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ The following parameters are available in the `chocolatey` class:
* [`log_output`](#-chocolatey--log_output)
* [`chocolatey_version`](#-chocolatey--chocolatey_version)
* [`install_proxy`](#-chocolatey--install_proxy)
* [`install_ignore_proxy`](#-chocolatey--install_ignore_proxy)
* [`install_proxy_user`](#-chocolatey--install_proxy_user)
* [`install_proxy_password`](#-chocolatey--install_proxy_password)
* [`install_tempdir`](#-chocolatey--install_tempdir)

##### <a name="-chocolatey--choco_install_location"></a>`choco_install_location`

Expand Down Expand Up @@ -127,11 +131,9 @@ Default value: `false`
Data type: `String[1]`

Specifies the source file for 7za.exe.
Supports all sources supported by Puppet's file resource. You should use
a 32bit binary for compatibility.
Defaults to 'https://chocolatey.org/7za.exe'.
Defaults to 'https://community.chocolatey.org/7za.exe'.

Default value: `'https://chocolatey.org/7za.exe'`
Default value: `'https://community.chocolatey.org/7za.exe'`

##### <a name="-chocolatey--choco_install_timeout_seconds"></a>`choco_install_timeout_seconds`

Expand All @@ -150,9 +152,9 @@ Data type: `Stdlib::Filesource`
A url that will return
`chocolatey.nupkg`. This must be a url, but not necessarily an OData feed.
Any old url location will work. Defaults to
`'https://chocolatey.org/api/v2/package/chocolatey/'`.
`'https://community.chocolatey.org/api/v2/package/chocolatey/'`.

Default value: `'https://chocolatey.org/api/v2/package/chocolatey/'`
Default value: `'https://community.chocolatey.org/api/v2/package/chocolatey/'`

##### <a name="-chocolatey--enable_autouninstaller"></a>`enable_autouninstaller`

Expand Down Expand Up @@ -194,6 +196,39 @@ Proxy server to use to use for installation of chocolatey itself or

Default value: `undef`

##### <a name="-chocolatey--install_ignore_proxy"></a>`install_ignore_proxy`

Data type: `Optional[Boolean]`

If set to 'true' the installation script will ignore
the configured 'install_proxy'

Default value: `undef`

##### <a name="-chocolatey--install_proxy_user"></a>`install_proxy_user`

Data type: `Optional[String[1]]`

The username to use when using a proxy that requires authentication.

Default value: `undef`

##### <a name="-chocolatey--install_proxy_password"></a>`install_proxy_password`

Data type: `Optional[Sensitive]`

The password to use when using a proxy that requires authentication.

Default value: `undef`

##### <a name="-chocolatey--install_tempdir"></a>`install_tempdir`

Data type: `Optional[Stdlib::Windowspath]`

The temporary directory Chocolatey extracts to when installing.

Default value: `undef`

## Resource types

### <a name="chocolateyconfig"></a>`chocolateyconfig`
Expand Down
2 changes: 1 addition & 1 deletion lib/puppet/provider/package/chocolatey.rb
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def self.listcmd
args << '-lo'
args << '-r' if compiled_choco?

[command(:chocolatey), *args]
["\"#{command(:chocolatey)}\"", *args]
end

def self.instances
Expand Down
38 changes: 25 additions & 13 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@
# Defaults to `false`.
#
# @param [String] seven_zip_download_url Specifies the source file for 7za.exe.
# Supports all sources supported by Puppet's file resource. You should use
# a 32bit binary for compatibility.
# Defaults to 'https://chocolatey.org/7za.exe'.
# Defaults to 'https://community.chocolatey.org/7za.exe'.
#
# @param [Integer] choco_install_timeout_seconds How long in seconds should
# be allowed for the install of Chocolatey (including .NET Framework 4 if
Expand All @@ -56,7 +54,7 @@
# @param [String] chocolatey_download_url A url that will return
# `chocolatey.nupkg`. This must be a url, but not necessarily an OData feed.
# Any old url location will work. Defaults to
# `'https://chocolatey.org/api/v2/package/chocolatey/'`.
# `'https://community.chocolatey.org/api/v2/package/chocolatey/'`.
#
# @param [Boolean] enable_autouninstaller [Deprecated] - Should auto
# uninstaller be turned on? Auto uninstaller is what allows Chocolatey to
Expand All @@ -73,16 +71,30 @@
#
# @param install_proxy Proxy server to use to use for installation of chocolatey itself or
# `undef` to not use a proxy
#
# @param [Boolean] install_ignore_proxy If set to 'true' the installation script will ignore
# the configured 'install_proxy'
#
# @param install_proxy_user The username to use when using a proxy that requires authentication.
#
# @param install_proxy_password The password to use when using a proxy that requires authentication.
#
# @param install_tempdir The temporary directory Chocolatey extracts to when installing.
#
class chocolatey (
Stdlib::Windowspath $choco_install_location = $facts['choco_install_path'],
Boolean $use_7zip = false,
String[1] $seven_zip_download_url = 'https://chocolatey.org/7za.exe',
Integer $choco_install_timeout_seconds = 1500,
Stdlib::Filesource $chocolatey_download_url = 'https://chocolatey.org/api/v2/package/chocolatey/',
Boolean $enable_autouninstaller = true,
Boolean $log_output = false,
String[1] $chocolatey_version = $facts['chocolateyversion'],
Optional[String[1]] $install_proxy = undef,
Stdlib::Windowspath $choco_install_location = $facts['choco_install_path'],
Boolean $use_7zip = false,
String[1] $seven_zip_download_url = 'https://community.chocolatey.org/7za.exe',
Integer $choco_install_timeout_seconds = 1500,
Stdlib::Filesource $chocolatey_download_url = 'https://community.chocolatey.org/api/v2/package/chocolatey/',
Boolean $enable_autouninstaller = true,
Boolean $log_output = false,
String[1] $chocolatey_version = $facts['chocolateyversion'],
Optional[String[1]] $install_proxy = undef,
Optional[Boolean] $install_ignore_proxy = undef,
Optional[String[1]] $install_proxy_user = undef,
Optional[Sensitive] $install_proxy_password = undef,
Optional[Stdlib::Windowspath] $install_tempdir = undef,
) {
class { 'chocolatey::install': }
-> class { 'chocolatey::config': }
Expand Down
56 changes: 28 additions & 28 deletions manifests/install.pp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,7 @@
class chocolatey::install {
assert_private()

$install_proxy = $chocolatey::install_proxy
$_install_proxy = $install_proxy ? {
undef => '$false',
default => "'${install_proxy}'",
}
$_download_url = $chocolatey::chocolatey_download_url
$seven_zip_download_url = $chocolatey::seven_zip_download_url
$seven_zip_exe = "${facts['choco_temp_dir']}\\7za.exe"
$_download_url = $chocolatey::chocolatey_download_url

# lint:ignore:only_variable_string
if "${_download_url}" =~ /^http(s)?:\/\/.*api\/v2\/package.*\/$/ and "${chocolatey::chocolatey_version}" =~ /\d+\./ {
Expand All @@ -22,40 +15,47 @@
}
# lint:endignore

if $chocolatey::use_7zip {
$unzip_type = '7zip'
file { $seven_zip_exe:
ensure => file,
source => $seven_zip_download_url,
replace => false,
mode => '0755',
before => Exec['install_chocolatey_official'],
}
} else {
$unzip_type = 'windows'
}

registry_value { 'ChocolateyInstall environment value':
ensure => present,
path => 'HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\ChocolateyInstall',
type => 'string',
data => $chocolatey::choco_install_location,
}

# New install script exclusively uses environment variables in powershell.
# Filter out undef values and build an array of ['key=value'] to be passed in to the execs environment attribute.
# The 'chocolateyVersion' can also be passed to the install script. Instead only pass an explicit 'chocolateyDownloadUrl' that we
# already build correctly if $chocolatey::chocolatey_version is set. This allows for a custom $chocolatey:chocolatey_download_url
# to be used with no modification to the script. If the version is unset, the default behavior remains the same which is to attempt
# to install the latest available version.
$install_parameters = {
'download_url' => $download_url,
'unzip_type' => $unzip_type,
'_install_proxy' => $_install_proxy,
'seven_zip_exe' => $seven_zip_exe,
}
'ChocolateyInstall' => $chocolatey::choco_install_location,
'chocolateyDownloadUrl' => $download_url,
'chocolateyProxyLocation' => $chocolatey::install_proxy,
'chocolateyProxyUser' => $chocolatey::install_proxy_user,
'chocolateyProxyPassword' => $chocolatey::install_proxy_password.unwrap,
'chocolateyIgnoreProxy' => $chocolatey::install_ignore_proxy,
# install script defaults to using 7zip, this module defaults to using windows compression.
# this is ignored by install script if $PSVersionTable.PSVersion >= 5
'chocolateyUseWindowsCompression' => $chocolatey::use_7zip ? {
true => false,
false => true,
},
'TEMP' => $chocolatey::install_tempdir,
}.filter |$k, $v| { $v != undef }.map |$k, $v| { join([$k, $v], '=') }

# install.ps1 obtained from: https://community.chocolatey.org/install.ps1
# Lightly modified to allow a custom $seven_zip_download_url. This has the benefit of using the same proxy
# if configured to use a proxy. For this module this is also a change in behavior as previously this was managed
# as a separate File resource.
# Since it was modified the [SIG] block was also removed
exec { 'install_chocolatey_official':
command => epp('chocolatey/InstallChocolatey.ps1.epp', $install_parameters),
command => epp('chocolatey/install.ps1.epp'),
creates => "${chocolatey::choco_install_location}\\bin\\choco.exe",
provider => powershell,
timeout => $chocolatey::choco_install_timeout_seconds,
logoutput => $chocolatey::log_output,
environment => ["ChocolateyInstall=${chocolatey::choco_install_location}"],
environment => $install_parameters,
require => Registry_value['ChocolateyInstall environment value'],
}
}
81 changes: 65 additions & 16 deletions spec/classes/install_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@
end

context 'contains install.pp' do
['c:\local_folder', 'C:\\ProgramData\\chocolatey'].each do |param_value|
context "choco_install_location => #{param_value}" do
let(:params) { { choco_install_location: param_value } }
let(:exec_environment) { catalogue.resource('exec', 'install_chocolatey_official').send(:parameters)[:environment] }

it { is_expected.to contain_exec('install_chocolatey_official').with_creates("#{param_value}\\bin\\choco.exe") }
['c:\local_folder', 'C:\\ProgramData\\chocolatey', 'C:\Program Files\chocolatey'].each do |choco_install_path|
context "choco_install_location => #{choco_install_path}" do
let(:params) { { choco_install_location: choco_install_path } }

it { is_expected.to contain_exec('install_chocolatey_official').with_creates("#{choco_install_path}\\bin\\choco.exe") }
it { expect(exec_environment).to include("ChocolateyInstall=#{choco_install_path}") }
it { is_expected.to contain_registry_value('ChocolateyInstall environment value').with_data(choco_install_path) }
end
end

Expand All @@ -32,27 +36,72 @@
context 'use_7zip => false' do
let(:params) { { use_7zip: false } }

it {
expect(subject).not_to contain_file('C:\Temp\7za.exe')
}
it { expect(exec_environment).to include('chocolateyUseWindowsCompression=true') }
end

context 'use_7zip => true' do
let(:params) { { use_7zip: true } }

context 'seven_zip_download_url default' do
let(:params) { { use_7zip: true } }
it { expect(exec_environment).to include('chocolateyUseWindowsCompression=false') }
end

it { is_expected.to contain_file('C:\Temp\7za.exe').with_source('https://chocolatey.org/7za.exe') }
['https://packages.organization.net/7za.exe'].each do |seven_zip_url|
context "seven_zip_download_url => '#{seven_zip_url}'" do
let(:params) do
super().merge({ seven_zip_download_url: seven_zip_url })
end

it { is_expected.to contain_exec('install_chocolatey_official').with_command(%r{.*Request-File -Url #{seven_zip_url}.*}) }
end
end
end

['http://proxy.megacorp.com:3128'].each do |proxy_url|
context "install_proxy => '#{proxy_url}'" do
let(:params) { { install_proxy: proxy_url } }

it { expect(exec_environment).to include("chocolateyProxyLocation=#{proxy_url}") }

context 'install_proxy_user => \'Bob\'' do
let(:params) do
super().merge({ install_proxy_user: 'Bob' })
end

it { expect(exec_environment).to include('chocolateyProxyUser=Bob') }
end

context "seven_zip_download_url => 'https://packages.organization.net/7za.exe'" do
let(:params) do
{
use_7zip: true,
seven_zip_download_url: 'https://packages.organization.net/7za.exe'
}
context 'install_proxy_password => \'SomeP@$$\'' do
let(:params) do
super().merge({ install_proxy_password: sensitive('SomeP@$$') })
end

it { expect(exec_environment).to include('chocolateyProxyPassword=SomeP@$$') }
end

context 'install_proxy_ignore => true' do
let(:params) do
super().merge({ install_ignore_proxy: true })
end

it { expect(exec_environment).to include('chocolateyIgnoreProxy=true') }
end
end
end

['https://internalurl/to/chocolatey.nupkg'].each do |param_value|
context "download_url => '#{param_value}'" do
let(:params) { { chocolatey_download_url: param_value } }

it { expect(exec_environment).to include("chocolateyDownloadUrl=#{param_value}") }
end
end

['C:/temp'].each do |param_value|
context "install_tempdir => '#{param_value}'" do
let(:params) { { install_tempdir: param_value } }

it { is_expected.to contain_file('C:\Temp\7za.exe').with_source('https://packages.organization.net/7za.exe') }
it { expect(exec_environment).to include("TEMP=#{param_value}") }
end
end
end
Expand Down
Loading
Loading