root / snf-network-dnshook @ ae809198
History | View | Annotate | Download (5 kB)
1 |
#!/bin/bash |
---|---|
2 |
# |
3 |
# Copyright 2014 GRNET S.A. All rights reserved. |
4 |
# |
5 |
# Redistribution and use in source and binary forms, with or |
6 |
# without modification, are permitted provided that the following |
7 |
# conditions are met: |
8 |
# |
9 |
# 1. Redistributions of source code must retain the above |
10 |
# copyright notice, this list of conditions and the following |
11 |
# disclaimer. |
12 |
# |
13 |
# 2. Redistributions in binary form must reproduce the above |
14 |
# copyright notice, this list of conditions and the following |
15 |
# disclaimer in the documentation and/or other materials |
16 |
# provided with the distribution. |
17 |
# |
18 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS |
19 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
20 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
21 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR |
22 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
23 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
24 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
25 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
26 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
27 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
28 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
29 |
# POSSIBILITY OF SUCH DAMAGE. |
30 |
# |
31 |
# The views and conclusions contained in the software and |
32 |
# documentation are those of the authors and should not be |
33 |
# interpreted as representing official policies, either expressed |
34 |
# or implied, of GRNET S.A. |
35 |
|
36 |
# Configuration Fallbacks. All can(must for some of them) be overwritten by /etc/default/snf-network |
37 |
TTL=300 |
38 |
# the bind server IP/FQDN |
39 |
SERVER="" |
40 |
# this is the .vm.synnefo.live. |
41 |
# Leave empty if only reverse dns management is needed. |
42 |
# TODO: make this zone to be instance specific!!! |
43 |
FZONE="" |
44 |
# the file with dns authorization keys |
45 |
KEYFILE="" |
46 |
MAC2EUI64="/usr/bin/mac2eui64" |
47 |
|
48 |
source /etc/default/snf-network |
49 |
source /usr/lib/snf-network/common.sh |
50 |
|
51 |
if [ -z "$SERVER" -o -z "$FZONE" -o ! -e "$KEYFILE" ]; then |
52 |
exit 0 |
53 |
fi |
54 |
|
55 |
update_dns () { |
56 |
|
57 |
if [ "x$GANETI_OP_CODE" = "xOP_INSTANCE_CREATE" ]; then |
58 |
update_all add |
59 |
elif [ "x$GANETI_OP_CODE" = "xOP_INSTANCE_REMOVE" ]; then |
60 |
update_all delete |
61 |
elif [ "x$GANETI_OP_CODE" = "xOP_INSTANCE_RENAME" ]; then |
62 |
update_all delete |
63 |
# Let's override a variable and add ourselves |
64 |
GANETI_INSTANCE_NAME=$GANETI_INSTANCE_NEW_NAME |
65 |
update_all add |
66 |
elif [ "x$GANETI_OP_CODE" = "xOP_INSTANCE_STARTUP" ]; then |
67 |
update_all add |
68 |
elif [ "x$GANETI_OP_CODE" = "xOP_INSTANCE_SHUTDOWN" ]; then |
69 |
update_all delete |
70 |
elif [ "x$GANETI_OP_CODE" = "xOP_INSTANCE_REBOOT" ]; then |
71 |
update_all add |
72 |
elif [ "x$GANETI_OP_CODE" = "xOP_INSTANCE_SET_PARAMS" ]; then |
73 |
update_all add |
74 |
fi |
75 |
|
76 |
} |
77 |
|
78 |
|
79 |
# Query nameserver for entries related to the specific instance |
80 |
# An example output is the following: |
81 |
# www.google.com has address 173.194.113.114 |
82 |
# www.google.com has address 173.194.113.115 |
83 |
# www.google.com has address 173.194.113.116 |
84 |
# www.google.com has address 173.194.113.112 |
85 |
# www.google.com has address 173.194.113.113 |
86 |
# www.google.com has IPv6 address 2a00:1450:4001:80b::1012 |
87 |
query_dns () { |
88 |
|
89 |
HOSTQ="host -s -R 3 -W 3" |
90 |
HOST_IP_ALL=$($HOSTQ $GANETI_INSTANCE_NAME.$FZONE $SERVER | sed -n 's/.*has address //p') |
91 |
HOST_IP6_ALL=$($HOSTQ $GANETI_INSTANCE_NAME.$FZONE $SERVER | sed -n 's/.*has IPv6 address //p') |
92 |
|
93 |
} |
94 |
|
95 |
|
96 |
# Reset all entries related to the specific instance |
97 |
# This should be invoced only during instance modification |
98 |
# because we do not know which nics have been modify |
99 |
reset_dns () { |
100 |
|
101 |
if [ "x$GANETI_OP_CODE" = "xOP_INSTANCE_SET_PARAMS" ]; then |
102 |
query_dns |
103 |
# This should remove the A, AAAA, CNAME entries |
104 |
send_command "update delete $GANETI_INSTANCE_NAME.$FZONE" |
105 |
for ip in $HOST_IP_ALL; do |
106 |
get_rev4_info $ip |
107 |
# This should remove the IPv4 reverse entry |
108 |
send_command "update delete $RLPART.$RZONE" |
109 |
done |
110 |
for ip6 in $HOST_IP6_ALL; do |
111 |
get_rev6_info $ip6 |
112 |
# This should remove the IPv6 reverse entry |
113 |
send_command "update delete $R6LPART$R6ZONE." |
114 |
done |
115 |
fi |
116 |
|
117 |
} |
118 |
|
119 |
|
120 |
# Main starts here |
121 |
|
122 |
|
123 |
# Exit if we do not have instance name. |
124 |
# It should be exported to hooks for instance related opcodes. |
125 |
if [ -z "$GANETI_INSTANCE_NAME" ]; then |
126 |
exit 0 |
127 |
fi |
128 |
|
129 |
# This runs only for instance modification |
130 |
reset_dns |
131 |
|
132 |
# If GANETI_INSTANCE_NIC_COUNT is not set then nothing happens |
133 |
FIRST=0 |
134 |
LAST=$((GANETI_INSTANCE_NIC_COUNT - 1)) |
135 |
for idx in $(seq $FIRST $LAST); do |
136 |
ip=GANETI_INSTANCE_NIC${idx}_IP |
137 |
mac=GANETI_INSTANCE_NIC${idx}_MAC |
138 |
mode=GANETI_INSTANCE_NIC${idx}_MODE |
139 |
link=GANETI_INSTANCE_NIC${idx}_LINK |
140 |
subnet=GANETI_INSTANCE_NIC${idx}_NETWORK_SUBNET |
141 |
subnet6=GANETI_INSTANCE_NIC${idx}_NETWORK_SUBNET6 |
142 |
tags=GANETI_INSTANCE_NIC${idx}_NETWORK_TAGS |
143 |
eval IP=\$$ip |
144 |
eval MAC=\$$mac |
145 |
eval MODE=\$$mode |
146 |
eval LINK=\$$link |
147 |
eval NETWORK_SUBNET=\$$subnet |
148 |
eval NETWORK_SUBNET6=\$$subnet6 |
149 |
eval NETWORK_TAGS=\$$tags |
150 |
|
151 |
for tag in $NETWORK_TAGS; do |
152 |
case $tag in |
153 |
$DNS_TAG) |
154 |
|
155 |
get_info |
156 |
$SNF_NETWORK_LOG $0 "update dns for $GANETI_INSTANCE_NAME $IP $EUI64" |
157 |
update_dns |
158 |
|
159 |
;; |
160 |
esac |
161 |
|
162 |
done |
163 |
|
164 |
done |