#!/bin/bash
# Program:
#   Deploy PXE PV VM to DRBL client
# Author: 
#   Jazz, Rock {jazz, rock}@nchc.org.tw
# Version:
#    1.0
# History:                                                                                          
#   2010/08/27  Rock    First release (1.0) 

# [Source]
source /opt/drbl-virt/conf/drbl-virt.conf
source $Work_Path/functions_drbl_virt
#source ./functions_drbl_PXE_PV-VM_create


# [Declation]
# = 1. Varibales declation =
vm_name=""
vm_ram="256"
vm_mac=""
vm_cpu="1"
vm_dir="/home/domains"

# = 2. Functions declation =
Usage(){
echo "Usage: drbl_PXE_PV-VM_deploy [options]"
echo "Options:"
echo "-h|--host      deploy vm to which host"
echo "-v|--vm        vm name"
echo "-c|--cpu       vm cpu (default: 1)"
echo "-r|--ram       vm ram size(M) (default: 256)"
echo "Example:"
echo "drbl_PXE_KVM-VM_deploy -h drbl101 -v drbl131 -r 512"
}


# [Main]
check_root
# = 1. Parse parameters = 
if [ $# -eq 0 ]; then
Usage && exit
fi

while [ $# -gt 0 ]; do
    case "$1" in
        -h|--host)
            shift 
            if [ -z "$(echo $1 |grep ^-.)" ]; then
                if [ -n "$(echo $1)" ]; then
                    client_name=$1
                else
                    echo "-h host value is null"
                    Usage && exit 2
                fi
            shift 
            fi 
            ;; 
        -v|--vm)
            shift
            if [ -z "$(echo $1 |grep ^-.)" ]; then
                if [ -n "$(echo $1)" ]; then
                    vm_name=$1
                else
                    echo "-v vm name is null"
                    Usage && exit 2
                fi
            shift
            fi
            ;;
            -c|--cpu)
            if [ -z "$(echo $1 |grep ^-.)" ]; then
                if [ -n "$(echo $1)" ]; then
                    vm_cpu=$1
                else
                    echo "-c vm cpu is null"
                    Usage && exit 2
                fi
            shift
            fi
        -r|--ram)
            shift 
            if [ -z "$(echo $1 |grep ^-.)" ]; then
                if [ -n "$(echo $1)" ]; then
                    vm_ram=$1
                else
                    echo "-r ram_value is null"
                    Usage && exit 2
                fi
            shift 
            fi 
            ;; 
        -*)
            echo "$0 $1 invalid option" >&2
            echo ""
            Usage >&2
            exit 2
            ;;
        *)  Usage >&2
            exit 2
            ;;
    esac
done


# = 2. Check null value =
[ -z $client_name ] && echo "[Error] no host" && Usage && exit 2
[ -z $vm_name ] && echo "[Error] no VM name" && Usage && exit 2


# = 3. Check exist
# check drbl client exist
cat $Work_Home/etc/IP_HOST_TABLE | grep $client_name
if [ $? -ne 0 ]; then
    echo "[Error] This host is not DRBL client"
    exit 2
fi
# check vm name exist
IP_VM_files=$(ls $Work_Home/etc | grep 'IP_VM_eth[0-9]$')
vm_exist_jude="no"
for vm_IP_file in $IP_VM_files
do
    if [ -n "$(grep "$vm_name" $Work_Home/etc/$vm_IP_file)" ]; then
        vm_exist_jude="yes"
    fi
done

if [ $vm_exist_jude == "no" ]; then
    echo "[Error] This VM name isn't DRBL PXE VM"
    exit 2
fi

# 4. Get Mac Address
IP_VM_right_file=""
IP_VM_right_line=""
IP_VM_eth=""
for IP_VM_file in $IP_VM_files
do
    IP_VM_line=$(cat -n $Work_Home/etc/$IP_VM_file | grep "$vm_name" | awk '{print $1}') 
    if [ -n $IP_VM_line ]; then
        IP_VM_right_file=$IP_VM_file
        IP_VM_right_line=$IP_VM_line
    fi
done

# if no value, exit and check IP_VM_ethX
if [ -z $IP_VM_right_file ]; then
    echo ""
    echo "[Error] don't fine ${vm_name}'s IP address"
    echo "Please check $Wokr_Home/etc/"
    exit 2
fi

# start get mac address
IP_VM_eth=$(echo $IP_VM_right_file | sed 's/IP_VM_//g' )
vm_mac=$(cat $Work_Home/etc/macadr-VM-${IP_VM_eth}.txt | sed -n "${IP_VM_right_line}p")

# = 4. ssh host to deploy VM  =
# local varibales
host_IP=$(cat $Work_Home/etc/IP_HOST_TABLE | grep $client_name | awk '{print $1}')

# start ssh
vnc_nus=$(ssh -o StrictHostKeyChecking=no $host_IP "netstat -tunlp | grep kvm | wc -l")
vnc_nus=$((vnc_nus+1))
ssh -o StrictHostKeyChecking=no $host_IP "kvm -name $vm_name -smp $vm_cpu -m $vm_ram -net nic,macaddr=$vm_mac -net tap -vnc :$vnc_nus -boot n &"

