From 5a30570b9a21e3a7855b17fa06ba01c6594948c6 Mon Sep 17 00:00:00 2001 From: Max Arnold Date: Mon, 4 Jan 2021 10:30:54 +0700 Subject: [PATCH 1/3] Always use upstream Salt bootstrap script on Windows --- plugins/provisioners/salt/bootstrap-salt.ps1 | 155 +------------------ website/content/docs/provisioning/salt.mdx | 4 +- 2 files changed, 8 insertions(+), 151 deletions(-) diff --git a/plugins/provisioners/salt/bootstrap-salt.ps1 b/plugins/provisioners/salt/bootstrap-salt.ps1 index 3e4acbf66..a1bf9822b 100644 --- a/plugins/provisioners/salt/bootstrap-salt.ps1 +++ b/plugins/provisioners/salt/bootstrap-salt.ps1 @@ -1,151 +1,8 @@ -Param( - [string]$version, - [string]$pythonVersion = "2", - [string]$runservice, - [string]$minion, - [string]$master -) +# Powershell supports only TLS 1.0 by default. Add support for TLS 1.2 and TLS 1.3 +[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]'Tls12,Tls13' -# Constants -$ServiceName = "salt-minion" -$startupType = "Manual" +# Download the upstream bootstrap script +(New-Object System.Net.WebClient).DownloadFile('https://winbootstrap.saltstack.com', 'upstream-bootstrap.ps1') -# Version to install - default to latest if there is an issue -If ($version -notmatch "2\d{3}\.\d{1,2}\.\d+(\-\d{1})?"){ - $version = '2018.3.3' -} - -If ($pythonVersion -notmatch "\d+") { - $pythonVersion = "2" - Write-Host "Defaulting to minion Python version $pythonVersion" -} - -If ($runservice.ToLower() -eq "true"){ - Write-Host "Service is set to run." - [bool]$runservice = $True -} -ElseIf ($runservice.ToLower() -eq "false"){ - Write-Host "Service will be stopped and set to manual." - [bool]$runservice = $False -} -Else { - # Param passed in wasn't clear so defaulting to true. - Write-Host "Service defaulting to run." - [bool]$runservice = $True -} - - -# Create C:\tmp\ - if Vagrant doesn't upload keys and/or config it might not exist -New-Item C:\tmp\ -ItemType directory -force | out-null - -# Copy minion keys & config to correct location -New-Item C:\salt\conf\pki\minion\ -ItemType directory -force | out-null - -# Check if minion keys have been uploaded -If (Test-Path C:\tmp\minion.pem) { - cp C:\tmp\minion.pem C:\salt\conf\pki\minion\ - cp C:\tmp\minion.pub C:\salt\conf\pki\minion\ -} - -# Detect architecture -If ([IntPtr]::Size -eq 4) { - $arch = "x86" -} Else { - $arch = "AMD64" -} - -# Download minion setup file -$minionFilename = "Salt-Minion-$version-$arch-Setup.exe" -$versionYear = [regex]::Match($version, "\d+").Value -If ([convert]::ToInt32($versionYear) -ge 2017) -{ - $minionFilename = "Salt-Minion-$version-Py$pythonVersion-$arch-Setup.exe" -} -Write-Host "Downloading Salt minion installer $minionFilename" -$webclient = New-Object System.Net.WebClient -$url = "https://repo.saltstack.com/windows/$minionFilename" -$file = "C:\tmp\salt.exe" - -[int]$retries = 0 -Do { - try { - $retries++ - $ErrorActionPreference='Stop' - $webclient.DownloadFile($url, $file) - break - } catch [Exception] { - if($retries -eq 5) { - $_ - $_.GetType() - $_.Exception - $_.Exception.StackTrace - Write-Host - exit 1 - } - Write-Warning "Retrying download in 2 seconds. Retry # $retries" - Start-Sleep -s 2 - } -} -Until($retries -eq 5) - - -# Install minion silently -Write-Host "Installing Salt minion..." -#Wait for process to exit before continuing... -If($PSBoundParameters.ContainsKey('minion') -and $PSBoundParameters.ContainsKey('master')) { - C:\tmp\salt.exe /S /minion-name=$minion /master=$master | Out-Null - Write-Host C:\tmp\salt.exe /S /minion-name=$minion /master=$master | Out-Null -} -ElseIf($PSBoundParameters.ContainsKey('minion') -and !$PSBoundParameters.ContainsKey('master')) { - C:\tmp\salt.exe /S /minion-name=$minion | Out-Null - Write-Host C:\tmp\salt.exe /S /minion-name=$minion | Out-Null -} -ElseIf(!$PSBoundParameters.ContainsKey('minion') -and $PSBoundParameters.ContainsKey('master')) { - C:\tmp\salt.exe /S /master=$master | Out-Null - Write-Host C:\tmp\salt.exe /S /master=$master | Out-Null -} -Else { - C:\tmp\salt.exe /S | Out-Null - Write-Host C:\tmp\salt.exe /S | Out-Null -} - -# Check if minion config has been uploaded -If (Test-Path C:\tmp\minion) { - cp C:\tmp\minion C:\salt\conf\ -} - -# Wait for salt-minion service to be registered before trying to start it -$service = Get-Service salt-minion -ErrorAction SilentlyContinue -While (!$service) { - Start-Sleep -s 2 - $service = Get-Service salt-minion -ErrorAction SilentlyContinue -} - -If($runservice) { - # Start service - Start-Service -Name "salt-minion" -ErrorAction SilentlyContinue - - # Check if service is started, otherwise retry starting the - # service 4 times. - $try = 0 - While (($service.Status -ne "Running") -and ($try -ne 4)) { - Start-Service -Name "salt-minion" -ErrorAction SilentlyContinue - $service = Get-Service salt-minion -ErrorAction SilentlyContinue - Start-Sleep -s 2 - $try += 1 - } - - # If the salt-minion service is still not running, something probably - # went wrong and user intervention is required - report failure. - If ($service.Status -eq "Stopped") { - Write-Host "Failed to start Salt minion" - exit 1 - } -} -Else { - Write-Host "Stopping salt minion" - Set-Service "$ServiceName" -startupType "$startupType" - Stop-Service "$ServiceName" -} - -Write-Host "Salt minion successfully installed" \ No newline at end of file +# Run the upstream bootstrap script with passthrough arguments +./upstream-bootstrap.ps1 @args diff --git a/website/content/docs/provisioning/salt.mdx b/website/content/docs/provisioning/salt.mdx index 28ae56682..5c002f0b0 100644 --- a/website/content/docs/provisioning/salt.mdx +++ b/website/content/docs/provisioning/salt.mdx @@ -74,14 +74,14 @@ for the bootstrap script. - `always_install` (boolean) - Installs salt binaries even if they are already detected, default `false` -- `bootstrap_script` (string) - Path to your customized salt-bootstrap.sh script. Not supported on Windows guest machines. +- `bootstrap_script` (string) - Path to your customized salt-bootstrap.sh script (or bootstrap-salt.ps1 for Windows). - `bootstrap_options` (string) - Additional command-line options to pass to the bootstrap script. - `version` (string) - Version of minion to be installed. Defaults to latest version. When specifying `version` you must also specify a `install_type`. -- `python_version` (string, default: "2") - Major Python version of minion to be installed. Only valid for minion versions >= 2017.7.0. Only supported on Windows guest machines. +- `python_version` (string, default: "3") - Major Python version of minion to be installed. Only valid for minion versions >= 2017.7.0. Only supported on Windows guest machines. ## Minion Options From f412672b24147878b42c889e9a6f464e5e8f8550 Mon Sep 17 00:00:00 2001 From: Max Arnold Date: Wed, 6 Jan 2021 21:56:57 +0700 Subject: [PATCH 2/3] Download upstream Salt bootstrap script into the same temp directory --- plugins/provisioners/salt/bootstrap-salt.ps1 | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/plugins/provisioners/salt/bootstrap-salt.ps1 b/plugins/provisioners/salt/bootstrap-salt.ps1 index a1bf9822b..cdc78a13b 100644 --- a/plugins/provisioners/salt/bootstrap-salt.ps1 +++ b/plugins/provisioners/salt/bootstrap-salt.ps1 @@ -1,8 +1,11 @@ # Powershell supports only TLS 1.0 by default. Add support for TLS 1.2 and TLS 1.3 [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]'Tls12,Tls13' +# Define script root for PowerShell 2.0 +$ScriptRoot = Split-Path $script:MyInvocation.MyCommand.Path + # Download the upstream bootstrap script -(New-Object System.Net.WebClient).DownloadFile('https://winbootstrap.saltstack.com', 'upstream-bootstrap.ps1') +(New-Object System.Net.WebClient).DownloadFile('https://winbootstrap.saltstack.com', "${ScriptRoot}\bootstrap_salt_upstream.ps1") # Run the upstream bootstrap script with passthrough arguments -./upstream-bootstrap.ps1 @args +& "${ScriptRoot}\bootstrap_salt_upstream.ps1" @args From e0211ebeb12d3542735d5e25fde7c1eaa23df81f Mon Sep 17 00:00:00 2001 From: Max Arnold Date: Thu, 7 Jan 2021 02:13:19 +0700 Subject: [PATCH 3/3] Use saltproject.io domain instead of saltstack.com --- plugins/provisioners/salt/bootstrap-salt.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/provisioners/salt/bootstrap-salt.ps1 b/plugins/provisioners/salt/bootstrap-salt.ps1 index cdc78a13b..a2b11e538 100644 --- a/plugins/provisioners/salt/bootstrap-salt.ps1 +++ b/plugins/provisioners/salt/bootstrap-salt.ps1 @@ -5,7 +5,7 @@ $ScriptRoot = Split-Path $script:MyInvocation.MyCommand.Path # Download the upstream bootstrap script -(New-Object System.Net.WebClient).DownloadFile('https://winbootstrap.saltstack.com', "${ScriptRoot}\bootstrap_salt_upstream.ps1") +(New-Object System.Net.WebClient).DownloadFile('https://winbootstrap.saltproject.io', "${ScriptRoot}\bootstrap_salt_upstream.ps1") # Run the upstream bootstrap script with passthrough arguments & "${ScriptRoot}\bootstrap_salt_upstream.ps1" @args