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.

130 lines
2.6 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 () {
# TODO: Check dependencies
# TODO: Check required options are set
# TODO: Check available volume space
# 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"
# Increase volume size
new_volume_size=$(expr $volume_size + $BUFFER)
log "Increasing volume size to ${new_volume_size}GB..."
action_json=$(digital_ocean_api "volumes/2cbebc6a-ff42-11e9-a238-0a58ac14a19d/actions" '{"type":"resize","size_gigabytes":'$new_volume_size'}')
action_id=$(echo $action_json | jq -r '.action.id')
log "Created action \"${action_id}\""
# Wait for volume resize
log "Waiting for action to complete..."
while true; do
resize_status=$(digital_ocean_api "actions/$action_id" | jq -r '.action.status')
if [[ "${resize_status}" == "completed" ]]; then
break
fi
sleep 1
done
log "Volume resize complete!"
# TODO: Resize filesystem
# sudo resize2fs $DEVICE
}
main