-
Notifications
You must be signed in to change notification settings - Fork 551
/
salt-quick-start.ps1
162 lines (140 loc) · 4.87 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
function Convert-PSObjectToHashtable {
param (
[Parameter(ValueFromPipeline)]
$InputObject
)
if ($null -eq $InputObject) { return $null }
$is_enum = $InputObject -is [System.Collections.IEnumerable]
$not_string = $InputObject -isnot [string]
if ($is_enum -and $not_string) {
$collection = @(
foreach ($object in $InputObject) {
Convert-PSObjectToHashtable $object
}
)
Write-Host -NoEnumerate $collection
} elseif ($InputObject -is [PSObject]) {
$hash = @{}
foreach ($property in $InputObject.PSObject.Properties) {
$hash[$property.Name] = Convert-PSObjectToHashtable $property.Value
}
$hash
} else {
$InputObject
}
}
function Expand-ZipFile {
# Extract a zip file
#
# Used by:
# - Install-SaltMinion
#
# 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'"
}
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]'Tls12'
$global:ProgressPreference = 'SilentlyContinue'
$RepoUrl = "https://repo.saltproject.io/salt/py3/onedir"
if ([IntPtr]::Size -eq 4) {
$arch = "x86"
} else {
$arch = "amd64"
}
$enc = [System.Text.Encoding]::UTF8
try {
$response = Invoke-WebRequest -Uri "$RepoUrl/repo.json" -UseBasicParsing
if ($response.Content.GetType().Name -eq "Byte[]") {
$psobj = $enc.GetString($response.Content) | ConvertFrom-Json
} else {
$psobj = $response.Content | ConvertFrom-Json
}
$hash = Convert-PSObjectToHashtable $psobj
} catch {
Write-Host "repo.json not found at: $RepoUrl"
$hash = @{}
}
$searchVersion = "latest"
if ( $hash.Contains($searchVersion)) {
foreach ($item in $hash.($searchVersion).Keys) {
if ( $item.EndsWith(".zip") ) {
if ( $item.Contains($arch) ) {
$saltFileName = $hash.($searchVersion).($item).name
$saltVersion = $hash.($searchVersion).($item).version
$saltSha512 = $hash.($searchVersion).($item).SHA512
}
}
}
}
if ( $saltFileName -and $saltVersion -and $saltSha512 ) {
if ( $RepoUrl.Contains("minor") ) {
$saltFileUrl = @($RepoUrl, $saltVersion, $saltFileName) -join "/"
} else {
$saltFileUrl = @($RepoUrl, "minor", $saltVersion, $saltFileName) -join "/"
}
}
Write-Host "* INFO: Downloading Salt"
Invoke-WebRequest -Uri $saltFileUrl -OutFile .\salt.zip
Write-Host "* INFO: Extracting Salt"
Expand-ZipFile -ZipFile .\salt.zip -Destination .
$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
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"
Write-Host "* INFO: $PATH\salt"
$env:Path = "$PATH\salt;$env:PATH"
Write-Host "* INFO: Setting the SALT_SALTFILE environment variable"
Write-Host "* INFO: $PATH\salt\Saltfile"
$env:SALT_SALTFILE="$PATH\salt\Saltfile"
Write-Host "* INFO: Create Salt states in $PATH\salt\srv\salt"