Fix quickstart for Windows with new repo

This commit is contained in:
twangboy 2024-12-04 08:40:04 -07:00
parent bbaa32a888
commit c4fdafaef3
No known key found for this signature in database
GPG key ID: 93FF3BDEB278C9EB

View file

@ -1,39 +1,31 @@
function Convert-PSObjectToHashtable { <#
param ( .SYNOPSIS
[Parameter(ValueFromPipeline)] A simple Powershell script to quickly start using Salt.
$InputObject
)
if ($null -eq $InputObject) { return $null }
$is_enum = $InputObject -is [System.Collections.IEnumerable] .DESCRIPTION
$not_string = $InputObject -isnot [string] This script will download the latest onedir version of Salt and extract it
if ($is_enum -and $not_string) { into the same directory where the script is run. The script sets up an
$collection = @( environment that will allow you to run salt-call commands. To remove, just
foreach ($object in $InputObject) { delete the `salt` directory. The environment variables will only be set for
Convert-PSObjectToHashtable $object the current powershell session.
}
)
Write-Host -NoEnumerate $collection .EXAMPLE
} elseif ($InputObject -is [PSObject]) { ./salt-quick-start.ps1
$hash = @{}
foreach ($property in $InputObject.PSObject.Properties) { .LINK
$hash[$property.Name] = Convert-PSObjectToHashtable $property.Value 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
#>
$hash # This is so the -Verbose parameter will work
} else { [CmdletBinding()] param()
$InputObject
}
}
function Expand-ZipFile { function Expand-ZipFile {
# Extract a zip file # Extract a zip file
# #
# Used by:
# - Install-SaltMinion
#
# Args: # Args:
# ZipFile (string): The file to extract # ZipFile (string): The file to extract
# Destination (string): The location to extract to # Destination (string): The location to extract to
@ -81,60 +73,151 @@ function Expand-ZipFile {
Write-Debug "Finished unzipping '$ZipFile' to '$Destination'" 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,
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]'Tls12' [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"
)
$global:ProgressPreference = 'SilentlyContinue' if ( !(Test-Path $Path) ) {
Write-Verbose "Invalid path for hashing: $Path"
$RepoUrl = "https://repo.saltproject.io/salt/py3/onedir" return @{}
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 { if ( (Get-Item -Path $Path) -isnot [System.IO.FileInfo]) {
Write-Host "repo.json not found at: $RepoUrl" Write-Verbose "Not a file for hashing: $Path"
$hash = @{} return @{}
} }
$searchVersion = "latest"
if ( $hash.Contains($searchVersion)) { $Path = Resolve-Path -Path $Path
foreach ($item in $hash.($searchVersion).Keys) {
if ( $item.EndsWith(".zip") ) { Switch ($Algorithm) {
if ( $item.Contains($arch) ) { SHA1 {
$saltFileName = $hash.($searchVersion).($item).name $hasher = [System.Security.Cryptography.SHA1CryptoServiceProvider]::Create()
$saltVersion = $hash.($searchVersion).($item).version }
$saltSha512 = $hash.($searchVersion).($item).SHA512 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()
} }
} }
} }
if ( $saltFileName -and $saltVersion -and $saltSha512 ) {
if ( $RepoUrl.Contains("minor") ) { #===============================================================================
$saltFileUrl = @($RepoUrl, $saltVersion, $saltFileName) -join "/" # Script settings
} else { #===============================================================================
$saltFileUrl = @($RepoUrl, "minor", $saltVersion, $saltFileName) -join "/" [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: Downloading Salt" Write-Host "* INFO: Extracting Salt: " -NoNewline
Invoke-WebRequest -Uri $saltFileUrl -OutFile .\salt.zip
Write-Host "* INFO: Extracting Salt"
Expand-ZipFile -ZipFile .\salt.zip -Destination . 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 $PATH = $(Get-Location).Path
$saltfile_contents = @" $saltfile_contents = @"
salt-call: salt-call:
local: True local: True
@ -143,20 +226,42 @@ salt-call:
cachedir: $PATH\salt\var\cache\salt cachedir: $PATH\salt\var\cache\salt
file_root: $PATH\salt\srv\salt file_root: $PATH\salt\srv\salt
"@ "@
Set-Content -Path .\salt\Saltfile -Value $saltfile_contents 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\var\log\salt" -Type Directory -Force | Out-Null
New-Item -Path "$PATH\salt\conf" -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\var\cache\salt" -Type Directory -Force | Out-Null
New-Item -Path "$PATH\salt\srv\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: Adding Salt to current path: " -NoNewline
Write-Host "* INFO: $PATH\salt"
$env:Path = "$PATH\salt;$env:PATH" $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" Write-Host "* INFO: Setting the SALT_SALTFILE environment variable: "-NoNewline
Write-Host "* INFO: $PATH\salt\Saltfile"
$env:SALT_SALTFILE="$PATH\salt\Saltfile" $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: Create Salt states in $PATH\salt\srv\salt"
Write-Host "* INFO: Try running salt-call test.ping"
Write-Host ""