Skip to content

Instantly share code, notes, and snippets.

@srpomeroy
Created July 5, 2018 17:37
Show Gist options
  • Save srpomeroy/69efc971532367bd984df3861c43b05b to your computer and use it in GitHub Desktop.
Save srpomeroy/69efc971532367bd984df3861c43b05b to your computer and use it in GitHub Desktop.
Utilizes RSC and RIGHTST to download RightScripts and ServerTemplates from a RightScale Account
# Requires
# rsc - https://github.com/rightscale/rsc
# rightst - https://github.com/rightscale/right_st
# assumes they are stored in c:\rightscale\rsc\ and c:\rightscale\rightst\
# Variables
$basepath = "$env:SystemDrive\rightscale"
$outputpath = "path-to-backup-location"
$rightstaccount = "Account Name"
$account = "12345" #Account Number
$oauth = "abc...123" #Refresh Token from CM
$server = "us-3.rightscale.com" #CM API Endpoint
$downloadrightscripts = $true
$downloadservertemplates = $true
$scriptfilter = $null
#$scriptfilter = "RightScale -*"
$stfilter = $null
#$stfilter = "RightScale -*"
#Set Environment Variables
$env:RIGHT_ST_LOGIN_ACCOUNT_ID = $account
$env:RIGHT_ST_LOGIN_ACCOUNT_HOST = $server
$env:RIGHT_ST_LOGIN_ACCOUNT_REFRESH_TOKEN = $oauth
#Paths
$rsc = "$basepath\rsc\rsc.exe" #https://github.com/rightscale/rsc
$rsc_url = "https://binaries.rightscale.com/rsbin/rsc/v6/rsc-windows-amd64.zip"
$rightst = "$basepath\right_st\right_st.exe" #https://github.com/rightscale/right_st
$rightst_url = "https://binaries.rightscale.com/rsbin/right_st/v1/right_st-windows-amd64.zip"
#Confirm RSC exists
if(Test-Path -Path $rsc) {
Write-Output "RSC already exists. Skipping download"
}
else {
Write-Output "RSC does not exist. Downloading..."
$output = Join-Path $env:TEMP $rsc_url.Split("/")[-1]
(New-Object System.Net.WebClient).DownloadFile($rsc_url, $output)
Expand-Archive -Path $output -DestinationPath $basepath -Force
}
#Confirm RIGHT_ST exists
if(Test-Path -Path $rightst) {
Write-Output "RIGHT_ST already exists. Skipping download"
}
else {
Write-Output "RIGHT_ST does not exist. Downloading..."
$output = Join-Path $env:TEMP $rightst_url.Split("/")[-1]
(New-Object System.Net.WebClient).DownloadFile($rightst_url, $output)
Expand-Archive -Path $output -DestinationPath $basepath -Force
}
#Confirm Right_ST account is configured
$rightstaccounts = &$rightst "config" "show"
if ($rightstaccounts -like "*${rightstaccount}:*") {
Write-Output "$rightstaccount is configured with Right_ST"
}
else {
Write-Output "$rightstaccount is not configure with Right_ST."
Write-Output "Run '$rightst config account $rightstaccount' to configure the account first"
EXIT 1
}
#Date suffix
$date = Get-Date -Format dd-MMM-yy_HHmmss
#Main
$outputpath = $outputpath + '\' + $date
if(!(Test-Path -Path $outputpath)) {
New-Item -Path $outputpath -ItemType Directory | Out-Null
}
#RightScripts
if($downloadrightscripts) {
$scripts = (&$rsc "cm15" "index" "/api/right_scripts" "--account=$account" "--host=$server" "--refreshToken=$oauth" | ConvertFrom-Json)
if($scriptfilter) {
$scripts = $scripts | Where-Object Name -like $scriptfilter
}
foreach($script in $scripts) {
&$rightst "-a=$rightstaccount" "rightscript" "download" $($script.id) $outputpath
}
}
#ServerTemplates
IF($downloadservertemplates) {
$servertemplates = (&$rsc "cm15" "index" "/api/server_templates" "--account=$account" "--host=$server" "--refreshToken=$oauth" | ConvertFrom-Json)
if($stfilter) {
$servertemplates = $servertemplates | Where-Object Name -like $stfilter
}
foreach($servertemplate in $servertemplates) {
&$rightst "-a=$rightstaccount" "st" "download" $($servertemplate.name) $outputpath
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment