#!/bin/sh

#
# Copyright (C) 2007,2008 Canonical, Ltd.
# Author: Jamie Strandboge <jamie@canonical.com>
# License: GPLv3
#
set -e

ustconf="$HOME/.uqt-vm-tools.conf"
if [ -s "$ustconf" ]; then
    . "$ustconf"
else
    echo "Could not find '$ustconf'"
    exit 1
fi

. $UQT_VM_TOOLS/libvm.sh
abort_if_root

help() {
    echo "Usage: vm-cmd [-f] [-r] [-s] [-t TIMEOUT] -p <prefix> <command>"
    echo "Usage: vm-cmd [-f] [-r] [-s] [-t TIMEOUT] <vm> <command>"
    echo " -p PREFIX      use PREFIX to construct hostname list"
    echo " -s             start the VM (and shutdown if it wasn't running)"
    echo " -t TIMEOUT     how long to wait for VM to come up if -s used"
    echo " -f             force the SSH keys to be taken"
    echo " -r             login to the VM as root"
    echo ""
    echo "Eg:"
    echo "$ vm-cmd -p sec 'sudo reboot'"
    echo ""
    echo "This will reboot (via ssh) all sec-<release>-<arch> machines"
    echo "that are running, where <release> is in '$vm_release_list' and"
    echo "<arch> is in $vm_archs."
    echo ""
    echo "$ vm-cmd clean-jaunty-i386 'sudo apt-get update'"
    echo ""
    echo "This will run 'apt-get update' on the single VM named 'clean-jaunty-i386'"
}

prefix=
start=
force=
asroot=
timeout=90
while getopts "hfrst:p:" opt
do
    case "$opt" in
        p) prefix="$OPTARG";;
        s) start=1;;
        f) force=1;;
        r) asroot=1;;
        t) timeout="$OPTARG";;
        ?|h) help
           exit 1
           ;;
    esac
done
shift $(( $OPTIND - 1 ))

machines=
if [ -n "$prefix" ]; then
    for a in $vm_archs ; do
        for m in `vm_get_machines_by_prefix "${prefix}" "$a"` ; do
            machines="$machines $m"
        done
    done
else
    if [ -z "$1" ]; then
        help
        exit 1
    fi

    machines="$1"
    shift || true
fi

for m in $machines ; do
        started=""
        echo "----- $m -----"
        if [ -n "$start" ]; then
            if ! vm_running "$m" ; then
                echo "Starting $m ..."
                virsh start "$m" >/dev/null 2>&1
                started=1
            fi
            host=`vm_wait "$m" $timeout`
        else
            host=`vm_ping ${m}`
        fi
        if [ -z "$host" ]; then
            echo "Skipped $m"
            echo ""
            continue
        fi

        # '-t' forces tty allocation (for sudo passwords)
        args="-t -o BatchMode=yes"
        if [ -n "$force" ]; then
            args="-o StrictHostKeyChecking=no $args"
        fi

        if [ -n "$asroot" ]; then
            args="$args -l root"
        fi

        echo "Command: ssh ${args} $host \"$@\""
        ssh $args "$host" "$@" || true
        echo ""

        if [ -n "$started" ]; then
            echo "Shutting down $host ..."
            ssh $args -l root "$host" "shutdown -h now" || true
            # wait for the system to actually shut down
            count=0
            while [ "$count" -lt "$timeout" ]; do
                if [ -z $(vm_ping ${m}) ]; then
                    break
                fi
                sleep 1
            done
        fi
done
