-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloudflare-ops.sh
68 lines (57 loc) · 2.94 KB
/
cloudflare-ops.sh
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
61
62
63
64
65
66
67
68
#!/bin/bash
echo
if [ $# -ne 2 ]; then
echo "Error: Invalid number of arguments"
echo "Usage: cloudflare-ops.sh dev-mode <status / on / off>"
echo " OR"
echo " cloudflare-ops.sh cache <clear / purge>"
echo
exit 1
fi
CF_OPRN=$1
CF_VAL=$2
if [ -z "$CLOUDFLARE_ZONE_ID" ]; then
echo 'Error: CLOUDFLARE_ZONE_ID is not set.'
echo
exit 1
fi
if [ -z "$CLOUDFLARE_API_TOKEN" ]; then
echo 'Error: CLOUDFLARE_API_TOKEN is Empty.'
echo
exit 1
fi
shopt -s nocasematch
if [ $CF_OPRN = 'dev-mode' ]; then
if [ $CF_VAL = 'on' ]; then
echo "Enabling CloudFlare Development Mode for next 3 hrs."
curl -s -X PATCH "https://api.cloudflare.com/client/v4/zones/$CLOUDFLARE_ZONE_ID/settings/development_mode" -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" -H "Content-Type:application/json" --data '{"value":"on"}'
echo;echo
echo "CloudFlare Development Mode status: "
curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$CLOUDFLARE_ZONE_ID/settings/development_mode" -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" -H "Content-Type: application/json" | awk 'match($0,/"value":"[a-z]+/){print substr($0,RSTART+9,RLENGTH-9)}'
elif [ $CF_VAL = 'off' ]; then
echo "Disabling CloudFlare Development Mode."
curl -s -X PATCH "https://api.cloudflare.com/client/v4/zones/$CLOUDFLARE_ZONE_ID/settings/development_mode" -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" -H "Content-Type:application/json" --data '{"value":"off"}'
echo;echo
echo "CloudFlare Development Mode status: "
curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$CLOUDFLARE_ZONE_ID/settings/development_mode" -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" -H "Content-Type: application/json" | awk 'match($0,/"value":"[a-z]+/){print substr($0,RSTART+9,RLENGTH-9)}'
elif [ $CF_VAL = 'status' ]; then
echo "CloudFlare Development Mode status: "
curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$CLOUDFLARE_ZONE_ID/settings/development_mode" -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" -H "Content-Type: application/json" | awk 'match($0,/"value":"[a-z]+/){print substr($0,RSTART+9,RLENGTH-9)}'
else
echo "Invalid dev-mode option. Valid options are <on / off>."
fi
elif [ $CF_OPRN = 'cache' ]; then
if [ $CF_VAL = 'clear' ]; then
echo "Purging CloudFlare cache."
curl -s -X POST "https://api.cloudflare.com/client/v4/zones/$CLOUDFLARE_ZONE_ID/purge_cache" -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" -H "Content-Type:application/json" --data '{"purge_everything":true}'
elif [ $CF_VAL = 'purge' ]; then
echo "Purging CloudFlare cache."
curl -s -X POST "https://api.cloudflare.com/client/v4/zones/$CLOUDFLARE_ZONE_ID/purge_cache" -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" -H "Content-Type:application/json" --data '{"purge_everything":true}'
else
echo "Invalid cache option. Valid options are <clear / purge>."
fi
else
echo "Invalid operation. Valid ops are <dev-mode / cache>."
fi
shopt -u nocasematch
echo