|
1 | 1 | <# |
2 | 2 | .SYNOPSIS |
3 | 3 | A simple Powershell script to download and install a salt minion on windows. |
| 4 | +
|
4 | 5 | .DESCRIPTION |
5 | 6 | The script will download the official salt package from saltstack. It will install a specific |
6 | 7 | package version and accept parameters for the master and minion ids. Finally, it can stop and |
7 | 8 | set the windows service to "manual" for local testing. |
| 9 | +
|
8 | 10 | .EXAMPLE |
9 | 11 | ./bootstrap-salt.ps1 |
10 | 12 | Runs without any parameters. Uses all the default values/settings. |
| 13 | +
|
11 | 14 | .EXAMPLE |
12 | 15 | ./bootstrap-salt.ps1 -version 2015.4.1-3 |
13 | 16 | Specifies a particular version of the installer. |
| 17 | +
|
14 | 18 | .EXAMPLE |
15 | 19 | ./bootstrap-salt.ps1 -runservice false |
16 | 20 | Specifies the salt-minion service to stop and be set to manual. |
17 | 21 | Useful for testing locally from the command line with the --local switch |
| 22 | +
|
18 | 23 | .EXAMPLE |
19 | 24 | ./bootstrap-salt.ps1 -minion minion-box -master master-box |
20 | 25 | Specifies the minion and master ids in the minion config. |
21 | 26 | Defaults to the installer values of "minion" and "master". |
| 27 | +
|
22 | 28 | .EXAMPLE |
23 | 29 | ./bootstrap-salt.ps1 -minion minion-box -master master-box -version 2015.5.2 -runservice false |
24 | 30 | Specifies all the optional parameters in no particular order. |
| 31 | +
|
25 | 32 | .PARAMETER version - Default version defined in this script. |
26 | 33 |
|
27 | 34 | .PARAMETER runservice - Boolean flag to stop the windows service and set to "manual". |
|
47 | 54 | [Parameter(Mandatory=$false,ValueFromPipeline=$true)] |
48 | 55 | # Doesn't support versions prior to "YYYY.M.R-B" |
49 | 56 | [ValidatePattern('^(201[0-9]\.[0-9]\.[0-9](\-\d{1})?)$')] |
50 | | - [string]$version = "2015.8.8-2", |
| 57 | + [string]$version = '', |
51 | 58 |
|
52 | 59 | [Parameter(Mandatory=$false,ValueFromPipeline=$true)] |
53 | 60 | [ValidateSet("true","false")] |
@@ -99,15 +106,33 @@ If ([IntPtr]::Size -eq 4) { |
99 | 106 | $arch = "AMD64" |
100 | 107 | } |
101 | 108 |
|
| 109 | +# If version isn't supplied, use latest. |
| 110 | +if (!$version) { |
| 111 | + # Find latest version of Salt Minion |
| 112 | + $repo = Invoke-Restmethod 'http://repo.saltstack.com/windows/' |
| 113 | + $regex = "<\s*a\s*[^>]*?href\s*=\s*[`"']*([^`"'>]+)[^>]*?>" |
| 114 | + $returnMatches = new-object System.Collections.ArrayList |
| 115 | + $resultingMatches = [Regex]::Matches($repo, $regex, "IgnoreCase") |
| 116 | + foreach($match in $resultingMatches) |
| 117 | + { |
| 118 | + $cleanedMatch = $match.Groups[1].Value.Trim() |
| 119 | + [void] $returnMatches.Add($cleanedMatch) |
| 120 | + } |
| 121 | + if ($arch -eq 'x86') {$returnMatches = $returnMatches | Where {$_ -like "Salt-Minion*x86-Setup.exe"}} |
| 122 | + else {$returnMatches = $returnMatches | Where {$_ -like "Salt-Minion*AMD64-Setup.exe"}} |
| 123 | + |
| 124 | + $version = $(($returnMatches | Sort-Object -Descending)[0]).Split(("n-","-A","-x"),([System.StringSplitOptions]::RemoveEmptyEntries))[1] |
| 125 | +} |
| 126 | + |
102 | 127 | # Download minion setup file |
103 | | -Write-Host -NoNewline "Downloading Salt minion installer Salt-Minion-$version-$arch-Setup.exe" |
| 128 | +Write-Output -NoNewline "Downloading Salt minion installer Salt-Minion-$version-$arch-Setup.exe" |
104 | 129 | $webclient = New-Object System.Net.WebClient |
105 | 130 | $url = "https://repo.saltstack.com/windows/Salt-Minion-$version-$arch-Setup.exe" |
106 | 131 | $file = "C:\tmp\salt.exe" |
107 | 132 | $webclient.DownloadFile($url, $file) |
108 | 133 |
|
109 | 134 | # Install minion silently |
110 | | -Write-Host -NoNewline "Installing Salt minion" |
| 135 | +Write-Output -NoNewline "Installing Salt minion" |
111 | 136 | #Wait for process to exit before continuing. |
112 | 137 | C:\tmp\salt.exe /S /minion-name=$minion /master=$master | Out-Null |
113 | 138 |
|
@@ -141,14 +166,14 @@ If($runservice) { |
141 | 166 | # If the salt-minion service is still not running, something probably |
142 | 167 | # went wrong and user intervention is required - report failure. |
143 | 168 | If ($service.Status -eq "Stopped") { |
144 | | - Write-Host -NoNewline "Failed to start salt minion" |
| 169 | + Write-Output -NoNewline "Failed to start salt minion" |
145 | 170 | exit 1 |
146 | 171 | } |
147 | 172 | } |
148 | 173 | Else { |
149 | | - Write-Host -NoNewline "Stopping salt minion and setting it to 'Manual'" |
| 174 | + Write-Output -NoNewline "Stopping salt minion and setting it to 'Manual'" |
150 | 175 | Set-Service "salt-minion" -startupType "Manual" |
151 | 176 | Stop-Service "salt-minion" |
152 | 177 | } |
153 | 178 |
|
154 | | -Write-Host -NoNewline "Salt minion successfully installed" |
| 179 | +Write-Output "Salt minion successfully installed" |
0 commit comments