proxmox_routing/creationRoutesVM.sh

249 lines
10 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# Script ajout de routes VM
# A CONFIGURER/VERIFIER :
# - USER_IPAM
# - PASSWORD_IPAM
# - ID_RANGES_V4
# - ID_RANGES_V6
# - LOG_PLACE
# - CHEMIN_CACHE
write_log() {
# $1: log level (INFO, ERR, etc.)
# $2: message
printf "$(date -Isecond) - $1: $2\n" >> \
${LOG_PLACE}/$(date -Idate)_vm_routing.log
}
# IPAM creds
if [ -f creds ]; then
source creds
else
printf 'Cant source "creds": file not found\n'
fi
# check for commands in the path
for bin in curl ipv6calc ip; do
if [ -z $(which ${bin} 2>/dev/null) ]; then
MISSING="${MISSING} ${bin}"
fi
done
if [ ! -z "${MISSING}" ]; then
printf "Your missing this:${MISSING}\n"
exit 1
fi
# ID subnets IPAM V4 et V6 contenants les VMs
ID_RANGES_V4=( "74" "181" )
ID_RANGES_V6=( "34" )
# Booléen si informations trouvées pour V4 ou V6
BOOL_V4=0
BOOL_V6=0
# Chemin fichier de LOG (et création du répertoire s'il n'existe pas)
LOG_PLACE='./vm_id_log/'
if [ ! -d "${LOG_PLACE}" ];then
mkdir -p ${LOG_PLACE}
write_log INFO 'Creation of the log dir'
fi
write_log INFO 'Beginning of the script execution'
# Chemin fichier de CACHE (et création du répertoire s'il n'existe pas)
CHEMIN_CACHE='./vm_id_cache/'
if [ ! -d "${CHEMIN_CACHE}" ];then
mkdir -p ${CHEMIN_CACHE}
write_log INFO 'Creation of the cache dir'
fi
# Fonction d'aide
usage() {
printf "Usage: ./creationRoutesVM.sh <VM_ID>\n"
printf "VM_ID est l'id d'une VM dans PROXMOX, un entier supérieur à zéro\n"
printf "option : \t-h, --help Affiche ce message daide\n"
}
if [ $? != 0 ]; then
echo "$(date -Isecond) - ERREUR : probleme au lancement du script" >> ${LOG_PLACE}/$(date -Idate)_vm_id.log
exit 1
fi
# On vérifie la bonne présence d'un paramètre VM_ID
if [ $1 ]; then
if [ $1 = '-h' ] || [ $1 = '--help' ]; then
usage
exit 0
elif [ $# -lt 1 ]; then
echo "Paramètre VM_ID manquant (--help pour plus d'infos)"
echo "$(date -Isecond) - ERREUR : Paramètre VM_ID manquant (--help pour plus d'infos)" >> ${LOG_PLACE}/$(date -Idate)_vm_id.log
usage
exit 1
fi
else
echo "Paramètre VM_ID manquant (--help pour plus d'infos)"
echo "$(date -Isecond) - ERREUR : Paramètre VM_ID manquant (--help pour plus d'infos)" >> ${LOG_PLACE}/$(date -Idate)_vm_id.log
usage
exit 1
fi
# On vérifie que le paramètre est bien un entier supérieur à zéro
expr $1 + 0 1>/dev/null 2>&1
statut=$?
if [ $statut -lt 2 ] && [ $1 -gt 0 ]; then
VM_ID=$1
else
echo "Paramètre VM_ID incorrect, ce doit être un entier supérieur à zéro (--help pour plus d'infos)"
echo "$(date -Isecond) - ERREUR : Paramètre VM_ID incorrect, ce doit être un entier supérieur à zéro (--help pour plus d'infos)" >> ${LOG_PLACE}/$(date -Idate)_vm_id.log
usage
exit 1
fi
# Récupération d'un TOKEN pour utiliser IPAM via l'API
RES_AUTHENT=$(curl -k -X POST --user ${USER_IPAM}:${PASSWORD_IPAM} https://ipam.grifon.fr/api/apiculteur/user/ -sS)
CODE_RETOUR_RES_AUTHENT=$(echo ${RES_AUTHENT} | jq '.code')
# Si OK, on utilise l'IPAM, sinon on passe par le fichier de cache
if [ ${CODE_RETOUR_RES_AUTHENT} -eq 200 ] ;then
# Mise du TOKEN dans une variable et retrait des éventuelles guillemets ajoutées lors de la récupération via jq
TOKEN=$(echo ${RES_AUTHENT} | jq '.data.token')
TOKEN=$(echo $TOKEN | sed -e "s/\"//g")
# On vérifie que le TOKEN n'est pas vide
if [ ! -n "${TOKEN}" ]; then
echo "TOKEN Vide";
echo "$(date -Isecond) - ERREUR : TOKEN Vide" >> ${LOG_PLACE}/$(date -Idate)_vm_id.log
exit 1
fi
# Pour chaque subnet V4
for ID_RANGE_V4 in ${ID_RANGES_V4[@]}; do
# Si les infos V4 n'ont pas encore été trouvées
if [ $BOOL_V4 -eq 0 ]; then
# Récupération des informations V4 et vérification du code HTTP
INFOS_V4=$(curl -s -k --header "Content-type: application/x-www-form-urlencoded" --header "token: $TOKEN" -X GET https://ipam.grifon.fr/api/apiculteur/subnets/${ID_RANGE_V4}/addresses)
CODE_RETOUR_INFOS_V4=$(echo ${INFOS_V4} | jq '.code')
# Mise à jour du fichier de cache pour ce ID de subnet
echo ${INFOS_V4} > ${CHEMIN_CACHE}/${ID_RANGE_V4}
if [ ${CODE_RETOUR_INFOS_V4} -eq 200 ] ;then
# Recherche des informations correspondants à la VM_ID
VM_IPV4=$(echo ${INFOS_V4} | jq -r ".data[] | select(.custom_vm_id==\"${VM_ID}\") | .ip")
# Si la valeur n'est pas vide et correspond à une IPV4
if [ -n "${VM_IPV4}" ]; then
# Ajout de la route associée
echo "ip -4 route add ${VM_IPV4}/32 dev tap${VM_ID}i0 (source : IPAM)"
write_log INFO "ip -4 route add ${VM_IPV4}/32 dev tap${VM_ID}i0 (source : IPAM)"
ip -4 route add ${VM_IPV4}/32 dev tap${VM_ID}i0
BOOL_V4=1
else
# En cas d'echec, on continue de parcourir les autres RANGES
echo "Valeur vide ou ne correspond pas à une IPV4"
echo "$(date -Isecond) - NOTICE : Valeur vide ou ne correspond pas à une IPV4" >> ${LOG_PLACE}/$(date -Idate)_vm_id.log
fi
else
# En cas d'echec, on continue de parcourir les autres RANGES
echo "Echec de la récupération des infos V4, code HTTP reçu différent de 200 : ${CODE_RETOUR_INFOS_V4}"
echo "$(date -Isecond) - NOTICE : Echec de la récupération des infos V4, code HTTP reçu différent de 200 : ${CODE_RETOUR_INFOS_V4}" >> ${LOG_PLACE}/$(date -Idate)_vm_id.log
fi
fi
done
# Pour chaque subnet V6
for ID_RANGE_V6 in ${ID_RANGES_V6[@]}; do
# Si les infos V6 n'ont pas encore été trouvées
if [ $BOOL_V6 -eq 0 ]; then
# Récupération des informations V6 et vérification du code HTTP
INFOS_RANGES_V6=$(curl -s -k --header "Content-type: application/x-www-form-urlencoded" --header "token: $TOKEN" -X GET https://ipam.grifon.fr/api/apiculteur/subnets/${ID_RANGE_V6}/slaves/)
CODE_RETOUR_INFOS_RANGES_V6=$(echo ${INFOS_RANGES_V6} | jq '.code')
# Mise à jour du fichier de cache pour cet ID de subnet
echo ${INFOS_RANGES_V6} > ${CHEMIN_CACHE}/${ID_RANGE_V6}
if [ ${CODE_RETOUR_INFOS_V4} -eq 200 ] ;then
# Recherche des informations correspondants à la VM_ID
VM_IPV6=$(echo ${INFOS_RANGES_V6} | jq -r ".data[] | select(.custom_vm_id==\"${VM_ID}\") | .subnet")
# Si la valeur n'est pas vide et correspond à un range IPV6
if [ -n "${VM_IPV6}" ]; then
# Ajout de la route associée
echo "ip -6 route add ${VM_IPV6}/48 dev tap${VM_ID}i0 (source : IPAM)"
write_log INFO "ip -6 route add ${VM_IPV6}/48 dev tap${VM_ID}i0 (source IPAM)"
ip -6 route add ${VM_IPV6}/48 dev tap${VM_ID}i0
BOOL_V6=1
else
# En cas d'echec, on continue de parcourir les autres RANGES
echo "Valeur vide ou ne correspond pas à un range IPV6"
echo "$(date -Isecond) - NOTICE : Valeur vide ou ne correspond pas à un range IPV6" >> ${LOG_PLACE}/$(date -Idate)_vm_id.log
fi
else
# En cas d'echec, on continue de parcourir les autres RANGES
echo "Echec de la récupération des infos V6, code HTTP reçu différent de 200 : ${CODE_RETOUR_INFOS_V6}"
echo "$(date -Isecond) - NOTICE : Echec de la récupération des infos V6, code HTTP reçu différent de 200 : ${CODE_RETOUR_INFOS_V6}" >> ${LOG_PLACE}/$(date -Idate)_vm_id.log
fi
fi
done
# Sinon, on passe par le fichier de cache
else
# Pour chaque subnet V4
for ID_RANGE_V4 in ${ID_RANGES_V4[@]}; do
# Si les infos V4 n'ont pas encore été trouvées
if [ $BOOL_V4 -eq 0 ]; then
# Récupération des informations V4 en cache pour cet ID de subnet si le fichier de cache associé existe
if [ -f "${CHEMIN_CACHE}/${ID_RANGE_V4}" ];then
INFOS_V4=$(cat ${CHEMIN_CACHE}/${ID_RANGE_V4})
# Recherche des informations correspondants à la VM_ID
VM_IPV4=$(echo ${INFOS_V4} | jq -r ".data[] | select(.custom_vm_id==\"${VM_ID}\") | .ip")
# Si la valeur n'est pas vide et correspond à une IPV4
if [ -n "${VM_IPV4}" ]; then
# Ajout de la route associée
echo "ip -4 route add ${VM_IPV4}/32 dev tap${VM_ID}i0 (source : CACHE)"
write_log INFO "ip -4 route add ${VM_IPV4}/32 dev tap${VM_ID}i0 (source: cache)"
ip -4 route add ${VM_IPV4}/32 dev tap${VM_ID}i0
BOOL_V4=1
else
# En cas d'echec, on continue de parcourir les autres RANGES
echo "Valeur vide ou ne correspond pas à une IPV4"
echo "$(date -Isecond) - NOTICE : Valeur vide ou ne correspond pas à une IPV4" >> ${LOG_PLACE}/$(date -Idate)_vm_id.log
fi
else
# En cas d'echec, on continue de parcourir les autres RANGES
echo "Echec de la récupération des infos V4, code HTTP reçu différent de 200 : ${CODE_RETOUR_INFOS_V4}"
echo "$(date -Isecond) - NOTICE : Echec de la récupération des infos V4, code HTTP reçu différent de 200 : ${CODE_RETOUR_INFOS_V4}" >> ${LOG_PLACE}/$(date -Idate)_vm_id.log
fi
fi
done
# Pour chaque subnet V6
for ID_RANGE_V6 in ${ID_RANGES_V6[@]}; do
# Si les infos V6 n'ont pas encore été trouvées
if [ $BOOL_V6 -eq 0 ]; then
# Récupération des informations V6 en cache pour cet ID de subnet si le fichier de cache associé existe
if [ -f "${CHEMIN_CACHE}/${ID_RANGE_V6}" ];then
INFOS_RANGES_V6=$(cat ${CHEMIN_CACHE}/${ID_RANGE_V6})
# Recherche des informations correspondants à la VM_ID
VM_IPV6=$(echo ${INFOS_RANGES_V6} | jq -r ".data[] | select(.custom_vm_id==\"${VM_ID}\") | .subnet")
# Si la valeur n'est pas vide et correspond à un range IPV6
if [ -n "${VM_IPV6}" ]; then
# Ajout de la route associée
echo "ip -6 route add ${VM_IPV6}/48 dev tap${VM_ID}i0 (source : CACHE)"
write_log INFO "ip -6 route add ${VM_IPV6}/48 dev tap${VM_ID}i0 (source: cache)"
ip -6 route add ${VM_IPV6}/48 dev tap${VM_ID}i0
BOOL_V6=1
else
# En cas d'echec, on continue de parcourir les autres RANGES
echo "Valeur vide ou ne correspond pas à un range IPV6"
echo "$(date -Isecond) - NOTICE : Valeur vide ou ne correspond pas à un range IPV6" >> ${LOG_PLACE}/$(date -Idate)_vm_id.log
fi
else
# En cas d'echec, on continue de parcourir les autres RANGES
echo "Echec de la récupération des infos V6, code HTTP reçu différent de 200 : ${CODE_RETOUR_INFOS_V6}"
echo "$(date -Isecond) - NOTICE : Echec de la récupération des infos V6, code HTTP reçu différent de 200 : ${CODE_RETOUR_INFOS_V6}" >> ${LOG_PLACE}/$(date -Idate)_vm_id.log
fi
fi
done
fi
write_log INFO 'Script ended'
exit 0