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 | #!/usr/bin/env bash |
| 18 | #************************************************************/ |
| 19 | #** File: nova_vsg_setup.sh */ |
| 20 | #** Contents: Contains shell script to setup VSG and to */ |
| 21 | #** monitor creation of vcpe docker in VSG */ |
| 22 | #************************************************************/ |
| 23 | |
| 24 | date |
| 25 | echo "nova_vsg_setup.sh: Begin" |
| 26 | |
| 27 | function generate_vsg_mac() { |
| 28 | # Check if VSG IP exists, if it does not exist, create new Proxy LXC |
| 29 | if ! grep -q "$vsgIp$vsg_str" "$file_vsg_ip"; then |
| 30 | # get the last line of previous vSG IP, if exists |
| 31 | if [ -f "$file_vsg_ip" ]; then |
| 32 | last_line=$( tail -1 $file_vsg_ip | head -1 ) |
| 33 | value=${last_line#*:} |
| 34 | var=1 |
| 35 | |
| 36 | for arr_val in $value; do |
| 37 | if [ $var == 1 ]; then |
| 38 | vsg_eth0=$( printf '%d\n' "0x${arr_val}" ) |
| 39 | vsg_eth0=$((vsg_eth0+1)) |
| 40 | elif [ $var == 2 ]; then |
| 41 | vsg_eth1=$( printf '%d\n' "0x${arr_val}" ) |
| 42 | vsg_eth1=$((vsg_eth1+1)) |
| 43 | elif [ $var == 3 ]; then |
| 44 | let "lxc = 0x3e" |
| 45 | fi |
| 46 | var=$((var+1)) |
| 47 | done |
| 48 | else |
| 49 | let "vsg_eth0 = 0x16" |
| 50 | let "vsg_eth1 = 0x21" |
| 51 | fi |
| 52 | |
| 53 | hex_vsg_eth0=$( printf "%02x\n" $vsg_eth0 ) |
| 54 | hex_vsg_eth1=$( printf "%02x\n" $vsg_eth1 ) |
| 55 | hex_lxc=$( printf "%02x\n" $lxc ) |
| 56 | else |
| 57 | echo "vsgIP already in file" |
| 58 | fi |
| 59 | } |
| 60 | |
| 61 | function vsg_script_transfer() { |
| 62 | echo "vsg_script_transfer" |
| 63 | scp -r $PPPOE_APPS_DIR/$PPPOE_AP_MGMT_DIR "ubuntu@$vsgIp:$vsg_home_dir" |
| 64 | scp -r $PPPOE_APPS_DIR/$IPV6_AP_DIR "ubuntu@$vsgIp:$vsg_home_dir" |
| 65 | scp $HOME_DIR/$vsg_vcpe_proxy_setup_script "ubuntu@$vsgIp:$vsg_home_dir" |
| 66 | scp $HOME_DIR/$respin_vcpeproxy_docker_script "ubuntu@$vsgIp:$vsg_home_dir" |
| 67 | scp $HOME_DIR/$proxy_ap_mgmt_start_script "ubuntu@$vsgIp:$vsg_home_dir" |
| 68 | scp $HOME_DIR/$proxy_ap_mgmt_stop_script "ubuntu@$vsgIp:$vsg_home_dir" |
| 69 | scp $HOME_DIR/$proxy_ipv6_setup "ubuntu@$vsgIp:$vsg_home_dir" |
| 70 | } |
| 71 | |
| 72 | function setup_vcpe_monitoring() { |
| 73 | echo "Entering setup_vcpe_monitoring..." |
| 74 | |
| 75 | # |
| 76 | # Install the necessary software in vSG |
| 77 | # WIll not necessary after creating custom docker image |
| 78 | # with all the necessary software pre-installed. |
| 79 | # |
| 80 | vsg_ssh_setup |
| 81 | # Load PPPoE VCPE docker image to avoid pulling from docker repository |
| 82 | transfer_gw_scripts_and_docker_files |
| 83 | install_soft |
| 84 | # |
| 85 | # In CORD-2.0, we will use vcpe docker instance created by XOS. |
| 86 | # So, there is no need to create a separate dpbr0 bridge in VSG |
| 87 | # |
| 88 | create_vsg_env_file |
| 89 | scp $HOME_DIR/$vsg_env_file ubuntu@$vsgIp:$vsg_home_dir |
| 90 | } |
| 91 | |
| 92 | function create_vsg_env_file() { |
| 93 | echo "Entering create_vsg_env_file....." |
| 94 | echo "vsg_home_dir=$vsg_home_dir; export vsg_home_dir" >>$HOME_DIR/$vsg_env_file |
| 95 | # |
| 96 | # Scripts running in VSG and VCP refer to the location of |
| 97 | # artifacts in their environment by using the env. variable $HOME_DIR |
| 98 | # HOME_DIR is same as $vsg_home_dir. |
| 99 | # |
| 100 | echo "HOME_DIR=$vsg_home_dir; export HOME_DIR" >>$HOME_DIR/$vsg_env_file |
| 101 | echo "vcpe_monitor_script=$vcpe_monitor_script; export vcpe_monitor_script" >>$HOME_DIR/$vsg_env_file |
| 102 | echo "vcpe_setup_script=$vcpe_setup_script; export vcpe_setup_script" >>$HOME_DIR/$vsg_env_file |
| 103 | echo "file_vsg_ip=$file_vsg_ip; export file_vsg_ip" >>$HOME_DIR/$vsg_env_file |
| 104 | echo "vsg_id=$vsg_id; export vsg_id" >>$HOME_DIR/$vsg_env_file |
| 105 | echo "file_vcpe_id=$file_vcpe_id; export file_vcpe_id" >>$HOME_DIR/$vsg_env_file |
| 106 | echo "file_vcpe_names=$file_vcpe_names; export file_vcpe_names" >>$HOME_DIR/$vsg_env_file |
| 107 | echo "nova_compute_ip=$nova_compute_ip; export nova_compute_ip" >>$HOME_DIR/$vsg_env_file |
| 108 | echo "br_wan_ip=$br_wan_ip; export br_wan_ip" >>$HOME_DIR/$vsg_env_file |
| 109 | echo "docker_mount_file=$docker_mount_file; export docker_mount_file" >>$HOME_DIR/$vsg_env_file |
| 110 | echo "proxy_ap_mgmt_start_script=$proxy_ap_mgmt_start_script; export proxy_ap_mgmt_start_script" >>$HOME_DIR/$vsg_env_file |
| 111 | echo "proxy_ipv6_setup=$proxy_ipv6_setup; export proxy_ipv6_setup" >>$HOME_DIR/$vsg_env_file |
| 112 | echo "proxy_ap_mgmt_stop_script=$proxy_ap_mgmt_stop_script; export proxy_ap_mgmt_stop_script" >>$HOME_DIR/$vsg_env_file |
| 113 | echo "vsg_vcpe_gwbr_setup_script=$vsg_vcpe_gwbr_setup_script; export vsg_vcpe_gwbr_setup_script" >>$HOME_DIR/$vsg_env_file |
| 114 | echo "vsg_vcpe_proxy_setup_script=$vsg_vcpe_proxy_setup_script; export vsg_vcpe_proxy_setup_script" >>$HOME_DIR/$vsg_env_file |
| 115 | echo "respin_vcpeproxy_docker_script=$respin_vcpeproxy_docker_script; export respin_vcpeproxy_docker_script" >>$HOME_DIR/$vsg_env_file |
| 116 | echo "vcpe_gwbr_ip=$vcpe_gwbr_ip; export vcpe_gwbr_ip" >>$HOME_DIR/$vsg_env_file |
| 117 | echo "CONTAINER_VOLUMES=$CONTAINER_VOLUMES; export CONTAINER_VOLUMES" >>$HOME_DIR/$vsg_env_file |
| 118 | echo "DOCKER_SPINUP_DIR=$DOCKER_SPINUP_DIR; export DOCKER_SPINUP_DIR" >>$HOME_DIR/$vsg_env_file |
| 119 | echo "vsg_env_file=$vsg_env_file; export vsg_env_file" >>$HOME_DIR/$vsg_env_file |
| 120 | echo "pppoe_vcpe_image_tar=$pppoe_vcpe_image_tar; export pppoe_vcpe_image_tar" >>$HOME_DIR/$vsg_env_file |
| 121 | } |
| 122 | |
| 123 | function install_soft() { |
| 124 | echo "Installing required VSG software" |
| 125 | time ssh ubuntu@$vsgIp "sudo apt-get update" |
| 126 | echo "Installing iptables.." |
| 127 | time ssh ubuntu@$vsgIp "sudo apt-get install iptables -y" |
| 128 | echo "Installing tcpdump..." |
| 129 | time ssh ubuntu@$vsgIp "sudo apt-get install tcpdump -y" |
| 130 | echo "installing Node Js.." |
| 131 | time ssh ubuntu@$vsgIp "sudo apt-get install nodejs -y" |
| 132 | echo "installing...sshpass.." |
| 133 | time ssh ubuntu@$vsgIp "sudo apt-get install sshpass -y" |
| 134 | } |
| 135 | |
| 136 | function setup_vcpegw_bridge_in_vsg() { |
| 137 | echo "Transfer and setup vcpegw bridge VSG Instance" |
| 138 | scp $HOME_DIR/$vsg_vcpe_gwbr_setup_script "ubuntu@$vsgIp:$vsg_home_dir" |
| 139 | sleep 2 |
| 140 | # |
| 141 | # Environment variables file $vsg_env_file should have been transferred |
| 142 | # out to the VSG instance in setup_vsg function. |
| 143 | # |
| 144 | ssh ubuntu@$vsgIp "source $vsg_home_dir/$vsg_env_file;$vsg_home_dir/$vsg_vcpe_gwbr_setup_script" |
| 145 | sleep 2 |
| 146 | } |
| 147 | |
| 148 | function start_vcpe_monitoring() { |
| 149 | echo "Transfer VCPE monitoring and setup script to VSG Instance" |
| 150 | scp $HOME_DIR/$vcpe_monitor_script "ubuntu@$vsgIp:$vsg_home_dir" |
| 151 | scp $HOME_DIR/$vcpe_setup_script "ubuntu@$vsgIp:$vsg_home_dir" |
| 152 | touch $HOME_DIR/$file_vcpe_names |
| 153 | scp $HOME_DIR/$file_vcpe_names "ubuntu@$vsgIp:$vsg_home_dir" |
| 154 | |
| 155 | sleep 1 |
| 156 | # |
| 157 | # Environment variables file $vsg_env_file should have been transferred |
| 158 | # out to the VSG instance in setup_vsg function. |
| 159 | # |
| 160 | ssh ubuntu@$vsgIp "source $vsg_home_dir/$vsg_env_file;$vsg_home_dir/$vcpe_monitor_script > $vsg_home_dir/vcpe_monitor.log 2>&1 &" |
| 161 | sleep 2 |
| 162 | } |
| 163 | |
| 164 | function transfer_gw_scripts_and_docker_files() { |
| 165 | echo "Transferring GW script and docker files to VSG Instance" |
| 166 | scp $HOME_DIR/$file_vsg_ip "ubuntu@$vsgIp:$vsg_home_dir" |
| 167 | scp $HOME_DIR/$docker_mount_file "ubuntu@$vsgIp:$vsg_home_dir" |
| 168 | ssh ubuntu@$vsgIp "cd $vsg_home_dir;tar -xvf $docker_mount_file" |
| 169 | |
| 170 | if [ -f $HOME_DIR/$pppoe_vcpe_image_tar ]; then |
| 171 | scp $HOME_DIR/$pppoe_vcpe_image_tar "ubuntu@$vsgIp:$vsg_home_dir" |
| 172 | |
| 173 | # Load PPPoE VCPE docker image into docker repository |
| 174 | ssh ubuntu@$vsgIp "cd $vsg_home_dir; sudo docker load -i ./$pppoe_vcpe_image_tar" |
| 175 | fi |
| 176 | sleep 5 |
| 177 | } |
| 178 | |
| 179 | function vsg_ssh_setup() { |
| 180 | echo "Setting up ssh in VSG" |
| 181 | ssh ubuntu@$vsgIp "mkdir /home/ubuntu/.ssh" |
| 182 | scp /home/ubuntu/.ssh/config "ubuntu@$vsgIp:/home/ubuntu/.ssh/" |
| 183 | scp /home/ubuntu/.ssh/id_rsa "ubuntu@$vsgIp:/home/ubuntu/.ssh/" |
| 184 | ssh ubuntu@$vsgIp "sudo sed -i '1 a $nova_compute_ip nova-compute-1 nova-compute-1' /etc/hosts" |
| 185 | } |
| 186 | |
| 187 | # |
| 188 | # Connect VcpeGW bridge to VSG |
| 189 | # |
| 190 | function connect_vcpegw_bridge_to_vsg() { |
| 191 | echo "Executing connect_vcpegw_bridge_to_vsg..." |
| 192 | source ${HOME_DIR}/admin-openrc.sh |
| 193 | uuid=`nova list --all-tenants|grep $vsgIp|awk '{print $2}'` |
| 194 | if [ -z $uuid ]; then |
| 195 | echo "Cannot find $vsgIp in nova list" |
| 196 | return 1 |
| 197 | fi |
| 198 | inst_name=`sudo virsh domname $uuid` |
| 199 | inst_id=`sudo virsh list |grep $inst_name|awk '{print $1}'` |
| 200 | echo "uuid=$uuid inst_name=$inst_name inst_id=$inst_id" |
| 201 | sudo virsh attach-interface $inst_id bridge $VSGGW_BR_NAME |
| 202 | # |
| 203 | # NOTE: To remove attached interface, use the following command. |
| 204 | # sudo virsh detach-interface $inst-name bridge <Mac-address of eth2 in VSG> |
| 205 | # |
| 206 | # Check whether the interface eth2 ($NETCFG_UP_IFACE) |
| 207 | # is created inside the VSG instance |
| 208 | ssh ubuntu@$vsgIp "ifconfig $NETCFG_UP_IFACE" |
| 209 | ssh ubuntu@$vsgIp "sudo ip link set dev $NETCFG_UP_IFACE up" |
| 210 | } |
| 211 | |
| 212 | if [ -z "$1" ] |
| 213 | then |
| 214 | echo "VSG Ip Required" |
| 215 | echo "Usage: nova_vsg_setup.sh <VSG IP> <VSG_ID>" |
| 216 | exit 1 |
| 217 | fi |
| 218 | |
| 219 | if [ -z "$2" ]; then |
| 220 | echo "VSG ID is required.." |
| 221 | echo "Usage: nova_vsg_setup.sh <VSG IP> <VSG_ID>" |
| 222 | exit 1 |
| 223 | fi |
| 224 | |
| 225 | if [ -z $HOME_DIR ]; then |
| 226 | HOME_DIR=`pwd` |
| 227 | echo "Missing HOME_DIR setting. Using current dir as HOME: $HOME" |
| 228 | fi |
| 229 | |
| 230 | if [ -z $VCPEGW_BR_NAME ]; then |
| 231 | echo "VCPEGW_BR_NAME is not configured" |
| 232 | echo "$0 Script executed terminated.." |
| 233 | exit 1 |
| 234 | fi |
| 235 | |
| 236 | if [ -z $VCPEGW_DOCKER_IMAGE ]; then |
| 237 | echo "VCPE Gateway Docker Image is not configured" |
| 238 | echo "$0 Script executed terminated.." |
| 239 | exit 1 |
| 240 | fi |
| 241 | |
| 242 | vsgIp=$1; export vsgIp |
| 243 | vsg_id=$2; export vsg_id |
| 244 | VSG_ID=$vsg_id;export VSG_ID |
| 245 | vsg_str="_vsg" |
| 246 | value=0 |
| 247 | vsg_value=0 |
| 248 | post_file=".conf" |
| 249 | dnsmasq_file="/etc/dnsmasq.conf" |
| 250 | vsg_home_dir=/home/ubuntu; export vsg_home_dir |
| 251 | vcpe_monitor_script=vsg_vcpe_monitor.sh |
| 252 | vcpe_setup_script=vsg_vcpe_gwbr_setup.sh |
| 253 | proxy_ap_mgmt_start_script=vcpe_proxy_ap_mgmt_start.sh |
| 254 | proxy_ap_mgmt_stop_script=vcpe_proxy_ap_mgmt_stop.sh |
| 255 | proxy_ipv6_setup=vcpe_proxy_ipv6_setup.sh |
| 256 | file_vsg_ip=vsg_ip_list.txt |
| 257 | file_vcpe_id=vcpe_id_list.txt |
| 258 | file_vcpe_names=vcpe_names_list.txt |
| 259 | vsg_env_file=$VSG_ENV_FILE |
| 260 | vsg_vcpe_gwbr_setup_script=vsg_vcpe_gwbr_setup.sh |
| 261 | vsg_vcpe_proxy_setup_script=vsg_vcpe_proxy_setup.sh |
| 262 | docker_mount_file=docker_mounts.tar |
| 263 | respin_vcpeproxy_docker_script=vsg_respin_vcpeproxy_docker.sh |
| 264 | pppoe_vcpe_image_tar=$PPPOE_VCPE_TAR_FILE |
| 265 | CONTAINER_VOLUMES=/var/container_volumes; export CONTAINER_VOLUMES |
| 266 | DOCKER_SPINUP_DIR=/usr/local/sbin; export DOCKER_SPINUP_DIR |
| 267 | if [ -f ${HOME_DIR}/$file_vsp_ip ]; then |
| 268 | echo "$vsgIp" >>${HOME_DIR}/$file_vsg_ip |
| 269 | else |
| 270 | echo "$vsgIp" >${HOME_DIR}/$file_vsg_ip |
| 271 | fi |
| 272 | |
| 273 | echo "Setting up VSG VM Instance $vsgIp ..." |
| 274 | |
| 275 | nova_compute_ip=$(ip addr show |grep br-int|grep 172.27|awk '{print $2}'|sed 's/\/24//') |
| 276 | br_wan_ip=$( ssh ubuntu@$vsgIp "/sbin/ifconfig br-wan | grep 'inet addr:' | cut -d: -f2 | awk '{ print \$1}'" ) |
| 277 | export nova_compute_ip |
| 278 | export br_wan_ip |
| 279 | echo "Setting VSG instance ($vsg_id) CP_PREFIX=$VCPEPROXY_CP_IP_PREFIX" |
| 280 | vcpe_gwbr_cval=0 |
| 281 | vcpe_gwbr_dval=$(( vsg_id + 1 )) |
| 282 | vcpe_gwbr_ip=`echo $VCPEPROXY_CP_IP_PREFIX.$vcpe_gwbr_cval.$vcpe_gwbr_dval` |
| 283 | export vcpe_gwbr_ip |
| 284 | |
| 285 | echo "VCPEGW_BRIDGE_IP in VSG ($VSG_ID).......$vcpe_gwbr_ip" |
| 286 | |
| 287 | if [ -f /home/ubuntu/.ssh/known_hosts ]; then |
| 288 | echo "Removing $vsgIp from /home/ubuntu/.ssh/known_hosts" |
| 289 | ssh-keygen -f "/home/ubuntu/.ssh/known_hosts" -R $vsgIp |
| 290 | fi |
| 291 | setup_vcpe_monitoring |
| 292 | vsg_script_transfer |
| 293 | connect_vcpegw_bridge_to_vsg |
| 294 | setup_vcpegw_bridge_in_vsg |
| 295 | start_vcpe_monitoring |
| 296 | date |
| 297 | echo "nova_vsg_setup.sh: End" |