Skip to content

Instantly share code, notes, and snippets.

@chernjie
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chernjie/8f4da1ca8254b5022885 to your computer and use it in GitHub Desktop.
Save chernjie/8f4da1ca8254b5022885 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
use() {
for i do
if ! command -v $i > /dev/null
then
echo command $i not found
exit 1
fi
done
}
use rumm
use json
use curl
use tee
use column
COLOR_RED() { echo -en "\033[31m"; }
COLOR_GREEN() { echo -en "\033[32m"; }
COLOR_YELLOW(){ echo -en "\033[33m"; }
COLOR_BLUE() { echo -en "\033[34m"; }
COLOR_RESET() { echo -en "\033[0m"; }
_error()
{
COLOR_RED
echo $@
COLOR_RESET
exit 1
}
_login()
{
test -f ~/.rummrc || rumm login
local API_USR=$(json -f ~/.rummrc environments.default.username)
local API_KEY=$(json -f ~/.rummrc environments.default.api_key)
local API_REG=$(json -f ~/.rummrc environments.default.region)
REGION=${REGION:-$API_REG}
curl -s https://identity.api.rackspacecloud.com/v2.0/tokens \
-XPOST \
-d '{"auth":{"RAX-KSKEY:apiKeyCredentials":{"username":"'$API_USR'", "apiKey":"'$API_KEY'"}}}' \
-H "Content-Type: application/json" |
json > rackspace.session
}
_is_logged_in()
{
test -f rackspace.session || _error Please run: $0 login
}
_token()
{
json -f rackspace.session access.token.id
}
_getBaseUrl()
{
local API_REG=$(json -f ~/.rummrc environments.default.region)
REGION=${REGION:-$API_REG}
json -f rackspace.session access.serviceCatalog |
json -C 'this.name=="cloudServersOpenStack"' -a endpoints |
json -C 'this.region=="'$REGION'"' -a publicURL
}
_getServers()
{
_is_logged_in
curl -s $(_getBaseUrl)/servers -H "X-Auth-Token: $(_token)" | json > rackspace.servers
}
_getServerIdFromName()
{
json -f rackspace.servers servers | json -C 'this.name=="'$1'"' -a id
}
_update()
{
test -f rackspace.servers || _getServers
test $# -lt 3 && _error $0 update "<server> <metadata_key> <value>"
_is_logged_in
local _id=$(_getServerIdFromName $1)
curl -s $(_getBaseUrl)/servers/$_id/metadata/$2 -H "X-Auth-Token: $(_token)" | json | tee "./metadata-$1-$2.json"
local _exists=$(json -f "./metadata-$1-$2.json" meta.$2)
local _method=$(test -n "$_exists" && echo POST || echo PUT)
cat <<EOD
curl -s $(_getBaseUrl)/servers/$_id/metadata/$2 \\
-H "X-Auth-Token: $(_token)" \\
-H "Content-Type: application/json" \\
-d '{"meta":{"$2":"$3"}}' -X$_method
EOD
}
_usage()
{
echo $0 update '<server>' RackConnectLBPool '<pool>'
}
### OPTIONS ###
case $1 in
login) _login;;
update) shift; _update $@;;
servers) _getServers
json -f rackspace.servers servers | json -a name id | column -t;;
*) _usage; grep -A100 OPTIONS $0;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment