summaryrefslogtreecommitdiffstats
path: root/deploy/network.sh
diff options
context:
space:
mode:
Diffstat (limited to 'deploy/network.sh')
-rwxr-xr-xdeploy/network.sh173
1 files changed, 32 insertions, 141 deletions
diff --git a/deploy/network.sh b/deploy/network.sh
index e5133c9e..ac0a73e2 100755
--- a/deploy/network.sh
+++ b/deploy/network.sh
@@ -1,163 +1,54 @@
-function destroy_nat() {
- sudo virsh net-destroy $1 2>&1
- sudo virsh net-undefine $1 2>&1
- rm -rf $COMPASS_DIR/deploy/work/network/$1.xml
-}
-
-function destroy_bridge()
-{
- bridge=$1
- nic=$2
- ip="$3"
-
- brige_info=$(ip addr show $bridge 2>/dev/null)
- if [[ -z $brige_info ]]; then
- return
- fi
-
- if [[ -n $ip ]]; then
- ip addr delete $ip dev $bridge | true
- fi
-
- ips=`echo "$brige_info" | grep 'inet ' | sed "s/inet //g"`
-
- routes=$(ip route show | grep $bridge)
-
- ip link set $bridge down
-
- brctl delbr $bridge
-
- if [[ -n $ips ]]; then
- echo "$ips" | while read line; do
- echo $line | sed "s/$bridge/dev $nic/g" | xargs ip addr add | true
- done
- fi
-
- if [[ -n $routes ]]; then
- echo "$routes" | while read line; do
- echo $line | sed "s/$bridge/$nic/g" | xargs ip route add | true
- done
- fi
-}
-
-function get_broadcast_addr()
-{
- ip=(${1//[!0-9]/ })
- mask=(${2//[!0-9]/ })
-
- for i in {0..3}; do
- num=$((${ip[$i]} | $((${mask[$i]} ^ 255))))
- if [[ -z $broadcast ]]; then
- broadcast="$num"
- else
- broadcast="$broadcast.$num"
- fi
- done
- echo ${broadcast}
-}
-function get_mask_len()
+function setup_bridge_net()
{
- mask=`echo $1 | awk -F'.' '{print ($1*(2^24)+$2*(2^16)+$3*(2^8)+$4)}'`
- mask_len=`echo "obase=2;${mask}"|bc|awk -F'0' '{print length($1)}'`
- echo $mask_len
-}
-
-function create_bridge()
-{
- bridge=$1
+ net_name=$1
nic=$2
- ips=$(ip addr show $nic | grep 'inet ' | sed "s/inet //g")
- routes=$(ip route show | grep $nic)
- ip addr flush $nic
-
- brctl addbr $bridge
- brctl addif $bridge $nic
- ip link set $bridge up
-
- if [[ -n $ips ]]; then
- echo "$ips" | while read line; do
- echo $line | sed -e "s/$nic/dev $bridge/g" | xargs ip addr add
- done
- fi
+ 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
- if [[ -n $routes ]]; then
- echo "$routes" | while read line; do
- echo $line | sed "s/$nic/$bridge/g" | xargs ip route add | true
- done
- fi
+ sudo virsh net-define $WORK_DIR/network/$net_name.xml
+ sudo virsh net-start $net_name
}
-function add_install_ip() {
- mask_len=`get_mask_len $INSTALL_MASK`
- broadcast=`get_broadcast_addr $INSTALL_GW $INSTALL_MASK`
- ip addr add $INSTALL_GW/$mask_len brd $broadcast dev $bridge
-}
+function setup_nat_net() {
+ net_name=$1
+ gw=$2
+ mask=$3
+ ip_start=$4
+ ip_end=$5
-function setup_install_bridge() {
- destroy_bridge br_install $INSTALL_NIC $INSTALL_GW
- create_bridge br_install $INSTALL_NIC
- add_install_ip
-}
-
-function setup_external_net() {
- if [[ -z `brctl show br_external 2>/dev/null` ]]; then
- brctl addbr br_external
- fi
-
- if [[ -z $EXTERNAL_NIC ]]; then
- return
- fi
-
- if [[ $TYPE == baremetal && $EXTERNAL_NIC == $INSTALL_NIC ]]; then
- exit 1
- fi
-
- destroy_bridge br_external $EXTERNAL_NIC ""
- create_bridge br_external $EXTERNAL_NIC
-}
-
-function setup_install_nat() {
- destroy_nat install
+ sudo virsh net-destroy $net_name
+ sudo virsh net-undefine $net_name
# create install network
- sed -e "s/REPLACE_BRIDGE/br_install/g" \
- -e "s/REPLACE_NAME/install/g" \
- -e "s/REPLACE_GATEWAY/$INSTALL_GW/g" \
- -e "s/REPLACE_MASK/$INSTALL_MASK/g" \
- -e "s/REPLACE_START/$INSTALL_IP_START/g" \
- -e "s/REPLACE_END/$INSTALL_IP_END/g" \
+ 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/install.xml
+ > $WORK_DIR/network/$net_name.xml
- sudo virsh net-define $WORK_DIR/network/install.xml
- sudo virsh net-start install
+ sudo virsh net-define $WORK_DIR/network/$net_name.xml
+ sudo virsh net-start $net_name
}
function create_nets() {
- destroy_nat mgmt
- # create mgmt network
- sed -e "s/REPLACE_BRIDGE/br_mgmt/g" \
- -e "s/REPLACE_NAME/mgmt/g" \
- -e "s/REPLACE_GATEWAY/$MGMT_GW/g" \
- -e "s/REPLACE_MASK/$MGMT_MASK/g" \
- -e "s/REPLACE_START/$MGMT_IP_START/g" \
- -e "s/REPLACE_END/$MGMT_IP_END/g" \
- $COMPASS_DIR/deploy/template/network/nat.xml \
- > $WORK_DIR/network/mgmt.xml
-
- sudo virsh net-define $WORK_DIR/network/mgmt.xml
- sudo virsh net-start mgmt
+ setup_nat_net mgmt $MGMT_GW $MGMT_MASK $MGMT_IP_START $MGMT_IP_END
# create install network
- if [[ ! -z $VIRT_NUMBER ]];then
- setup_install_nat
+ if [[ -n $INSTALL_NIC ]]; then
+ setup_bridge_net install $INSTALL_NIC
else
- setup_install_bridge
+ setup_nat_net install $INSTALL_GW $INSTALL_MASK
fi
# create external network
- setup_external_net
+ setup_bridge_net external $EXTERNAL_NIC
}
-