Skip to content

Commit

Permalink
(MODULES-8431) Update windows installation to use powershell
Browse files Browse the repository at this point in the history
This commit updates the installation method on windows to use a powershell
script rather than a batch file
  • Loading branch information
Sean P. McDonald committed Jan 15, 2019
1 parent 63ad770 commit 6aba3a6
Show file tree
Hide file tree
Showing 5 changed files with 326 additions and 147 deletions.
19 changes: 19 additions & 0 deletions lib/puppet/parser/functions/windows_msi_installargs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Puppet::Parser::Functions
newfunction(:windows_msi_installargs, :arity => 1, :type => :rvalue, :doc => <<-EOS
Return the $install_options parameter as a string usable in an msiexec command
EOS
) do |args|

install_args = args[0]

arg_string = install_args.map do |option|
if option.class == Hash
key_value = option.shift
"#{key_value[0]}=\"#{key_value[1]}\""
else
option
end
end
return arg_string.join(' ')
end
end
35 changes: 19 additions & 16 deletions manifests/windows/install.pp
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,27 @@
$_https_source = "https://downloads.puppetlabs.com/windows/${dir}${package_file_name}"
}

$_install_options = $install_options ? {
[] => ['REINSTALLMODE="amus"'],
default => $install_options
}
$_installps1 = windows_native_path("${::env_temp_variable}/install_puppet.ps1")

$_source = $source ? {
undef => $_https_source,
/^[a-zA-Z]:/ => windows_native_path($source),
default => $source,
}

$_msi_location = $_source ? {
/^puppet:/ => windows_native_path("${::env_temp_variable}/puppet-agent.msi"),
default => $_source,
}

$_installbat = windows_native_path("${::env_temp_variable}/install_puppet.bat")
if $_source =~ /^puppet:/ {
file{ $_msi_location:
source => $_source,
before => File["${_installbat}"],
before => File["${_installps1}"],
}
}

$_cmd_location = $::rubyplatform ? {
/i386/ => 'C:\\Windows\\system32\\cmd.exe',
default => "${::system32}\\cmd.exe"
$_install_options = $install_options ? {
[] => windows_msi_installargs(['REINSTALLMODE="amus"']),
default => windows_msi_installargs($install_options)
}

if (member($::puppet_agent::service_names, 'puppet')) {
Expand All @@ -62,24 +56,33 @@
$_agent_startup_mode = undef
}

if $msi_move_locked_files {
$_move_dll_workaround = '$true'
} else {
$_move_dll_workaround = '$false'
}

$_timestamp = strftime('%Y_%m_%d-%H_%M')
$_logfile = windows_native_path("${::env_temp_variable}/puppet-${_timestamp}-installer.log")
$_puppet_master = $::puppet_master_server
$_install_pid_file_loc = windows_native_path("${::env_temp_variable}/puppet_agent_install.pid")

notice ("Puppet upgrade log file at ${_logfile}")
debug ("Installing puppet from ${_msi_location}")
file { "${_installbat}":

file { "${_installps1}":
ensure => file,
content => template('puppet_agent/install_puppet.bat.erb')
content => template('puppet_agent/install_puppet.ps1.erb')
}
-> exec { 'install_puppet.bat':
command => "${::system32}\\cmd.exe /c start /b ${_cmd_location} /c \"${_installbat}\" ${::puppet_agent_pid}",
-> exec { 'install_puppet.ps1':
command => "${::system32}\\cmd.exe /c start /b ${::system32}\\WindowsPowerShell\\v1.0\\powershell.exe -ExecutionPolicy Bypass -NoProfile -NoLogo -NonInteractive -File ${_installps1} ${::puppet_agent_pid}",
path => $::path,
}

# PUP-5480/PE-15037 Cache dir loses inheritable SYSTEM perms
exec { 'fix inheritable SYSTEM perms':
command => "${::system32}\\icacls.exe \"${::puppet_client_datadir}\" /grant \"SYSTEM:(OI)(CI)(F)\"",
unless => "${::system32}\\icacls.exe \"${::puppet_client_datadir}\" | findstr \"SYSTEM:(OI)(CI)(F)\"",
require => Exec['install_puppet.bat'],
require => Exec['install_puppet.ps1'],
}
}
76 changes: 28 additions & 48 deletions spec/classes/puppet_agent_windows_install_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
RSpec.describe 'puppet_agent', tag: 'win' do
package_version = '1.10.100.1'
global_params = {
:package_version => package_version
:package_version => package_version,
}

{'5.1' => {:expect_arch => 'x86', :appdata => 'C:\Documents and Settings\All Users\Application Data'},
Expand Down Expand Up @@ -70,8 +70,8 @@
})}

