aboutsummaryrefslogtreecommitdiff
#!/bin/sh
set -eu

usage() {
	cat <<-'EOF'
		Usage:
		  vm-check.sh [-x] -t TYPE -H HOST -r REMOTE [-- ARGS]
		  vm-check.sh -h
	EOF
}

help() {
	cat <<-'EOF'


		Options:
		  -x            shutdown the VM before running checks
		  -t TYPE       the type of VM being tested
		  -H HOST       the name of the host to SSH into
		  -r REMOTE     the base address of the remote package-repository
		  -h, --help    show this message



		Run end-to-end tests of the package repository.
	EOF
}


for flag in "$@"; do
	case "$flag" in
		--)
			break
			;;
		--help)
			usage
			help
			exit
			;;
		*)
			;;
	esac
done

SHUTDOWN=false
while getopts 'xt:H:r:h' flag; do
	case "$flag" in
		x)
			SHUTDOWN=true
			;;
		t)
			TYPE="$OPTARG"
			;;
		H)
			HOST="$OPTARG"
			;;
		r)
			REMOTE="$OPTARG"
			;;
		h)
			usage
			help
			exit
			;;
		*)
			usage >&2
			exit 2
			;;
	esac
done
shift $((OPTIND - 1))

eval "$(assert-arg -- "${TYPE:-}"   '-t TYPE')"
eval "$(assert-arg -- "${HOST:-}"   '-H HOST')"
eval "$(assert-arg -- "${REMOTE:-}" '-r REMOTE')"


SEP="$(color -c blacki ':')"

pre() {
        # Same as:
        # sed -u "s|^|[$CMD]: |"
        # but the "-u" option is not POSIX
        IFS=''
        while read -r line; do
                printf '%s%s\n' "$1" "$line"
        done
}

pre_guest() {
	pre "$(color -c green "$HOST")$SEP$(color -c yellow "$TYPE")$SEP "
}

pre_host() {
	pre "$(color -c blueb "$0")$SEP "
}

STATUS_F="$(mkstemp)"
shutdown() {
	STATUS="$(cat "$STATUS_F" || echo 1)"
	rm -f "$STATUS_F"
	if [ "$SHUTDOWN" = true ]; then
		vm down "$HOST" 2>&1 | pre_host
	fi
	exit "$STATUS"
}
trap shutdown EXIT

{

	{
		if [ "$SHUTDOWN" = true ]; then
			vm down "$HOST"
		fi
		vm up "$HOST"
		untill -m5 -n5 ssh "$HOST" uptime
		scp tests/checks/"$TYPE".sh "$HOST":check.sh
	} 2>&1 | pre_host

	{
		STATUS=0
		# shellcheck disable=2029
		ssh "$HOST" sh check.sh "$@" "$REMOTE" || STATUS=$?
		printf '%s' "$STATUS" > "$STATUS_F"
	} 2>&1 | pre_guest

} | ts -s '%.s'