github.com/nevins-b/terraform@v0.3.8-0.20170215184714-bbae22007d5a/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=14400" \
    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      --add-header="Cache-Control: max-age=31536000" \
    68      --exclude "*" \
    69      --include "*.js" \
    70      --recursive \
    71      modify "s3://hc-sites/$PROJECT/latest/"
    72  
    73    echo "Overriding css mime-types..."
    74    s3cmd \
    75      --mime-type="text/css" \
    76      --add-header="Cache-Control: max-age=31536000" \
    77      --exclude "*" \
    78      --include "*.css" \
    79      --recursive \
    80      modify "s3://hc-sites/$PROJECT/latest/"
    81  
    82    echo "Overriding svg mime-types..."
    83    s3cmd \
    84      --mime-type="image/svg+xml" \
    85      --add-header="Cache-Control: max-age=31536000" \
    86      --exclude "*" \
    87      --include "*.svg" \
    88      --recursive \
    89      modify "s3://hc-sites/$PROJECT/latest/"
    90  fi
    91  
    92  # Perform a soft-purge of the surrogate key.
    93  if [ -z "$NO_PURGE" ]; then
    94    echo "Purging Fastly cache..."
    95    curl \
    96      --fail \
    97      --silent \
    98      --output /dev/null \
    99      --request "POST" \
   100      --header "Accept: application/json" \
   101      --header "Fastly-Key: $FASTLY_API_KEY" \
   102      --header "Fastly-Soft-Purge: 1" \
   103      "https://api.fastly.com/service/$FASTLY_SERVICE_ID/purge/site-$PROJECT"
   104  fi
   105  
   106  # Warm the cache with recursive wget.
   107  if [ -z "$NO_WARM" ]; then
   108    echo "Warming Fastly cache..."
   109    echo ""
   110    echo "If this step fails, there are likely missing or broken assets or links"
   111    echo "on the website. Run the following command manually on your laptop, and"
   112    echo "search for \"ERROR\" in the output:"
   113    echo ""
   114    echo "wget --recursive --delete-after https://$PROJECT_URL/"
   115    echo ""
   116    wget \
   117      --recursive \
   118      --delete-after \
   119      --quiet \
   120      "https://$PROJECT_URL/"
   121  fi