it { is_expected.to contain_class('puppet_agent::windows::install') }
it { is_expected.to contain_file('C:\tmp\install_puppet.bat').with_content(
%r[#{Regexp.escape("msiexec.exe /qn /norestart /i \"#{values[:appdata]}\\Puppetlabs\\packages\\puppet-agent-#{values[:expect_arch]}.msi\"")}])
it { is_expected.to contain_file('C:\tmp\install_puppet.ps1').with_content(
%r[#{Regexp.escape("\$Source=\'#{values[:appdata]}\\Puppetlabs\\packages\\puppet-agent-#{values[:expect_arch]}.msi\'")}])
}
it { is_expected.to contain_exec('fix inheritable SYSTEM perms') }
end
Expand All @@ -83,18 +83,18 @@
{:install_options => ['OPTION1=value1','OPTION2=value2'],})
}
it {
is_expected.to contain_file('C:\tmp\install_puppet.bat').with_content(/msiexec.exe .+ OPTION1=value1 OPTION2=value2/)
is_expected.to contain_file('C:\tmp\install_puppet.ps1').with_content(/\$InstallArgs='OPTION1=value1 OPTION2=value2'/)
}
it {
is_expected.not_to contain_file('C:\tmp\install_puppet.bat').with_content(/msiexec.exe .+ REINSTALLMODE="amus"/)
is_expected.not_to contain_file('C:\tmp\install_puppet.ps1').with_content(/\$InstallArgs='REINSTALLMODE="amus"'/)
}
end
end

context 'Default INSTALLMODE Option' do
describe 'REINSTALLMODE=amus' do
it {
is_expected.to contain_file('C:\tmp\install_puppet.bat').with_content(/msiexec.exe .+ REINSTALLMODE="amus"/)
is_expected.to contain_file('C:\tmp\install_puppet.ps1').with_content(/\$InstallArgs='REINSTALLMODE="amus"'/)
}
end
end
Expand All @@ -105,9 +105,8 @@
{:source => 'https://alternate.com/puppet-agent.msi',})
}
it {
is_expected.to contain_file('C:\tmp\install_puppet.bat').with_content(
/msiexec.exe \/qn \/norestart \/i "https:\/\/alternate.com\/puppet-agent.msi"/)
is_expected.to contain_file('C:\tmp\install_puppet.bat').with_content(/\/l\*vx "C:\\tmp\\puppet-\d+_\d+_\d+-\d+_\d+-installer.log"/)
is_expected.to contain_file('C:\tmp\install_puppet.ps1').with_content(/\$Source='https:\/\/alternate.com\/puppet-agent.msi'/)
is_expected.to contain_file('C:\tmp\install_puppet.ps1').with_content(/\$Logfile='C:\\tmp\\puppet-\d+_\d+_\d+-\d+_\d+-installer.log'/)
}
it { is_expected.to contain_exec('fix inheritable SYSTEM perms') }
end
Expand All @@ -117,9 +116,8 @@
{:source => 'C:/tmp/puppet-agent-x64.msi',})
}
it {
is_expected.to contain_file('C:\tmp\install_puppet.bat').with_content(
/msiexec.exe \/qn \/norestart \/i "C:\\tmp\\puppet-agent-x64\.msi"/)
is_expected.to contain_file('C:\tmp\install_puppet.bat').with_content(/\/l\*vx "C:\\tmp\\puppet-\d+_\d+_\d+-\d+_\d+-installer.log"/)
is_expected.to contain_file('C:\tmp\install_puppet.ps1').with_content(/\$Source='C:\\tmp\\puppet-agent-x64\.msi'/)
is_expected.to contain_file('C:\tmp\install_puppet.ps1').with_content(/\$Logfile='C:\\tmp\\puppet-\d+_\d+_\d+-\d+_\d+-installer.log'/)
}
it { is_expected.to contain_exec('fix inheritable SYSTEM perms') }
end
Expand All @@ -129,21 +127,8 @@
{:source => 'C:\Temp/ Folder\puppet-agent-x64.msi',})
}
it {
is_expected.to contain_file('C:\tmp\install_puppet.bat').with_content(
/msiexec.exe \/qn \/norestart \/i "C:\\Temp Folder\\puppet-agent-x64\.msi"/)
is_expected.to contain_file('C:\tmp\install_puppet.bat').with_content(/\/l\*vx "C:\\tmp\\puppet-\d+_\d+_\d+-\d+_\d+-installer.log"/)
}
it { is_expected.to contain_exec('fix inheritable SYSTEM perms') }
end

