Enhance installation scripts for cross-platform support and improve error handling

Signed-off-by: Matthias Bertschy <matthias.bertschy@gmail.com>
This commit is contained in:
Matthias Bertschy
2025-12-15 08:52:05 +01:00
parent 06241fce03
commit 5fed9cc507
3 changed files with 131 additions and 36 deletions

View File

@@ -29,6 +29,8 @@ builds:
- CGO_ENABLED=0 - CGO_ENABLED=0
goos: goos:
- linux - linux
- darwin
- windows
goarch: goarch:
- amd64 - amd64
- arm64 - arm64
@@ -136,7 +138,7 @@ krews:
short_description: Scan resources and cluster configs against security frameworks. short_description: Scan resources and cluster configs against security frameworks.
release: release:
draft: true draft: false
footer: >- footer: >-
--- ---

View File

@@ -1,39 +1,96 @@
Write-Host "Installing Kubescape..." -ForegroundColor Cyan Write-Host "Installing Kubescape..." -ForegroundColor Cyan
$BASE_DIR=$env:USERPROFILE + "\.kubescape" $BASE_DIR = "$env:USERPROFILE\.kubescape"
$packageName = "/kubescape.exe" $KUBESCAPE_EXEC = "kubescape.exe"
# Get latest release url # Determine architecture
$config = Invoke-WebRequest -UseBasicParsing "https://api.github.com/repos/kubescape/kubescape/releases/latest" | ConvertFrom-Json $arch = if ([Environment]::Is64BitOperatingSystem) {
$url = $config.html_url.Replace("/tag/","/download/") if ($env:PROCESSOR_ARCHITECTURE -eq "ARM64") { "arm64" } else { "amd64" }
$fullUrl = $url + $packageName } else {
Write-Host "Error: 32-bit systems are not supported" -ForegroundColor Red
exit 1
}
# Create a new directory if needed # Get latest release version from GitHub API
New-Item -Path $BASE_DIR -ItemType "directory" -ErrorAction SilentlyContinue function Get-LatestVersion {
try {
$release = Invoke-RestMethod -Uri "https://api.github.com/repos/kubescape/kubescape/releases/latest" -UseBasicParsing
return $release.tag_name
} catch {
Write-Host "Error: Failed to fetch latest release version" -ForegroundColor Red
exit 1
}
}
# Parse command line arguments for version
$version = $null
for ($i = 0; $i -lt $args.Count; $i++) {
if ($args[$i] -eq "-v" -and $i + 1 -lt $args.Count) {
$version = $args[$i + 1]
}
}
# Get version (use provided or fetch latest)
if (-not $version) {
$version = Get-LatestVersion
Write-Host "Latest version: $version" -ForegroundColor Cyan
}
# Remove 'v' prefix if present for the filename
$versionNum = $version -replace '^v', ''
# Create installation directory if needed
New-Item -Path $BASE_DIR -ItemType "directory" -ErrorAction SilentlyContinue | Out-Null
# Build download URL with new naming pattern: kubescape_{version}_windows_{arch}.exe
$downloadUrl = "https://github.com/kubescape/kubescape/releases/download/$version/kubescape_${versionNum}_windows_${arch}.exe"
Write-Host "Downloading from: $downloadUrl" -ForegroundColor Cyan
$outputPath = Join-Path $BASE_DIR $KUBESCAPE_EXEC
# Download the binary # Download the binary
$useBitTransfer = $null -ne (Get-Module -Name BitsTransfer -ListAvailable) -and ($PSVersionTable.PSVersion.Major -le 5) try {
if ($useBitTransfer) $useBitTransfer = $null -ne (Get-Module -Name BitsTransfer -ListAvailable) -and ($PSVersionTable.PSVersion.Major -le 5)
{ if ($useBitTransfer) {
Write-Information -MessageData 'Using a fallback BitTransfer method since you are running Windows PowerShell' Write-Host "Using BitsTransfer for download..." -ForegroundColor Gray
Start-BitsTransfer -Source $fullUrl -Destination $BASE_DIR\kubescape.exe Start-BitsTransfer -Source $downloadUrl -Destination $outputPath
} else {
$ProgressPreference = 'SilentlyContinue' # Speeds up Invoke-WebRequest
Invoke-WebRequest -Uri $downloadUrl -OutFile $outputPath -UseBasicParsing
}
} catch {
Write-Host "Error: Failed to download kubescape" -ForegroundColor Red
Write-Host $_.Exception.Message -ForegroundColor Red
exit 1
}
} # Verify download was successful
else if (-not (Test-Path $outputPath) -or (Get-Item $outputPath).Length -eq 0) {
{ Write-Host "Error: Download failed or file is empty" -ForegroundColor Red
Invoke-WebRequest -Uri $fullUrl -OutFile $BASE_DIR\kubescape.exe Remove-Item $outputPath -ErrorAction SilentlyContinue
} exit 1
}
# Update user PATH if needed # Update user PATH if needed
$currentPath = [Environment]::GetEnvironmentVariable("Path", "User") $currentPath = [Environment]::GetEnvironmentVariable("Path", "User")
if (-not $currentPath.Contains($BASE_DIR)) { if (-not $currentPath.Contains($BASE_DIR)) {
$confirmation = Read-Host "Add kubescape to user path? (y/n)" $confirmation = Read-Host "Add kubescape to user PATH? (y/n)"
if ($confirmation -eq 'y') { if ($confirmation -eq 'y') {
$env:Path=[Environment]::GetEnvironmentVariable("Path", "User") + ";$BASE_DIR;" $newPath = $currentPath + ";$BASE_DIR"
[Environment]::SetEnvironmentVariable("Path", "${env:Path}", "User") [Environment]::SetEnvironmentVariable("Path", $newPath, "User")
$env:Path = $env:Path + ";$BASE_DIR"
Write-Host "Added $BASE_DIR to PATH" -ForegroundColor Green
} }
} }
Write-Host "Finished Installation.`n" -ForegroundColor Green Write-Host "`nFinished Installation." -ForegroundColor Green
kubescape version
Write-Host "`nUsage: $ kubescape scan" -ForegroundColor Magenta # Try to run version command
try {
& $outputPath version
} catch {
Write-Host "Installed to: $outputPath" -ForegroundColor Green
}
Write-Host "`nUsage: kubescape scan" -ForegroundColor Magenta

