two_with_bash/lib/subnet.sh
GnomeZworc 545c9d61aa
fix syntax
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2026-04-06 18:23:44 +02:00

115 lines
5.2 KiB
Bash

#!/bin/bash
. ./lib/db.sh
. ./lib/colors.sh
. ./lib/vpc.sh
. ./lib/dhcp.sh
function check_subnet_exist {
local vpc_name="${1}"
local subnet_name="${2}"
local vxlan_id="${3}"
local subnet_id="${4}"
print_in_color "${COLOR_GREY}" "Check in db if ${subnet_name} exist"
check_in_db subnet "${subnet_name};${vpc_name}"
[ "$?" -eq "0" ] || return 1
print_in_color "${COLOR_GREY}" "Check in linux if ${subnet_name} exist"
ip link show | grep -E '^[0-9]*:'|sed -e 's/ //g' | cut -d: -f 2 | grep "br-${subnet_id}" > /dev/null || return 1
ip -n "${vpc_name}" link show | grep -E '^[0-9]*:'|sed -e 's/ //g' | cut -d: -f 2 | grep "br-${subnet_id}" > /dev/null || return 1
ip link show | grep -E '^[0-9]*:'|sed -e 's/ //g' | cut -d: -f 2 | grep "vs-${subnet_id}-e" > /dev/null || return 1
ip -n "${vpc_name}" link show | grep -E '^[0-9]*:'|sed -e 's/ //g' | cut -d: -f 2 | grep "vs-${subnet_id}-i" > /dev/null || return 1
ip link show | grep -E '^[0-9]*:'|sed -e 's/ //g' | cut -d: -f 2 | grep "vxlan-${vxlan_id}" > /dev/null || return 1
return 0
}
function create_subnet {
local vpc_name="${1}"
local subnet_name="${2}"
local vxlan_id="${3}"
local local_ip="${4}"
local gateway_ip="${5}"
local subnet="${6}"
local subnet_id="$(echo "${subnet_name}" | cut -d\- -f2)"
check_subnet_exist "${vpc_name}" "${subnet_name}" "${vxlan_id}" "${subnet_id}"
[ "$?" -eq "0" ] && \
{
print_in_color "${COLOR_ORANGE}" "Would have create ${subnet_name}"
} || \
{
print_in_color "${COLOR_GREEN}" "Create ${subnet_name}"
print_in_color "${COLOR_GREEN}" " - add ${subnet_name} in db"
add_in_db "subnet" "${subnet_name}" "${vpc_name}" "${vxlan_id}" "${local_ip}" "${gateway_ip}" "${subnet}"
print_in_color "${COLOR_GREEN}" " - create veth"
ip link add "vs-${subnet_id}-e" type veth peer name "vs-${subnet_id}-i" netns "${vpc_name}"
print_in_color "${COLOR_GREEN}" " - add bridges"
brctl addbr "br-${subnet_id}"
brctl stp "br-${subnet_id}" off
ip netns exec "${vpc_name}" brctl addbr "br-${subnet_id}"
ip netns exec "${vpc_name}" brctl stp "br-${subnet_id}" off
print_in_color "${COLOR_GREEN}" " - add vxlan"
ip link add "vxlan-${vxlan_id}" type vxlan \
id "${vxlan_id}" \
dstport 4789 \
local "${local_ip}" \
nolearning
print_in_color "${COLOR_GREEN}" " - add interface in bridge"
brctl addif "br-${subnet_id}" "vs-${subnet_id}-e"
ip netns exec "${vpc_name}" brctl addif "br-${subnet_id}" "vs-${subnet_id}-i"
brctl addif "br-${subnet_id}" "vxlan-${vxlan_id}"
print_in_color "${COLOR_GREEN}" " - up interface"
ip link set up dev "vs-${subnet_id}-e"
ip link set up dev "vxlan-${vxlan_id}"
ip link set up dev "br-${subnet_id}"
ip -n "${vpc_name}" link set up dev "vs-${subnet_id}-i"
ip -n "${vpc_name}" link set up dev "br-${subnet_id}"
print_in_color "${COLOR_GREEN}" " - add subnet ip"
ip -n "${vpc_name}" a add "${gateway_ip}" dev "br-${subnet_id}"
print_in_color "${COLOR_GREEN}" " - add subnet route"
ip -n "${vpc_name}" r add "${subnet}" dev "br-${subnet_id}" scope link
print_in_color "${COLOR_GREEN}" " - add subnet firewall"
ebtables -A FORWARD --out-interface "br-${subnet_id}" -p arp --arp-op Request --arp-ip-dst "${gateway_ip}" -j DROP
ebtables -A FORWARD --out-interface "br-${subnet_id}" -p IPv4 --ip-protocol udp --ip-source-port 67:68 --ip-destination-port 67:68 -j DROP
/opt/two/bin/dhcp -name "${vpc_name}_br-${subnet_id}" -subnet "${subnet}" -gateway "${gateway_ip}"
}
return 0
}
function delete_subnet {
local subnet_name="${1}"
local subnet_id="$(echo "${subnet_name}" | cut -d\- -f2)"
subnet_def=$(get_from_db "subnet" "${subnet_name}")
local vpc_name="$(echo "${subnet_def}" | cut -d\; -f 2)"
local vxlan_id="$(echo "${subnet_def}" | cut -d\; -f 3)"
local gateway_ip="$(echo "${subnet_def}" | cut -d\; -f 5)"
local subnet="$(echo "${subnet_def}" | cut -d\; -f 6)"
print_in_color "${COLOR_RED}" "Delete ${subnet_name}"
delete_in_db "subnet" "${subnet_name}"
ip -n "${vpc_name}" route del "${subnet}" dev "br-${subnet_id}"
ip link del dev "vxlan-${vxlan_id}"
systemctl stop "dnsmasq@${vpc_name}_br-${subnet_id}.service"
rm /etc/dnsmasq.d/${vpc_name}_br-${subnet_id}.conf
ebtables -D FORWARD -p arp --arp-op Request --arp-ip-dst "${gateway_ip}" --out-interface "br-${subnet_id}" -j DROP
ebtables -D FORWARD --out-interface "br-${subnet_id}" -p IPv4 --ip-protocol udp --ip-source-port 67:68 --ip-destination-port 67:68 -j DROP
ip link del dev "br-${subnet_id}"
ip -n "${vpc_name}" link del dev "br-${subnet_id}"
ip link del dev "veth-${subnet_id}-ext"
print_in_color "${COLOR_GREY}" "Try to delete ${vpc_name}"
count_in_db "subnet" "${vpc_name}"
[ "$?" -eq "0" ] && delete_vpc "${vpc_name}"
}