describe 'C:/Temp/ Folder/puppet-agent-x64.msi' do
let(:params) { global_params.merge(
{:source => 'C:/Temp/ Folder/puppet-agent-x64.msi',})
}
it {
is_expected.to contain_file('C:\tmp\install_puppet.bat').with_content(
/msiexec.exe \/qn \/norestart \/i "C:\\Temp Folder\\puppet-agent-x64\.msi"/)
is_expected.to contain_file('C:\tmp\install_puppet.bat').with_content(/\/l\*vx "C:\\tmp\\puppet-\d+_\d+_\d+-\d+_\d+-installer.log"/)
is_expected.to contain_file('C:\tmp\install_puppet.ps1').with_content(/\$Source='C:\\Temp Folder\\puppet-agent-x64\.msi'/)
is_expected.to contain_file('C:\tmp\install_puppet.ps1').with_content(/\$Logfile='C:\\tmp\\puppet-\d+_\d+_\d+-\d+_\d+-installer.log'/)
}
it { is_expected.to contain_exec('fix inheritable SYSTEM perms') }
end
Expand All @@ -153,22 +138,20 @@
{:source => "\\\\garded\\c$\\puppet-agent-x64.msi",})
}
it {
is_expected.to contain_file('C:\tmp\install_puppet.bat').with_content(
/msiexec.exe \/qn \/norestart \/i "\\\\garded\\c\$\\puppet-agent-x64\.msi"/)
is_expected.to contain_file('C:\tmp\install_puppet.bat').with_content(/\/l\*vx "C:\\tmp\\puppet-\d+_\d+_\d+-\d+_\d+-installer.log"/)
is_expected.to contain_file('C:\tmp\install_puppet.ps1').with_content(/\$Source='\\\\garded\\c\$\\puppet-agent-x64\.msi'/)
is_expected.to contain_file('C:\tmp\install_puppet.ps1').with_content(/\$Logfile='C:\\tmp\\puppet-\d+_\d+_\d+-\d+_\d+-installer.log'/)
}
it { is_expected.to contain_exec('fix inheritable SYSTEM perms') }
end

