#!/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>
#
# autopkgtest-virt-ssh setup script that configures a container; this is only used for
# testing autopkgtest itself, autopkgtest-virt-lxd 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=autopkgtest
SUDO_PASSWORD=autopkgtest

CONTAINER=""
IMAGE=""
INSTALL_KEY=
ENABLE_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" ] || IMAGE="$2"
    if [ -z "$IMAGE}" ]; then
        echo "ERROR: $0 needs to be called with image name" >&1
        exit 1
    fi

    [ -n "$CONTAINER" ] || CONTAINER=`mktemp -u autopkgtest-test-XXX`

    lxc launch --ephemeral "$IMAGE" "$CONTAINER" >/dev/null

    # wait for and parse IPv4
    while ! OUT=`lxc info $CONTAINER|grep 'eth0:.*inet[^6]'`; do
        sleep 1
    done
    IP=$(echo "$OUT" | grep -o '10\.[0-9]\+\.[0-9]\+\.[0-9]\+')

    # create user
    # password: python3 -c 'from crypt import *; print(crypt("autopkgtest", mksalt(METHOD_CRYPT)))'
    lxc exec $CONTAINER -- useradd --password FJfXYBhFnX6xA --create-home $USER

    # install SSH
    lxc exec $CONTAINER -- eatmydata apt-get install -y openssh-server >/dev/null 2>&1

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

    if [ -n "$ENABLE_SUDO" ]; then
        lxc exec $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 -I $IMAGE
EOF
}

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

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

# parse options
eval set -- $(getopt -o "ksSn:I:c" -- "$@")
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 ;;
        -I)
            IMAGE="$2"; shift 2 ;;
        --)
            shift; break ;;
        *)
            echo "$0: unsupported option $1" >&2
            exit 1;;
    esac
done

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