github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/images/bootstrap/runner.sh (about)

     1  #!/usr/bin/env bash
     2  # Copyright 2018 The Kubernetes Authors.
     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  # generic runner script, handles DIND, bazelrc for caching, etc.
    17  
    18  # Check if the job has opted-in to bazel remote caching and if so generate 
    19  # .bazelrc entries pointing to the remote cache
    20  export BAZEL_REMOTE_CACHE_ENABLED=${BAZEL_REMOTE_CACHE_ENABLED:-false}
    21  if [[ "${BAZEL_REMOTE_CACHE_ENABLED}" == "true" ]]; then
    22      echo "Bazel remote cache is enabled, generating .bazelrcs ..."
    23      # if we have a test-infra checkout (because bootstrap), use that since
    24      # it is newer and this is probably k/k so we can push fixes faster this way
    25      # otherwise run the one baked into the image
    26      # TODO(bentheelder): someday only support the pod-utils
    27      if [[ -d "./test-infra" ]]; then
    28          ./test-infra/images/bootstrap/create_bazel_cache_rcs.sh
    29      else
    30          /usr/local/bin/create_bazel_cache_rcs.sh
    31      fi
    32  fi
    33  
    34  
    35  # used by cleanup_dind to ensure binfmt_misc entries are not persisted
    36  # TODO(bentheelder): consider moving *all* cleanup into a more robust program
    37  cleanup_binfmt_misc() {
    38      # make sure the vfs is mounted
    39      # TODO(bentheelder): if this logic is moved out and made more general
    40      # we need to check that the host actually has binfmt_misc support first.
    41      if [ ! -f /proc/sys/fs/binfmt_misc/status ]; then
    42          mount binfmt_misc -t binfmt_misc /proc/sys/fs/binfmt_misc
    43      fi
    44      # https://www.kernel.org/doc/html/v4.13/admin-guide/binfmt-misc.html
    45      # You can remove one entry or all entries by echoing -1 
    46      # to /proc/.../the_name or /proc/sys/fs/binfmt_misc/status.
    47      echo -1 >/proc/sys/fs/binfmt_misc/status
    48      # list entries
    49      ls -al /proc/sys/fs/binfmt_misc
    50  }
    51  
    52  # runs custom docker data root cleanup binary and debugs remaining resources
    53  cleanup_dind() {
    54      barnacle || true
    55      # list what images and volumes remain
    56      echo "Remaining docker images and volumes are:"
    57      docker images --all || true
    58      docker volume ls || true
    59      # cleanup binfmt_misc
    60      echo "Cleaning up binfmt_misc ..."
    61      # note: we run this in a subshell so we can trace it for now
    62      (set -x; cleanup_binfmt_misc || true)
    63  }
    64  
    65  # Check if the job has opted-in to docker-in-docker availability.
    66  export DOCKER_IN_DOCKER_ENABLED=${DOCKER_IN_DOCKER_ENABLED:-false}
    67  if [[ "${DOCKER_IN_DOCKER_ENABLED}" == "true" ]]; then
    68      echo "Docker in Docker enabled, initializing..."
    69      printf '=%.0s' {1..80}; echo
    70      # If we have opted in to docker in docker, start the docker daemon,
    71      service docker start
    72      # the service can be started but the docker socket not ready, wait for ready
    73      WAIT_N=0
    74      MAX_WAIT=5
    75      while true; do
    76          # docker ps -q should only work if the daemon is ready
    77          docker ps -q > /dev/null 2>&1 && break
    78          if [[ ${WAIT_N} -lt ${MAX_WAIT} ]]; then
    79              WAIT_N=$((WAIT_N+1))
    80              echo "Waiting for docker to be ready, sleeping for ${WAIT_N} seconds."
    81              sleep ${WAIT_N}
    82          else
    83              echo "Reached maximum attempts, not waiting any longer..."
    84              break
    85          fi
    86      done
    87      cleanup_dind
    88      printf '=%.0s' {1..80}; echo
    89      echo "Done setting up docker in docker."
    90  fi
    91  
    92  # disable error exit so we can run post-command cleanup
    93  set +o errexit
    94  
    95  # add $GOPATH/bin to $PATH
    96  export PATH=$GOPATH/bin:$PATH
    97  # Authenticate gcloud, allow failures
    98  if [[ -n "${GOOGLE_APPLICATION_CREDENTIALS:-}" ]]; then
    99    gcloud auth activate-service-account --key-file="${GOOGLE_APPLICATION_CREDENTIALS}" || true
   100  fi
   101  
   102  # Use a reproducible build date based on the most recent git commit timestamp.
   103  export SOURCE_DATE_EPOCH=$(git log -1 --pretty=%ct || true)
   104  
   105  # actually start bootstrap and the job
   106  "$@"
   107  EXIT_VALUE=$?
   108  
   109  # cleanup after job
   110  if [[ "${DOCKER_IN_DOCKER_ENABLED}" == "true" ]]; then
   111      echo "Cleaning up after docker in docker."
   112      printf '=%.0s' {1..80}; echo
   113      cleanup_dind
   114      printf '=%.0s' {1..80}; echo
   115      echo "Done cleaning up after docker in docker."
   116  fi
   117  
   118  # preserve exit value from job / bootstrap
   119  exit ${EXIT_VALUE}