Last active
September 1, 2017 00:43
-
-
Save gertd/49659eca2ee0f39d5c9a20d0a3c461fc to your computer and use it in GitHub Desktop.
This file contains 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
param ( | |
[Parameter(Mandatory = $false)] | |
[ValidateNotNullOrEmpty()] | |
[ValidateSet('Install', 'Uninstall', 'Start', 'Stop', 'Status', 'Info', 'Open', 'Deploy', 'Upgrade', 'Help')] | |
[String]$rootCmd | |
) | |
function Download-File{[CmdletBinding()]param($url, $targetFile, $proxy) | |
Write-Verbose "Downloading '${url}' to '${targetFile}'" | |
$uri = New-Object "System.Uri" "$url" | |
$request = [System.Net.HttpWebRequest]::Create($uri) | |
if($proxy -ne $null) | |
{ | |
$request.Proxy = $proxy | |
} | |
$request.set_Timeout(15000) #15 second timeout | |
$response = $request.GetResponse() | |
$totalLength = [System.Math]::Floor($response.get_ContentLength()/1024) | |
$responseStream = $response.GetResponseStream() | |
$targetStream = New-Object -TypeName System.IO.FileStream -ArgumentList $targetFile, Create | |
$buffer = new-object byte[] 10KB | |
$count = $responseStream.Read($buffer,0,$buffer.length) | |
$downloadedBytes = $count | |
$sw = [System.Diagnostics.Stopwatch]::StartNew() | |
while ($count -gt 0) | |
{ | |
$targetStream.Write($buffer, 0, $count) | |
$count = $responseStream.Read($buffer,0,$buffer.length) | |
$downloadedBytes = $downloadedBytes + $count | |
if ($sw.Elapsed.TotalMilliseconds -ge 500) { | |
$activity = "Downloading file '$($url.split('/') | Select -Last 1)'" | |
$status = "Downloaded ($([System.Math]::Floor($downloadedBytes/1024))K of $($totalLength)K): " | |
$percentComplete = ((([System.Math]::Floor($downloadedBytes/1024)) / $totalLength) * 100) | |
Write-Progress -activity $activity -status $status -PercentComplete $percentComplete | |
$sw.Reset(); | |
$sw.Start() | |
} | |
} | |
Write-Progress -activity "Finished downloading file '$($url.split('/') | Select -Last 1)'" -Completed -status "Done" | |
$targetStream.Flush() | |
$targetStream.Close() | |
$targetStream.Dispose() | |
$responseStream.Dispose() | |
} | |
function Install-Puppet-Discovery($pd) { | |
Write-Output 'puppet-discovery install' | |
Write-Output "temp location = $pd" | |
if (-Not (Test-Path (Join-Path -Path $pd -ChildPath "vbox.exe"))) { | |
Write-Output 'downloading virtualbox ...' | |
Download-File -url "http://download.virtualbox.org/virtualbox/5.1.26/VirtualBox-5.1.26-117224-Win.exe" -targetFile (Join-Path -Path $pd -ChildPath "vbox.exe") | |
} | |
if (-Not (Test-Path (Join-Path -Path $pd -ChildPath "minikube.exe"))) { | |
Write-Output 'downloading minikube ...' | |
Download-File -url "https://storage.googleapis.com/minikube/releases/latest/minikube-windows-amd64.exe" -targetFile (Join-Path -Path $pd -ChildPath "minikube.exe") | |
} | |
if (-Not (Test-Path (Join-Path -Path $pd -ChildPath "kubectl.exe"))) { | |
Write-Output 'downloading kubectl ...' | |
Download-File -url "https://storage.googleapis.com/kubernetes-release/release/v1.7.0/bin/windows/amd64/kubectl.exe" -targetFile (Join-Path -Path $pd -ChildPath "kubectl.exe") | |
} | |
Write-Output 'register minikube alias ...' | |
New-Alias minikube (Join-Path -Path $pd -ChildPath "minikube.exe") -Force | |
Write-Output 'register kubectl alias ...' | |
New-Alias kubectl (Join-Path -Path $pd -ChildPath "kubectl.exe") -Force | |
if (-Not (Test-Path $env:TEMP\VirtualBox\*amd64.msi -Type Leaf)) { | |
Write-Output 'expand virtualbox ...' | |
$cmd = (Join-Path -Path $pd -ChildPath "vbox.exe") + " --extract --silent"; | |
Start-Process -FilePath (Join-Path -Path $pd -ChildPath "vbox.exe") -ArgumentList "--extract --silent" -Wait -NoNewWindow | |
} | |
if (-Not (Test-Path $env:ProgramFiles\Oracle\\VirtualBox\VBoxManage.exe -Type Leaf)) { | |
Write-Output 'install virtualbox ...' | |
$msi = Get-Item -Path $env:TEMP\VirtualBox\*amd64.msi | |
$arg = "/i " + $msi.FullName + " /passive"; | |
Start-Process -FilePath $env:windir\system32\msiexec.exe -ArgumentList $arg -Wait -NoNewWindow | |
} | |
Write-Output 'configure minikube cluster ...' | |
Start-Process -FilePath (Join-Path -Path $pd -ChildPath "minikube.exe") -ArgumentList 'config set cpus 1' -Wait -NoNewWindow | |
Start-Process -FilePath (Join-Path -Path $pd -ChildPath "minikube.exe") -ArgumentList 'config set memory 4096' -Wait -NoNewWindow | |
Start-Process -FilePath (Join-Path -Path $pd -ChildPath "minikube.exe") -ArgumentList 'config set kubernetes-version v1.7.2' -Wait -NoNewWindow | |
Start-Process -FilePath (Join-Path -Path $pd -ChildPath "minikube.exe") -ArgumentList 'config view' -Wait -NoNewWindow | |
Write-Output 'start minikube cluster ...' | |
Start-Process -FilePath (Join-Path -Path $pd -ChildPath "minikube.exe") -ArgumentList 'start' -Wait -NoNewWindow | |
Write-Output 'wait till cluster is in Running state ...' | |
$i = 1 | |
while($true) | |
{ | |
Start-Process -FilePath (Join-Path -Path $pd -ChildPath "minikube.exe") -ArgumentList 'status --format "{{.MinikubeStatus}}"' -Wait -NoNewWindow -RedirectStandardOutput $pd\status.txt | |
$status = Get-Content $pd\status.txt | |
if ($status -eq "Running") { | |
break | |
} | |
Start-Sleep -Seconds $i | |
$i++ | |
if ($i -ge 10) { | |
Write-Error -Message "Status retrieval timedout" -Category QuotaExceeded -ErrorId 1001 | |
break | |
} | |
} | |
Write-Output 'configure kubectl for minikube ...' | |
Start-Process -FilePath (Join-Path -Path $pd -ChildPath "minikube.exe") -ArgumentList 'update-context' -Wait -NoNewWindow | |
Write-Output 'reconfigure minikube vm ...' | |
Start-Process -FilePath (Join-Path -Path $pd -ChildPath "minikube.exe") -ArgumentList 'ssh "sudo sysctl -w vm.max_map_count=262144"' -Wait -NoNewWindow | |
Write-Output 'deploy services ...' | |
Start-Process -FilePath (Join-Path -Path $pd -ChildPath "kubectl.exe") -ArgumentList 'run hello-minikube --image=gertd/node-env:v0.01 --port=8080' -Wait -NoNewWindow | |
Start-Process -FilePath (Join-Path -Path $pd -ChildPath "kubectl.exe") -ArgumentList 'expose deployment hello-minikube --type=NodePort' -Wait -NoNewWindow | |
} | |
function Uninstall-Puppet-Discovery($pd) { | |
Write-Output 'puppet-discovery uninstall' | |
Start-Process -FilePath (Join-Path -Path $pd -ChildPath "minikube.exe") -ArgumentList 'stop' -Wait -NoNewWindow | |
Start-Process -FilePath (Join-Path -Path $pd -ChildPath "minikube.exe") -ArgumentList 'delete' -Wait -NoNewWindow | |
$msi = Get-Item -Path $env:TEMP\VirtualBox\*amd64.msi | |
if ($msi.Exist) { | |
$arg = "/uninstall " + $f.FullName + " /passive" | |
Start-Process -FilePath $env:windir\system32\msiexec.exe -ArgumentList $arg -Wait | |
} | |
if (Test-Path $env:TEMP\puppet-discovery -Type Container) { | |
Write-Output 'cleanup puppet-discovery installation location ...' | |
Remove-Item -Path $env:TEMP\puppet-discovery -Recurse -Force | |
} | |
if (Test-Path $env:TEMP\VirtualBox -Type Container) { | |
Write-Output 'cleanup VirtualBox installation location ...' | |
Remove-Item -Path $env:TEMP\VirtualBox -Recurse -Force | |
} | |
} | |
function Start-Puppet-Discovery($pd) { | |
Write-Output 'puppet-discovery start' | |
Start-Process -FilePath (Join-Path -Path $pd -ChildPath "minikube.exe") -ArgumentList 'start' -Wait -NoNewWindow | |
} | |
function Stop-Puppet-Discovery($pd) { | |
Write-Output 'puppet-discovery stop' | |
Start-Process -FilePath (Join-Path -Path $pd -ChildPath "minikube.exe") -ArgumentList 'stop' -Wait -NoNewWindow | |
} | |
function Status-Puppet-Discovery($pd) { | |
Write-Output 'puppet-discovery status' | |
Start-Process -FilePath (Join-Path -Path $pd -ChildPath "minikube.exe") -ArgumentList 'status --format "{{.MinikubeStatus}}"' -Wait -NoNewWindow -RedirectStandardOutput $pd\status.txt | |
$status = Get-Content $pd\status.txt | |
Write-Output $status | |
} | |
function Info-Puppet-Discovery { | |
Write-Output 'puppet-discovery info' | |
### minikube service mini-nginx-ingress-controller --url --https | |
} | |
function Open-Puppet-Discovery($pd) { | |
Write-Output 'puppet-discovery open' | |
### TODO need retry logic on failure | |
Start-Process -FilePath (Join-Path -Path $pd -ChildPath "minikube.exe") -ArgumentList 'service hello-minikube --url' -Wait -NoNewWindow -RedirectStandardOutput $pd\url.txt | |
Write-Output 'open browser ...' | |
$url = Get-Content $pd\url.txt | |
Start-Process -FilePath $url | |
} | |
function Deploy-Puppet-Discovery($pd) { | |
Write-Output 'puppet-discovery deploy' | |
Start-Process -FilePath (Join-Path -Path $pd -ChildPath "kubectl.exe") -ArgumentList 'run hello-minikube --image=gertd/node-env:v0.01 --port=8080' -Wait -NoNewWindow | |
Start-Process -FilePath (Join-Path -Path $pd -ChildPath "kubectl.exe") -ArgumentList 'expose deployment hello-minikube --type=NodePort' -Wait -NoNewWindow | |
} | |
function Upgrade-Puppet-Discovery($pd) { | |
Write-Output 'puppet-discovery upgrade' | |
Start-Process -FilePath (Join-Path -Path $pd -ChildPath "kubectl.exe") -ArgumentList 'set image deployments/hello-minikube hello-minikube=gertd/node-env:v0.02' -Wait -NoNewWindow | |
} | |
function Help-Puppet-Discovery { | |
Write-Output 'puppet-discovery help' | |
Write-Output '' | |
Write-Output 'Commands: [Install | Uninstall | Start | Stop | Status | Info | Open | Upgrade | Help ] ' | |
Write-Output '' | |
Write-Output ' Install - Install the puppet-discovery control-plane and services' | |
Write-Output ' Uninstall - Remove the puppet-discovery control-plane and services (removes all collected !!!)' | |
Write-Output ' Start - Start the puppet-discovery services' | |
Write-Output ' Stop - Stop the puppet-discovery services' | |
Write-Output ' Status - Show puppet-discovery service status' | |
Write-Output ' Info - List all puppet-discovery service endpoints' | |
Write-Output ' Open - Open puppet-discovery dashboard inside browser' | |
Write-Output ' Deploy - Deploy puppet-discovery services' | |
Write-Output ' Upgrade - Upgrade puppet-discovery to new latest version' | |
Write-Output ' Help - This help screen' | |
} | |
$pd = New-Item $env:TEMP\puppet-discovery -Type Directory -Force | |
switch($rootCmd.ToLower()) | |
{ | |
{($_ -eq "install")} { Install-Puppet-Discovery $pd } | |
{($_ -eq "uninstall")} { Uninstall-Puppet-Discovery $pd } | |
{($_ -eq "start")} { Start-Puppet-Discovery $pd } | |
{($_ -eq "stop")} { Stop-Puppet-Discovery $pd } | |
{($_ -eq "status")} { Status-Puppet-Discovery $pd } | |
{($_ -eq "info")} { Info-Puppet-Discovery $pd } | |
{($_ -eq "open")} { Open-Puppet-Discovery $pd } | |
{($_ -eq "deploy")} { Deploy-Puppet-Discovery $pd } | |
{($_ -eq "upgrade")} { Upgrade-Puppet-Discovery $pd } | |
{($_ -eq "help")} { Help-Puppet-Discovery } | |
default { Help-Puppet-Discovery } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment