github.com/ns1/terraform@v0.7.10-0.20161109153551-8949419bef40/website/scripts/deploy.sh (about)

     1  #!/bin/sh
     2  set -e
     3  
     4  PROJECT="terraform"
     5  PROJECT_URL="www.terraform.io"
     6  FASTLY_SERVICE_ID="7GrxRJP3PVBuqQbyxYQ0MV"
     7  
     8  # Ensure the proper AWS environment variables are set
     9  if [ -z "$AWS_ACCESS_KEY_ID" ]; then
    10    echo "Missing AWS_ACCESS_KEY_ID!"
    11    exit 1
    12  fi
    13  
    14  if [ -z "$AWS_SECRET_ACCESS_KEY" ]; then
    15    echo "Missing AWS_SECRET_ACCESS_KEY!"
    16    exit 1
    17  fi
    18  
    19  # Ensure the proper Fastly keys are set
    20  if [ -z "$FASTLY_API_KEY" ]; then
    21    echo "Missing FASTLY_API_KEY!"
    22    exit 1
    23  fi
    24  
    25  # Ensure we have s3cmd installed
    26  if ! command -v "s3cmd" >/dev/null 2>&1; then
    27    echo "Missing s3cmd!"
    28    exit 1
    29  fi
    30  
    31  # Get the parent directory of where this script is and cd there
    32  DIR="$(cd "$(dirname "$(readlink -f "$0")")/.." && pwd)"
    33  
    34  # Delete any .DS_Store files for our OS X friends.
    35  find "$DIR" -type f -name '.DS_Store' -delete
    36  
    37  # Upload the files to S3 - we disable mime-type detection by the python library
    38  # and just guess from the file extension because it's surprisingly more
    39  # accurate, especially for CSS and javascript. We also tag the uploaded files
    40  # with the proper Surrogate-Key, which we will later purge in our API call to
    41  # Fastly.
    42  if [ -z "$NO_UPLOAD" ]; then
    43    echo "Uploading to S3..."
    44  
    45    # Check that the site has been built
    46    if [ ! -d "$DIR/build" ]; then
    47      echo "Missing compiled website! Run 'make build' to compile!"
    48      exit 1
    49    fi
    50  
    51    s3cmd \
    52      --quiet \
    53      --delete-removed \
    54      --guess-mime-type \
    55      --no-mime-magic \
    56      --acl-public \
    57      --recursive \
    58      --add-header="Cache-Control: max-age=31536000" \
    59      --add-header="x-amz-meta-surrogate-key: site-$PROJECT" \
    60      sync "$DIR/build/" "s3://hc-sites/$PROJECT/latest/"
    61  
    62    # The s3cmd guessed mime type for text files is often wrong. This is
    63    # problematic for some assets, so force their mime types to be correct.
    64    echo "Overriding javascript mime-types..."
    65    s3cmd \
    66      --mime-type="application/javascript" \
    67      --exclude "*" \
    68      --include "*.js" \
    69      --recursive \
    70      modify "s3://hc-sites/$PROJECT/latest/"
    71  
    72    echo "Overriding css mime-types..."
    73    s3cmd \
    74      --mime-type="text/css" \
    75      --exclude "*" \
    76      --include "*.css" \
    77      --recursive \
    78      modify "s3://hc-sites/$PROJECT/latest/"
    79  
    80    echo "Overriding svg mime-types..."
    81    s3cmd \
    82      --mime-type="image/svg+xml" \
    83      --exclude "*" \
    84      --include "*.svg" \
    85      --recursive \
    86      modify "s3://hc-sites/$PROJECT/latest/"
    87  fi
    88  
    89  # Perform a soft-purge of the surrogate key.
    90  if [ -z "$NO_PURGE" ]; then
    91    echo "Purging Fastly cache..."
    92    curl \
    93      --fail \
    94      --silent \
    95      --output /dev/null \
    96      --request "POST" \
    97      --header "Accept: application/json" \
    98      --header "Fastly-Key: $FASTLY_API_KEY" \
    99      --header "Fastly-Soft-Purge: 1" \
   100      "https://api.fastly.com/service/$FASTLY_SERVICE_ID/purge/site-$PROJECT"
   101  fi
   102  
   103  # Warm the cache with recursive wget.
   104  if [ -z "$NO_WARM" ]; then
   105    echo "Warming Fastly cache..."
   106    echo ""
   107    echo "If this step fails, there are likely missing or broken assets or links"
   108    echo "on the website. Run the following command manually on your laptop, and"
   109    echo "search for \"ERROR\" in the output:"
   110    echo ""
   111    echo "wget --recursive --delete-after https://$PROJECT_URL/"
   112    echo ""
   113    wget \
   114      --recursive \
   115      --delete-after \
   116      --quiet \
   117      "https://$PROJECT_URL/"
   118  fi