Skip to content

Instantly share code, notes, and snippets.

@srpomeroy
Created July 6, 2018 03:23
Show Gist options
  • Save srpomeroy/920ff09db6dde4391fcccb0427f91925 to your computer and use it in GitHub Desktop.
Save srpomeroy/920ff09db6dde4391fcccb0427f91925 to your computer and use it in GitHub Desktop.
Commonly used functions in RightScale CATs
# Required prolog
name 'LIB - Common functions'
rs_ca_ver 20160622
short_description "Common functions"
package "common/functions"
# Checks if the account supports the selected cloud
define checkCloudSupport($cloud_name, $param_location) do
# Gather up the list of clouds supported in this account.
@clouds = rs_cm.clouds.get()
$supportedClouds = @clouds.name[] # an array of the names of the supported clouds
# Check if the selected/mapped cloud is in the list and yell if not
if logic_not(contains?($supportedClouds, [$cloud_name]))
raise "Your trial account does not support the "+$param_location+" cloud. Contact RightScale for more information on how to enable access to that cloud."
end
end
# Imports the server templates found in the given map.
# It assumes a "name" and "rev" mapping
define importServerTemplate($stmap) do
foreach $st in keys($stmap) do
$server_template_name = map($stmap, $st, "name")
$server_template_rev = map($stmap, $st, "rev")
@pub_st=rs_cm.publications.index(filter: ["name=="+$server_template_name, "revision=="+$server_template_rev])
@pub_st.import()
end
end
# Creates CREDENTIAL objects in Cloud Management for each of the named items in the given array.
define createCreds($credname_array) do
foreach $cred_name in $credname_array do
@cred = rs_cm.credentials.get(filter: join(["name==",$cred_name]))
if empty?(@cred)
$cred_value = join(split(uuid(), "-"))[0..14] # max of 16 characters for mysql username and we're adding a letter next.
$cred_value = "a" + $cred_value # add an alpha to the beginning of the value - just in case.
@task=rs_cm.credentials.create({"name":$cred_name, "value": $cred_value})
end
end
end
# Returns either an RDP or SSH link for the given server.
# This link can be provided as an output for a CAT and the user can select it to to get the
# RDP or SSH file just like in Cloud Management.
#
# INPUTS:
# @server - server resource for which you want the link
# $link_type - "SSH" or "RDP" to indicate which type of access link you want back.
# $shard - the API shard to use. This can be found using the "find_shard.rb" definition.
# $account_number - the account number. This can be found using the "find_account_number.rb" definition.
#
define get_server_access_link(@server, $link_type, $shard, $account_number) return $server_access_link do
$rs_endpoint = "https://us-"+$shard+".rightscale.com"
$instance_href = @server.current_instance().href
$response = http_get(
url: $rs_endpoint+"/api/instances",
headers: {
"X-Api-Version": "1.6",
"X-Account": $account_number
}
)
$instances = $response["body"]
$instance_of_interest = select($instances, { "href" : $instance_href })[0]
# rs_cm.audit_entries.create(notify: "None", audit_entry: { auditee_href: @server, summary: join(["instance of interest"]), detail: to_s($instance_of_interest)})
$legacy_id = $instance_of_interest["legacy_id"]
$cloud_id = $instance_of_interest["links"]["cloud"]["id"]
$instance_public_ips = $instance_of_interest["public_ip_addresses"]
$instance_private_ips = $instance_of_interest["private_ip_addresses"]
$instance_ip = switch(empty?($instance_public_ips), to_s($instance_private_ips[0]), to_s($instance_public_ips[0]))
# rs_cm.audit_entries.create(notify: "None", audit_entry: { auditee_href: @server, summary: join(["instance_ip: ", $instance_ip]), detail: ""})
$server_access_link_root = "https://my.rightscale.com/acct/"+$account_number+"/clouds/"+$cloud_id+"/instances/"+$legacy_id
if $link_type == "RDP"
$server_access_link = $server_access_link_root +"/rdp?host=" + $instance_ip
elsif $link_type == "SSH"
$server_access_link = "ssh://rightscale@" + $instance_ip
else
raise "Incorrect link_type, " + $link_type + ", passed to get_server_access_link()."
end
# rs_cm.audit_entries.create(notify: "None", audit_entry: { auditee_href: @server, summary: "access link", detail: $server_access_link})
end
# Returns the RightScale account number in which the CAT was launched.
define find_account_number() return $rs_account_number do
$cloud_accounts = to_object(first(rs_cm.cloud_accounts.get()))
@info = first(rs_cm.cloud_accounts.get())
$info_links = @info.links
$rs_account_info = select($info_links, { "rel": "account" })[0]
$rs_account_href = $rs_account_info["href"]
$rs_account_number = last(split($rs_account_href, "/"))
#rs_cm.audit_entries.create(notify: "None", audit_entry: { auditee_href: @deployment, summary: "rs_account_number" , detail: to_s($rs_account_number)})
end
# Returns the RightScale shard for the account the given CAT is launched in.
# It relies on the fact that when a CAT is launched, the resultant deployment description includes a link
# back to Self-Service.
# This link is exploited to identify the shard.
# Of course, this is somewhat dangerous because if the deployment description is changed to remove that link,
# this code will not work.
# Similarly, since the deployment description is also based on the CAT description, if the CAT author or publisher
# puts something like "selfservice-8" in it for some reason, this code will likely get confused.
# However, for the time being it's fine.
define find_shard(@deployment) return $shard_number do
$deployment_description = @deployment.description
#rs_cm.audit_entries.create(notify: "None", audit_entry: { auditee_href: @deployment, summary: "deployment description" , detail: $deployment_description})
# initialize a value
$shard_number = "UNKNOWN"
foreach $word in split($deployment_description, "/") do
if $word =~ "selfservice-"
#rs_cm.audit_entries.create(notify: "None", audit_entry: { auditee_href: @deployment, summary: join(["found word:",$word]) , detail: ""})
foreach $character in split($word, "") do
if $character =~ /[0-9]/
$shard_number = $character
#rs_cm.audit_entries.create(notify: "None", audit_entry: { auditee_href: @deployment, summary: join(["found shard:",$character]) , detail: ""})
end
end
end
end
end
# Used to get a credential
# Requires admin permission
define get_cred($cred_name) return $cred_value do
@cred = rs_cm.credentials.get(filter: "name=="+$cred_name, view: "sensitive")
$cred_hash = to_object(@cred)
$cred_value = ""
foreach $detail in $cred_hash["details"] do
if $detail["name"] == $cred_name
$cred_value = $detail["value"]
end
end
end
# replaces spaces and things with url encoding characters
define url_encode($string) return $encoded_string do
$encoded_string = gsub($string, " ", "%20")
end
# Puts url encoded quote around the string
define wrap_encoded_quote($string) return $wrapped_string do
$wrapped_string = join(["%27",$string,"%27"])
end
# create an audit entry
define log($summary, $details) do
rs_cm.audit_entries.create(notify: "None", audit_entry: { auditee_href: @@deployment, summary: $summary , detail: $details})
end
# Used for retry mechanism
define handle_retries($attempts) do
if $attempts < 3
$_error_behavior = "retry"
sleep(60)
end # If it fails 3 times just let it raise the error
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment