From 339ebda83ab40ab7e5ab6c3e4998c0857169ff8d Mon Sep 17 00:00:00 2001 From: Andrew Reusch Date: Wed, 26 Aug 2020 11:09:20 -0700 Subject: [PATCH 1/2] improve docker/bash.sh workflow * properly quote command-line arguments * mount repo at $(pwd) by default; fixes problems when using git-worktree. --- docker/bash.sh | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/docker/bash.sh b/docker/bash.sh index 80f4a9577be1..0d6795eb648d 100755 --- a/docker/bash.sh +++ b/docker/bash.sh @@ -59,6 +59,17 @@ if [ "$#" -lt 1 ]; then exit -1 fi +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +WORKSPACE="$(pwd)" + +if [ "$1" == "--repo-mount-point" ]; then + shift + REPO_MOUNT_POINT="$1" + shift +else + REPO_MOUNT_POINT="${WORKSPACE}" +fi + DOCKER_IMAGE_NAME=$(lookup_image_spec "$1") if [ -z "${DOCKER_IMAGE_NAME}" ]; then DOCKER_IMAGE_NAME=("$1") @@ -70,7 +81,7 @@ if [ "$#" -eq 1 ]; then if [[ $(uname) == "Darwin" ]]; then # Docker's host networking driver isn't supported on macOS. # Use default bridge network and expose port for jupyter notebook. - CI_DOCKER_EXTRA_PARAMS+=( "${CI_DOCKER_EXTRA_PARAMS[@]}" "-p 8888:8888" ) + CI_DOCKER_EXTRA_PARAMS=( "${CI_DOCKER_EXTRA_PARAMS[@]}" "-p" "8888:8888" ) else CI_DOCKER_EXTRA_PARAMS+=( "${CI_DOCKER_EXTRA_PARAMS[@]}" "--net=host" ) fi @@ -83,9 +94,6 @@ if [ $interactive -eq 1 ]; then CI_DOCKER_EXTRA_PARAMS=( "${CI_DOCKER_EXTRA_PARAMS[@]}" -it ) fi -SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -WORKSPACE="$(pwd)" - # Use nvidia-docker if the container is GPU. if [[ ! -z $CUDA_VISIBLE_DEVICES ]]; then CUDA_ENV="-e CUDA_VISIBLE_DEVICES=${CUDA_VISIBLE_DEVICES}" @@ -160,12 +168,12 @@ fi ${DOCKER_BINARY} run --rm --pid=host\ ${DOCKER_DEVICES}\ ${WORKSPACE_VOLUMES}\ - -v ${WORKSPACE}:/workspace \ + -v ${WORKSPACE}:${REPO_MOUNT_POINT} \ -v ${SCRIPT_DIR}:/docker \ "${CI_DOCKER_MOUNT_CMD[@]}" \ "${EXTRA_MOUNTS[@]}" \ - -w /workspace \ - -e "CI_BUILD_HOME=/workspace" \ + -w "${REPO_MOUNT_POINT}" \ + -e "CI_BUILD_HOME=${REPO_MOUNT_POINT}" \ -e "CI_BUILD_USER=$(id -u -n)" \ -e "CI_BUILD_UID=$(id -u)" \ -e "CI_BUILD_GROUP=$(id -g -n)" \ From 51fd01e67115caae1c8b1dd38451cf969bc5f2e0 Mon Sep 17 00:00:00 2001 From: Andrew Reusch Date: Wed, 4 Aug 2021 17:23:00 -0700 Subject: [PATCH 2/2] address leandron comments --- docker/bash.sh | 108 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 78 insertions(+), 30 deletions(-) diff --git a/docker/bash.sh b/docker/bash.sh index 0d6795eb648d..9dfbf6d47d3f 100755 --- a/docker/bash.sh +++ b/docker/bash.sh @@ -22,7 +22,7 @@ # # Usage: bash.sh [-i] [--net=host] [--mount path] # -# Usage: docker/bash.sh +# Usage: docker/bash.sh [-i] [-- # Starts an interactive session # # Usage2: docker/bash.sh [-i] [COMMAND] @@ -34,40 +34,88 @@ set -e source "$(dirname $0)/dev_common.sh" || exit 2 -interactive=0 -if [ "$1" == "-i" ]; then - interactive=1 - shift -fi +# Parse command-line options +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +WORKSPACE="$(pwd)" -CI_DOCKER_EXTRA_PARAMS=( ) -if [[ "$1" == "--net=host" ]]; then - CI_DOCKER_EXTRA_PARAMS+=('--net=host') - shift 1 -fi +function print_usage() { + echo "Usage: ${BASH_SOURCE[0]} [options] [COMMAND]" + echo + echo "Description:" + echo " Run COMMAND in docker container named CONTAINER_NAME." + echo " When CONTAINER_NAME matches a ci_ variable in Jenkinsfile, use the revision specified there." + echo + echo "[options]" + echo " -i Run docker interactively and allocated pseudo-terminal (pass -it to docker)" + echo " --net=host Use docker host networking" + echo " --mount []: Mount into docker container (pass -v :)" + echo " --repo-mount-point " + echo " Mount tvm repo at . By default, mounts using the same path" + echo " as on the local filesystem." + echo + echo "Examples:" + echo + echo " docker/bash.sh tlcpack/ci-cpu:v0.01 tests/scripts/task_config_build_cpu.sh" + echo " - Run tests/scripts/task_config_build_cpu.sh non-interactively. This is the CI's" + echo " usage pattern." + echo + echo " docker/bash.sh -i ci_gpu bash" + echo " - Run bash interactively in the container revision assigned to ci_gpu in Jenkinsfile" + echo + echo " docker/bash.sh --net=host ci_gpu notebook path/to/jupyter/notebook.py" + echo " - Run Jupyter notebook using host networking to expose the port." + echo " NOTE: this method works only on linux." + echo + echo " docker/bash.sh ci_cpu" + echo " - Run bash in ci_cpu container (revision from Jenkinsfile) and either use host " + echo " networking (linux/windows) or forward port 8080 (OS X) to enable Jupyter." +} -# Mount external directory to the docker +interactive=0 +CI_DOCKER_EXTRA_PARAMS=( ) CI_DOCKER_MOUNT_CMD=( ) -if [ "$1" == "--mount" ]; then - shift 1 - CI_DOCKER_MOUNT_CMD=( -v "$1:$1" ) - shift 1 -fi +REPO_MOUNT_POINT="${WORKSPACE}" + +while [ true ]; do + case "$1" in + "-i") + interactive=1 + shift + ;; + "--net=host") + CI_DOCKER_EXTRA_PARAMS+=('--net=host') + shift + ;; + "--mount") + shift + CI_DOCKER_MOUNT_CMD=( -v "$1:$1" ) + shift + ;; + "--repo-mount-point") + shift + REPO_MOUNT_POINT="$1" + shift + ;; + "--help") + print_usage + exit 2 + ;; + "--") + shift + break; + ;; + "-*") + echo "$0: unrecognized argument: $1" >&2 + exit 2 + ;; + *) + break; + ;; + esac +done if [ "$#" -lt 1 ]; then - echo "Usage: docker/bash.sh [-i] [--net=host] [COMMAND]" - exit -1 -fi - -SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -WORKSPACE="$(pwd)" - -if [ "$1" == "--repo-mount-point" ]; then - shift - REPO_MOUNT_POINT="$1" - shift -else - REPO_MOUNT_POINT="${WORKSPACE}" + exit 2 fi DOCKER_IMAGE_NAME=$(lookup_image_spec "$1")