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