Only requests the IPAM API when needed

This commit is contained in:
Alarig Le Lay 2019-01-24 15:56:16 +01:00
parent bdfac0b931
commit f2ad8b19cf

View file

@ -90,6 +90,17 @@ else
exit 1
fi
IFACE="tap${VM_ID}i0"
IP4_NEIGH="$(ip -4 neigh show dev ${IFACE})"
IP6_NEIGH="$(ip -6 neigh show dev ${IFACE})"
IP4_ROUTE="$(ip -4 route show dev ${IFACE})"
IP6_ROUTE="$(ip -6 route show dev ${IFACE})"
if [ "${IP4_NEIGH}" ] && [ "${IP6_NEIGH}" ] && [ "${IP4_ROUTE}" ] && [ "${IP6_ROUTE}" ]; then
write_log INFO 'Nothing to do'
exit 0
fi
# Récupération d'un TOKEN pour utiliser IPAM via l'API
write_log DEBUG 'Connecting to API'
RES_AUTHENT=$(${CURL} -X POST --user ${USER_IPAM}:${PASSWORD_IPAM} ${URL}/user/)
@ -167,13 +178,13 @@ for ID_RANGE_V4 in ${ID_RANGES_V4}; do
# Si la valeur n'est pas vide et correspond à une IPV4
if [ -n "${VM_IPV4}" ]; then
# Ajout de la route associée
COMMAND="ip -4 route add ${VM_IPV4}/32 dev tap${VM_ID}i0"
COMMAND="ip -4 route add ${VM_IPV4}/32 dev ${IFACE}"
echo "${COMMAND}"
write_log INFO "${COMMAND}"
${COMMAND}
# Updating ARP
COMMAND="ip neigh add ${VM_IPV4} lladdr ${MAC} dev tap${VM_ID}i0"
COMMAND="ip neigh add ${VM_IPV4} lladdr ${MAC} dev ${IFACE}"
echo "${COMMAND}"
write_log INFO "${COMMAND}"
${COMMAND}
@ -181,7 +192,7 @@ for ID_RANGE_V4 in ${ID_RANGES_V4}; do
# If the previous command fails, its because trafic has
# already be sent to the VM
if [ $? = 2 ]; then
ip neigh replace ${VM_IPV4} lladdr ${MAC} dev tap${VM_ID}i0
ip neigh replace ${VM_IPV4} lladdr ${MAC} dev ${IFACE}
fi
else
# En cas d'echec, on continue de parcourir les autres RANGES
@ -207,16 +218,16 @@ for ID_RANGE_V6 in ${ID_RANGES_V6}; do
fi
# Activation of IPv6 on the tap interface
sysctl net.ipv6.conf.tap${VM_ID}i0.disable_ipv6=0
sysctl net.ipv6.conf.${IFACE}.disable_ipv6=0
# Ajout de la route associée
COMMAND="ip -6 route add ${VM_IPV6}/48 via ${VM_NH} dev tap${VM_ID}i0"
COMMAND="ip -6 route add ${VM_IPV6}/48 via ${VM_NH} dev ${IFACE}"
echo "${COMMAND}"
write_log INFO "${COMMAND}"
${COMMAND}
# Updating NDP
COMMAND="ip neigh add ${VM_NH} lladdr ${MAC} dev tap${VM_ID}i0"
COMMAND="ip neigh add ${VM_NH} lladdr ${MAC} dev ${IFACE}"
echo "${COMMAND}"
write_log INFO "${COMMAND}"
${COMMAND}
@ -224,7 +235,7 @@ for ID_RANGE_V6 in ${ID_RANGES_V6}; do
# If the previous command fails, its because trafic has
# already be sent to the VM
if [ $? = 2 ]; then
ip neigh replace ${VM_NH} lladdr ${MAC} dev tap${VM_ID}i0
ip neigh replace ${VM_NH} lladdr ${MAC} dev ${IFACE}
fi
else
# En cas d'echec, on continue de parcourir les autres RANGES