#!/bin/sh
# This testsuite is part of autopkgtest
# autopkgtest is a tool for testing Debian binary packages
# autopkgtest is Copyright (C) 2006-2014 Canonical Ltd.
# Author: Martin Pitt <martin.pitt@ubuntu.com>
#
# adt-virt-ssh setup script that configures a container; this is only used for
# testing autopkgtest itself, adt-virt-lxc is much better for actual test runs

set -e

# add testbed capabilities here (possibly dynamically), see
# doc/README.virtualisation-server.rst
CAPABILITIES='isolation-container revert revert-full-system'
USER=adt_test
SUDO_PASSWORD=adt_test

CONTAINER=""
TEMPLATE=""
INSTALL_KEY=
ENABLE_SUDO=

[ `id -u` -eq 0 ] || PREFIX="sudo "

# create a testbed (if necessary), configure ssh, copy ssh key into it,
# configure sudo, etc.; print a list of "key=value" parameters to stdout on
# success
# required: login, hostname, and one of identity or password
# optional: port, options, capabilities
open() {
    [ -z "$2" ] || TEMPLATE="$2"
    if [ -z "$TEMPLATE}" ]; then
        echo "ERROR: $0 needs to be called with template container name" >&1
        exit 1
    fi

    [ -n "$CONTAINER" ] || CONTAINER=`mktemp -u adt-testsuite-XXX`

    OUT=`$PREFIX lxc-start-ephemeral -n $CONTAINER -o $TEMPLATE -d`
    # parse out IP
    IP=$(echo "$OUT" | grep -o '10\.[0-9]\+\.[0-9]\+\.[0-9]\+')

    # create user
    # password: python3 -c 'from crypt import *; print(crypt("adt_test", mksalt(METHOD_CRYPT)))'
    $PREFIX lxc-attach --name=$CONTAINER -- useradd --password uMBz978HXrv8s --create-home $USER

    if [ -n "$INSTALL_KEY" ]; then
        key=`cat $HOME/.ssh/id_rsa.pub`
        $PREFIX lxc-attach --name=$CONTAINER -- su -c "mkdir ~/.ssh; echo '$key' > ~/.ssh/authorized_keys" $USER
        echo "identity=$HOME/.ssh/id_rsa"
    fi

    if [ -n "$ENABLE_SUDO" ]; then
        $PREFIX lxc-attach --name=$CONTAINER -- sh -ec "echo '$USER ALL=(ALL) $ENABLE_SUDO' > /etc/sudoers.d/autopkgtest"
    fi

    cat<<EOF
login=$USER
hostname=$IP
capabilities=$CAPABILITIES
password=$SUDO_PASSWORD
extraopts=-n $CONTAINER -T $TEMPLATE
EOF
}

revert() {
    if [ -z "$CONTAINER" ]; then
        echo "Needs to be called with -n <container name>" >&2
        exit 1
    fi
    cleanup
    open
}

reboot() {
    echo "reboot not implemented" >&2
    exit 1
}

cleanup() {
    if [ -z "$CONTAINER" ]; then
        echo "Needs to be called with -n <container name>" >&2
        exit 1
    fi
    $PREFIX lxc-stop --kill --name=$CONTAINER || true
}

# parse options
eval set -- $(getopt -o "ksSn:T:" -- "$@")
while true; do
    case "$1" in
        -k)
            INSTALL_KEY=1; shift ;;
        -s)
            ENABLE_SUDO="ALL"; shift ;;
        -S)
            ENABLE_SUDO="NOPASSWD: ALL"; shift ;;
        -n)
            CONTAINER="$2"; shift 2 ;;
        -T)
            TEMPLATE="$2"; shift 2 ;;
        --)
            shift; break ;;
        *)
            echo "$0: unsupported option $1" >&2
            exit 1;;
    esac
done

case "$1" in
    open)
        open $@;;
    cleanup)
        cleanup $@;;
    revert)
        revert $@;;
    reboot)
        reboot $@;;
    '')
        echo "Needs to be called with command as first argument" >&2
        exit 1
        ;;
    *)
        echo "invalid command $1" >&2
        exit 1
        ;;
esac
