-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloudflare-cache-purge.bash
More file actions
executable file
·60 lines (51 loc) · 1.83 KB
/
cloudflare-cache-purge.bash
File metadata and controls
executable file
·60 lines (51 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/bin/bash
set -e
echo "Started at $(date)"
echo "Will watch for ${K8S_WWW_DEPLOYMENT?} to change in ns ${K8S_WWW_NAMESPACE?}"
echo "And purge app ${K8S_WWW_APP?}'s URLs: ${CLOUDFLARE_PURGE_URLS:-entire zone}"
echo "Current deployments:"
kubectl -n "$K8S_WWW_NAMESPACE" get deployment
deployment_image() {
kubectl -n "$K8S_WWW_NAMESPACE" get deployment "$K8S_WWW_DEPLOYMENT" --output=json \
| jq -r '.spec.template.spec.containers[].image'
}
replicaset_images() {
kubectl -n "$K8S_WWW_NAMESPACE" get replicaset --output=json \
| jq -r ".items[] | select(.metadata.labels.app == \"${K8S_WWW_APP}\" and .spec.replicas > 0) | .spec.template.spec.containers[].image"
}
new_image="$(deployment_image)"
attempts=0
echo "Waiting for ${new_image} to be ready..."
while test "$(replicaset_images)" != "$new_image"; do
attempts="$((attempts + 1))"
echo "Other images are still running (checked ${attempts} times)"
sleep 3
if test "$attempts" -ge 100; then
echo "Timed out waiting for other deployments to stop"
exit 1
fi
done
echo "Ready at $(date)"
purge() {
iterations=3
wait=30
endpoint="https://api.cloudflare.com/client/v4/zones/${CLOUDFLARE_ZONEID?}/purge_cache"
for ((i=1; i<=iterations; i++))
do
echo "Purging cache at $endpoint, iteration $i..."
curl -X POST "$endpoint" \
-H "X-Auth-Email: ${CLOUDFLARE_EMAIL?}" \
-H "X-Auth-Key: ${CLOUDFLARE_TOKEN?}" \
-H 'Content-Type: application/json' \
-d "$*" | jq .
if [ $i -lt $iterations ]; then
echo "Sleeping for $wait seconds..."
sleep $wait
fi
done
}
if test -n "$CLOUDFLARE_PURGE_URLS"; then
purge '{"purge_everything": true}'
else
purge "$(jq -n --arg urls "$CLOUDFLARE_PURGE_URLS" '{"files": $urls | split(" ")}')"
fi