-
Notifications
You must be signed in to change notification settings - Fork 552
/
salt-quick-start.ps1
267 lines (240 loc) · 9 KB
/
salt-quick-start.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<#
.SYNOPSIS
A simple Powershell script to quickly start using Salt.
.DESCRIPTION
This script will download the latest onedir version of Salt and extract it
into the same directory where the script is run. The script sets up an
environment that will allow you to run salt-call commands. To remove, just
delete the `salt` directory. The environment variables will only be set for
the current powershell session.
.EXAMPLE
./salt-quick-start.ps1
.LINK
Salt Bootstrap GitHub Project (script home) - https://github.com/saltstack/salt-bootstrap
Original Vagrant Provisioner Project - https://github.com/saltstack/salty-vagrant
Vagrant Project (utilizes this script) - https://github.com/mitchellh/vagrant
Salt Download Location - https://packages.broadcom.com/artifactory/saltproject-generic/windows/
Salt Manual Install Directions (Windows) - https://docs.saltproject.io/salt/install-guide/en/latest/topics/install-by-operating-system/windows.html
#>
# This is so the -Verbose parameter will work
[CmdletBinding()] param()
function Expand-ZipFile {
# Extract a zip file
#
# Args:
# ZipFile (string): The file to extract
# Destination (string): The location to extract to
#
# Error:
# Sets the failed status and exits with a scriptFailed exit code
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string] $ZipFile,
[Parameter(Mandatory = $true)]
[string] $Destination
)
if (!(Test-Path -Path $Destination)) {
Write-Debug "Creating missing directory: $Destination"
New-Item -ItemType directory -Path $Destination
}
Write-Debug "Unzipping '$ZipFile' to '$Destination'"
if ($PSVersionTable.PSVersion.Major -ge 5) {
# PowerShell 5 introduced Expand-Archive
Write-Debug "Using Expand-Archive to unzip"
try{
Expand-Archive -Path $ZipFile -DestinationPath $Destination -Force
} catch {
Write-Debug "Failed to unzip $ZipFile : $_"
exit 1
}
} else {
# This method will work with older versions of powershell, but it is
# slow
Write-Debug "Using Shell.Application to unzip"
$objShell = New-Object -Com Shell.Application
$objZip = $objShell.NameSpace($ZipFile)
try{
foreach ($item in $objZip.Items()) {
$objShell.Namespace($Destination).CopyHere($item, 0x14)
}
} catch {
Write-Debug "Failed to unzip $ZipFile : $_"
exit 1
}
}
Write-Debug "Finished unzipping '$ZipFile' to '$Destination'"
}
function Get-FileHash {
# Get-FileHash is a built-in cmdlet in powershell 5+ but we need to support
# powershell 3. This will overwrite the powershell 5 commandlet only for
# this script. But it will provide the missing cmdlet for powershell 3
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[String] $Path,
[Parameter(Mandatory=$false)]
[ValidateSet(
"SHA1",
"SHA256",
"SHA384",
"SHA512",
# https://serverfault.com/questions/820300/
# why-isnt-mactripledes-algorithm-output-in-powershell-stable
"MACTripleDES", # don't use
"MD5",
"RIPEMD160",
IgnoreCase=$true)]
[String] $Algorithm = "SHA256"
)
if ( !(Test-Path $Path) ) {
Write-Verbose "Invalid path for hashing: $Path"
return @{}
}
if ( (Get-Item -Path $Path) -isnot [System.IO.FileInfo]) {
Write-Verbose "Not a file for hashing: $Path"
return @{}
}
$Path = Resolve-Path -Path $Path
Switch ($Algorithm) {
SHA1 {
$hasher = [System.Security.Cryptography.SHA1CryptoServiceProvider]::Create()
}
SHA256 {
$hasher = [System.Security.Cryptography.SHA256]::Create()
}
SHA384 {
$hasher = [System.Security.Cryptography.SHA384]::Create()
}
SHA512 {
$hasher = [System.Security.Cryptography.SHA512]::Create()
}
MACTripleDES {
$hasher = [System.Security.Cryptography.MACTripleDES]::Create()
}
MD5 {
$hasher = [System.Security.Cryptography.MD5]::Create()
}
RIPEMD160 {
$hasher = [System.Security.Cryptography.RIPEMD160]::Create()
}
}
Write-Verbose "Hashing using $Algorithm algorithm"
try {
$data = [System.IO.File]::OpenRead($Path)
$hash = $hasher.ComputeHash($data)
$hash = [System.BitConverter]::ToString($hash) -replace "-",""
return @{
Path = $Path;
Algorithm = $Algorithm.ToUpper();
Hash = $hash
}
} catch {
Write-Verbose "Error hashing: $Path"
Write-Verbose "ERROR: $_"
return @{}
} finally {
if ($null -ne $data) {
$data.Close()
}
}
}
#===============================================================================
# Script settings
#===============================================================================
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]'Tls12'
$global:ProgressPreference = 'SilentlyContinue'
#===============================================================================
# Declare Variables
#===============================================================================
$ApiUrl = "https://packages.broadcom.com/artifactory/api/storage/saltproject-generic/onedir"
# Detect architecture ($arch)
if ([IntPtr]::Size -eq 4) { $arch = "x86" } else { $arch = "amd64" }
#===============================================================================
# Setting up quickstart environment
#===============================================================================
Write-Host ""
Write-Host "Setting up quickstart environment for Salt" -ForegroundColor Cyan
Write-Verbose "Getting version information from Artifactory"
$response = Invoke-WebRequest $ApiUrl -UseBasicParsing
# Convert the output to a powershell object
$psobj = $response.ToString() | ConvertFrom-Json
$Version = $psobj.children[-1].uri.Trim("/")
Write-Verbose "Getting sha256 hash and download url from Artifactory"
$saltFileName = "salt-$Version-onedir-windows-$arch.zip"
$response = Invoke-WebRequest "$ApiUrl/$Version/$saltFileName" -UseBasicParsing
$psobj = $response.ToString() | ConvertFrom-Json
$saltFileUrl = $psobj.downloadUri
$saltSha256 = $psobj.checksums.sha256
Write-Verbose "URL: $saltFileUrl"
Write-Host "* INFO: Downloading Salt: " -NoNewline
Invoke-WebRequest -Uri $saltFileUrl -OutFile .\salt.zip
if ( Test-Path -Path .\salt.zip ) {
Write-Host "Success" -ForegroundColor Green
} else {
Write-Host "Failed" -ForegroundColor Red
exit 1
}
$localSha256 = (Get-FileHash -Path .\salt.zip -Algorithm SHA256).Hash
Write-Verbose "Local Hash: $localSha256"
Write-Verbose "Remote Hash: $saltSha256"
Write-Host "* INFO: Comparing Hash: " -NoNewline
if ( $localSha256 -eq $saltSha256 ) {
Write-Host "Success" -ForegroundColor Green
} else {
Write-Host "Failed" -ForegroundColor Red
exit 1
}
Write-Host "* INFO: Extracting Salt: " -NoNewline
Expand-ZipFile -ZipFile .\salt.zip -Destination .
if ( Test-Path -Path .\salt\Scripts\python.exe ) {
Write-Host "Success" -ForegroundColor Green
} else {
Write-Host "Failed" -ForegroundColor Red
exit 1
}
Write-Host "* INFO: Creating Saltfile: " -NoNewline
$PATH = $(Get-Location).Path
$saltfile_contents = @"
salt-call:
local: True
config_dir: $PATH\salt\conf
log_file: $PATH\salt\var\log\salt\minion
cachedir: $PATH\salt\var\cache\salt
file_root: $PATH\salt\srv\salt
"@
Set-Content -Path .\salt\Saltfile -Value $saltfile_contents
if ( Test-Path -Path .\salt\Saltfile ) {
Write-Host "Success" -ForegroundColor Green
} else {
Write-Host "Failed" -ForegroundColor Red
exit 1
}
New-Item -Path "$PATH\salt\var\log\salt" -Type Directory -Force | Out-Null
New-Item -Path "$PATH\salt\conf" -Type Directory -Force | Out-Null
New-Item -Path "$PATH\salt\var\cache\salt" -Type Directory -Force | Out-Null
New-Item -Path "$PATH\salt\srv\salt" -Type Directory -Force | Out-Null
Write-Host "* INFO: Adding Salt to current path: " -NoNewline
$env:Path = "$PATH\salt;$env:PATH"
Write-Verbose $env:Path
if ( $env:PATH -Like "*$PATH\salt*" ) {
Write-Host "Success" -ForegroundColor Green
} else {
Write-Host "Failed" -ForegroundColor Red
exit 1
}
Write-Host "* INFO: $PATH\salt"
Write-Host "* INFO: Setting the SALT_SALTFILE environment variable: "-NoNewline
$env:SALT_SALTFILE="$PATH\salt\Saltfile"
if ( Test-Path -Path $env:SALT_SALTFILE ) {
Write-Host "Success" -ForegroundColor Green
} else {
Write-Host "Failed" -ForegroundColor Red
exit 1
}
Write-Host "* INFO: $PATH\salt\Saltfile"
Write-Host ""
Write-Host "You can now run simple salt-call commands" -ForegroundColor Cyan
Write-Host "* INFO: Create Salt states in $PATH\salt\srv\salt"
Write-Host "* INFO: Try running salt-call test.ping"
Write-Host ""