Created
September 16, 2021 15:34
-
-
Save phbits/8bc5a91fb2a2c46defe4ae85e9befc6e to your computer and use it in GitHub Desktop.
Gets latest Minikube stable or beta release.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function Get-Minikube | |
{ | |
[CmdletBinding()] | |
param | |
( | |
<# | |
.SYNOPSIS | |
Gets latest Minikube stable or beta release. | |
.DESCRIPTION | |
Downloads Minikube to the specified Path and updates the | |
'Path' environment variable to include the specified Path. | |
Requests administrator access if running as a regular user and 'Path' | |
environment variable needs to be updated. | |
.EXAMPLE | |
Get-Minikube | |
Downloads the latest stable release to C:\minikube\ and adds this directory | |
location to the 'Path' environment variable. | |
.EXAMPLE | |
Get-Minikube -Beta | |
Downloads the latest Beta release to C:\minikube\ and adds this directory | |
location to the 'Path' environment variable. | |
.EXAMPLE | |
Get-Minikube -Path 'D:\somedir\minikube' -Verbose | |
Downloads the latest stable release to D:\somedir\minikube\ and adds this directory | |
location to the 'Path' environment variable. Verbose messages will be displayed. | |
#> | |
[Parameter()] | |
[Alias('Folder')] | |
[System.String] | |
# Directory to save minikube.exe and add to path | |
$Path = 'C:\minikube' | |
, | |
[Parameter()] | |
[System.Management.Automation.SwitchParameter] | |
# Get latest Beta instead of latest stable release | |
$Beta | |
) | |
$minikubeFileName = 'minikube.exe' | |
$minikubeDest = Join-Path -Path $Path -ChildPath $minikubeFileName | |
$pathVariable = '{0}\' -f $Path.TrimEnd('\') # ensure a single trailing backslash | |
$latestReleaseUrl = 'https://github.com/kubernetes/minikube/releases/latest/download/minikube-windows-amd64.exe' | |
$allReleasesUrl = 'https://api.github.com/repos/kubernetes/minikube/releases' | |
try | |
{ | |
if ($(Test-Path -LiteralPath $Path -PathType Container) -eq $false) | |
{ | |
Write-Verbose -Message "Creating '$($Path)'." | |
$null = New-Item -Path $Path -Force -ItemType Directory | |
} else { | |
if ($(Test-Path -LiteralPath $minikubeDest) -eq $true) | |
{ | |
Write-Verbose -Message "Existing minikube.exe found. Renaming to 'minikube.exe.bak'." | |
$null = Move-Item -LiteralPath $minikubeDest -Destination "$($minikubeDest).bak" -Force | |
} | |
} | |
if ($Beta -eq $true) | |
{ | |
Write-Verbose -Message 'Downloading all release information.' | |
$response = Invoke-WebRequest -Uri $allReleasesUrl -UseBasicParsing | |
Write-Verbose -Message 'Parsing response for latest beta release.' | |
$json = $response.Content | ConvertFrom-Json | |
$item = ($json | ?{ $_.prerelease -eq $true })[0].assets | ?{ $_.name -eq 'minikube-windows-amd64.exe' } | |
Write-Verbose -Message "Downloading latest beta release '$($item.browser_download_url)'." | |
Invoke-WebRequest -Uri $item.browser_download_url -OutFile $minikubeDest -UseBasicParsing | |
} | |
else | |
{ | |
Write-Verbose -Message "Downloading latest stable release '$($latestReleaseUrl)'." | |
Invoke-WebRequest -OutFile $minikubeDest -Uri $latestReleaseUrl -UseBasicParsing | |
} | |
Write-Verbose -Message "Checking 'Path' Environment variable for '$($pathVariable)'." | |
$currentPath = [Environment]::GetEnvironmentVariable('Path', [EnvironmentVariableTarget]::Machine) | |
[System.Collections.Generic.HashSet[string]] $pathHashSet = $currentPath.ToLower().Split(';') | |
if ($pathHashSet.Contains($pathVariable.ToLower()) -eq $false) | |
{ | |
Write-Verbose -Message "'Path' Environment variable does not contain '$($pathVariable)'." | |
$newPathVariable = '{0};{1}' -f $currentPath.TrimEnd(';'), $pathVariable | |
$isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]'Administrator') | |
if ($isAdmin -eq $true) | |
{ | |
Write-Verbose -Message 'Detected administrator prompt.' | |
[Environment]::SetEnvironmentVariable('Path', $newPathVariable, [EnvironmentVariableTarget]::Machine) | |
Write-Verbose -Message "'Path' Environment variable updated. Added '$($pathVariable)'." | |
} | |
else | |
{ | |
Write-Verbose -Message 'Not using an administrator prompt.' | |
Write-Verbose -Message "Prompting for credentials to set 'Path' Environment variable." | |
$pathUpdateCmd = "[Environment]::SetEnvironmentVariable('Path', '{0}', [EnvironmentVariableTarget]::Machine)" -f $newPathVariable | |
$startProcessSplat = @{ | |
FilePath = 'powershell.exe' | |
ArgumentList = @( $pathUpdateCmd ) | |
Verb = 'runAs' | |
} | |
Start-Process @startProcessSplat | |
Write-Verbose -Message "'Path' Environment variable updated. Added '$($pathVariable)'." | |
} | |
} | |
else | |
{ | |
Write-Verbose -Message "'Path' Environment variable already contains '$($pathVariable)'." | |
} | |
} | |
catch | |
{ | |
throw $_ | |
} | |
} # End Function Get-Minikube |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment