#!/bin/bash

source $(dirname $0)/../../etc/vmm/kvm/kvmrc
source $(dirname $0)/../../scripts_common.sh

HOST=$1
DOMAIN=$2
NAME=$3
REQUEST_ID=$4
request_index=$5

user=$(whoami)
if [ "$user" == "$ADMIN_USERNAME" ]; then
    LIBVIRT_URI=qemu+ssh://$user@$HOST/system
else
    LIBVIRT_URI=qemu+tcp://$user@$HOST/system
fi

retry_if "active block job" 3 5 exec_and_log_brest \
    "virsh --connect $LIBVIRT_URI snapshot-revert --force $DOMAIN $NAME" \
    "Could not revert to snapshot $NAME for domain $DOMAIN." \
    $ONE_AD_LOGS/$REQUEST_ID/$request_index \
    $ONE_AD_LOGS/$REQUEST_ID/wait/fin

echo "0" > $ONE_AD_LOGS/$REQUEST_ID/$request_index.st
echo "" > $ONE_AD_LOGS/$REQUEST_ID/$request_index.log
touch $ONE_AD_LOGS/$REQUEST_ID/wait/fin