Skip to content

Instantly share code, notes, and snippets.

@dfrankel33
Last active March 13, 2017 18:11
Show Gist options
  • Save dfrankel33/adfe321602f943770d23ee44624668d9 to your computer and use it in GitHub Desktop.
Save dfrankel33/adfe321602f943770d23ee44624668d9 to your computer and use it in GitHub Desktop.
#Usage:
# -Execute entire script to load functions
# -Execute the Install-RSC function
# -Execute the Publish-CAT function
# After loading functions, built-in help notes are available. ie. Get-Help Publish-CAT
function Publish-CAT {
<#
.SYNOPSIS
Upload a CAT to RightScale Self-Service. And optionally publish the CAT to the Self-Service Catalog.
.PARAMETER RsEndpoint
RightScale API Endpoint. Only valid values are: us-3.rightscale.com OR us-4.rightscale.com
.PARAMETER RsAccountNum
RightScale Account Number
.PARAMETER RsEmail
Email address of RightScale user
.PARAMETER RsPassword
Password of RightScale user
.PARAMETER Source
Absolute or relative path to CAT file
.PARAMETER Publish
Switch. If set, the CAT will be published to the Self-Service Catalog after being uploaded.
.PARAMETER LogFile
Absolute or relative path to log file
.EXAMPLE
C:\PS> Publish-CAT -RsEndpoint us-3.rightscale.com -RsAccountNum 123456 -RsEmail john.doe@example.com -RsPassword P@ssw0rd -Source .\Base-Windows.rb -Publish -LogFile C:\Temp\Log.txt
.NOTES
Author: RightScale
Jan-2017
#>
param (
[Parameter(Mandatory=$true)]
[string]
$RsEndpoint,
[Parameter(Mandatory=$true)]
[string]
$RsAccountNum,
[Parameter(Mandatory=$true)]
[string]
$RsEmail,
[Parameter(Mandatory=$true)]
[string]
$RsPassword,
[Parameter(Mandatory=$true)]
[string]
$Source,
[Parameter(Mandatory=$false)]
[switch]
$Publish,
[Parameter(Mandatory=$true)]
[string]
$LogFile
)
$cat = Get-Content $Source
$cat_name = $cat.Split("`n") | ForEach-Object { if ($_ -like "name*") { Write-Output $_.Split("`"")[1] } }
Write-LogFile -Message "Uploading CAT - $cat_name" -MessageType "INFO" -LogFile $LogFile
$cat_check = .\rsc.exe --email $RsEmail --pwd $RsPassword --host $RsEndpoint --account $RsAccountNum ss index "/api/designer/collections/$RsAccountNum/templates" "filter[]=name==$cat_name" | ConvertFrom-Json
if ($cat_check) {
Write-LogFile -Message "CAT with the same name is already present in Account No. $RsAccountNum" -MessageType "INFO" -LogFile $LogFile
Write-LogFile -Message "Updating CAT in Self-Service Designer" -MessageType "INFO" -LogFile $LogFile
.\rsc.exe --email $RsEmail --pwd $RsPassword --host $RsEndpoint --account $RsAccountNum ss update $($cat_check.href) "source=$Source"
} else {
Write-LogFile -Message "CAT with the same name not found in Account No. $RsAccountNum" -MessageType "INFO" -LogFile $LogFile
Write-LogFile -Message "Uploading CAT to Self-Service Designer" -MessageType "INFO" -LogFile $LogFile
.\rsc.exe --email $RsEmail --pwd $RsPassword --host $RsEndpoint --account $RsAccountNum ss create "/api/designer/collections/$RsAccountNum/templates" "source=$Source"
}
$cat_postcheck = .\rsc.exe --email $RsEmail --pwd $RsPassword --host $RsEndpoint --account $RsAccountNum ss index "/api/designer/collections/$RsAccountNum/templates" "filter[]=name==$cat_name" | ConvertFrom-Json
if ($cat_postcheck) {
Write-LogFile -Message "CAT upload complete" -MessageType "INFO" -LogFile $LogFile
Write-LogFile -Message "CAT HREF - $($cat_postcheck.href)" -MessageType "INFO" -LogFile $LogFile
} else {
Write-LogFile -Message "CAT upload failed" -MessageType "ERROR" -LogFile $LogFile
EXIT 1
}
if ($Publish) {
Write-LogFile -Message "Publish flag set. Attempting to publish CAT to Self-Service Catalog" -MessageType "INFO" -LogFile $LogFile
.\rsc.exe --email $RsEmail --pwd $RsPassword --host $RsEndpoint --account $RsAccountNum ss publish $($cat_postcheck.href)
$catalog = .\rsc.exe --email $RsEmail --pwd $RsPassword --host $RsEndpoint --account $RsAccountNum ss index "/api/catalog/catalogs/$RsAccountNum/applications" | ConvertFrom-Json
$application = $catalog | Where-Object name -eq $cat_name
if ($application) {
Write-LogFile -Message "CAT successfully published to Self-Service Catalog" -MessageType "INFO" -LogFile $LogFile
} else {
Write-LogFile -Message "Failed to publish CAT to Self-Service Catalog" -MessageType "ERROR" -LogFile $LogFile
EXIT 1
}
} else {
Write-LogFile -Message "Publish flag not set. Will not attempt to publish CAT to Self-Service Catalog" -MessageType "INFO" -LogFile $LogFile
}
}
function Write-LogFile {
[CmdletBinding()]
Param([string]$Message, [string]$MessageType, [string]$LogFile)
$source = $((Get-Variable -Scope 1 MyInvocation -ValueOnly).MyCommand.Name)
$LogMessage = "$(Get-Date -Format s) - $source - $($MessageType.ToUpper()) - $Message"
Write-Verbose $LogMessage
Add-Content -Path $LogFile -Value $LogMessage
}
function Install-RSC {
<#
.SYNOPSIS
This function will check if the RightScale RSC tool exists in the current path and will download it if not already present.
.PARAMETER LogFile
Absolute or relative path to log file
.EXAMPLE
C:\PS> Install-RSC -LogFile C:\Temp\Log.txt
.NOTES
Author: RightScale
Jan-2017
#>
param(
[Parameter(Mandatory=$true)]
[string]
$LogFile
)
$functionName = $MyInvocation.InvocationName
$rscSource = "https://binaries.rightscale.com/rsbin/rsc/v6/rsc-windows-amd64.zip"
$currentPath = (Get-Item -Path ".\" -Verbose).FullName
if(Test-Path "$currentPath\rsc.exe") {
Write-LogFile -Message "RSC is already installed" -MessageType "INFO" -LogFile $LogFile
}
else {
Write-LogFile -Message "RSC is NOT installed. Installing..." -MessageType "INFO" -LogFile $LogFile
$wc = New-Object System.Net.Webclient
$file = $rscSource.split("/")[-1]
Write-LogFile -Message "Downloading RSC..." -MessageType "INFO" -LogFile $LogFile
$downloadedFile = "${env:TEMP}\$file"
if (Test-Path $downloadedFile) {
Remove-Item $downloadedFile -Force
}
$wc.DownloadFile($rscSource,$downloadedFile)
Expand-ZIPFile –File $downloadedfile –Destination $currentPath
Move-Item -Path "$currentPath\rsc\rsc.exe" -Destination $currentPath -Force
Remove-Item -Path "$currentPath\rsc"
Write-LogFile -Message "RSC downloaded succesfully" -MessageType "INFO" -LogFile $LogFile
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment