aboutsummaryrefslogtreecommitdiffstats
path: root/deploy/network.sh
blob: 836af0f9a5bddb1f6755c71151c35bbbc3a2ee6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
function setup_bridge_net()
{
    net_name=$1
    nic=$2

    sudo virsh net-destroy $net_name
    sudo virsh net-undefine $net_name

    sed -e "s/REPLACE_NAME/$net_name/g" \
        -e "s/REPLACE_NIC/$nic/g" \
    $COMPASS_DIR/deploy/template/network/bridge_nic.xml \
    > $WORK_DIR/network/$net_name.xml

    sudo virsh net-define $WORK_DIR/network/$net_name.xml
    sudo virsh net-start $net_name
}

function save_network_info()
{
    sudo ovs-vsctl list-br |grep br-external
    br_exist=$?
    external_nic=`ip route |grep '^default'|awk '{print $NF}'`
    route_info=`ip route |grep -Eo '^default via [^ ]+'`
    ip_info=`ip addr show $external_nic|grep -Eo '[^ ]+ brd [^ ]+ '`
    if [ $br_exist -eq 0 ]; then
        if [ "$external_nic" != "br-external" ]; then
            sudo ovs-vsctl --may-exist add-port br-external $external_nic
            sudo ip addr flush $external_nic
            sudo ip addr add $ip_info dev br-external
            sudo ip route add $route_info dev br-external
        fi
    else
        sudo ovs-vsctl add-br br-external
        sudo ovs-vsctl add-port br-external $external_nic
        sudo ip addr flush $external_nic
        sudo ip addr add $ip_info dev br-external
        sudo ip route add $route_info dev br-external
    fi
}

function setup_bridge_external()
{
    sudo virsh net-destroy external
    sudo virsh net-undefine external

    save_network_info
    sed -e "s/REPLACE_NAME/external/g" \
        -e "s/REPLACE_OVS/br-external/g" \
    $COMPASS_DIR/deploy/template/network/bridge_ovs.xml \
    > $WORK_DIR/network/external.xml

    sudo virsh net-define $WORK_DIR/network/external.xml
    sudo virsh net-start external
}

function setup_nat_net() {
    net_name=$1
    gw=$2
    mask=$3
    ip_start=$4
    ip_end=$5

    sudo virsh net-destroy $net_name
    sudo virsh net-undefine $net_name
    # create install network
    sed -e "s/REPLACE_BRIDGE/br_$net_name/g" \
        -e "s/REPLACE_NAME/$net_name/g" \
        -e "s/REPLACE_GATEWAY/$gw/g" \
        -e "s/REPLACE_MASK/$mask/g" \
        -e "s/REPLACE_START/$ip_start/g" \
        -e "s/REPLACE_END/$ip_end/g" \
        $COMPASS_DIR/deploy/template/network/nat.xml \
        > $WORK_DIR/network/$net_name.xml

    sudo virsh net-define $WORK_DIR/network/$net_name.xml
    sudo virsh net-start $net_name
}

function create_nets() {
    setup_nat_net mgmt $MGMT_GW $MGMT_MASK $MGMT_IP_START $MGMT_IP_END

    # create install network
    if [[ -n $INSTALL_NIC ]]; then
        setup_bridge_net install $INSTALL_NIC
    else
        setup_nat_net install $INSTALL_GW $INSTALL_MASK
    fi

    # create external network
    setup_bridge_external
}