build-cran-binaries/scripts/purge_cdn_cache.sh

64 lines
No EOL
1.6 KiB
Shell

#!/bin/bash
set -o pipefail
set -x
# Function to URL-encode a string using Python
urlencode() {
python3 -c "import urllib.parse, sys; print(urllib.parse.quote(sys.argv[1], safe=''))" "$1"
}
# Function to send the purge request using the provided API key
purge() {
local api_key="$1"
local url="$2"
curl -sL -X POST -H "AccessKey: $api_key" "$url"
}
# Function to generate the purge URLs for a given domain, arch, and OS ID
generate_urls() {
local domain="$1"
local arch="$2"
local os_id="$3"
# Array of resource paths (can be modified or extended as needed)
local resources=(
"src/contrib/PACKAGES"
"src/contrib/PACKAGES.gz"
"src/contrib/PACKAGES.rds"
"src/contrib/PACKAGES.db"
)
local urls=()
for resource in "${resources[@]}"; do
local target_url="https://${domain}/${arch}/${os_id}/latest/${resource}"
local encoded
encoded=$(urlencode "$target_url")
urls+=( "https://api.bunny.net/purge?url=${encoded}&async=false" )
done
echo "${urls[@]}"
}
# Function to process multiple domains.
# Usage: process_domains <BUNNY_API_KEY> <ARCH> <OS_ID> domain1 [domain2 ... domainN]
purge_cdn_cache() {
local api_key="$1"
local arch="$2"
local os_id="$3"
shift 3 # Remove the first three arguments, leaving the domains
for domain in "$@"; do
# Generate the list of URLs for the current domain
local urls
urls=($(generate_urls "$domain" "$arch" "$os_id"))
# Purge each URL
for url in "${urls[@]}"; do
purge "$api_key" "$url"
done
done
}
# Example usage:
# purge_cdn_cache "$BUNNY_API_KEY" "$ARCH" "$OS_ID" "domain1.com" "domain2.com"