Home | History | Annotate | Download | only in builds
      1 #!/usr/bin/env bash
      2 # Copyright 2016 The TensorFlow Authors. All Rights Reserved.
      3 #
      4 # Licensed under the Apache License, Version 2.0 (the "License");
      5 # you may not use this file except in compliance with the License.
      6 # You may obtain a copy of the License at
      7 #
      8 #     http://www.apache.org/licenses/LICENSE-2.0
      9 #
     10 # Unless required by applicable law or agreed to in writing, software
     11 # distributed under the License is distributed on an "AS IS" BASIS,
     12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13 # See the License for the specific language governing permissions and
     14 # limitations under the License.
     15 # ==============================================================================
     16 #
     17 # Build and test TensorFlow docker images.
     18 # The tests include Python unit tests-on-install and tutorial tests.
     19 #
     20 # Usage: docker_test.sh <IMAGE_TYPE> <TAG> <WHL_PATH>
     21 # Arguments:
     22 #   IMAGE_TYPE : Type of the image: (CPU|GPU)
     23 #   TAG        : Docker image tag
     24 #   WHL_PATH   : Path to the whl file to be installed inside the docker image
     25 #
     26 #   e.g.: docker_test.sh CPU someone/tensorflow:0.8.0 pip_test/whl/tensorflow-0.8.0-cp27-none-linux_x86_64.whl
     27 #
     28 
     29 # Helper functions
     30 # Exit after a failure
     31 die() {
     32   echo $@
     33   exit 1
     34 }
     35 
     36 # Convert to lower case
     37 to_lower () {
     38   echo "$1" | tr '[:upper:]' '[:lower:]'
     39 }
     40 
     41 
     42 # Helper function to traverse directories up until given file is found.
     43 function upsearch () {
     44   test / == "$PWD" && return || \
     45       test -e "$1" && echo "$PWD" && return || \
     46       cd .. && upsearch "$1"
     47 }
     48 
     49 
     50 # Verify command line argument
     51 if [[ $# != "3" ]]; then
     52   die "Usage: $(basename $0) <IMAGE_TYPE> <TAG> <WHL_PATH>"
     53 fi
     54 IMAGE_TYPE=$(to_lower "$1")
     55 DOCKER_IMG_TAG=$2
     56 WHL_PATH=$3
     57 
     58 # Verify image type
     59 if [[ "${IMAGE_TYPE}" == "cpu" ]]; then
     60   DOCKERFILE="tensorflow/tools/docker/Dockerfile"
     61 elif [[ "${IMAGE_TYPE}" == "gpu" ]]; then
     62   DOCKERFILE="tensorflow/tools/docker/Dockerfile.gpu"
     63 else
     64   die "Unrecognized image type: $1"
     65 fi
     66 
     67 # Verify docker binary existence
     68 if [[ -z $(which docker) ]]; then
     69   die "FAILED: docker binary unavailable"
     70 fi
     71 
     72 # Locate the base directory
     73 BASE_DIR=$(upsearch "${DOCKERFILE}")
     74 if [[ -z "${BASE_DIR}" ]]; then
     75   die "FAILED: Unable to find the base directory where the dockerfile "\
     76 "${DOCKERFFILE} resides"
     77 fi
     78 echo "Base directory: ${BASE_DIR}"
     79 
     80 pushd ${BASE_DIR} > /dev/null
     81 
     82 # Build docker image
     83 DOCKERFILE_PATH="${BASE_DIR}/${DOCKERFILE}"
     84 DOCKERFILE_DIR="$(dirname ${DOCKERFILE_PATH})"
     85 
     86 # Check to make sure that the whl file exists
     87 test -f ${WHL_PATH} || \
     88     die "whl file does not exist: ${WHL_PATH}"
     89 
     90 TMP_WHL_DIR="${DOCKERFILE_DIR}/whl"
     91 mkdir -p "${TMP_WHL_DIR}"
     92 cp "${WHL_PATH}" "${TMP_WHL_DIR}/" || \
     93     die "FAILED to copy whl file from ${WHL_PATH} to ${TMP_WHL_DIR}/"
     94 
     95 docker build -t "${DOCKER_IMG_TAG}" -f "${DOCKERFILE_PATH}" \
     96 "${DOCKERFILE_DIR}" || \
     97     die "FAILED to build docker image from Dockerfile ${DOCKERFILE_PATH}"
     98 
     99 # Clean up
    100 rm -rf "${TMP_WHL_DIR}" || \
    101     die "Failed to remove temporary directory ${TMP_WHL_DIR}"
    102 
    103 
    104 # Add extra params for cuda devices and libraries for GPU container.
    105 if [ "${IMAGE_TYPE}" == "gpu" ]; then
    106   devices=$(\ls /dev/nvidia* | xargs -I{} echo '--device {}:{}')
    107   libs=$(\ls /usr/lib/x86_64-linux-gnu/libcuda.* | xargs -I{} echo '-v {}:{}')
    108   GPU_EXTRA_PARAMS="${devices} ${libs}"
    109 else
    110   GPU_EXTRA_PARAMS=""
    111 fi
    112 
    113 # Run docker image with source directory mapped
    114 docker run -v ${BASE_DIR}:/tensorflow-src -w /tensorflow-src \
    115 ${GPU_EXTRA_PARAMS} \
    116 "${DOCKER_IMG_TAG}" \
    117 /bin/bash -c "tensorflow/tools/ci_build/builds/run_pip_tests.sh && "\
    118 "tensorflow/tools/ci_build/builds/test_tutorials.sh && "\
    119 "tensorflow/tools/ci_bukld/builds/integration_tests.sh"
    120 
    121 RESULT=$?
    122 
    123 popd > /dev/null
    124 if [[ ${RESULT} == 0 ]]; then
    125   echo "SUCCESS: Built and tested docker image: ${DOCKER_IMG_TAG}"
    126 else
    127   die "FAILED to build and test docker image: ${DOCKER_IMG_TAG}"
    128 fi
    129