Answers for "Curl Shell Script To Purge Cache From Stackpath CDN"

0

Curl Shell Script To Purge Cache From Stackpath CDN

#!/usr/bin/env bash
# Usage: 
# A simple shell script to purge Stackpath cdn cache using API and curl 
# command line. Tested on Ubuntu/Debian/RHEL/Fedora Linux and FreeBSD/macOS Unix
# Syntax:
# /path/to/stackpath.purge.sh https://your-domain/url
# ----------------------------------------------------------------------------
# Written by Vivek Gite <http://www.cyberciti.biz/>
# (c) 2020 nixCraft under GNU GPL v2.0+
# ----------------------------------------------------------------------------
# Last updated: 22/May/2012
# ----------------------------------------------------------------------------
set -e
 
################
# Set me first #
################
stack_id="YOUR-STACK-ID-HERE"
 
# APK keys 
client_id='YOUR-API-CLIENT-ID-HERE'
client_pwd='YOUR-API-AUTH-PASSWORD-HERE'
 
######################
## No editing below ##
######################
# All ulrs 
urls="$@"
 
# Make sure we get at least url else die with usage 
[ "$urls" == "" ] && { echo "Usage: $0 url1 url2 ..."; exit 1; }
 
# Get auth stuff; aka bearer
b="$(curl -s --request POST \
  --url https://gateway.stackpath.com/identity/v1/oauth2/token \
  --header 'Accept: application/json' \
  --header 'Content-Type: application/json' \
  --data "{
    \"client_id\": \"${client_id}\",
    \"client_secret\": \"${client_pwd}\",
    \"grant_type\": \"client_credentials\"
}" | jq '.access_token')"
 
# Clean up bearer for the curl
bearer="${b//\"/}"
 
# Let us start purging urls one-by-one
for u in $urls
do
	echo -n "Purgin url $u:"
	purge_id=$(curl -s --request POST \
	  --url https://gateway.stackpath.com/cdn/v1/stacks/$stack_id/purge \
	  --header 'accept: application/json' \
	  --header "authorization: Bearer $bearer" \
	  --header 'content-type: application/json' \
	  --data "{\"items\":[
  			{
				\"url\":\"${u}\",
				\"purgeAllDynamic\":true
			}
		  ]
	  }" | jq '.id')
 
# Get url purge status
	purge_id="${purge_id//\"/}"
  	curl -s -i --request GET \
	  --url https://gateway.stackpath.com/cdn/v1/stacks/$stack_id/purge/$purge_id \
	  --header 'accept: application/json' \
	  --header "authorization: Bearer $bearer" | egrep -o 'HTTP/.*|{.*}'
    echo ""
 
done
Posted by: Guest on June-28-2021

Browse Popular Code Answers by Language