apollo_docker.sh 5.2 KB
Newer Older
D
Dong Li 已提交
1 2
#!/usr/bin/env bash

3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
###############################################################################
# Copyright 2017 The Apollo Authors. All Rights Reserved.
#
# Licensed 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.
###############################################################################
D
Dong Li 已提交
18 19 20 21

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd "${DIR}"

L
Lei Wang 已提交
22 23
# the machine type, currently support x86_64, aarch64
MACHINE_ARCH=$(uname -m)
D
Dong Li 已提交
24 25 26 27
source ${DIR}/scripts/apollo_base.sh

TIME=$(date  +%Y%m%d_%H%M)
if [ -z "${DOCKER_REPO}" ]; then
L
Lei Wang 已提交
28
    DOCKER_REPO=apolloauto/apollo
D
Dong Li 已提交
29 30 31
fi

function print_usage() {
L
Lei Wang 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
  RED='\033[0;31m'
  BLUE='\033[0;34m'
  BOLD='\033[1m'
  NONE='\033[0m'

  echo -e "\n${RED}Usage${NONE}:
  .${BOLD}/apollo_docker.sh${NONE} [OPTION]"

  echo -e "\n${RED}Options${NONE}:
  ${BLUE}build${NONE}: run build only
  ${BLUE}build_opt${NONE}: build optimized binary for the code
  ${BLUE}build_gpu${NONE}: run build only with Caffe GPU mode support
  ${BLUE}build_opt_gpu${NONE}: build optimized binary with Caffe GPU mode support
  ${BLUE}build_fe${NONE}: compile frontend javascript code, this requires all the node_modules to be installed already
  ${BLUE}buildify${NONE}: fix style of BUILD files
  ${BLUE}check${NONE}: run build/lint/test, please make sure it passes before checking in new code
  ${BLUE}clean${NONE}: run Bazel clean
  ${BLUE}config${NONE}: run configurator tool
  ${BLUE}coverage${NONE}: generate test coverage report
  ${BLUE}doc${NONE}: generate doxygen document
  ${BLUE}lint${NONE}: run code style check
  ${BLUE}usage${NONE}: print this menu
  ${BLUE}release${NONE}: build release version
  ${BLUE}test${NONE}: run all unit tests
  ${BLUE}version${NONE}: display current commit and date
  ${BLUE}push${NONE}: pushes the images to Docker hub
58 59
  ${BLUE}gen${NONE}: generate a docker release image
  ${BLUE}ota_gen${NONE}: generate a ota docker release image
L
Lei Wang 已提交
60
  "
D
Dong Li 已提交
61 62 63
}

function start_build_docker() {
64
  docker ps --format "{{.Names}}" | grep apollo_dev_$USER 1>/dev/null 2>&1
65 66
  if [ $? != 0 ]; then    
    # If Google is reachable, we fetch the docker image directly. 
67
    if ping -q -c 1 -W 1 www.google.com 1>/dev/null 2>&1; then
68 69
      opt=""
    else
70 71
      ping -q -c 1 -W 1 www.baidu.com 1>/dev/null 2>&1
      # If Baidu is unreachable, we use local images. 
X
Xiangquan Xiao 已提交
72
      if [ $? -ne 0 ]; then
73 74
        opt="-l"
      fi
75 76
    fi
    bash docker/scripts/dev_start.sh ${opt}
D
Dong Li 已提交
77 78 79 80
  fi
}

function gen_docker() {
L
Lei Wang 已提交
81
  IMG="apolloauto/apollo:run-${MACHINE_ARCH}-20181017_1330"
82 83 84 85 86 87 88 89
  RELEASE_DIR=${HOME}/.cache/apollo_release
  APOLLO_DIR="${RELEASE_DIR}/apollo"

  if [ ! -d "${APOLLO_DIR}" ]; then
    echo "Release directory does not exist!"
    exit 1
  fi

L
Lei Wang 已提交
90 91
  RELEASE_NAME="${DOCKER_REPO}:release-${MACHINE_ARCH}-${TIME}"
  DEFAULT_NAME="${DOCKER_REPO}:release-${MACHINE_ARCH}-latest"
D
Dong Li 已提交
92
  docker pull $IMG
93 94
  echo "time : ${TIME}" >> ${APOLLO_DIR}/meta.ini
  echo "tag: ${RELEASE_NAME}" >> ${APOLLO_DIR}/meta.ini
D
Dong Li 已提交
95

L
Lei Wang 已提交
96 97 98 99 100
  docker ps -a --format "{{.Names}}" | grep 'apollo_release' 1>/dev/null
  if [ $? == 0 ];then
    docker stop apollo_release 1>/dev/null
    docker rm -f apollo_release 1>/dev/null
  fi
D
Dong Li 已提交
101 102 103 104
  docker run -it \
      -d \
      --name apollo_release \
      --net host \
105
      -v $HOME/.cache:/root/mnt \
D
Dong Li 已提交
106 107 108
      -w /apollo \
      "$IMG"

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
  if [ "$OTA_RELEASE" != "1" ]; then
    docker exec apollo_release bash -c 'cp -Lr /root/mnt/apollo_release/apollo /'
  else
    RELEASE_TGZ="apollo_release.tar.gz"
    SEC_RELEASE_TGZ="sec_apollo_release.tar.gz"

    if [ -e "$HOME/.cache/$RELEASE_TGZ" ]; then
      rm $HOME/.cache/$RELEASE_TGZ
    fi

    if [ -e "$HOME/.cache/$SEC_RELEASE_TGZ" ]; then
      rm $HOME/.cache/$SEC_RELEASE_TGZ
    fi

    # generate security release package
    tar czf $HOME/.cache/$RELEASE_TGZ -C $HOME/.cache apollo_release
    python modules/tools/ota/create_sec_package.py
    docker exec apollo_release cp /root/mnt/${SEC_RELEASE_TGZ} /root
  fi

D
Dong Li 已提交
129 130
  CONTAINER_ID=$(docker ps | grep apollo_release | awk '{print $1}')
  docker commit "$CONTAINER_ID" "$DEFAULT_NAME"
131
  docker tag "$DEFAULT_NAME" "$RELEASE_NAME"
D
Dong Li 已提交
132 133 134 135
  docker stop "$CONTAINER_ID"
}

function push() {
136 137 138
  DEFAULT_NAME="${DOCKER_REPO}:release-${MACHINE_ARCH}-latest"
  LATEST_IMAG_ID=$(docker images --format "{{.Repository}}:{{.Tag}} {{.ID}}" | grep "${DEFAULT_NAME}" | cut -d " " -f 2)
  RELEASE_NAME=$(docker images --format "{{.Repository}}:{{.Tag}} {{.ID}}" | grep ${LATEST_IMAG_ID}| grep -v "${DEFAULT_NAME}" | cut -d " " -f 1)
D
Dong Li 已提交
139 140 141 142
  docker push "$DEFAULT_NAME"
  docker push "$RELEASE_NAME"
}

L
Lei Wang 已提交
143
if [ $# == 0 ];then
D
Dong Li 已提交
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
    print_usage
    exit 1
fi

start_build_docker

case $1 in
  print_usage)
    print_usage
    ;;
  push)
    push
    ;;
  gen)
    gen_docker
    ;;
160 161 162 163
  ota_gen)
    OTA_RELEASE=1
    gen_docker
    ;;
D
Dong Li 已提交
164
  *)
165
    docker exec -u $USER apollo_dev_$USER bash -c "./apollo.sh $@"
D
Dong Li 已提交
166 167
    ;;
esac