Skip to content

Instantly share code, notes, and snippets.

@marcocitus
Last active December 10, 2018 13:16
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 marcocitus/c17076c38da4525c7138cb80cd388c99 to your computer and use it in GitHub Desktop.
Save marcocitus/c17076c38da4525c7138cb80cd388c99 to your computer and use it in GitHub Desktop.
Cloud API tools
#!/bin/sh
name=Marco
formation_name=${1:-test}
formations_url=https://console.citusdata.com/api/v1/formations
api_token=$(cat $HOME/.cituscloud/token)
formation_dir=$HOME/.cituscloud/formations/$formation_name/
worker_count=2
function log {
echo "$(date '+[%Y-%m-%d %H:%M:%S]')" "$*" >&2
}
function create_formation {
curl -s -H "Authorization: Token $api_token" -H "Content-Type: application/json" --request POST -d "$1" "$formations_url/" | jq -r .id
}
function get_formation_state {
curl -s -H "Authorization: Token $api_token" "$formations_url/$1" | jq -r .state
}
function get_formation_url {
curl -s -H "Authorization: Token $api_token" "$formations_url/$1" | jq -r .database_url
}
mkdir -p "$formation_dir"
# Create a new formation and store the ID
read json_request << EOM
{"tune_attributes": { "coordinator_tune": "t2.small", "worker_tune": "t2.small", "worker_count": "$worker_count", "coordinator_disk_size": "100", "worker_disk_size": 100}, "name": "$name-$formation_name", "region": "us-east-1"}
EOM
formation_id=$(create_formation "$json_request" | tee "$formation_dir/id")
if [ -z "$formation_id" ] || [ "$formation_id" = "null" ]; then
log Failed to create formation
exit 1
fi
# Wait for formation to become steady
state=$(get_formation_state $formation_id)
while [ "$state" != "steady" ]; do
log Current state of formation $formation_id is $state
sleep 20
state=$(get_formation_state $formation_id)
done
log Formation $name-$formation_name with ID $formation-id is $state, waiting for workers
# Get, store and return the database URL
database_url=$(get_formation_url $formation_id | tee "$formation_dir/url")
# Wait for all workers to become healthy
healthy_workers_query="SELECT count(*) FROM run_command_on_workers('SELECT 1') WHERE success"
healthy_worker_count=$(psql -tAX "$database_url" -c "$healthy_workers_query")
while [ "$healthy_worker_count" != "$worker_count" ]; do
log Current number of available workers is $healthy_worer_count
sleep 20
healthy_worker_count=$(psql -tAX "$database_url" -c "$healthy_workers_query")
done
log Formation $name-$formation_name with ID $formation_id is ready with $healthy_worker_count workers
echo $database_url
#!/bin/sh
formation_name=${1:-test}
formations_url=https://console.citusdata.com/api/v1/formations
api_token=$(cat $HOME/.cituscloud/token)
function log {
echo "$(date '+[%Y-%m-%d %H:%M:%S]')" "$*" >&2
}
function api_delete {
curl -s -H "Authorization: Token $api_token" --request DELETE $* | jq -r .success
}
formation_dir=$HOME/.cituscloud/formations/$formation_name
if [ ! -s "$formation_dir/id" ]; then
log Formation "$formation_name" not found
exit 1
fi
formation_id=$(cat "$formation_dir/id")
if [ -z "$formation_id" ]; then
log Formation ID cannot be empty
exit 1
fi
api_delete "$formations_url/$formation_id" && rm -r "$formation_dir"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment