#!/bin/bash

if [[ $# -lt 1 || $# -gt 2 ]];then
    echo -e "\033[32m Usage: $0 domain [uuid] \033[0m"
    exit 1
fi

logger(){
    echo [$1] $2
    echo `date +"%Y-%m-%d %H:%M:%S "` [$1] $2 >> /tmp/$hostname.log
    if [[ ($1 != 'debug') && ( -n $u_uuid ) ]];then
        curl http://vm-mgr.service.ucloud.cn/vm-mgr/v1/vm/receive -X POST -H 'Content-Type:application/json' -d "{\"Action\":\"delete\",\"UID\":\"$u_uuid\",\"SubStatus\":\"$1\",\"Info\":\"$2\"}"
    fi
    if [[ $1 = 'success' ]];then
        exit 0
    elif [[ $1 = 'failed' ]];then
        exit 1
    fi
} 

domain=$1
u_uuid=$2
hostname=$domain

virsh dumpxml --migratable $domain > /opt/data/${domain}img/${domain}.xml
virsh destroy $domain
virsh undefine $domain

mv "/opt/data/${domain}img/" "/opt/data/${domain}img.delete-`date +%F-%H%M`"

if [[ -b /dev/vg/$domain ]];then
    lvrename vg $domain $domain.delete-`date +%F-%H%M`
fi

logger "success" "delete_success"