describe 'default source' do
it {
is_expected.to contain_file('C:\tmp\install_puppet.bat').with_content(
/msiexec.exe \/qn \/norestart \/i "https:\/\/downloads.puppetlabs.com\/windows\/puppet-agent-#{package_version}-#{values[:expect_arch]}\.msi"/)
is_expected.to contain_file('C:\tmp\install_puppet.bat').with_content(/\/l\*vx "C:\\tmp\\puppet-\d+_\d+_\d+-\d+_\d+-installer\.log"/)
is_expected.to contain_file('C:\tmp\install_puppet.ps1').with_content(/\$Source='https:\/\/downloads.puppetlabs.com\/windows\/puppet-agent-#{package_version}-#{values[:expect_arch]}\.msi'/)
is_expected.to contain_file('C:\tmp\install_puppet.ps1').with_content(/\$Logfile='C:\\tmp\\puppet-\d+_\d+_\d+-\d+_\d+-installer.log'/)
}
it {
should contain_exec('install_puppet.bat').with { {
'command' => 'C:\windows\sysnative\cmd.exe /c start /b "C:\tmp\install_puppet.bat" 42',
should contain_exec('install_puppet.ps1').with { {
'command' => 'C:\windows\sysnative\cmd.exe /c start /b C:\windows\sysnative\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -NoProfile -NoLogo -NonInteractive -Command C:\tmp\install_puppet.ps1 42',
} }
}
it {
Expand All @@ -182,10 +165,8 @@
{:source => 'puppet:///puppet_agent/puppet-agent-1.1.0-x86.msi'})
}
it {
is_expected.to contain_file('C:\tmp\puppet-agent.msi').with_before('File[C:\tmp\install_puppet.bat]')
is_expected.to contain_file('C:\tmp\install_puppet.bat').with_content(
/msiexec.exe \/qn \/norestart \/i "C:\\tmp\\puppet-agent.msi"/
)
is_expected.to contain_file('C:\tmp\puppet-agent.msi').with_before('File[C:\tmp\install_puppet.ps1]')
is_expected.to contain_file('C:\tmp\install_puppet.ps1').with_content(/\$Source='C:\\tmp\\puppet-agent.msi'/)
}
it { is_expected.to contain_exec('fix inheritable SYSTEM perms') }
end
Expand All @@ -197,8 +178,7 @@
{:arch => 'x86'})
}
it {
is_expected.to contain_file('C:\tmp\install_puppet.bat').with_content(
/msiexec.exe \/qn \/norestart \/i "https:\/\/downloads.puppetlabs.com\/windows\/puppet-agent-#{package_version}-x86.msi"/
is_expected.to contain_file('C:\tmp\install_puppet.ps1').with_content(/\$Source='https:\/\/downloads.puppetlabs.com\/windows\/puppet-agent-#{package_version}-x86.msi'/
)
}
end
Expand Down Expand Up @@ -227,7 +207,7 @@
context 'msi_move_locked_files =>' do
describe 'default' do
it {
is_expected.to contain_file('C:\tmp\install_puppet.bat').without_content(/Move puppetres\.dll/)
is_expected.to contain_file('C:\tmp\install_puppet.ps1').with_content(/\$UseLockedFilesWorkaround=\$false/)
}
end

Expand All @@ -237,7 +217,7 @@
}

it {
is_expected.to contain_file('C:\tmp\install_puppet.bat').without_content(/Move puppetres\.dll/)
is_expected.to contain_file('C:\tmp\install_puppet.ps1').with_content(/\$UseLockedFilesWorkaround=\$false/)
}
end

Expand All @@ -247,7 +227,7 @@
}

it {
is_expected.to contain_file('C:\tmp\install_puppet.bat').with_content(/Move puppetres\.dll/)
is_expected.to contain_file('C:\tmp\install_puppet.ps1').with_content(/\$UseLockedFilesWorkaround=\$true/)
}
end
end
Expand All @@ -272,8 +252,8 @@
let(:params) { global_params }

it {
is_expected.to contain_exec('install_puppet.bat').with { {
'command' => 'C:\windows\sysnative\cmd.exe /c start /b C:\windows\system32\cmd.exe /c "C:\tmp\install_puppet.bat" 42',
is_expected.to contain_exec('install_puppet.ps1').with { {
'command' => 'C:\windows\sysnative\cmd.exe /c start /b C:\windows\system32\cmd.exe /c "C:\tmp\install_puppet.ps1" 42',
} }
}

Expand All @@ -285,8 +265,8 @@
let(:params) { global_params }

it {
is_expected.to contain_exec('install_puppet.bat').with { {
'command' => 'C:\windows\sysnative\cmd.exe /c start /b C:\windows\sysnative\cmd.exe /c "C:\tmp\install_puppet.bat" 42',
is_expected.to contain_exec('install_puppet.ps1').with { {
'command' => 'C:\windows\sysnative\cmd.exe /c start /b C:\windows\sysnative\cmd.exe /c "C:\tmp\install_puppet.ps1" 42',
} }
}

Expand Down
83 changes: 0 additions & 83 deletions templates/install_puppet.bat.erb

This file was deleted.

Loading

0 comments on commit 6aba3a6

Please sign in to comment.