ota.sh 2.9 KB
Newer Older
L
Lei Wang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#!/usr/bin/env bash

###############################################################################
# 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.
###############################################################################

APOLLO_ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." && pwd )"
20
CACHE_DIR="${APOLLO_ROOT}/.cache"
L
Lei Wang 已提交
21 22 23 24

function update() {
  UPDATE_TAG=$(python ${APOLLO_ROOT}/modules/tools/ota/query_client.py)
  if [ "$?" != "0" ]; then
L
Lei Wang 已提交
25 26
    echo $UPDATE_TAG
    exit 1
L
Lei Wang 已提交
27 28 29 30 31 32
  fi

  tip="Type 'y' or 'Y' to start upgrade, or type any other key to exit"
  echo $tip
  read -n 1 user_agreed
  if [ "$user_agreed" != "y" ] && [ "$user_agreed" != "Y" ]; then
L
Lei Wang 已提交
33
    exit 1
L
Lei Wang 已提交
34
  fi
35 36
  cp ${APOLLO_ROOT}/scripts/ota.sh "${CACHE_DIR}"
  ssh $DOCKER_USER@localhost  bash ${CACHE_DIR}/ota.sh download $UPDATE_TAG
L
Lei Wang 已提交
37 38
  python ${APOLLO_ROOT}/modules/tools/ota/verify_client.py
  if [ "$?" != "0" ]; then
L
Lei Wang 已提交
39
    exit 1
L
Lei Wang 已提交
40 41
  fi

42 43
  [ -e "${CACHE_DIR}/apollo_release" ] && rm -rf "${CACHE_DIR}/apollo_release"
  tar xzf ${CACHE_DIR}/apollo_release.tar.gz -C ${CACHE_DIR}
L
Lei Wang 已提交
44
  NEW_TAG="${UPDATE_TAG}-local"
L
Lei Wang 已提交
45

46
  ssh $DOCKER_USER@localhost  bash ${CACHE_DIR}/ota.sh setup $NEW_TAG
L
Lei Wang 已提交
47
  python ${APOLLO_ROOT}/modules/tools/ota/update_client.py ${UPDATE_TAG}
L
Lei Wang 已提交
48 49 50
}

function clean() {
51 52 53 54
  rm -rf ${CACHE_DIR}/apollo_update
  rm -rf ${CACHE_DIR}/apollo_release.tar.gz
  rm -rf ${CACHE_DIR}/sec_apollo_release.tar.gz
  rm -rf ${CACHE_DIR}/ota.sh
L
Lei Wang 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
  docker stop test_container 1>/dev/null
  docker rm test_container 1>/dev/null
}

function setup() {
  docker exec test_container cp -Lr /root/mnt/apollo_release/apollo /
  docker commit test_container $1
  echo "Please restart release docker with new release image: $1"
  clean
}

function download() {
  UPDATE_TAG=$1
  docker pull $UPDATE_TAG
  if [ "$?" != "0" ]; then
L
Lei Wang 已提交
70
    echo "Downloading fails!"
L
Lei Wang 已提交
71 72 73 74 75 76 77 78 79
    exit 1
  else
    echo "New release image has been downloaded!"
  fi
  docker ps -a --format "{{.Names}}" | grep 'test_container' 1>/dev/null
  if [ $? == 0 ]; then
      docker stop test_container 1>/dev/null
      docker rm -f test_container 1>/dev/null
  fi
80
  docker run -d -it --name test_container -v ${CACHE_DIR}:/root/mnt $UPDATE_TAG
L
Lei Wang 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
  docker exec test_container cp /root/sec_apollo_release.tar.gz /root/mnt
}

case $1 in
  update)
    update
    ;;
  download)
    download $2
    ;;
  setup)
    setup $2
    ;;
  *)
    echo "Usage: ota.sh update"
    ;;
esac