functions.sh 1.42 KB
Newer Older
unknown's avatar
unknown committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#functions
decodeDataFromJson(){
    echo `echo $1 \
	    | sed 's/{\"data\"\:{//g' \
	    | sed 's/\\\\\//\//g' \
	    | sed 's/[{}]//g' \
            | awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}' \
	    | sed 's/\"\:\"/\|/g' \
	    | sed 's/[\,]/ /g' \
	    | sed 's/\"// g' \
	    | grep -w $2 \
	    | awk -F "|" '{print $2}'`
}

checkTaskStatus(){
    for i in `seq 1 $RETRY`;
    do
        TASK_ID_DATA=`curl -s -k $USEPROXY -b "PVEAuthCookie=$TICKET" -H "CSRFPreventionToken: $CSRF" $HOST/api2/json/nodes/$NODE/tasks/$1/status`
        TASK_ID_STATUS=$(decodeDataFromJson $TASK_ID_DATA 'status')
        TASK_ID_EXITSTATUS=$(decodeDataFromJson $TASK_ID_DATA 'exitstatus')

        if [ "$TASK_ID_STATUS" = "stopped" ]; then
            if [ "$TASK_ID_EXITSTATUS" = "OK" ]; then
                echo "Task completed $TASK_ID_EXITSTATUS"
                break
            else
                echo "Task failed with status $TASK_ID_EXITSTATUS"
                exit 99
            fi
        else
            echo "Current task status $TASK_ID_STATUS. Waiting..."
        fi

        if [ $i -eq $RETRY ]; then
            echo "Failed after timeout"
                exit 99
        fi

        sleep 1
    done
}

runRemoteSSH(){
44
    ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i "$1" [email protected]$TARGET_IP "$2"
j38560's avatar
j38560 committed
45 46 47 48 49
}

runRemoteSQL(){
    echo "Running SQL $1"
    mysql -u deployment -h $TARGET_IP < "$1"
unknown's avatar
unknown committed
50
}