Skip to content

Instantly share code, notes, and snippets.

@gregtaylor99
Last active December 17, 2015 08:09
Show Gist options
  • Save gregtaylor99/5578224 to your computer and use it in GitHub Desktop.
Save gregtaylor99/5578224 to your computer and use it in GitHub Desktop.
RightscaleTagging
Set a Tag on an Instance
#!/bin/sh -ex
instance_id=556677
tag="mytags%3Abar%3Dfoo"
# Issue curl command to get and store the cookie
curl -H "$api_version" -b "$api_cookie" --request PUT https://my.rightscale.com/api/acct/"$api_account"/tags/set?resource_href=/ec2_instances/"$instance_id" -d 'tags[]='"$tag"
Set a Tag on a server's current instance
#!/bin/sh -ex
tag="server%3Ainstance%3Dcurrent"
# login to API
source ./rs-login.sh
# set the server ID
server_id=836587
# get XML from API
server_xml=$(curl -s -H "$api_version" -b "$api_cookie" \
https://my.rightscale.com/api/acct/"$api_account"/servers/"$server_id")
# datamine the current-instance-href from the server XML
tag_element=current-instance-href
read href_xml <<< $(echo "$server_xml" | grep "$tag_element")
href_xml="${href_xml#*>}"
current_instance_url="${href_xml%<*}"
instance_id=$(echo "$current_instance_url" | cut -d'/' -f8)
# Issue curl command to get and store the cookie
curl -H "$api_version" -b "$api_cookie" --request PUT https://my.rightscale.com/api/acct/"$api_account"/tags/set?resource_href=/ec2_instances/"$instance_id" -d 'tags[]='"$tag"
Set a Tag on a server's next instance
Note: To set multiple tags instead of a single tag as shown in this example, format your -d parameter value like tags[]=tag1&tags[]=tag2&tags[]=tag3.
#!/bin/sh -ex
server_id=5678
tag="mytags%3Afoo%3Dbar"
# Issue curl command to get and store the cookie
curl -H "$api_version" -b "$api_cookie" --request PUT https://my.rightscale.com/api/acct/"$api_account"/tags/set?resource_href=/servers/"$server_id" -d 'tags[]='"$tag"
Search for EC2 instances with a tag
Search for all server instances with a specific tag (for example, rs_node:dns_hostname) or a tag and value combination (for example, rs_node:dns_hostname:camille.foobar.com).
#!/bin/bash -e
# RightScript: Find EC2 instances with a tag
: ${TAG_SEARCH="rs_node%3Adns_hostname"}
# default: tags with rs_node:dns_hostname
# get all instances with tag
curl -H "$api_version" -b "$api_cookie" https://my.rightscale.com/api/acct/"$api_account"/tags/search?'resource_type=ec2_instance&tags='"$TAG_SEARCH"
Search for server instances referencing any tag in a list of tags (for example, either rs_test_tag:firsttag=myTag or rs_another_tag:thisTag=anotherTag).
#!/bin/bash -e
# RightScript: Find EC2 instances that match at least one of a number of tags
# This is a template for use testing API functions.
# It relies on a file called user-details.sh to pull in the credentials. This file should contain similar to the following:
#
#USER="user.name@company.com"
#PASSWORD="MySecurePassword"
#ACCT="0000"
#
# This file should setup variables containing the username and password
source ./user-details.sh
# This line sets up the login cookies
curl -c <./mySavedCookies> -u "$USER:$PASSWORD" https://my.rightscale.com/api/acct/$ACCT/login?api_version=1.0
#Make the API call to find matching servers
curl -b <./mySavedCookies> -X GET -H 'X-API-VERSION:1.0' -d "resource_type=ec2_instance" -d "tags[]=rs_test_tag:firsttag=myTag" -d "tags[]=rs_another_tag:thisTag=anotherTag" "https://my.rightscale.com/api/acct/$ACCT/tags/search"
You can modify the search to require server instances to match all tags in a list, by adding the match_all parameter.
#!/bin/bash -e
# RightScript: Find EC2 instances that match at least one of a number of tags
# This is a template for use testing API functions.
# It relies on a file called user-details.sh to pull in the credentials. This file should contain similar to the following:
#
#USER="user.name@company.com"
#PASSWORD="MySecurePassword"
#ACCT="0000"
#
# This file should setup variables containing the username and password
source ./user-details.sh
# This line sets up the login cookies
curl -c <./mySavedCookies> -u "$USER:$PASSWORD" https://my.rightscale.com/api/acct/$ACCT/login?api_version=1.0
#Make the API call to find matching servers
curl -b <./mySavedCookies> -X GET -H 'X-API-VERSION:1.0' -d "match_all=true" -d "resource_type=ec2_instance" -d "tags[]=rs_test_tag:firsttag=myTag" -d "tags[]=rs_another_tag:thisTag=anotherTag" "https://my.rightscale.com/api/acct/$ACCT/tags/search"
Ping all servers with a tag
Quick method to ping all servers with a specific tag.
#!/bin/bash
# rs-ping-tagged-servers.sh
# RightScript: Ping all EC2 servers with a tag
. "$HOME/.rightscale/rs_api_config.sh"
. "$HOME/.rightscale/rs_api_creds.sh"
[[ $1 ]] && TAG_SEARCH="$1"
: ${TAG_SEARCH="rs_login%3Astate%3Dactive"} # default: tags with rs_login:state=active
url="https://my.rightscale.com/api/acct/$rs_api_account_id/tags/search?resource_type=ec2_instance&tags=$TAG_SEARCH"
echo "GET: $url"
api_result=$(curl -s -H "X-API-VERSION: $rs_api_version" -b "$rs_api_cookie" "$url")
#echo "$api_result" | grep "<state>operational"
hrefs_xml=$(echo "$api_result" | grep "<href>" | uniq) # get unique hrefs of servers with tag
#echo "hrefs_xml: $hrefs_xml"
# get the server IDs only
server_ids=$(while read -r line; do
line=${line#*servers/}
server_id=${line%%/*}
echo "$server_id"
done <<< "$hrefs_xml")
#echo "$server_ids"
echo "Search and ping servers with tag, '"$TAG_SEARCH"'".
while read server_id; do
tag_element="<ip-address>"
echo " INFO: get-server-settings($server_id)"
server_settings_xml=$(rs-get-server-settings.sh "$server_id")
read ip_xml <<< $(echo "$server_settings_xml" | grep "$tag_element")
ip_xml="${ip_xml#*>}"
ip="${ip_xml%<*}"
read aws_id_xml <<< $(echo "$server_settings_xml" | grep "aws-id")
aws_id_xml="${aws_id_xml#*>}"
aws_id="${aws_id_xml%<*}"
if [[ $ip ]]; then
echo ' AWS Instance ID: '"$aws_id"
echo ' RightScale Server ID: '"$server_id"
echo ' IP Address: '"$ip"
#echo "$server_settings_xml"
ping -c 3 "$ip"
else
echo " INFO: $server_id has no IP address."
fi
done <<< "$server_ids"
echo 'Ping test complete.'
Run a RightScript on all servers with a tag
#!/bin/bash -e
# rs-run-rightscript-tagged.sh right_script_id tag [additional_postdata]
# e.g. rs-run-rightscript-tagged.sh 220337 "node:hostname=foo.bar.suf"
# rs-run-rightscript-tagged.sh 400347 "node:hostname" "-d 'server[parameters][DOWNLOAD_DIR]=text:/tmp'"
[[ ! $1 ]] && echo 'No right_script ID provided, exiting.' && exit 1
[[ ! $2 ]] && echo 'No tag ID provided, exiting.' && exit 1
script_id="$1"
TAG_SEARCH="$2"
[[ $3 ]] && add_post_data=" $3"
. "$HOME/.rightscale/rs_api_config.sh"
. "$HOME/.rightscale/rs_api_creds.sh"
echo "Searching for servers with tag, '$TAG_SEARCH'"
url="https://my.rightscale.com/api/acct/$rs_api_account_id/tags/search?resource_type=ec2_instance&tags=$TAG_SEARCH"
echo "GET: $url"
api_result=$(curl -s -H "X-API-VERSION: $rs_api_version" -b "$rs_api_cookie" "$url")
#echo "$api_result"
hrefs_xml=$(echo "$api_result" | grep "<href>" | uniq) # get unique hrefs of servers with tag
#echo "hrefs_xml: $hrefs_xml"
# get the server IDs only
server_ids=$(while read -r line; do
line=${line#*servers/}
server_id=${line%%/*}
echo "$server_id"
done <<< "$hrefs_xml")
#echo "$server_ids"
while read server_id; do
echo
echo "Running RightScript $script_id on $server_id."
url="https://my.rightscale.com/api/acct/$rs_api_account_id/servers/$server_id/run_script"
post_data="-d right_script=https://my.rightscale.com/api/acct/$rs_api_account_id/right_scripts/$script_id$add_post_data"
echo "POST: $url"
echo " post-data: $post_data"
api_cmd="curl -s -b $rs_api_cookie -H X-API-VERSION:$rs_api_version $post_data $url"
#echo "+ $api_cmd"
$api_cmd
done <<< "$server_ids"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment