117 lines
No EOL
5.3 KiB
Bash
117 lines
No EOL
5.3 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 "veth-${subnet_id}-ext" > /dev/null || return 1
|
|
ip -n "${vpc_name}" link show | grep -E '^[0-9]*:'|sed -e 's/ //g' | cut -d: -f 2 | grep "veth-${subnet_id}-int" > /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 "veth-${subnet_id}-ext" type veth peer name "veth-${subnet_id}-int" 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}" "veth-${subnet_id}-ext"
|
|
ip netns exec "${vpc_name}" brctl addif "br-${subnet_id}" "veth-${subnet_id}-int"
|
|
brctl addif "br-${subnet_id}" "vxlan-${vxlan_id}"
|
|
|
|
|
|
print_in_color "${COLOR_GREEN}" " - up interface"
|
|
ip link set up dev "veth-${subnet_id}-ext"
|
|
ip link set up dev "vxlan-${vxlan_id}"
|
|
ip link set up dev "br-${subnet_id}"
|
|
ip -n "${vpc_name}" link set up dev "veth-${subnet_id}-int"
|
|
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
|
|
|
|
generate_dhcp_file "${subnet}" "${vpc_name}_br-${subnet_id}" "${gateway_ip}"
|
|
systemctl start "dnsmasq@${vpc_name}_br-${subnet_id}.service"
|
|
|
|
}
|
|
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}-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}"
|
|
} |