Merge pull request #1828 from twangboy/fix_win

Handle missing registry entry properly
This commit is contained in:
Shane Lee 2022-05-18 09:25:52 -07:00 committed by GitHub
commit 6a07978663
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,11 +1,11 @@
<# <#
.SYNOPSIS .SYNOPSIS
A simple Powershell script to download and install a salt minion on windows. A simple Powershell script to download and install a Salt minion on Windows.
.DESCRIPTION .DESCRIPTION
The script will download the official salt package from saltstack. It will The script will download the official Salt package from SaltProject. It will
install a specific package version and accept parameters for the master and install a specific package version and accept parameters for the master and
minion ids. Finally, it can stop and set the windows service to "manual" for minion ids. Finally, it can stop and set the Windows service to "manual" for
local testing. local testing.
.EXAMPLE .EXAMPLE
@ -18,8 +18,9 @@
.EXAMPLE .EXAMPLE
./bootstrap-salt.ps1 -pythonVersion 3 ./bootstrap-salt.ps1 -pythonVersion 3
Specifies the Python version of the installer. Can be "2" or "3". Defaults to "2". Specifies the Python version of the installer. Can be "2" or "3". Defaults
Python 3 installers are only available for Salt 2017.7.0 and newer. to "2". Python 3 installers are only available for Salt 2017.7.0 and newer.
Starting with Python 3002 only Python 3 installers are available.
.EXAMPLE .EXAMPLE
./bootstrap-salt.ps1 -runservice false ./bootstrap-salt.ps1 -runservice false
@ -71,36 +72,36 @@
#> #>
#=============================================================================== #===============================================================================
# Commandlet Binding # Bind Parameters
#=============================================================================== #===============================================================================
[CmdletBinding()] [CmdletBinding()]
Param( param(
[Parameter(Mandatory=$false,ValueFromPipeline=$true)] [Parameter(Mandatory=$false, ValueFromPipeline=$True)]
# Doesn't support versions prior to "YYYY.M.R-B" # Doesn't support versions prior to "YYYY.M.R-B"
# Supports new version and latest # Supports new version and latest
# Option 1 means case insensitive # Option 1 means case insensitive
[ValidatePattern('^(\d{4}(\.\d{1,2}){0,2}(\-\d{1})?)|(latest)$', Options=1)] [ValidatePattern('^(\d{4}(\.\d{1,2}){0,2}(\-\d{1})?)|(latest)$', Options=1)]
[string]$version = '', [string]$Version = '',
[Parameter(Mandatory=$false,ValueFromPipeline=$true)] [Parameter(Mandatory=$false, ValueFromPipeline=$True)]
# Doesn't support Python versions prior to "2017.7.0" # Doesn't support Python versions prior to "2017.7.0"
[ValidateSet("2","3")] [ValidateSet("2","3")]
[string]$pythonVersion = "3", [string]$PythonVersion = "3",
[Parameter(Mandatory=$false,ValueFromPipeline=$true)] [Parameter(Mandatory=$false, ValueFromPipeline=$True)]
[ValidateSet("true","false")] [ValidateSet("true","false")]
[string]$runservice = "true", [string]$RunService = "true",
[Parameter(Mandatory=$false,ValueFromPipeline=$true)] [Parameter(Mandatory=$false, ValueFromPipeline=$True)]
[string]$minion = "not-specified", [string]$Minion = "not-specified",
[Parameter(Mandatory=$false,ValueFromPipeline=$true)] [Parameter(Mandatory=$false, ValueFromPipeline=$True)]
[string]$master = "not-specified", [string]$Master = "not-specified",
[Parameter(Mandatory=$false,ValueFromPipeline=$true)] [Parameter(Mandatory=$false, ValueFromPipeline=$True)]
[string]$repourl= "https://repo.saltproject.io/windows", [string]$RepoUrl= "https://repo.saltproject.io/windows",
[Parameter(Mandatory=$false,ValueFromPipeline=$true)] [Parameter(Mandatory=$false, ValueFromPipeline=$True)]
[switch]$ConfigureOnly [switch]$ConfigureOnly
) )
@ -125,8 +126,8 @@ function Get-IsUacEnabled
#=============================================================================== #===============================================================================
# Check for Elevated Privileges # Check for Elevated Privileges
#=============================================================================== #===============================================================================
If (!(Get-IsAdministrator)) { if (!(Get-IsAdministrator)) {
If (Get-IsUacEnabled) { if (Get-IsUacEnabled) {
# We are not running "as Administrator" - so relaunch as administrator # We are not running "as Administrator" - so relaunch as administrator
# Create a new process object that starts PowerShell # Create a new process object that starts PowerShell
$newProcess = new-object System.Diagnostics.ProcessStartInfo "PowerShell"; $newProcess = new-object System.Diagnostics.ProcessStartInfo "PowerShell";
@ -149,10 +150,10 @@ If (!(Get-IsAdministrator)) {
[System.Diagnostics.Process]::Start($newProcess); [System.Diagnostics.Process]::Start($newProcess);
# Exit from the current, unelevated, process # Exit from the current, unelevated, process
Exit exit
} }
Else { else {
Throw "You must be administrator to run this script" throw "You must be administrator to run this script"
} }
} }
@ -166,15 +167,15 @@ Write-Verbose "master: $master"
Write-Verbose "minion: $minion" Write-Verbose "minion: $minion"
Write-Verbose "repourl: $repourl" Write-Verbose "repourl: $repourl"
If ($runservice.ToLower() -eq "true") { if ($runservice.ToLower() -eq "true") {
Write-Verbose "Windows service will be set to run" Write-Verbose "Windows service will be set to run"
[bool]$runservice = $True [bool]$runservice = $True
} }
ElseIf ($runservice.ToLower() -eq "false") { elseif ($runservice.ToLower() -eq "false") {
Write-Verbose "Windows service will be stopped and set to manual" Write-Verbose "Windows service will be stopped and set to manual"
[bool]$runservice = $False [bool]$runservice = $False
} }
Else { else {
# Param passed in wasn't clear so defaulting to true. # Param passed in wasn't clear so defaulting to true.
Write-Verbose "Windows service defaulting to run automatically" Write-Verbose "Windows service defaulting to run automatically"
[bool]$runservice = $True [bool]$runservice = $True
@ -186,22 +187,28 @@ Else {
$ConfiguredAnything = $False $ConfiguredAnything = $False
$RootDir = "C:\salt"
$SaltRegKey = "HKLM:\SOFTWARE\Salt Project\Salt" $SaltRegKey = "HKLM:\SOFTWARE\Salt Project\Salt"
$RootDir = If ((Get-ItemProperty $SaltRegKey).root_dir -ne $null) { if (Test-Path -Path $SaltRegKey) {
(Get-ItemProperty $SaltRegKey).root_dir if ($null -ne (Get-ItemProperty $SaltRegKey).root_dir) {
} Else { $RootDir = (Get-ItemProperty $SaltRegKey).root_dir
"C:\salt" }
} }
$ConfDir = "$RootDir\conf" $ConfDir = "$RootDir\conf"
$PkiDir = "$ConfDir\pki\minion" $PkiDir = "$ConfDir\pki\minion"
Write-Verbose "ConfDir: $ConfDir"
# Create C:\tmp\ # Create C:\tmp\
New-Item C:\tmp\ -ItemType directory -Force | Out-Null New-Item C:\tmp\ -ItemType directory -Force | Out-Null
# Copy Vagrant Files to their proper location. Vagrant files will be placed #===============================================================================
# in C:\tmp # Copy Vagrant Files to their proper location.
#===============================================================================
# Vagrant files will be placed in C:\tmp
# Check if minion keys have been uploaded, copy to correct location # Check if minion keys have been uploaded, copy to correct location
If (Test-Path C:\tmp\minion.pem) { if (Test-Path C:\tmp\minion.pem) {
New-Item $PkiDir -ItemType Directory -Force | Out-Null New-Item $PkiDir -ItemType Directory -Force | Out-Null
Copy-Item -Path C:\tmp\minion.pem -Destination $PkiDir -Force | Out-Null Copy-Item -Path C:\tmp\minion.pem -Destination $PkiDir -Force | Out-Null
Copy-Item -Path C:\tmp\minion.pub -Destination $PkiDir -Force | Out-Null Copy-Item -Path C:\tmp\minion.pub -Destination $PkiDir -Force | Out-Null
@ -211,20 +218,20 @@ If (Test-Path C:\tmp\minion.pem) {
# Check if minion config has been uploaded # Check if minion config has been uploaded
# This should be done before the installer is run so that it can be updated with # This should be done before the installer is run so that it can be updated with
# id: and master: settings when the installer runs # id: and master: settings when the installer runs
If (Test-Path C:\tmp\minion) { if (Test-Path C:\tmp\minion) {
New-Item $ConfDir -ItemType Directory -Force | Out-Null New-Item $ConfDir -ItemType Directory -Force | Out-Null
Copy-Item -Path C:\tmp\minion -Destination $ConfDir -Force | Out-Null Copy-Item -Path C:\tmp\minion -Destination $ConfDir -Force | Out-Null
$ConfiguredAnything = $True $ConfiguredAnything = $True
} }
# Check if grains config has been uploaded # Check if grains config has been uploaded
If (Test-Path C:\tmp\grains) { if (Test-Path C:\tmp\grains) {
New-Item $ConfDir -ItemType Directory -Force | Out-Null New-Item $ConfDir -ItemType Directory -Force | Out-Null
Copy-Item -Path C:\tmp\grains -Destination $ConfDir -Force | Out-Null Copy-Item -Path C:\tmp\grains -Destination $ConfDir -Force | Out-Null
$ConfiguredAnything = $True $ConfiguredAnything = $True
} }
If ($ConfigureOnly -and !$ConfiguredAnything) { if ($ConfigureOnly -and !$ConfiguredAnything) {
Write-Output "No configuration or keys were copied over. No configuration was done!" Write-Output "No configuration or keys were copied over. No configuration was done!"
exit 0 exit 0
} }
@ -232,31 +239,30 @@ If ($ConfigureOnly -and !$ConfiguredAnything) {
#=============================================================================== #===============================================================================
# Detect architecture # Detect architecture
#=============================================================================== #===============================================================================
If ([IntPtr]::Size -eq 4) { if ([IntPtr]::Size -eq 4) {
$arch = "x86" $arch = "x86"
} } else {
Else {
$arch = "AMD64" $arch = "AMD64"
} }
#=============================================================================== #===============================================================================
# Use version "Latest" if no version is passed # Use version "Latest" if no version is passed
#=============================================================================== #===============================================================================
If ((!$version) -or ($version.ToLower() -eq 'latest')){ if ((!$version) -or ($version.ToLower() -eq 'latest')){
$versionSection = "Latest-Py$pythonVersion" $versionSection = "Latest-Py$PythonVersion"
} else { } else {
$versionSection = $version $versionSection = $version
$year = $version.Substring(0, 4) $year = $version.Substring(0, 4)
If ([int]$year -ge 2017) { if ([int]$year -ge 2017) {
If ($pythonVersion -eq "3") { if ($PythonVersion -eq "3") {
$versionSection = "$version-Py3" $versionSection = "$version-Py3"
} Else { } else {
$versionSection = "$version-Py2" $versionSection = "$version-Py2"
} }
} }
} }
If (!$ConfigureOnly) { if (!$ConfigureOnly) {
#=============================================================================== #===============================================================================
# Download minion setup file # Download minion setup file
#=============================================================================== #===============================================================================
@ -275,9 +281,9 @@ If (!$ConfigureOnly) {
# - master: salt # - master: salt
# - Start the service # - Start the service
$parameters = "" $parameters = ""
If($minion -ne "not-specified") {$parameters = "/minion-name=$minion"} if($minion -ne "not-specified") {$parameters = "/minion-name=$minion"}
If($master -ne "not-specified") {$parameters = "$parameters /master=$master"} if($master -ne "not-specified") {$parameters = "$parameters /master=$master"}
If($runservice -eq $false) {$parameters = "$parameters /start-service=0"} if($runservice -eq $false) {$parameters = "$parameters /start-service=0"}
#=============================================================================== #===============================================================================
# Install minion silently # Install minion silently
@ -291,12 +297,12 @@ If (!$ConfigureOnly) {
#=============================================================================== #===============================================================================
# Wait for salt-minion service to be registered before trying to start it # Wait for salt-minion service to be registered before trying to start it
$service = Get-Service salt-minion -ErrorAction SilentlyContinue $service = Get-Service salt-minion -ErrorAction SilentlyContinue
While (!$service) { while (!$service) {
Start-Sleep -s 2 Start-Sleep -s 2
$service = Get-Service salt-minion -ErrorAction SilentlyContinue $service = Get-Service salt-minion -ErrorAction SilentlyContinue
} }
If($runservice) { if($runservice) {
# Start service # Start service
Write-Output "Starting the Salt minion service" Write-Output "Starting the Salt minion service"
Start-Service -Name "salt-minion" -ErrorAction SilentlyContinue Start-Service -Name "salt-minion" -ErrorAction SilentlyContinue
@ -304,7 +310,7 @@ If (!$ConfigureOnly) {
# Check if service is started, otherwise retry starting the # Check if service is started, otherwise retry starting the
# service 4 times. # service 4 times.
$try = 0 $try = 0
While (($service.Status -ne "Running") -and ($try -ne 4)) { while (($service.Status -ne "Running") -and ($try -ne 4)) {
Start-Service -Name "salt-minion" -ErrorAction SilentlyContinue Start-Service -Name "salt-minion" -ErrorAction SilentlyContinue
$service = Get-Service salt-minion -ErrorAction SilentlyContinue $service = Get-Service salt-minion -ErrorAction SilentlyContinue
Start-Sleep -s 2 Start-Sleep -s 2
@ -313,12 +319,12 @@ If (!$ConfigureOnly) {
# If the salt-minion service is still not running, something probably # If the salt-minion service is still not running, something probably
# went wrong and user intervention is required - report failure. # went wrong and user intervention is required - report failure.
If ($service.Status -eq "Stopped") { if ($service.Status -eq "Stopped") {
Write-Output -NoNewline "Failed to start salt minion" Write-Output -NoNewline "Failed to start salt minion"
exit 1 exit 1
} }
} }
Else { else {
Write-Output -NoNewline "Stopping salt minion and setting it to 'Manual'" Write-Output -NoNewline "Stopping salt minion and setting it to 'Manual'"
Set-Service "salt-minion" -StartupType "Manual" Set-Service "salt-minion" -StartupType "Manual"
Stop-Service "salt-minion" Stop-Service "salt-minion"
@ -328,9 +334,9 @@ If (!$ConfigureOnly) {
#=============================================================================== #===============================================================================
# Script Complete # Script Complete
#=============================================================================== #===============================================================================
If ($ConfigureOnly) { if ($ConfigureOnly) {
Write-Output "Salt minion successfully configured" Write-Output "Salt minion successfully configured"
} }
Else { else {
Write-Output "Salt minion successfully installed" Write-Output "Salt minion successfully installed"
} }