-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.sh
109 lines (83 loc) · 3.9 KB
/
main.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/bash
auth_email=$CLOUD_FLARE_EMAIL
auth_key=$CLOUD_FLARE_API_KEY
zone_identifier=$CLOUD_FLARE_ZONE
record_domain=$RECORD_DOMAIN
record_name=$RECORD_NAME
REGEX='^([^.]+)'
namespace=$NAMESPACE
error_exit()
{
echo "[Cloudflare] Error:"
echo "$1"
exit 1
}
run_delete_record()
{
# Seek for the record
record=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records?name=$record_search" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json")
# Set therecord identifier from result
record_identifier=$(echo "$record" | grep -Po '(?<="id":")[^"]*' | head -1)
echo "[Cloudflare] Record id found: $record_identifier"
# The execution of delete
delete=$(curl -s -X DELETE "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record_identifier" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json")
# The moment of truth
if [[ $delete == *"\"success\":false"* ]]; then
error_exit "[Cloudflare] Delete failed. DUMPING RESULTS:\n $delete"
else
echo -e "[Cloudflare] Record has been deleted from Cloudflare.\n https://$record_search \n"
exit 0
fi
}
if [ -z "${DEPLOYMENT_TARGET}" ];
then
echo "[Cloudflare] DEPLOYMENT_TARGET is unset so DEPLOYMENT will be used: '$DEPLOYMENT'";
deployment_target=$DEPLOYMENT
else
echo "[Cloudflare] DEPLOYMENT_TARGET is set to '$DEPLOYMENT_TARGET' and this will be used";
deployment_target=$DEPLOYMENT_TARGET
fi
echo "[Cloudflare] Move to the right namespace"
kubectl config use-context "$KUBE_CONTEXT"
echo "[Cloudflare] Get the LoadBalancer"
echo "kubectl get service -n $namespace $deployment_target -o jsonpath='{.status.loadBalancer.ingress[0].hostname}'"
elb_value=$(kubectl get service -n $namespace $deployment_target -o jsonpath='{.status.loadBalancer.ingress[0].hostname}')
echo $elb_value
export record_value=$elb_value
echo "[Cloudflare] Update or create the elb"
record_search="$record_name$record_domain"
[[ $record_search =~ $REGEX ]]
matched=${BASH_REMATCH[1]}
echo "[Cloudflare] Name of the record: $matched"
echo "[Cloudflare] Generated record content is : $record_search"
if [[ $DELETE_RECORD == 1 ]]; then
run_delete_record
exit 0
else
echo "[Cloudflare] Skipping DELETE_RECORD: $DELETE_RECORD !"
fi
# Seek for the record
record=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records?name=$record_search" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json")
# Can't do anything without both record
if [[ $record == *"\"count\":0"* ]]; then
create=$(curl -s -X POST "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" --data "{\"type\":\"CNAME\",\"proxied\":true,\"name\":\"$matched\",\"content\":\"$record_value\"}")
# The moment of truth
if [[ $create == *"\"success\":false"* ]]; then
error_exit "[Cloudflare] Creation failed. DUMPING RESULTS:\n$create"
else
echo -e "[Cloudflare] Record created in Cloudflare. \n https://$record_search \n"
exit 0
fi
fi
# Set therecord identifier from result
record_identifier=$(echo "$record" | grep -Po '(?<="id":")[^"]*' | head -1)
echo "[Cloudflare] Record id found: $record_identifier"
# The execution of update
update=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record_identifier" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" --data "{\"type\":\"CNAME\",\"proxied\":true,\"name\":\"$matched\",\"content\":\"$record_value\"}")
# The moment of truth
if [[ $update == *"\"success\":false"* ]]; then
error_exit "[Cloudflare] Update failed. DUMPING RESULTS:\n $update"
else
echo -e "[Cloudflare] Record has been synced to Cloudflare.\n https://$record_search \n"
exit 0
fi