1#!/bin/bash
2
3#
4# DOCKER_CMD should be either `docker` or `podman`.
5#
6# if using (rootless) podman, remember to set /etc/subuid
7# and /etc/subgid.
8#
9docker_cmd=${DOCKER_CMD:-"docker"}
10[ "$DOCKER_CMD" = "podman" ] && userns_podman="--userns=keep-id"
11
12einfo() {
13    echo "$*" >&2
14}
15
16die() {
17    echo "$*" >&2
18    exit 1
19}
20
21#
22# The caller is expected to override the CONTAINER environment
23# variable with the container they wish to launch.
24#
25BASE="registry.gitlab.com/xen-project/xen"
26case "_${CONTAINER}" in
27    _archlinux|_arch) CONTAINER="${BASE}/archlinux:current" ;;
28    _centos6) CONTAINER="${BASE}/centos:6" ;;
29    _centos7) CONTAINER="${BASE}/centos:7" ;;
30    _centos72) CONTAINER="${BASE}/centos:7.2" ;;
31    _fedora) CONTAINER="${BASE}/fedora:29";;
32    _jessie) CONTAINER="${BASE}/debian:jessie" ;;
33    _stretch|_) CONTAINER="${BASE}/debian:stretch" ;;
34    _unstable|_) CONTAINER="${BASE}/debian:unstable" ;;
35    _trusty) CONTAINER="${BASE}/ubuntu:trusty" ;;
36    _xenial) CONTAINER="${BASE}/ubuntu:xenial" ;;
37    _opensuse-leap|_leap) CONTAINER="${BASE}/suse:opensuse-leap" ;;
38    _opensuse-tumbleweed|_tumbleweed) CONTAINER="${BASE}/suse:opensuse-tumbleweed" ;;
39esac
40
41# Use this variable to control whether root should be used
42case "_${CONTAINER_UID0}" in
43    _1)   userarg= ;;
44    _0|_) userarg="-u $(id -u) $userns_podman" ;;
45esac
46
47# Save the commands for future use
48cmd=$@
49
50# If no command was specified, just drop us into a shell if we're interactive
51[ $# -eq 0 ] && tty -s && cmd="/bin/bash"
52
53# Are we in an interactive terminal?
54tty -s && termint=t
55
56#
57# Fetch the latest version of the container in hub.docker.com,
58# unless it's a newly created local copy.
59#
60if [[ "_${CONTAINER_NO_PULL}" != "_1" ]]; then
61    einfo "*** Ensuring ${CONTAINER} is up to date"
62    ${docker_cmd} pull ${CONTAINER} > /dev/null ||     \
63        die "Failed to update container"
64fi
65
66if hash greadlink > /dev/null 2>&1; then
67    READLINK=greadlink
68elif [[ $(uname -s) == "Darwin" ]]; then
69    echo "Unable to forward SSH agent without coreutils installed"
70    unset SSH_AUTH_SOCK
71else
72    READLINK=readlink
73fi
74
75# Ensure we've got what we need for SSH_AUTH_SOCK
76if [[ -n ${SSH_AUTH_SOCK} ]]; then
77    fullpath_sock=$(${READLINK} -f ${SSH_AUTH_SOCK} 2> /dev/null)
78    if [ $? -ne 0 ]; then
79        echo "Invalid SSH_AUTH_SOCK: ${SSH_AUTH_SOCK}"
80        unset SSH_AUTH_SOCK
81    else
82        SSH_AUTH_DIR=$(dirname ${fullpath_sock})
83        SSH_AUTH_NAME=$(basename ${fullpath_sock})
84    fi
85fi
86
87# Figure out the base of what we want as our sources
88# by using the top of the git repo
89if [[ -z ${CONTAINER_PATH} ]]; then
90    CONTAINER_PATH=$(git rev-parse --show-toplevel)
91fi
92
93# Kick off Docker
94einfo "*** Launching container ..."
95exec ${docker_cmd} run \
96    ${userarg} \
97    ${SSH_AUTH_SOCK:+-e SSH_AUTH_SOCK="/tmp/ssh-agent/${SSH_AUTH_NAME}"} \
98    -v "${CONTAINER_PATH}":/build:rw \
99    -v "${HOME}/.ssh":/root/.ssh:ro \
100    ${SSH_AUTH_DIR:+-v "${SSH_AUTH_DIR}":/tmp/ssh-agent} \
101    ${XEN_CONFIG_EXPERT:+-e XEN_CONFIG_EXPERT=${XEN_CONFIG_EXPERT}} \
102    ${CONTAINER_ARGS} \
103    -${termint}i --rm -- \
104    ${CONTAINER} \
105    ${cmd}
106