View File

@@ -8,22 +8,41 @@ KUBESCAPE_EXEC=kubescape
determine_os_and_arch() { determine_os_and_arch() {
osName=$(uname -s) osName=$(uname -s)
case $osName in case $osName in
*MINGW*) osName=windows ;; Linux*) osName=linux ;;
Darwin*) osName=macos ;; Darwin*) osName=darwin ;;
*) osName=ubuntu ;; *MINGW*|*CYGWIN*|*MSYS*)
echo -e "\033[31mError: Windows is not supported by this script. Please use the PowerShell installer or download manually from:"
echo -e "\033[1;35;40mhttps://github.com/kubescape/kubescape/releases"
exit 1
;;
*)
echo -e "\033[31mError: Unsupported operating system: $osName"
exit 1
;;
esac esac
arch=$(uname -m) arch=$(uname -m)
case $arch in case $arch in
*aarch64*|*arm64*) arch="-arm64" ;; x86_64|amd64) arch="amd64" ;;
*x86_64*) arch="" ;; aarch64|arm64) arch="arm64" ;;
*) *)
echo -e "\033[33mArchitecture $arch may be unsupported, will try to install the amd64 one anyway." echo -e "\033[31mError: Unsupported architecture: $arch"
arch="" exit 1
;; ;;
esac esac
} }
# Function to get the latest release version from GitHub API
get_latest_version() {
local latest_release
latest_release=$(curl -s "https://api.github.com/repos/kubescape/kubescape/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
if [ -z "$latest_release" ]; then
echo -e "\033[31mError: Failed to fetch latest release version"
exit 1
fi
echo "$latest_release"
}
# Function to remove old installations # Function to remove old installations
remove_old_install() { remove_old_install() {
local exec_path=$1 local exec_path=$1
@@ -33,26 +52,43 @@ remove_old_install() {
} }
# Parse command-line arguments # Parse command-line arguments
VERSION=""
while getopts v: option; do while getopts v: option; do
case ${option} in case ${option} in
v) RELEASE="download/${OPTARG}";; v) VERSION="${OPTARG}";;
*) ;; *) ;;
esac esac
done done
[ -z "${RELEASE}" ] && RELEASE="latest/download"
echo -e "\033[0;36mInstalling Kubescape..." echo -e "\033[0;36mInstalling Kubescape..."
determine_os_and_arch determine_os_and_arch
# Get version (use provided or fetch latest)
if [ -z "${VERSION}" ]; then
VERSION=$(get_latest_version)
echo -e "\033[0;36mLatest version: $VERSION"
fi
# Remove 'v' prefix if present for the filename
VERSION_NUM="${VERSION#v}"
mkdir -p $BASE_DIR mkdir -p $BASE_DIR
OUTPUT=$BASE_DIR/$KUBESCAPE_EXEC OUTPUT=$BASE_DIR/$KUBESCAPE_EXEC
DOWNLOAD_URL="https://github.com/kubescape/kubescape/releases/${RELEASE}/kubescape${arch}-${osName}-latest" # New URL pattern: kubescape_{version}_{os}_{arch}
DOWNLOAD_URL="https://github.com/kubescape/kubescape/releases/download/${VERSION}/kubescape_${VERSION_NUM}_${osName}_${arch}"
echo -e "\033[0;36mDownloading from: $DOWNLOAD_URL"
curl --progress-bar -L $DOWNLOAD_URL -o $OUTPUT curl --progress-bar -L $DOWNLOAD_URL -o $OUTPUT
# Verify download was successful
if [ ! -s "$OUTPUT" ]; then
echo -e "\033[31mError: Download failed or file is empty"
rm -f "$OUTPUT"
exit 1
fi
# Determine install directory # Determine install directory
install_dir=/usr/local/bin install_dir=/usr/local/bin
[ "$(id -u)" -ne 0 ] && install_dir=$BASE_DIR/bin && export PATH=$PATH:$BASE_DIR/bin [ "$(id -u)" -ne 0 ] && install_dir=$BASE_DIR/bin && export PATH=$PATH:$BASE_DIR/bin