agones.dev/agones@v1.53.0/build/build-image/cache/save_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  OUT_DIR="."
    19  KEY="cache"
    20  THRESHOLD="50M"
    21  PATHS=()
    22  
    23  function print_usage {
    24    echo "Usage: $0 --out=/cache_dir --key=cache-key --path=/path/to/files/1 --path=/path/to/files/2"
    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 upload the cache to. [optional]"
    29    echo "  -o, --out         The output directory to write the cache to. [optional]"
    30    echo "  -k, --key         The cache key used for this cache file."
    31    echo "  -p, --path        The files to store in the cache. Can be repeated."
    32    echo "  -t, --threshold   The parallel composite upload threshold [default: 50M]"
    33    echo "  -n, --no-clobber  Skips the save if the cache file already exists in GCS."
    34    echo
    35  }
    36  
    37  for i in "$@"; do
    38    case $i in
    39      -b=*|--bucket=*)
    40        BUCKET="${i#*=}"
    41        shift
    42        ;;
    43      -o=*|--out=*)
    44        OUT_DIR="${i#*=}"
    45        shift
    46        ;;
    47      -k=*|--key=*)
    48        KEY="${i#*=}"
    49        shift
    50        ;;
    51      -p=*|--path=*)
    52        PATHS+="${i#*=} "
    53        shift
    54        ;;
    55      -t=*|--threshold=*)
    56        THRESHOLD="${i#*=} "
    57        shift
    58        ;;
    59      -n|--no-clobber)
    60        NO_CLOBBER=true
    61        shift
    62        ;;
    63      -h|--help )
    64        print_usage
    65        exit 0
    66        ;;
    67      *)
    68        echo "Unknown option: ${i}"
    69        print_usage
    70        exit 1
    71        ;;
    72    esac
    73  done
    74  
    75  if [ -z "$KEY" ] || [ -z "$PATHS" ];then
    76    print_usage
    77    exit 1
    78  fi
    79  
    80  if [ "$NO_CLOBBER" = true ] && [ -z "$BUCKET" ];then
    81    echo "--bucket must be specified if --no-clobber is used"
    82    echo
    83    print_usage
    84    exit 1
    85  fi
    86  
    87  eval "KEY=$KEY"
    88  
    89  CACHE_FILE="${OUT_DIR}/${KEY}.tgz"
    90  
    91  if [ "$NO_CLOBBER" = true ];then
    92    BUCKET_FILE="$BUCKET/$KEY.tgz"
    93    FILE_LS=$(gsutil ls "$BUCKET_FILE")
    94    if [ "$FILE_LS" == "$BUCKET_FILE" ];then
    95      echo "Cache file exists, exiting save_cache without over-writing cache file."
    96      exit 0
    97    fi
    98  fi
    99  
   100  echo "Compressing cache to ${CACHE_FILE}..."
   101  tar cpzf "$CACHE_FILE" ${PATHS[@]} -P
   102  
   103  if [ -n "$BUCKET" ];then
   104    echo "Uploading cache to Google Cloud Storage..."
   105    gsutil -o GSUtil:parallel_composite_upload_threshold=${THRESHOLD} cp -R "$CACHE_FILE" "$BUCKET"
   106  fi