Last active
October 29, 2016 16:01
-
-
Save jahands/3db423d5ec5ff2a63d019a37fb8ac902 to your computer and use it in GitHub Desktop.
PowerShell module to create/destroy a server
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
# Tutorial: https://www.digitalocean.com/community/tutorials/how-to-use-doctl-the-official-digitalocean-command-line-client | |
Function Start-PSServerConnection { | |
doctl compute ssh powershell --ssh-user me | |
} | |
Function Start-PSServer { | |
Param( | |
[switch]$AutoConnect | |
) | |
# Make sure droplet doesn't exist | |
$droplet = (doctl compute droplet list -o json | Out-String | ConvertFrom-Json) | | |
Where-Object {$_.name -eq 'powershell'} | |
if($droplet -ne $null){ | |
Write-Error 'Droplet already exists!' | |
return | |
} | |
# Get latest powershell image ID | |
$image = (((doctl compute image list -o json | Out-String | ConvertFrom-Json) | | |
Where-Object {$_.name -eq 'powershell' -and $_.Public -eq $null}) | Sort-Object created_at -Descending)[0].id | |
# Get SSH key fingerprint | |
$sshKey = ((doctl compute ssh-key list -o json | Out-String | ConvertFrom-Json) | | |
Where-Object {$_.name -eq 'me'}).fingerprint | |
# Create the droplet | |
doctl compute droplet create powershell --size 2gb --image $image --region nyc3 --ssh-keys $sshKey | |
# Get droplet ID | |
$dropletID = ((doctl compute droplet list -o json | Out-String | ConvertFrom-Json) | | |
Where-Object {$_.name -eq 'powershell'}).id | |
Write-Output 'Creating droplet...' | |
# Get progress of snapshot until done | |
$status = 'in-progress' | |
while($status -eq 'in-progress'){ | |
Start-Sleep -Seconds 5 | |
$status = ((doctl compute droplet actions $dropletID -o json | Out-String | ConvertFrom-Json) | | |
Where-Object{$_.type -eq 'create'}).status | |
} | |
Write-Output 'Droplet created!' | |
if($AutoConnect){ | |
Start-Sleep -Seconds 40 | |
Start-PSServerConnection | |
} | |
} | |
Function Stop-PSServer { | |
Param( | |
[switch]$TakeSnapshot | |
) | |
# Get droplet ID | |
$dropletID = ((doctl compute droplet list -o json | Out-String | ConvertFrom-Json) | | |
Where-Object {$_.name -eq 'powershell'}).id | |
if($dropletID -eq $null){ | |
Write-Error 'Droplet does not exist!' | |
return | |
} | |
# Create snapshot | |
if($TakeSnapshot){ | |
doctl compute droplet-action snapshot $dropletID --snapshot-name powershell | |
Write-Output 'Creating snapshot...' | |
# Get progress of snapshot until done | |
$status = 'in-progress' | |
while($status -eq 'in-progress'){ | |
Start-Sleep -Seconds 5 | |
$status = ((doctl compute droplet actions $dropletID -o json | Out-String | ConvertFrom-Json) | | |
Where-Object{$_.type -eq 'snapshot'}).status | |
} | |
Write-Output 'Snapshot created!' | |
} | |
# Delete old snapshots to save $$ | |
Write-Output 'Deleting old snapshots...' | |
$images = (((doctl compute image list -o json | Out-String | ConvertFrom-Json) | | |
Where-Object {$_.name -eq 'powershell' -and $_.Public -eq $null}) | | |
Sort-Object created_at -Descending | Select-Object -ExpandProperty id -Skip 2) | |
foreach($image in $images){ | |
Write-Output "Deleting image: $image" | |
doctl compute image delete $image | |
} | |
# Destroy droplet | |
Write-Output 'Destroying droplet...' | |
doctl compute droplet delete $dropletID --force | |
$status = 'in-progress' | |
while($status -eq 'in-progress'){ | |
Start-Sleep -Seconds 5 | |
$status = ((doctl compute droplet actions $dropletID -o json | Out-String | ConvertFrom-Json) | | |
Where-Object{$_.type -eq 'destroy'}).status | |
} | |
Write-Output 'Droplet destroyed!' | |
} | |
Export-ModuleMember -Function *PSServer* |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment