Skip to content

Instantly share code, notes, and snippets.

@srpomeroy
Created November 8, 2018 21:01
Show Gist options
  • Save srpomeroy/667d360bb0de6f48ca99291ba017f331 to your computer and use it in GitHub Desktop.
Save srpomeroy/667d360bb0de6f48ca99291ba017f331 to your computer and use it in GitHub Desktop.
PowerShell script that maintains a RightScale MCI based on parameter inputs.
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
$rsRefreshToken,
[Parameter(Mandatory=$true)]
$rsAccount,
[Parameter(Mandatory=$true)]
$rsEndpoint,
[Parameter(Mandatory=$true)]
$rsMCIName,
[Parameter(Mandatory=$true)]
$cloudType,
[Parameter(Mandatory=$true)]
$cloudName,
[Parameter(Mandatory=$true)]
$imageId,
[Parameter(Mandatory=$true)]
$instanceTypeName
)
$cloud = rsc --account=$rsAccount --host=$rsEndpoint --refreshToken=$rsRefreshToken cm15 index /api/clouds "filter[]=name==$cloudName" "filter[]=cloud_type==$cloudType" | ConvertFrom-Json
if(!$cloud) {
Write-Output "Error! Cloud with type '$cloudType' and name '$cloudName' not found!"
}
else {
$cloudHref = $cloud.links | Where-Object {$_.rel -eq "self"} | Select-Object -ExpandProperty href
$image = rsc --account=$rsAccount --host=$rsEndpoint --refreshToken=$rsRefreshToken cm15 index $cloudHref/images "filter[]=resource_uid==$imageId" | ConvertFrom-Json
if(!$image) {
Write-Output "Error! Image with resource_uid '$imageId' not found!"
}
else {
$imageHref = $image.links | Where-Object {$_.rel -eq "self"} | Select-Object -ExpandProperty href
$mci = rsc --account=$rsAccount --host=$rsEndpoint --refreshToken=$rsRefreshToken cm15 index /api/multi_cloud_images "filter[]=name==$rsMCIName" | convertFrom-Json
if(!$mci) {
Write-Output "MCI does not exist. Creating..."
$mciHref = $(rsc --account=$rsAccount --host=$rsEndpoint --refreshToken=$rsRefreshToken --xh=Location cm15 create /api/multi_cloud_images "multi_cloud_image[description]=$rsMCIName" "multi_cloud_image[name]=$rsMCIName")
# Tag the MCI for RightLink 10 Install at Boot
# Linux: https://docs.rightscale.com/rl10/reference/10.6.2/rl10_install_at_boot.html
# Windows: https://docs.rightscale.com/rl10/reference/10.6.2/rl10_install_at_boot_windows.html
if($rsMCIName -like "*Windows*") {
Write-Output "Using RL10 Windows tags"
$rsRL10Tag = "rs_agent:powershell_url=https://rightlink.rightscale.com/rll/10.6.2/rightlink.boot.ps1"
}
elseif($rsMCIName -like "*Linux*") {
Write-Output "Using RL10 Linux tags"
$rsRL10Tag = "rs_agent:mime_shellscript=https://rightlink.rightscale.com/rll/10.6.2/rightlink.boot.sh"
}
Write-Output "Tagging MCI..."
rsc --account=$rsAccount --host=$rsEndpoint --refreshToken=$rsRefreshToken cm15 multi_add /api/tags/multi_add "resource_hrefs[]=$mciHref" "tags[]=rs_agent:type=right_link_lite" "tags[]=$rsRL10Tag"
# Get default instance type
$instanceType = rsc --account=$rsAccount --host=$rsEndpoint --refreshToken=$rsRefreshToken cm15 index $cloudHref/instance_types "filter[]=name==$instanceTypeName" | ConvertFrom-Json
if(!$instanceType) {
Write-Output "Instance type with name '$instanceTypeName' not found!"
}
else {
Write-Output "Creating MCI settings..."
$instanceTypeHref = $instanceType.links | Where-Object {$_.rel -eq "self"} | Select-Object -ExpandProperty href
rsc --account=$rsAccount --host=$rsEndpoint --refreshToken=$rsRefreshToken cm15 create $mciHref/settings "multi_cloud_image_setting[cloud_href]=$cloudHref" "multi_cloud_image_setting[image_href]=$imageHref" "multi_cloud_image_setting[instance_type_href]=$instanceTypeHref"
}
}
else {
# Look for existing MCI settings
$mciHref = $mci.links | Where-Object {$_.rel -eq "self"} | Select-Object -ExpandProperty href
$mciSettings = rsc --account=$rsAccount --host=$rsEndpoint --refreshToken=$rsRefreshToken cm15 index $mciHref/settings "filter[]=cloud_href==$cloudHref" | convertFrom-Json
if(!$mciSettings) {
Write-Output "MCI settings for cloud '$cloudName' not found!"
# Get default instance type
$instanceType = rsc --account=$rsAccount --host=$rsEndpoint --refreshToken=$rsRefreshToken cm15 index $cloudHref/instance_types "filter[]=name==$instanceTypeName" | ConvertFrom-Json
if(!$instanceType) {
Write-Output "Instance type with name '$instanceTypeName' not found!"
}
else {
Write-Output "Creating MCI settings..."
$instanceTypeHref = $instanceType.links | Where-Object {$_.rel -eq "self"} | Select-Object -ExpandProperty href
rsc --account=$rsAccount --host=$rsEndpoint --refreshToken=$rsRefreshToken cm15 create $mciHref/settings "multi_cloud_image_setting[cloud_href]=$cloudHref" "multi_cloud_image_setting[image_href]=$imageHref" "multi_cloud_image_setting[instance_type_href]=$instanceTypeHref"
}
}
else {
Write-Output "Updating MCI settings..."
$mciSettingsHref = $mciSettings.links | Where-Object {$_.rel -eq "self"} | Select-Object -ExpandProperty href
rsc --account=$rsAccount --host=$rsEndpoint --refreshToken=$rsRefreshToken cm15 update $mciSettingsHref "multi_cloud_image_setting[image_href]=$imageHref"
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment