blob: 7c475dafc1d5464d349dd7d9de62d07a7f46ca12 [file] [log] [blame]
Andy Bavier99c11d32016-09-14 17:21:20 -04001#!/usr/bin/env bash
2
3set -e
Andy Bavier99c11d32016-09-14 17:21:20 -04004
5CORDDIR=~/opencord
6VMDIR=/cord/build/
7CONFIG=config/cord_in_a_box.yml
Andy Baviera69ee722016-11-17 07:26:01 -08008SSHCONFIG=~/.ssh/config
Andy Bavier99c11d32016-09-14 17:21:20 -04009
10function cleanup_from_previous_test() {
Andy Bavier99c11d32016-09-14 17:21:20 -040011 echo "## Cleanup ##"
12
Andy Bavier2505f592016-11-11 15:58:55 -050013 echo "Destroying all Vagrant VMs"
Andy Bavier99c11d32016-09-14 17:21:20 -040014 cd $CORDDIR/build
15 vagrant destroy
16
Andy Bavier2505f592016-11-11 15:58:55 -050017 echo "Removing $CORDDIR"
18 cd ~
Andy Bavier99c11d32016-09-14 17:21:20 -040019 rm -rf $CORDDIR
Andy Bavier99c11d32016-09-14 17:21:20 -040020}
21
22function bootstrap() {
23 cd ~
24 sudo apt-get update
25 [ -e vagrant_1.8.5_x86_64.deb ] || wget https://releases.hashicorp.com/vagrant/1.8.5/vagrant_1.8.5_x86_64.deb
26 sudo dpkg -i vagrant_1.8.5_x86_64.deb
Andy Bavier1a35ae62016-11-15 19:58:13 -080027 sudo apt-get -y install qemu-kvm libvirt-bin libvirt-dev curl nfs-kernel-server git build-essential
Andy Bavier99c11d32016-09-14 17:21:20 -040028
29 [ -e ~/.ssh/id_rsa ] || ssh-keygen -t rsa -N "" -f ~/.ssh/id_rsa
Andy Bavier99c11d32016-09-14 17:21:20 -040030
31 USER=$(whoami)
32 sudo adduser $USER libvirtd
33
34 sudo curl -o /usr/local/bin/repo https://storage.googleapis.com/git-repo-downloads/repo
35 sudo chmod a+x /usr/local/bin/repo
36
37 if [ ! -d "$CORDDIR" ]
38 then
39 mkdir $CORDDIR && cd $CORDDIR
40 git config --global user.name 'Test User'
41 git config --global user.email 'test@null.com'
42 git config --global color.ui false
43
Scott Baker101b37c2016-10-24 16:22:43 -070044 repo init -u https://gerrit.opencord.org/manifest -b master -g build,onos,orchestration
Andy Bavier99c11d32016-09-14 17:21:20 -040045 repo sync
46
Zack Williamsdae7ff62016-11-14 15:20:06 -070047 # check out gerrit branches using repo
48 for gerrit_branch in ${GERRIT_BRANCHES[@]}; do
49 echo "checking out opencord gerrit branch: $gerrit_branch"
50 repo download ${gerrit_branch/:/ }
51 done
Andy Bavier99c11d32016-09-14 17:21:20 -040052 fi
53
54 cd $CORDDIR/build
Andy Bavier45e30bb2016-10-07 15:45:18 -040055 vagrant plugin install vagrant-libvirt --plugin-version 0.0.35
Andy Bavier99c11d32016-09-14 17:21:20 -040056 vagrant plugin install vagrant-mutate
57 vagrant box list ubuntu/trusty64 | grep virtualbox || vagrant box add ubuntu/trusty64
58 vagrant box list ubuntu/trusty64 | grep libvirt || vagrant mutate ubuntu/trusty64 libvirt --input-provider virtualbox
59}
60
61function cloudlab_setup() {
62 if [ -e /usr/testbed/bin/mkextrafs ]
63 then
Andy Bavier9c0835e2016-11-11 15:56:47 -050064 sudo mkdir -p /mnt/extra
Scott Baker8b9182d2016-10-27 17:53:52 -070065
Andy Bavier8ce395f2016-10-14 12:51:30 -040066 # Sometimes this command fails on the first try
Andy Bavier9c0835e2016-11-11 15:56:47 -050067 sudo /usr/testbed/bin/mkextrafs -r /dev/sdb -qf "/mnt/extra/" || sudo /usr/testbed/bin/mkextrafs -r /dev/sdb -qf "/mnt/extra/"
68
69 # Check that the mount succeeded (sometimes mkextrafs succeeds but device not mounted)
Andy Baviera69ee722016-11-17 07:26:01 -080070 mount | grep sdb || (echo "ERROR: mkextrafs failed, exiting!" && exit 1)
Scott Baker8b9182d2016-10-27 17:53:52 -070071
72 # we'll replace /var/lib/libvirt/images with a symlink below
73 [ -d /var/lib/libvirt/images/ ] && [ ! -h /var/lib/libvirt/images ] && sudo rmdir /var/lib/libvirt/images
74
Andy Bavier9c0835e2016-11-11 15:56:47 -050075 sudo mkdir -p /mnt/extra/libvirt_images
Andy Bavier2505f592016-11-11 15:58:55 -050076 if [ ! -e /var/lib/libvirt/images ]
77 then
78 sudo ln -s /mnt/extra/libvirt_images /var/lib/libvirt/images
79 fi
Andy Bavier99c11d32016-09-14 17:21:20 -040080 fi
81}
82
Andy Bavier2505f592016-11-11 15:58:55 -050083function vagrant_vms_up() {
Andy Bavier99c11d32016-09-14 17:21:20 -040084 cd $CORDDIR/build
85
86 sudo su $USER -c 'vagrant up corddev --provider libvirt'
Andy Bavier2505f592016-11-11 15:58:55 -050087 sudo su $USER -c 'vagrant up prod --provider libvirt'
Andy Baviera69ee722016-11-17 07:26:01 -080088
89 # This is a workaround for a weird issue with ARP cache timeout breaking 'vagrant ssh'
90 # It allows SSH'ing to the machine via 'ssh corddev'
Andy Bavier2505f592016-11-11 15:58:55 -050091 sudo su $USER -c "vagrant ssh-config corddev prod > $SSHCONFIG"
92
93 scp ~/.ssh/id_rsa* corddev:.ssh
94 ssh corddev "chmod go-r ~/.ssh/id_rsa"
Andy Bavier99c11d32016-09-14 17:21:20 -040095}
96
97function install_head_node() {
98 cd $CORDDIR/build
99
Andy Baviera69ee722016-11-17 07:26:01 -0800100 # SSH config saved earlier allows us to connect to VM without running 'vagrant'
Andy Baviera69ee722016-11-17 07:26:01 -0800101 ssh corddev "cd /cord/build; ./gradlew fetch"
102 ssh corddev "cd /cord/build; ./gradlew buildImages"
Andy Bavier2505f592016-11-11 15:58:55 -0500103 ssh corddev "cd /cord/build; ping -c 3 prod; ./gradlew -PdeployConfig=$VMDIR/$CONFIG -PtargetReg=10.100.198.201:5000 publish"
Andy Baviera69ee722016-11-17 07:26:01 -0800104 ssh corddev "cd /cord/build; ./gradlew -PdeployConfig=$VMDIR/$CONFIG deploy"
Andy Bavier99c11d32016-09-14 17:21:20 -0400105}
106
107function set_up_maas_user() {
Andy Bavier2505f592016-11-11 15:58:55 -0500108 # Set up MAAS user on server to restart nodes via libvirt
109 grep maas /etc/passwd || sudo useradd -m maas
Andy Bavier99c11d32016-09-14 17:21:20 -0400110 sudo adduser maas libvirtd
111
Andy Bavier2505f592016-11-11 15:58:55 -0500112 # Copy generated public key to maas user's authorized_keys
113 sudo su maas -c "mkdir -p ~/.ssh"
114 sudo cp $HOME/.ssh/id_rsa.pub ~maas/.ssh/authorized_keys
115 sudo chown maas:maas ~maas/.ssh/authorized_keys
Andy Bavier99c11d32016-09-14 17:21:20 -0400116
Andy Bavier2505f592016-11-11 15:58:55 -0500117 # Copy generated private key to maas user's home dir in prod VM
118 scp $HOME/.ssh/id_rsa prod:/tmp
119 ssh prod "sudo mkdir -p ~maas/.ssh"
120 ssh prod "sudo cp /tmp/id_rsa ~maas/.ssh/id_rsa"
121 ssh prod "sudo chown -R maas:maas ~maas/.ssh"
122}
Srikanth Vavilapallib1c4da02016-11-17 15:24:28 -0600123
Andy Bavier99c11d32016-09-14 17:21:20 -0400124function add_compute_node() {
Srikanth Vavilapallib1c4da02016-11-17 15:24:28 -0600125 echo add_compute_node: $1 $2
Andy Bavier2505f592016-11-11 15:58:55 -0500126
Andy Bavier99c11d32016-09-14 17:21:20 -0400127 cd $CORDDIR/build
Srikanth Vavilapallib1c4da02016-11-17 15:24:28 -0600128 sudo su $USER -c "vagrant up $1 --provider libvirt"
Andy Bavier99c11d32016-09-14 17:21:20 -0400129
Andy Bavier2505f592016-11-11 15:58:55 -0500130 # Change MAC address of bridge to match cord-pod service profile
131 # This change won't survive a reboot
132 BRIDGE=$( route -n | grep 10.6.1.0 | awk '{print $8}' )
133 sudo ifconfig $BRIDGE hw ether 02:42:0a:06:01:01
Andy Bavierb5796fb2016-10-20 12:06:47 -0400134
Andy Bavier2505f592016-11-11 15:58:55 -0500135 ip addr list | grep 10.6.1.129 || sudo ip address add 10.6.1.129 dev $BRIDGE
136 ip addr list | grep 10.6.1.193 || sudo ip address add 10.6.1.193 dev $BRIDGE
Srikanth Vavilapallib1c4da02016-11-17 15:24:28 -0600137
Andy Bavier2505f592016-11-11 15:58:55 -0500138 # Set up power cycling for the compute node and wait for it to be provisioned
139 ssh prod "cd /cord/build/ansible; ansible-playbook maas-provision.yml --extra-vars \"maas_user=maas vagrant_name=$2\""
Andy Bavier99c11d32016-09-14 17:21:20 -0400140
141 echo ""
142 echo "compute_node is fully provisioned!"
143}
144
145function run_e2e_test () {
146 cd $CORDDIR/build
147
Andy Bavier2505f592016-11-11 15:58:55 -0500148 # User has been added to the lbvirtd group, but su $USER to be safe
Andy Baviera69ee722016-11-17 07:26:01 -0800149 ssh corddev "cd /cord/build; ./gradlew -PdeployConfig=$VMDIR/$CONFIG postDeployTests"
Andy Bavier99c11d32016-09-14 17:21:20 -0400150}
151
152function run_diagnostics() {
153 echo "*** COLLECTING DIAGNOSTIC INFO NOT CURRENTLY IMPLEMENTED"
154 # Need to fix up inventory to collect info from compute nodes
155 # Using juju-ansible is one possibility
156 #echo "*** COLLECTING DIAGNOSTIC INFO - check ~/diag-* on the head node"
157 #ansible-playbook -i $INVENTORY cord-diag-playbook.yml
158}
159
160# Parse options
Zack Williamsdae7ff62016-11-14 15:20:06 -0700161GERRIT_BRANCHES=
Andy Bavier99c11d32016-09-14 17:21:20 -0400162RUN_TEST=0
Andy Bavier5c2e4fa2016-10-31 13:50:52 -0400163SETUP_ONLY=0
Andy Bavier99c11d32016-09-14 17:21:20 -0400164DIAGNOSTICS=0
165CLEANUP=0
Srikanth Vavilapallib1c4da02016-11-17 15:24:28 -0600166#By default, cord-in-a-box creates 1 compute node. If more than one compute is
167#needed, use -n option
168NUM_COMPUTE_NODES=1
Andy Bavier99c11d32016-09-14 17:21:20 -0400169
Srikanth Vavilapallib1c4da02016-11-17 15:24:28 -0600170while getopts "b:cdhn:st" opt; do
Andy Bavier99c11d32016-09-14 17:21:20 -0400171 case ${opt} in
Zack Williamsdae7ff62016-11-14 15:20:06 -0700172 b ) GERRIT_BRANCHES+=("$OPTARG")
Andy Bavier99c11d32016-09-14 17:21:20 -0400173 ;;
174 c ) CLEANUP=1
175 ;;
176 d ) DIAGNOSTICS=1
177 ;;
178 h ) echo "Usage:"
179 echo " $0 install OpenStack and prep XOS and ONOS VMs [default]"
Zack Williamsdae7ff62016-11-14 15:20:06 -0700180 echo " $0 -b <project:changeset/revision> checkout a changesets from gerrit. Can"
181 echo " be used multiple times."
Andy Bavier99c11d32016-09-14 17:21:20 -0400182 echo " $0 -c cleanup from previous test"
183 echo " $0 -d run diagnostic collector"
184 echo " $0 -h display this help message"
Srikanth Vavilapallib1c4da02016-11-17 15:24:28 -0600185 echo " $0 -n number of compute nodes to setup. currently max 2 nodes can be supported"
Andy Bavier5c2e4fa2016-10-31 13:50:52 -0400186 echo " $0 -s run initial setup phase only (don't start building CORD)"
Andy Bavier99c11d32016-09-14 17:21:20 -0400187 echo " $0 -t do install, bring up cord-pod configuration, run E2E test"
188 exit 0
189 ;;
Srikanth Vavilapallib1c4da02016-11-17 15:24:28 -0600190 n ) NUM_COMPUTE_NODES=$OPTARG
191 ;;
Andy Bavier5c2e4fa2016-10-31 13:50:52 -0400192 s ) SETUP_ONLY=1
193 ;;
Andy Bavier99c11d32016-09-14 17:21:20 -0400194 t ) RUN_TEST=1
195 ;;
196 \? ) echo "Invalid option: -$OPTARG"
197 exit 1
198 ;;
199 esac
200done
201
202# What to do
203if [[ $CLEANUP -eq 1 ]]
204then
205 cleanup_from_previous_test
206fi
207
Andy Bavier99c11d32016-09-14 17:21:20 -0400208bootstrap
209cloudlab_setup
Andy Bavier2505f592016-11-11 15:58:55 -0500210vagrant_vms_up
Andy Bavier5c2e4fa2016-10-31 13:50:52 -0400211
212if [[ $SETUP_ONLY -ne 0 ]]
213then
214 echo "Finished build environment setup, exiting..."
215 exit 0
216fi
217
Andy Bavier99c11d32016-09-14 17:21:20 -0400218install_head_node
219set_up_maas_user
Srikanth Vavilapallib1c4da02016-11-17 15:24:28 -0600220
Andy Bavier2505f592016-11-11 15:58:55 -0500221#Limiting the maximum number of compute nodes that can be supported to 2. If
Srikanth Vavilapallib1c4da02016-11-17 15:24:28 -0600222#more than 2 compute nodes are needed, this script need to be enhanced to set
223#the environment variable NUM_COMPUTE_NODES before invoking the Vagrant commands
224if [[ $NUM_COMPUTE_NODES -gt 2 ]]
225then
226 echo "currently max only two compute nodes can be supported..."
227 NUM_COMPUTE_NODES=2
228fi
229
230for i in `seq 1 $NUM_COMPUTE_NODES`;
231do
232 echo adding the compute node: compute-node-$i
233 add_compute_node compute_node-$i build_compute_node-$i
234done
Andy Bavier99c11d32016-09-14 17:21:20 -0400235
236if [[ $RUN_TEST -eq 1 ]]
237then
238 run_e2e_test
239fi
240
241if [[ $DIAGNOSTICS -eq 1 ]]
242then
243 run_diagnostics
244fi
245
246exit 0