You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
103 lines
1.8 KiB
103 lines
1.8 KiB
#!/usr/bin/env bash
|
|
|
|
# Parse options
|
|
POSITIONAL=()
|
|
while [[ $# -gt 0 ]]; do
|
|
key="$1"
|
|
|
|
LOG=false
|
|
|
|
case $key in
|
|
--token)
|
|
TOKEN="$2"
|
|
shift
|
|
shift
|
|
;;
|
|
|
|
--device)
|
|
DEVICE="$2"
|
|
shift
|
|
shift
|
|
;;
|
|
|
|
--volume-name)
|
|
VOLUME_NAME="$2"
|
|
shift
|
|
shift
|
|
;;
|
|
|
|
--volume-region)
|
|
VOLUME_REGION="$2"
|
|
shift
|
|
shift
|
|
;;
|
|
|
|
--buffer)
|
|
BUFFER="$2"
|
|
shift
|
|
shift
|
|
;;
|
|
|
|
--log)
|
|
LOG=true
|
|
shift
|
|
shift
|
|
;;
|
|
|
|
*)
|
|
POSITIONAL+=("$1")
|
|
shift
|
|
;;
|
|
esac
|
|
done
|
|
set -- "${POSITIONAL[@]}"
|
|
|
|
log () {
|
|
prefix=""
|
|
if [[ "$LOG" = true ]]; then
|
|
prefix="$(date "+%Y-%m-%d %H:%M:%S") "
|
|
fi
|
|
echo $prefix$@
|
|
}
|
|
|
|
log "DEBUG"
|
|
log "====="
|
|
log "TOKEN = ${TOKEN}"
|
|
log "DEVICE = ${DEVICE}"
|
|
log "VOLUME_NAME = ${VOLUME_NAME}"
|
|
log "VOLUME_REGION = ${VOLUME_REGION}"
|
|
log "BUFFER = ${BUFFER}"
|
|
log
|
|
|
|
digital_ocean_api () {
|
|
endpoint=$1
|
|
post_data=$2
|
|
|
|
method="GET"
|
|
if [[ $post_data ]]; then
|
|
method="POST"
|
|
fi
|
|
|
|
args=()
|
|
args+=("--silent")
|
|
args+=("-X" $method)
|
|
args+=("-H" "Content-Type: application/json")
|
|
args+=("-H" "Authorization: Bearer ${TOKEN}")
|
|
if [[ $post_data ]]; then
|
|
args+=("-d" $post_data)
|
|
fi
|
|
args+=("https://api.digitalocean.com/v2/${endpoint}")
|
|
|
|
curl "${args[@]}"
|
|
}
|
|
|
|
main () {
|
|
# Get volume data
|
|
log "Getting data for volume \"${VOLUME_NAME}\" in region \"${VOLUME_REGION}\"..."
|
|
volume_json=$(digital_ocean_api "volumes?region=${VOLUME_REGION}" | jq -r --arg VOLUME_NAME "${VOLUME_NAME}" '.volumes | .[] | select(.name==$VOLUME_NAME)')
|
|
volume_id=$(echo $volume_json | jq -r .id)
|
|
volume_size=$(echo $volume_json | jq -r .size_gigabytes)
|
|
log "Volume ID is \"${volume_id}\" and is currently ${volume_size}GB"
|
|
}
|
|
|
|
main
|
|
|