Skip to content

Instantly share code, notes, and snippets.

@murar8
Created May 14, 2021 09:24
Show Gist options
  • Save murar8/dbac36ac5613eb6e2543dee4386b4823 to your computer and use it in GitHub Desktop.
Save murar8/dbac36ac5613eb6e2543dee4386b4823 to your computer and use it in GitHub Desktop.
$ErrorActionPreference = "Stop"
######## Constants ########
$VaultModule = "SecretManagement.JustinGrote.CredMan"
$VaultModuleVersion = "1.0.0"
$KeyApiToken = "com.murar8.api-token"
$ApiUrl = "https://api.paperspace.io"
######## Functions ########
function Install-Module-If-Missing {
param (
[string] $Name,
[string] $Version
)
try {
Get-InstalledModule -Name $Name -RequiredVersion $Version | Out-Null
}
catch {
switch ($PSItem.FullyQualifiedErrorId) {
"NoMatchFound,Microsoft.PowerShell.PackageManagement.Cmdlets.GetPackage" {
Write-Host "Installing $Name module..."
Install-Module -Force -Repository PSGallery -Scope CurrentUser -Name $Name -RequiredVersion $Version
}
Default {
throw $PSItem.Exception
}
}
}
}
function Initialize-SecretVault ( [string] $Name) {
try {
Get-SecretVault -Name $Name | Out-Null
Test-SecretVault -Name $Name | Out-Null
}
catch [System.Management.Automation.ItemNotFoundException] {
Write-Host "Registering vault..."
Write-Host
Register-SecretVault -ModuleName $Name
}
}
function Get-ApiToken {
$ApiToken = Get-Secret -Vault $VaultModule -Name $KeyApiToken
Write-Host "Existing API token found."
Write-Host
$ApiToken
}
function Read-APIToken {
$ApiToken = Read-Host -Prompt 'Enter your paperspace API Token' -AsSecureString
Set-Secret -Vault $VaultModule -Name $KeyApiToken -SecureStringSecret $ApiToken
$ApiToken
}
function New-Headers ([System.Security.SecureString] $ApiToken) {
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($ApiToken)
$ClearToken = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
@{ 'x-api-key' = $ClearToken }
}
######## Script ########
Install-Module-If-Missing -Name Microsoft.PowerShell.SecretManagement -Version "1.0.0"
Install-Module-If-Missing -Name $VaultModule -Version $VaultModuleVersion
Initialize-SecretVault $VaultModule
$Headers = @{}
try {
$Headers = New-Headers -ApiToken (Get-ApiToken)
}
catch [System.Management.Automation.ItemNotFoundException] {
Write-Host "API token not found."
Write-Host
}
$Machines = @()
for (; ; ) {
try {
$Machines = Invoke-RestMethod -Uri "$ApiUrl/machines/getMachines" -Headers $Headers
break
}
catch [System.Net.WebException] {
$StatusCode = [int] $PSItem.Exception.Response.StatusCode
if (($StatusCode -ne 400) -and ($StatusCode -ne 401)) {
throw $PSItem
}
Write-Host -ForegroundColor DarkYellow "The provided API token is invalid."
$Headers = New-Headers -ApiToken (Read-ApiToken)
}
}
if ($Machines.count -lt 1) {
Write-Host -ForegroundColor DarkYellow "You need to create a machine first. visit https://console.paperspace.com/"
Read-Host -Prompt 'Press enter to quit';
exit 1
}
if ($Machines.count -gt 1) {
for (; ; ) {
Write-Host "Available machines:"
for ($i = $Machines.Count - 1; $i -ge 0 ; $i--) {
Write-Host "$($i) -> ID: '$($Machines[$i].Id)' Name: '$($Machines[$i].Name)'"
}
$Index = Read-Host -Prompt "Select a machine"
if (($Index -isnot [int]) -or ($Index -lt 0) -or ($Index -ge $Machines.Count)) {
continue
}
$Machine = $Machines[$Index]
break
}
}
else {
$Machine = $Machines[0]
}
try {
Write-Host "Starting machine '$($Machine.Name)'..."
Invoke-RestMethod -Method 'POST' -Uri "$ApiUrl/machines/$($Machine.Id)/start" -Headers $Headers | Out-Null
Write-Host "Machine started successfully!"
Start-Sleep -Seconds 5
Read-Host -Prompt 'Press enter to shut down the machine';
}
catch {
Write-Host -ForegroundColor DarkYellow "There was an error while trying to start the machine: $($PSItem.Exception)"
}
for (; ; ) {
try {
Write-Host "Stopping machine '$($Machine.Name)'"
Invoke-RestMethod -Method 'POST' -Uri "$ApiUrl/machines/$($Machine.Id)/stop" -Headers $Headers | Out-Null
Write-Host -NoNewline "Waiting for machine to stop..."
for (; ; ) {
Start-Sleep -Seconds 1
$Machine = Invoke-RestMethod -Uri "$ApiUrl/machines/getMachinePublic?machineId=$($Machine.Id)" -Headers $Headers
Write-Host -NoNewline "."
if ($Machine.state -eq "off") {
Write-Host "Machine stopped successfully!"
Read-Host -Prompt 'Press enter to quit';
exit 0
}
}
}
catch {
Write-Host -ForegroundColor DarkYellow "There was an error while trying to shut down the machine: $($PSItem.Exception)"
Read-Host -Prompt 'Press enter to retry';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment