Skip to content

Instantly share code, notes, and snippets.

@noweh
Last active September 21, 2021 10:20
Show Gist options
  • Save noweh/4f3b3b59af32a2ef5dc62ed8122f2a5f to your computer and use it in GitHub Desktop.
Save noweh/4f3b3b59af32a2ef5dc62ed8122f2a5f to your computer and use it in GitHub Desktop.
Purge CDNetworks caches (folder or file)
#!/bin/bash
usage() {
printf "Usage:\n$0\n[-t (type of content) url|folder]\n[-u (url to purge) ex: https://xxx.yyy.com/logo.png]" 1>&2;
exit 1;
}
purgeUrl="https://api.cdnetworks.com/ccm/purge/ItemIdReceiver"
# data stored in a .env file
username=$(grep CDNETWORKS_USERNAME .env | cut -d '=' -f2)
apiKey=$(grep CDNETWORKS_APIKEY .env | cut -c19-)
if [ $username ] && [ $apiKey ]; then
arrayTypes=(
"url"
"folder"
)
while getopts ":t:u:" arg; do
case $arg in
t)
type=${OPTARG}
if [[ ! " ${arrayTypes[@]} " =~ " $type " ]]; then
usage
fi
;;
u)
url=${OPTARG}
;;
*)
echo other
usage
;;
esac
done
shift $((OPTIND-1))
if [ -z $type ] || [ -z $url ]; then
usage
fi
echo $purgeUrl
echo "First arg: $type"
echo "Second arg: $url"
date=`env LANG="en_US.UTF-8" date -u "+%a, %d %b %Y %H:%M:%S GMT"`
echo $date
password=`echo -en "$date" | openssl dgst -sha1 -hmac $apiKey -binary | openssl enc -base64`
if [ $type == 'url' ]; then
curl -i --url $purgeUrl \
-X "POST" \
-u "$username:$password" \
-H "Date:$date" \
-H "Content-Type: application/json" \
-d'{
"urls": [
"'$url'"
],
"urlAction":"delete"
}'
fi
if [ $type == 'folder' ]; then
curl -i --url $purgeUrl \
-X "POST" \
-u "$username:$password" \
-H "Date:$date" \
-H "Content-Type: application/json" \
-d'{
"dirs": [
"'$url'"
],
"dirAction":"expire"
}'
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment