create subnet global management #15

Open
opened 2026-01-10 00:57:00 +00:00 by nicolas.boufideline · 0 comments

pour le moment c'est du bash, il faut le passer en compiler

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-${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}"
}
pour le moment c'est du bash, il faut le passer en compiler ```bash 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 } ``` ```bash 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}" } ```
Sign in to join this conversation.
No milestone
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
syonad/two#15
No description provided.