github.com/google/cloudprober@v0.11.3/tools/cloudprober_startup.sh (about)

     1  #!/bin/bash -eu
     2  #
     3  # Copyright 2017 Google Inc.
     4  #
     5  # Licensed under the Apache License, Version 2.0 (the "License");
     6  # you may not use this file except in compliance with the License.
     7  # You may obtain a copy of the License at
     8  #
     9  #      http://www.apache.org/licenses/LICENSE-2.0
    10  #
    11  # Unless required by applicable law or agreed to in writing, software
    12  # distributed under the License is distributed on an "AS IS" BASIS,
    13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  # See the License for the specific language governing permissions and
    15  # limitations under the License.
    16  
    17  # This startup_script makes it easy to run cloudprober inside a docker
    18  # image.
    19  while getopts 'i:j:v:' flag; do
    20    case "$flag" in
    21      i) IMAGE=${OPTARG} ;;
    22      j) JOB=${OPTARG} ;;
    23      v) VERSION=${OPTARG} ;;
    24    esac
    25  done
    26  
    27  shift $(($OPTIND - 1))
    28  cmd=$1
    29  if [ "$cmd" != "start" ]; then
    30    # First execution by cloud-init. Make the script accessible to all.
    31    chmod a+rx "$0"
    32    exit
    33  fi
    34  
    35  if [ -z "${JOB}" ]; then
    36    echo "-j <job_name> is a required parameter"
    37    exit 1
    38  fi
    39  
    40  [[ -f "/etc/default/${JOB}" ]] && . "/etc/default/${JOB}"
    41  
    42  VERSION=${VERSION:-latest}
    43  KERNEL_VERSION=$(uname -r)
    44  GOOGLE_RELEASE=$(grep GOOGLE_RELEASE /etc/lsb-release|cut -d"=" -f2)
    45  
    46  # Make sure that when this script exits, all child processes are exited as well
    47  # Sending a SIGHUP/SIGTERM to bash kills the shell but leaves the child processes
    48  # running. We don't want that behavior.
    49  #   "kill -- -$$" sends a SIGTERM to the process group.
    50  #   "trap - SIGTERM" removes the trap for SIGTERM to avoid recursion.
    51  trap "trap - SIGTERM && kill -- -$$" SIGINT SIGTERM EXIT
    52  # Get credentials to fetch docker image from gcr.io
    53  . /usr/share/google/dockercfg_update.sh
    54  docker pull "${IMAGE}:${VERSION}"
    55  DIGEST=$(docker inspect --format "{{.Id}}" "${IMAGE}:${VERSION}")
    56  VARS="kernel=${KERNEL_VERSION},google_release=${GOOGLE_RELEASE}"
    57  VARS="${VARS},${JOB}_tag=${VERSION},${JOB}_version=${DIGEST:0:12}"
    58  
    59  docker run -e "SYSVARS=${VARS}" --env-file <(env | grep CLOUDPROBER_)  \
    60    --net host --privileged -v /tmp:/tmp "${IMAGE}:${VERSION}"