Created
November 12, 2024 09:16
-
-
Save bpolaszek/575648b71aa8cc7095c4d901a339831f to your computer and use it in GitHub Desktop.
Cloudflare Cache Purger
This file contains 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
#!/bin/bash | |
# Initialize variables | |
declare -a hostnames | |
api_key="" | |
# Function to display usage | |
usage() { | |
echo "Usage: $0 --hostname domain1.com [--hostname domain2.com ...] [--api-key YOUR_API_KEY]" | |
echo | |
echo "Options:" | |
echo " --hostname Domain name(s) to purge cache for (can be specified multiple times)" | |
echo " --api-key Cloudflare API key (optional if CLOUDFLARE_API_KEY env variable is set)" | |
exit 1 | |
} | |
# Function to exit with error | |
die() { | |
echo "Error: $1" >&2 | |
exit 1 | |
} | |
# Parse command line arguments | |
while [[ $# -gt 0 ]]; do | |
case $1 in | |
--hostname) | |
hostnames+=("$2") | |
shift 2 | |
;; | |
--api-key) | |
api_key="$2" | |
shift 2 | |
;; | |
*) | |
usage | |
;; | |
esac | |
done | |
# Validate inputs | |
[[ ${#hostnames[@]} -eq 0 ]] && die "At least one hostname must be specified" | |
# Use environment variable if API key not provided | |
if [[ -z "$api_key" ]]; then | |
[[ -z "$CLOUDFLARE_API_KEY" ]] && die "No API key provided and CLOUDFLARE_API_KEY environment variable is not set" | |
api_key="$CLOUDFLARE_API_KEY" | |
fi | |
# Function to get zone ID for a hostname | |
get_zone_id() { | |
local hostname="$1" | |
local response | |
response=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$hostname" \ | |
-H "Authorization: Bearer $api_key" \ | |
-H "Content-Type: application/json") | |
# Check if the API call was successful | |
if ! echo "$response" | jq -e '.success' >/dev/null; then | |
echo "API_ERROR" | |
return | |
fi | |
# Extract zone ID | |
local zone_id | |
zone_id=$(echo "$response" | jq -r '.result[0].id') | |
# Check if zone ID was found | |
if [[ "$zone_id" == "null" ]] || [[ -z "$zone_id" ]]; then | |
echo "NOT_FOUND" | |
return | |
fi | |
echo "$zone_id" | |
} | |
# Function to purge cache for a zone | |
purge_cache() { | |
local zone_id="$1" | |
local response | |
response=$(curl -s -X POST "https://api.cloudflare.com/client/v4/zones/$zone_id/purge_cache" \ | |
-H "Authorization: Bearer $api_key" \ | |
-H "Content-Type: application/json" \ | |
--data '{"purge_everything":true}') | |
# Check if the purge was successful | |
if echo "$response" | jq -e '.success' >/dev/null; then | |
return 0 | |
else | |
return 1 | |
fi | |
} | |
# Main execution | |
echo "Starting cache purge process..." | |
# Check for jq dependency | |
command -v jq >/dev/null 2>&1 || die "jq is required but not installed" | |
# Process each hostname | |
failed=0 | |
for hostname in "${hostnames[@]}"; do | |
echo "Processing $hostname..." | |
# Get zone ID | |
zone_id=$(get_zone_id "$hostname") | |
case "$zone_id" in | |
API_ERROR) | |
echo "Failed to retrieve zone ID for $hostname: API error" | |
failed=1 | |
continue | |
;; | |
NOT_FOUND) | |
echo "Failed to retrieve zone ID for $hostname: Zone not found" | |
failed=1 | |
continue | |
;; | |
*) | |
echo "Found zone ID: $zone_id" | |
if purge_cache "$zone_id"; then | |
echo "Successfully purged cache for $hostname" | |
else | |
echo "Failed to purge cache for $hostname" | |
failed=1 | |
fi | |
;; | |
esac | |
done | |
# Exit with appropriate status code | |
if [[ $failed -eq 1 ]]; then | |
die "One or more operations failed" | |
else | |
echo "All cache purge operations completed successfully" | |
exit 0 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment