#!/bin/bash USAGE="update_storage.sh [-d] server-name new-size" [ "$1" == "-h" ] && echo "$USAGE" && exit [ "$1" == "-d" ] && DEBUG=$1 && shift [ -z "$1" ] && echo "No server found" && echo "$USAGE" && exit 1 SERVER_ID=$1 [ -z "$2" ] && echo "No new size found" && echo "$USAGE" && exit 1 NEW_SIZE=$2 STOPPED="stopped" STARTED="started" MAX_TRIES=5 SLEEP_TIME=10 _server_state() { [ -z "$1" ] && return upctl server show "$1" | grep State | awk '{print $2}' } _is_server_started() { [ -z "$1" ] && return _server_state "$1" | grep started } _stop_server() { [ -z "$1" ] && return [[ "$(_server_state "$1")" =~ $STOPPED ]] && return #[[ ! "$(_server_state "$1")" =~ $STOPPED ]] && upctl server stop "$1" 2>/dev/null local n=0 echo -n "Stopping $1 " while [[ ! "$(_server_state "$1")" =~ $STOPPED ]] do n=$((n+1)) echo -n ". " sleep $SLEEP_TIME [ "$n" -gt $MAX_TRIES ] && echo "Server $1 not $STOPPED" && exit 2 done echo " $(_server_state "$1")" } STORE_UUID=$(upctl server show "$SERVER_ID" -o json | jq -r '.storage[].uuid ' | sed 's/"//') [ -z "$STORE_UUID" ] && echo "No store found for $SERVER_ID" && exit 1 STORE_SIZE=$(upctl server show "$SERVER_ID" -o json | jq '.storage[].size ') echo "$SERVER_ID: store $STORE_UUID from $STORE_SIZE to $NEW_SIZE" [ "$STORE_SIZE" -gt "$NEW_SIZE" ] && echo "$NEW_SIZE is less then current $STORE_SIZE" && exit 1 _stop_server "$SERVER_ID" [ -n "$DEBUG" ] && _server_state "$SERVER_ID" if upctl storage modify "$STORE_UUID" --size "$NEW_SIZE" ; then [ -n "$DEBUG" ] && _server_state "$SERVER_ID" [[ ! "$(_server_state "$1")" =~ $STARTED ]] && upctl server start "$SERVER_ID" else echo "errors in modify $SERVER_ID $STORE_UUID" fi