agones.dev/agones@v1.53.0/build/build-image/cache/restore_cache (about)

     1  #!/usr/bin/env bash
     2  
     3  # Copyright 2023 Google LLC All Rights Reserved.
     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  BUCKET=""
    18  SRC_DIR="."
    19  KEY="cache"
    20  KEY_FALLBACK_PATTERN=""
    21  PATHS=()
    22  
    23  function print_usage {
    24    echo "Usage: $0 --bucket=gs://my-cache-bucket --key=cache-key"
    25    echo
    26    echo "Saves the specified paths to a cache file located in the out directory."
    27    echo
    28    echo "  -b,  --bucket       The cloud storage bucket to download the cache from. [optional]"
    29    echo "  -s,  --src          The local directory in which the cache is stored. [optional]"
    30    echo "  -k,  --key          The cache key used for this cache file. [optional]"
    31    echo "  -kf, --key_fallback The cache key fallback pattern to be used if exact cache key is not found. It will try to find a file based on the pattern specified and will retrieve the most recent one. [optional]"
    32    echo 
    33  }
    34  
    35  for i in "$@"; do
    36    case $i in
    37      -b=*|--bucket=*)
    38        BUCKET="${i#*=}"
    39        shift
    40        ;;
    41      -s=*|--src=* )
    42        SRC_DIR="${i#*=}"
    43        shift
    44        ;;
    45      -k=*|--key=* )
    46        KEY="${i#*=}"
    47        shift
    48        ;;
    49      -kf=*|--key_fallback=* )
    50        KEY_FALLBACK_PATTERN="${i#*=}"
    51        shift
    52        ;;
    53      -h|--help )
    54        print_usage
    55        exit 0
    56        ;;
    57      *)
    58        echo "Unknown option: ${i}"
    59        print_usage
    60        exit 1
    61        ;;
    62    esac
    63  done
    64  
    65  if [ -z "${BUCKET}" ] && [ -z "${SRC_DIR}" ];then
    66    echo "Require one of [--bucket, --src]"
    67    print_usage
    68    exit 1
    69  fi
    70  
    71  # Evaluate the key string so that bash variables work
    72  eval "KEY=\"$KEY\""
    73  eval "KEY_FALLBACK_PATTERN=\"$KEY_FALLBACK_PATTERN\""
    74  
    75  if [ -n "${BUCKET}" ];then
    76    REMOTE_CACHE_FILE="${BUCKET}/${KEY}.tgz"
    77  
    78    echo "Checking cache file existence for: ${REMOTE_CACHE_FILE}"
    79    REMOTE_CACHE_FILE_EXISTENCE=$(gsutil -q stat ${REMOTE_CACHE_FILE}; echo $?)
    80  
    81    # If cache file exists download and decompress it
    82    if [ $REMOTE_CACHE_FILE_EXISTENCE -eq 0 ];then
    83      echo "Downloading cache file: ${REMOTE_CACHE_FILE}..."
    84      gsutil -q cp "${REMOTE_CACHE_FILE}" "${SRC_DIR}"
    85  
    86      CACHE_FILE="${SRC_DIR}/${KEY}.tgz"
    87  
    88      echo "Restoring cache from file ${CACHE_FILE}..."
    89      tar xpzf "$CACHE_FILE" -P
    90      rm "$CACHE_FILE"
    91    else
    92      # Check if fallback key pattern has been specified
    93      if [ -z "${KEY_FALLBACK_PATTERN}" ]; then
    94        echo "No fallback key pattern specified. Can not restore cache!"
    95        exit 0
    96      fi
    97  
    98      # Try to find a fallback file
    99      FALLBACK_CACHE_FILE=$(gsutil ls -l $BUCKET/$KEY_FALLBACK_PATTERN** | sort -k 2,2r | grep -m 1 -i "$KEY_FALLBACK_PATTERN" | awk '{print $3}')
   100  
   101      # Check if fallback cache file has been found
   102      if [ -z "${FALLBACK_CACHE_FILE}" ]; then
   103        echo "No fallback cache file found! Can not restore cache!"
   104        exit 0
   105      fi
   106  
   107      # Download found fallback cache file
   108      echo "Downloading fallback cache file: ${FALLBACK_CACHE_FILE}..."
   109      gsutil -q cp "${FALLBACK_CACHE_FILE}" "${SRC_DIR}"
   110  
   111      FALLBACK_CACHE_FILE_NAME=$(echo ${FALLBACK_CACHE_FILE} | awk '{n=split($0,parts,"/"); print parts[n]}')
   112  
   113      echo "Restoring cache from fallback file ${FALLBACK_CACHE_FILE}..."
   114      tar xpzf "$FALLBACK_CACHE_FILE_NAME" -P
   115      rm "$FALLBACK_CACHE_FILE_NAME"
   116    fi
   117  # If SRC_DIR is specified, restore cache from local file
   118  elif [ -n "${SRC_DIR}" ]; then
   119    LOCAL_CACHE_FILE="${SRC_DIR}/${KEY}.tgz"
   120    echo "Restoring from local cache file ${LOCAL_CACHE_FILE}..."
   121    tar xpzf "$LOCAL_CACHE_FILE" -P
   122  fi