Matteo Scandolo | aca8665 | 2017-08-08 13:05:27 -0700 | [diff] [blame^] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
AyumuUeha | 76a01bc | 2017-05-18 13:34:13 +0900 | [diff] [blame] | 17 | #!/bin/bash |
| 18 | #************************************************************/ |
| 19 | #** File: vsg_respin_vcpeproxy_docker.sh */ |
| 20 | #** Contents: Contains shell script to setup dnsmasq , */ |
| 21 | #** CP plane path,etc and respin the vcpe */ |
| 22 | #** docker instance created by XOS. */ |
| 23 | #** */ |
| 24 | #** This script should be executed as super-user */ |
| 25 | #************************************************************/ |
| 26 | |
| 27 | echo "vsg_respin_vcpeproxy_docker.sh Execution: Begin" |
| 28 | function setup_cp_path_in_docker (){ |
| 29 | # Set up cp networking in docker using pipework |
| 30 | echo "Executing setup_cp_path_in_docker...." |
| 31 | # |
| 32 | # TODO: FOr some reason, MAC address argument seems to throw pipework off |
| 33 | # balance. Use auto-generated MAC address |
| 34 | # |
| 35 | # docker exec $VCPEPROXY_NAME ifconfig $VCPEPROXY_CP_IFACE>> /dev/null || pipework $VSG_CP_IFACE -i $VCPEPROXY_CP_IFACE $VCPEPROXY_NAME $VCPEPROXY_CP_IP/$VCPEGW_NETMASK_BITS@$VCPEPROXY_BRIDGE_IP |
| 36 | docker exec $VCPEPROXY_NAME ifconfig $VCPEPROXY_CP_IFACE>> /dev/null || pipework $VSG_CP_IFACE -i $VCPEPROXY_CP_IFACE $VCPEPROXY_NAME $VCPEPROXY_CP_IP/$VCPEGW_NETMASK_BITS |
| 37 | sleep 1 |
| 38 | echo "waiting for $VCPEPROXY_CP_IFACE" |
| 39 | pipework --wait -i $VCPEPROXY_CP_IFACE |
| 40 | sleep 1 |
| 41 | echo "$VCPEPROXY_CP_IFACE interface in container is UP" |
| 42 | docker exec $VCPEPROXY_NAME ifconfig $VCPEPROXY_CP_IFACE |
| 43 | docker exec $VCPEPROXY_NAME route add -net 10.6.1.0 netmask 255.255.255.0 gw 10.6.1.129 dev eth0 |
| 44 | } |
| 45 | |
| 46 | # |
| 47 | ## Prepare container volume files to be mounted to the docker instance |
| 48 | # |
| 49 | |
| 50 | function update_mounted_files() { |
| 51 | sed -i "s/@@VCPEPROXY_DHCP_LISTEN_ADDRESS@@/${VCPEPROXY_DHCP_LISTEN_ADDRESS}/g" ${DNSMASQ_VCPE_CONF} |
| 52 | sed -i "s/@@VCPEPROXY_DHCP_LOW@@/${VCPEPROXY_DHCP_LOW}/g" ${DNSMASQ_VCPE_CONF} |
| 53 | sleep 1 |
| 54 | sed -i "s/@@VCPEPROXY_DHCP_HIGH@@/${VCPEPROXY_DHCP_HIGH}/g" ${DNSMASQ_VCPE_CONF} |
| 55 | |
| 56 | # |
| 57 | # Update rc.local file |
| 58 | # |
| 59 | sed -i "s/@@VCPEPROXY_SUBNET@@/${VCPEPROXY_SUBNET}/g" ${RC_LOCAL} |
| 60 | } |
| 61 | |
| 62 | function assign_addresses_to_vcpeproxy() { |
| 63 | MAC_3OCT=$(( STAG % 256 )) |
| 64 | MAC_2OCT=$(( CTAG % 256 )) |
| 65 | MAC_1OCT=$(( VCPE_PROXY_ID % 256 )) |
| 66 | HEX_MAC_3OCT=$( printf "%02x" $MAC_3OCT ) |
| 67 | HEX_MAC_2OCT=$( printf "%02x" $MAC_2OCT ) |
| 68 | HEX_MAC_1OCT=$( printf "%02x" $MAC_1OCT ) |
| 69 | VCPEPROXY_CP_MAC=`echo $VCPEPROXY_CP_MAC_PREFIX:$HEX_MAC_3OCT:$HEX_MAC_2OCT:$HEX_MAC_1OCT` |
| 70 | echo "VCPEPROXY_CP_MAC .....$VCPEPROXY_CP_MAC" |
| 71 | export VCPEPROXY_CP_MAC |
| 72 | # |
| 73 | # |
| 74 | # vcpe_gwbr_ip is computed in nova_vsg_setup.sh script. |
| 75 | # |
| 76 | VCPEPROXY_BRIDGE_IP=`echo $vcpe_gwbr_ip` |
| 77 | export VCPEPROXY_BRIDGE_IP |
| 78 | echo "VCPE_BRIDGE_IP.......$VCPEPROXY_BRIDGE_IP" |
| 79 | # |
| 80 | # D-octet of the VCPEPROXY_CP_IP address is |
| 81 | # calculated under the assumption each VCPE_PROXY is assigned |
| 82 | # 4 (NUM_HOSTS value) additional WAN address one for each AP. |
| 83 | # |
| 84 | echo "CP_IP_PREFIX = $VCPEPROXY_CP_IP_PREFIX" |
| 85 | VCPEPROXY_CP_IP_CVAL=$(( VSG_ID % 256 )) |
| 86 | VCPEPROXY_CP_IP_DVAL=$(( (VCPE_PROXY_ID-1)*(NUM_HOSTS+1) +1 )) |
| 87 | VCPEPROXY_CP_IP=`echo $VCPEPROXY_CP_IP_PREFIX.$VCPEPROXY_CP_IP_CVAL.$VCPEPROXY_CP_IP_DVAL` |
| 88 | export VCPEPROXY_CP_IP |
| 89 | echo "VCPEPROXY_CP_IP..... $VCPEPROXY_CP_IP" |
| 90 | |
| 91 | VCPEPROXY_DHCP_LISTEN_ADDRESS=$VCPEPROXY_LOCAL_IP |
| 92 | export VCPEPROXY_DHCP_LISTEN_ADDRESS |
| 93 | echo "VCPEPROXY_DHCP_LISTEN_ADDRESS $VCPEPROXY_DHCP_LISTEN_ADDRESS" |
| 94 | |
| 95 | VCPEPROXY_DHCP_LOW=`echo $VCPEPROXY_LOCAL_IP_PREFIX.$VCPEPROXY_DHCP_LOW` |
| 96 | export VCPEPROXY_DHCP_LOW |
| 97 | echo "VCPEPROXY_AP_DHCP_LOW $VCPEPROXY_DHCP_LOW" |
| 98 | VCPEPROXY_DHCP_HIGH=`echo $VCPEPROXY_LOCAL_IP_PREFIX.$VCPEPROXY_DHCP_HIGH` |
| 99 | export VCPEPROXY_DHCP_HIGH |
| 100 | echo "VCPEPROXY_DHCP_HIGH $VCPEPROXY_DHCP_HIGH" |
| 101 | |
| 102 | VCPEPROXY_SUBNET=`echo $VCPEPROXY_LOCAL_IP_PREFIX.0` |
| 103 | export VCPEPROXY_SUBNET |
| 104 | echo "VCPEPROXY_SUBNET $VCPEPROXY_SUBNET" |
| 105 | } |
| 106 | |
| 107 | function pause_and_update_container_volume() { |
| 108 | echo "Entering pause_and_update_container_volume" |
| 109 | # |
| 110 | # Docker containers created by XOS run in auto-start mode. |
| 111 | # So, just pause the container instance and restart it to |
| 112 | # pickup DHCP changes. |
| 113 | # |
| 114 | echo "Pausing the container $VCPEPROXY_NAME" |
| 115 | if docker ps |grep $VCPEPROXY_NAME ; then |
| 116 | docker pause $VCPEPROXY_NAME |
| 117 | sleep 2 |
| 118 | docker ps -a |
| 119 | fi |
| 120 | echo "Checking container directory" |
| 121 | if [ -d $CONTAINER_DIR ]; then |
| 122 | echo "$CONTAINER_DIR exists...Just replace vcpe.conf and rc.local with template files" |
| 123 | cp $HOME_DIR/docker_mounts/etc/dnsmasq.d/vcpe.conf $DNSMASQ_VCPE_CONF |
| 124 | cp $HOME_DIR/docker_mounts/mount/rc.local $RC_LOCAL |
| 125 | else |
| 126 | echo "$CONATAINER_DIR does not exist..create it first" |
| 127 | echo "Creating container directory" |
| 128 | mkdir $CONTAINER_DIR |
| 129 | echo "Copying container volumes" |
| 130 | cp -r $HOME_DIR/docker_mounts/* $CONTAINER_DIR |
| 131 | fi |
| 132 | } |
| 133 | |
| 134 | function unpause_and_respin_docker() { |
| 135 | echo "Spinning up the stopped container" |
| 136 | docker unpause $VCPEPROXY_NAME |
| 137 | sleep 1 |
| 138 | docker restart $VCPEPROXY_NAME |
| 139 | sleep 5 |
| 140 | if docker ps -a |grep $VCPEPROXY_NAME |grep Up ; then |
| 141 | echo "Docker instance is up and running.." |
| 142 | else |
| 143 | echo "Error:...Docker instance $VCPEPROXY_NAME is not Up..." |
| 144 | fi |
| 145 | echo "Stopping ufw firewall in docker" |
| 146 | docker exec $VCPEPROXY_NAME ufw disable |
| 147 | docker exec $VCPEPROXY_NAME ufw status |
| 148 | } |
| 149 | |
| 150 | echo "Begin: Starting execution of $0 script" |
| 151 | |
| 152 | if [ -z $vsg_home_dir ]; then |
| 153 | echo "WARNING...HOME Directory may not be setup properly" |
| 154 | vsg_home_dir=`pwd` |
| 155 | fi |
| 156 | echo "Dump environment variables" |
| 157 | env >/tmp/vsg_env.out |
| 158 | |
| 159 | echo "Trying to respin $VCPEPROXY_NAME: [$STAG] [$CTAG]" |
| 160 | docker inspect $VCPEPROXY_NAME > /dev/null 2>&1 |
| 161 | |
| 162 | if [ "$?" == 1 ]; then |
| 163 | echo "$VCPEPROXY_NAME is not running!!!" |
| 164 | exit 1 |
| 165 | fi |
| 166 | if [ -z $VCPE_PROXY_ID ]; then |
| 167 | echo "WARNING****VCPE_PROXY_ID is not set.." |
| 168 | exit 1 |
| 169 | fi |
| 170 | if [ -z $CONTAINER_VOLUMES ];then |
| 171 | echo "WARNING**** CONTAINER_VOLUMES not set.." |
| 172 | CONTAINER_VOLUMES=/var/container_volumes; export CONTAINER_VOLUMES |
| 173 | fi |
| 174 | if [ -z DOCKER_SPINUP_DIR ]; then |
| 175 | echo "WARNING***** DOCKER SPINUP directory not set..." |
| 176 | DOCKER_SPINUP_DIR=/usr/local/sbin; export DOCKER_SPINUP_DIR |
| 177 | fi |
| 178 | HOME_DIR=$vsg_home_dir; export HOME_DIR |
| 179 | CONTAINER_DIR=$CONTAINER_VOLUMES/$VCPEPROXY_NAME |
| 180 | |
| 181 | VCPEPROXY_DHCP_HIGH=$(( VCPEPROXY_DHCP_LOW + VCPEPROXY_NUM_HOSTS )) |
| 182 | |
| 183 | DNSMASQ_VCPE_CONF=$CONTAINER_DIR/etc/dnsmasq.d/vcpe.conf;export DNSMASQ_VCPE_CONF |
| 184 | RC_LOCAL=$CONTAINER_DIR/mount/rc.local;export RC_LOCAL |
| 185 | |
| 186 | echo "Pausing the docker container $VCPEPROXY_NAME" |
| 187 | pause_and_update_container_volume |
| 188 | echo "Assign addresses for vcpe_proxy" |
| 189 | assign_addresses_to_vcpeproxy |
| 190 | echo "Updating container volumes" |
| 191 | update_mounted_files |
| 192 | unpause_and_respin_docker |
| 193 | echo "Setting up vcpeproxy control plane path.." |
| 194 | setup_cp_path_in_docker |
| 195 | |
| 196 | echo "Execution of vsg_respin_vcpeproxy_docker.sh script is complete: End" |
| 197 | |