bash.sh 2.92 KB
Newer Older
1
#!/usr/bin/env bash
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

20 21 22 23
#
# Start a bash, mount /workspace to be current directory.
#
# Usage: docker/bash.sh <CONTAINER_NAME>
24 25 26 27
#     Starts an interactive session
#
# Usage2: docker/bash.sh <CONTAINER_NAME> [COMMAND]
#     Execute command in the docker image, non-interactive
28 29
#
if [ "$#" -lt 1 ]; then
30
    echo "Usage: docker/bash.sh <CONTAINER_NAME> [COMMAND]"
31 32 33
    exit -1
fi

34 35 36 37
DOCKER_IMAGE_NAME=("$1")

if [ "$#" -eq 1 ]; then
    COMMAND="bash"
38 39 40 41 42 43 44
    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=("-it -p 8888:8888")
    else
        CI_DOCKER_EXTRA_PARAMS=("-it --net=host")
    fi
45 46 47 48 49
else
    shift 1
    COMMAND=("$@")
fi

50 51 52 53
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
WORKSPACE="$(pwd)"

# Use nvidia-docker if the container is GPU.
54 55 56 57 58 59
if [[ ! -z $CUDA_VISIBLE_DEVICES ]]; then
    CUDA_ENV="-e CUDA_VISIBLE_DEVICES=${CUDA_VISIBLE_DEVICES}"
else
    CUDA_ENV=""
fi

60
if [[ "${DOCKER_IMAGE_NAME}" == *"gpu"* ]]; then
61
    if ! type "nvidia-docker" 1> /dev/null 2> /dev/null
62 63 64 65 66 67 68 69 70 71
    then
        DOCKER_BINARY="docker"
        CUDA_ENV=" --gpus all "${CUDA_ENV}
    else
        DOCKER_BINARY="nvidia-docker"
    fi
else
    DOCKER_BINARY="docker"
fi

72 73
# Print arguments.
echo "WORKSPACE: ${WORKSPACE}"
74
echo "DOCKER CONTAINER NAME: ${DOCKER_IMAGE_NAME}"
75 76
echo ""

77 78
echo "Running '${COMMAND[@]}' inside ${DOCKER_IMAGE_NAME}..."

79 80 81
# By default we cleanup - remove the container once it finish running (--rm)
# and share the PID namespace (--pid=host) so the process inside does not have
# pid 1 and SIGKILL is propagated to the process inside (jenkins can kill it).
82
${DOCKER_BINARY} run --rm --pid=host\
83 84 85 86 87 88 89 90
    -v ${WORKSPACE}:/workspace \
    -v ${SCRIPT_DIR}:/docker \
    -w /workspace \
    -e "CI_BUILD_HOME=/workspace" \
    -e "CI_BUILD_USER=$(id -u -n)" \
    -e "CI_BUILD_UID=$(id -u)" \
    -e "CI_BUILD_GROUP=$(id -g -n)" \
    -e "CI_BUILD_GID=$(id -g)" \
91 92
    -e "PYTHONPATH=python:topi/python"\
    ${CUDA_ENV}\
93
    ${CI_DOCKER_EXTRA_PARAMS[@]} \
94
    ${DOCKER_IMAGE_NAME}\
95
    bash --login /docker/with_the_same_user \
96
    ${COMMAND[@]}