2015-07-04 07:25:31 +00:00
|
|
|
Param(
|
2015-07-07 06:24:23 +00:00
|
|
|
[string]$version,
|
|
|
|
[string]$runservice,
|
|
|
|
[string]$minion,
|
|
|
|
[string]$master
|
2015-07-04 07:25:31 +00:00
|
|
|
)
|
2015-07-07 06:24:23 +00:00
|
|
|
|
|
|
|
# Constants
|
|
|
|
$ServiceName = "salt-minion"
|
|
|
|
$startupType = "Manual"
|
|
|
|
|
|
|
|
# Version to install - default to latest if there is an issue
|
2017-03-30 21:00:46 +00:00
|
|
|
If ($version -notmatch "2\d{3}\.\d{1,2}\.\d+(\-\d{1})?"){
|
2017-04-05 15:08:29 +00:00
|
|
|
$version = '2016.11.3'
|
2015-07-04 07:25:31 +00:00
|
|
|
}
|
2014-05-28 09:09:59 +00:00
|
|
|
|
2015-07-07 06:24:23 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-28 13:24:47 +00:00
|
|
|
# Create C:\tmp\ - if Vagrant doesn't upload keys and/or config it might not exist
|
2015-04-14 12:57:16 +00:00
|
|
|
New-Item C:\tmp\ -ItemType directory -force | out-null
|
2014-05-28 13:24:47 +00:00
|
|
|
|
2014-05-19 12:01:31 +00:00
|
|
|
# Copy minion keys & config to correct location
|
2015-04-14 12:57:16 +00:00
|
|
|
New-Item C:\salt\conf\pki\minion\ -ItemType directory -force | out-null
|
2014-05-19 13:52:20 +00:00
|
|
|
|
|
|
|
# Check if minion keys have been uploaded
|
2015-07-07 06:24:23 +00:00
|
|
|
If (Test-Path C:\tmp\minion.pem) {
|
2014-05-19 13:52:20 +00:00
|
|
|
cp C:\tmp\minion.pem C:\salt\conf\pki\minion\
|
|
|
|
cp C:\tmp\minion.pub C:\salt\conf\pki\minion\
|
|
|
|
}
|
|
|
|
|
2014-05-17 00:19:34 +00:00
|
|
|
# Detect architecture
|
2015-07-07 06:24:23 +00:00
|
|
|
If ([IntPtr]::Size -eq 4) {
|
2015-03-06 21:14:11 +00:00
|
|
|
$arch = "x86"
|
2015-07-07 06:24:23 +00:00
|
|
|
} Else {
|
2014-05-17 00:19:34 +00:00
|
|
|
$arch = "AMD64"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Download minion setup file
|
2015-07-07 06:24:23 +00:00
|
|
|
Write-Host "Downloading Salt minion installer Salt-Minion-$version-$arch-Setup.exe"
|
2014-05-17 00:19:34 +00:00
|
|
|
$webclient = New-Object System.Net.WebClient
|
2015-11-19 03:55:32 +00:00
|
|
|
$url = "https://repo.saltstack.com/windows/Salt-Minion-$version-$arch-Setup.exe"
|
2014-05-17 00:19:34 +00:00
|
|
|
$file = "C:\tmp\salt.exe"
|
2017-09-23 01:08:30 +00:00
|
|
|
|
|
|
|
[int]$retries = 0
|
|
|
|
Do {
|
|
|
|
try {
|
|
|
|
$retries++
|
|
|
|
$ErrorActionPreference='Stop'
|
|
|
|
$webclient.DownloadFile($url, $file)
|
|
|
|
} catch [Exception] {
|
|
|
|
if($retries -eq 3) {
|
|
|
|
$_
|
|
|
|
$_.GetType()
|
|
|
|
$_.Exception
|
|
|
|
$_.Exception.StackTrace
|
|
|
|
Write-Host
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
Write-Warning "Retrying download in 2 seconds. Retry # $retries"
|
|
|
|
Start-Sleep -s 2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Until($retries -eq 3)
|
2014-05-17 00:19:34 +00:00
|
|
|
|
2015-07-07 06:24:23 +00:00
|
|
|
|
2014-05-17 00:19:34 +00:00
|
|
|
# Install minion silently
|
|
|
|
Write-Host "Installing Salt minion..."
|
2015-03-05 02:08:07 +00:00
|
|
|
#Wait for process to exit before continuing...
|
2015-07-07 06:24:23 +00:00
|
|
|
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
|
2017-04-05 15:08:29 +00:00
|
|
|
Write-Host C:\tmp\salt.exe /S | Out-Null
|
2015-07-07 06:24:23 +00:00
|
|
|
}
|
2015-03-05 02:08:07 +00:00
|
|
|
|
|
|
|
# Check if minion config has been uploaded
|
2015-07-07 06:24:23 +00:00
|
|
|
If (Test-Path C:\tmp\minion) {
|
2015-03-05 02:08:07 +00:00
|
|
|
cp C:\tmp\minion C:\salt\conf\
|
|
|
|
}
|
2014-05-17 00:19:34 +00:00
|
|
|
|
2014-05-19 17:00:18 +00:00
|
|
|
# Wait for salt-minion service to be registered before trying to start it
|
2014-05-19 11:14:25 +00:00
|
|
|
$service = Get-Service salt-minion -ErrorAction SilentlyContinue
|
2014-05-19 15:50:22 +00:00
|
|
|
While (!$service) {
|
2014-05-19 17:00:18 +00:00
|
|
|
Start-Sleep -s 2
|
|
|
|
$service = Get-Service salt-minion -ErrorAction SilentlyContinue
|
2014-05-19 15:50:22 +00:00
|
|
|
}
|
|
|
|
|
2015-07-07 06:24:23 +00:00
|
|
|
If($runservice) {
|
|
|
|
# Start service
|
2014-05-19 11:14:25 +00:00
|
|
|
Start-Service -Name "salt-minion" -ErrorAction SilentlyContinue
|
|
|
|
|
2017-04-05 15:08:29 +00:00
|
|
|
# Check if service is started, otherwise retry starting the
|
2015-07-07 06:24:23 +00:00
|
|
|
# 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"
|
2014-05-19 11:14:25 +00:00
|
|
|
}
|
|
|
|
|
2015-07-04 07:25:31 +00:00
|
|
|
Write-Host "Salt minion successfully installed"
|