Last active
August 30, 2018 12:46
-
-
Save kubek2k/178b3669cd5a7d9c14bdfb3b39b5954d to your computer and use it in GitHub Desktop.
Upload custom vcl to fastly and activate it after successful validation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set -e | |
function callFastly() { | |
endpoint="$1" | |
shift | |
curl -f -s -H "Fastly-Key: ${FASTLY_TOKEN}" -H "Accept: application/json" "https://api.fastly.com$endpoint" $* | |
} | |
function getCurrentVersion() { | |
callFastly "/service/$SERVICE_ID/version" | jq '.[0].number' | |
} | |
function getCurrentVCL() { | |
currentVersion=`getCurrentVersion` | |
echo $currentVersion | |
callFastly "/service/$SERVICE_ID/version/${currentVersion}/generated_vcl" | |
} | |
function createNewConfigurationVersion() { | |
currentVersion=`getCurrentVersion` | |
callFastly "/service/$SERVICE_ID/version/$currentVersion/clone" -X PUT | jq '.number' | |
} | |
function uploadVCL() { | |
callFastly "/service/$SERVICE_ID/version/$1/vcl" -X POST --data-urlencode name=test --data-urlencode "content@$2" | |
} | |
function validateConfiguration() { | |
callFastly "/service/$SERVICE_ID/version/$1/validate" | |
} | |
function activateConfiguration() { | |
callFastly "/service/$SERVICE_ID/version/$1/activate" -X PUT | |
} | |
assertOk() { | |
if [ $? -ne 0 ]; then | |
echo "Last command didn't succeed" | |
exit 1; | |
fi | |
} | |
if [ -z "$SERVICE_ID" ]; then | |
echo "No SERVICE_ID env variable set" | |
exit 1 | |
fi | |
if [ -z "$FASTLY_TOKEN" ]; then | |
echo "No FASTLY_TOKEN env variable set" | |
exit 1 | |
fi | |
if [ -z "$1" ]; then | |
echo "No filename provided for vcl" | |
exit 1 | |
fi | |
echo "Creating new configuration" | |
version=`createNewConfigurationVersion` | |
echo "Uploading VCL" | |
uploadVCL "$version" "$1" > /dev/null | |
echo "Validating configuration" | |
validations=`validateConfiguration $version` | |
status=`echo $validations | jq -r '.status'` | |
if [ "$status" != "ok" ]; then | |
echo "Validation of configuration failed" | |
exit 1 | |
fi | |
echo "Activating new configuration" | |
activateConfiguration $version > /dev/null | |
echo "All done." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment