Small changes in if* scripts
[snf-network] / snf-network-dnshook
1 #!/bin/bash
2
3 # Configuration Fallbacks. All can(must for some of them) be overwritten by /etc/default/snf-network
4 TTL=300
5 # the bind server IP/FQDN
6 SERVER=""
7 # this is the .vm.synnefo.live.
8 # Leave empty if only reverse dns management is needed.
9 # TODO: make this zone to be instance specific!!!
10 FZONE=""
11 # the file with dns authorization keys
12 KEYFILE=""
13 MAC2EUI64="/usr/bin/mac2eui64"
14
15 source /etc/default/snf-network
16 source /usr/lib/snf-network/common.sh
17
18 if [ -z "$SERVER" -o -z "$FZONE" -o ! -e "$KEYFILE" ]; then
19   exit 0
20 fi
21
22 update_dns () {
23
24   if [ "x$GANETI_OP_CODE" = "xOP_INSTANCE_CREATE" ]; then
25     update_all add
26   elif [ "x$GANETI_OP_CODE" = "xOP_INSTANCE_REMOVE" ]; then
27     update_all delete
28   elif [ "x$GANETI_OP_CODE" = "xOP_INSTANCE_RENAME" ]; then
29     update_all delete
30     # Let's override a variable and add ourselves
31     GANETI_INSTANCE_NAME=$GANETI_INSTANCE_NEW_NAME
32     update_all add
33   elif [ "x$GANETI_OP_CODE" = "xOP_INSTANCE_STARTUP" ]; then
34     update_all add
35   elif [ "x$GANETI_OP_CODE" = "xOP_INSTANCE_SHUTDOWN" ]; then
36     update_all delete
37   elif [ "x$GANETI_OP_CODE" = "xOP_INSTANCE_REBOOT" ]; then
38     update_all add
39   elif [ "x$GANETI_OP_CODE" = "xOP_INSTANCE_SET_PARAMS" ]; then
40     update_all add
41   fi
42
43 }
44
45
46 # Query nameserver for entries related to the specific instance
47 # An example output is the following:
48 # www.google.com has address 173.194.113.114
49 # www.google.com has address 173.194.113.115
50 # www.google.com has address 173.194.113.116
51 # www.google.com has address 173.194.113.112
52 # www.google.com has address 173.194.113.113
53 # www.google.com has IPv6 address 2a00:1450:4001:80b::1012
54 query_dns () {
55
56   HOSTQ="host -s -R 3 -W 3"
57   HOST_IP_ALL=$($HOSTQ $GANETI_INSTANCE_NAME.$FZONE $SERVER | sed -n 's/.*has address //p')
58   HOST_IP6_ALL=$($HOSTQ $GANETI_INSTANCE_NAME.$FZONE $SERVER | sed -n 's/.*has IPv6 address //p')
59
60 }
61
62
63 # Reset all entries related to the specific instance
64 # This should be invoced only during instance modification
65 # because we do not know which nics have been modify
66 reset_dns () {
67
68   if [ "x$GANETI_OP_CODE" = "xOP_INSTANCE_SET_PARAMS" ]; then
69     query_dns
70     # This should remove the A, AAAA, CNAME entries
71     send_command "update delete $GANETI_INSTANCE_NAME.$FZONE"
72     for ip in $HOST_IP_ALL; do
73       get_rev4_info $ip
74       # This should remove the IPv4 reverse entry
75       send_command "update delete $RLPART.$RZONE"
76     done
77     for ip6 in $HOST_IP6_ALL; do
78       get_rev6_info $ip6
79       # This should remove the IPv6 reverse entry
80       send_command "update delete $R6LPART$R6ZONE."
81     done
82   fi
83
84 }
85
86
87 # Main starts here
88
89
90 # Exit if we do not have instance name.
91 # It should be exported to hooks for instance related opcodes.
92 if [ -z "$GANETI_INSTANCE_NAME" ]; then
93   exit 0
94 fi
95
96 # This runs only for instance modification
97 reset_dns
98
99 # If GANETI_INSTANCE_NIC_COUNT is not set then nothing happens
100 FIRST=0
101 LAST=$((GANETI_INSTANCE_NIC_COUNT - 1))
102 for idx in $(seq $FIRST $LAST); do
103   ip=GANETI_INSTANCE_NIC${idx}_IP
104   mac=GANETI_INSTANCE_NIC${idx}_MAC
105   mode=GANETI_INSTANCE_NIC${idx}_MODE
106   link=GANETI_INSTANCE_NIC${idx}_LINK
107   subnet=GANETI_INSTANCE_NIC${idx}_NETWORK_SUBNET
108   subnet6=GANETI_INSTANCE_NIC${idx}_NETWORK_SUBNET6
109   tags=GANETI_INSTANCE_NIC${idx}_NETWORK_TAGS
110   eval IP=\$$ip
111   eval MAC=\$$mac
112   eval MODE=\$$mode
113   eval LINK=\$$link
114   eval SUBNET=\$$subnet
115   eval SUBNET6=\$$subnet6
116   eval TAGS=\$$tags
117
118   for tag in $TAGS; do
119     case $tag in
120     $DNS_TAG)
121
122       get_rev4_info "$IP"
123       get_eui64 "$MAC" "$SUBNET6"
124       get_rev6_info "$EUI64"
125       $SNF_NETWORK_LOG dnshook "update dns for $GANETI_INSTANCE_NAME $IP $EUI64"
126       update_dns
127
128       ;;
129     esac
130
131   done
132
133 done