blob: b4068cc98bc1d7c56c7ba903cd2c4ec2d85f2389 [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 Baviera4883ba2016-12-21 15:46:58 -050013 if [ -d $CORDDIR/build ]
14 then
15 echo "Destroying all Vagrant VMs"
16 cd $CORDDIR/build
17 sudo su $USER -c 'vagrant destroy'
18 fi
Andy Bavier99c11d32016-09-14 17:21:20 -040019
Andy Bavier2505f592016-11-11 15:58:55 -050020 echo "Removing $CORDDIR"
21 cd ~
Andy Bavier99c11d32016-09-14 17:21:20 -040022 rm -rf $CORDDIR
Andy Bavier99c11d32016-09-14 17:21:20 -040023}
24
25function bootstrap() {
26 cd ~
27 sudo apt-get update
28 [ -e vagrant_1.8.5_x86_64.deb ] || wget https://releases.hashicorp.com/vagrant/1.8.5/vagrant_1.8.5_x86_64.deb
Andy Bavierad3ea842016-12-21 16:19:07 -050029 dpkg -l vagrant || sudo dpkg -i vagrant_1.8.5_x86_64.deb
Andy Bavier1a35ae62016-11-15 19:58:13 -080030 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 -040031
32 [ -e ~/.ssh/id_rsa ] || ssh-keygen -t rsa -N "" -f ~/.ssh/id_rsa
Andy Bavier99c11d32016-09-14 17:21:20 -040033
Andy Bavier99c11d32016-09-14 17:21:20 -040034 sudo adduser $USER libvirtd
35
36 sudo curl -o /usr/local/bin/repo https://storage.googleapis.com/git-repo-downloads/repo
37 sudo chmod a+x /usr/local/bin/repo
38
39 if [ ! -d "$CORDDIR" ]
40 then
41 mkdir $CORDDIR && cd $CORDDIR
42 git config --global user.name 'Test User'
43 git config --global user.email 'test@null.com'
44 git config --global color.ui false
45
Scott Baker101b37c2016-10-24 16:22:43 -070046 repo init -u https://gerrit.opencord.org/manifest -b master -g build,onos,orchestration
Andy Bavier99c11d32016-09-14 17:21:20 -040047 repo sync
48
Zack Williamsdae7ff62016-11-14 15:20:06 -070049 # check out gerrit branches using repo
50 for gerrit_branch in ${GERRIT_BRANCHES[@]}; do
51 echo "checking out opencord gerrit branch: $gerrit_branch"
52 repo download ${gerrit_branch/:/ }
53 done
Andy Bavier99c11d32016-09-14 17:21:20 -040054 fi
55
56 cd $CORDDIR/build
Andy Bavierad3ea842016-12-21 16:19:07 -050057 vagrant plugin list | grep vagrant-libvirt || vagrant plugin install vagrant-libvirt --plugin-version 0.0.35
58 vagrant plugin list | grep vagrant-mutate || vagrant plugin install vagrant-mutate
Andy Bavier99c11d32016-09-14 17:21:20 -040059 vagrant box list ubuntu/trusty64 | grep virtualbox || vagrant box add ubuntu/trusty64
60 vagrant box list ubuntu/trusty64 | grep libvirt || vagrant mutate ubuntu/trusty64 libvirt --input-provider virtualbox
61}
62
63function cloudlab_setup() {
64 if [ -e /usr/testbed/bin/mkextrafs ]
65 then
Andy Bavier9c0835e2016-11-11 15:56:47 -050066 sudo mkdir -p /mnt/extra
Scott Baker8b9182d2016-10-27 17:53:52 -070067
Andy Bavier8ce395f2016-10-14 12:51:30 -040068 # Sometimes this command fails on the first try
Andy Bavier9c0835e2016-11-11 15:56:47 -050069 sudo /usr/testbed/bin/mkextrafs -r /dev/sdb -qf "/mnt/extra/" || sudo /usr/testbed/bin/mkextrafs -r /dev/sdb -qf "/mnt/extra/"
70
71 # Check that the mount succeeded (sometimes mkextrafs succeeds but device not mounted)
Andy Baviera69ee722016-11-17 07:26:01 -080072 mount | grep sdb || (echo "ERROR: mkextrafs failed, exiting!" && exit 1)
Scott Baker8b9182d2016-10-27 17:53:52 -070073
74 # we'll replace /var/lib/libvirt/images with a symlink below
75 [ -d /var/lib/libvirt/images/ ] && [ ! -h /var/lib/libvirt/images ] && sudo rmdir /var/lib/libvirt/images
76
Andy Bavier9c0835e2016-11-11 15:56:47 -050077 sudo mkdir -p /mnt/extra/libvirt_images
Andy Bavier2505f592016-11-11 15:58:55 -050078 if [ ! -e /var/lib/libvirt/images ]
79 then
80 sudo ln -s /mnt/extra/libvirt_images /var/lib/libvirt/images
81 fi
Andy Bavier99c11d32016-09-14 17:21:20 -040082 fi
83}
84
Andy Bavier2505f592016-11-11 15:58:55 -050085function vagrant_vms_up() {
Andy Bavier99c11d32016-09-14 17:21:20 -040086 cd $CORDDIR/build
87
88 sudo su $USER -c 'vagrant up corddev --provider libvirt'
Andy Bavier2505f592016-11-11 15:58:55 -050089 sudo su $USER -c 'vagrant up prod --provider libvirt'
Andy Baviera69ee722016-11-17 07:26:01 -080090
91 # This is a workaround for a weird issue with ARP cache timeout breaking 'vagrant ssh'
92 # It allows SSH'ing to the machine via 'ssh corddev'
Andy Bavier2505f592016-11-11 15:58:55 -050093 sudo su $USER -c "vagrant ssh-config corddev prod > $SSHCONFIG"
94
95 scp ~/.ssh/id_rsa* corddev:.ssh
96 ssh corddev "chmod go-r ~/.ssh/id_rsa"
Andy Bavier99c11d32016-09-14 17:21:20 -040097}
98
99function install_head_node() {
100 cd $CORDDIR/build
101
Andy Baviera69ee722016-11-17 07:26:01 -0800102 # SSH config saved earlier allows us to connect to VM without running 'vagrant'
Andy Baviera69ee722016-11-17 07:26:01 -0800103 ssh corddev "cd /cord/build; ./gradlew fetch"
104 ssh corddev "cd /cord/build; ./gradlew buildImages"
Andy Bavier2505f592016-11-11 15:58:55 -0500105 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 -0800106 ssh corddev "cd /cord/build; ./gradlew -PdeployConfig=$VMDIR/$CONFIG deploy"
Andy Bavier99c11d32016-09-14 17:21:20 -0400107}
108
109function set_up_maas_user() {
Andy Bavier2505f592016-11-11 15:58:55 -0500110 # Set up MAAS user on server to restart nodes via libvirt
111 grep maas /etc/passwd || sudo useradd -m maas
Andy Bavier99c11d32016-09-14 17:21:20 -0400112 sudo adduser maas libvirtd
113
Andy Bavier2505f592016-11-11 15:58:55 -0500114 # Copy generated public key to maas user's authorized_keys
115 sudo su maas -c "mkdir -p ~/.ssh"
116 sudo cp $HOME/.ssh/id_rsa.pub ~maas/.ssh/authorized_keys
117 sudo chown maas:maas ~maas/.ssh/authorized_keys
Andy Bavier99c11d32016-09-14 17:21:20 -0400118
Andy Bavier2505f592016-11-11 15:58:55 -0500119 # Copy generated private key to maas user's home dir in prod VM
120 scp $HOME/.ssh/id_rsa prod:/tmp
121 ssh prod "sudo mkdir -p ~maas/.ssh"
122 ssh prod "sudo cp /tmp/id_rsa ~maas/.ssh/id_rsa"
123 ssh prod "sudo chown -R maas:maas ~maas/.ssh"
124}
Srikanth Vavilapallib1c4da02016-11-17 15:24:28 -0600125
Andy Bavier99c11d32016-09-14 17:21:20 -0400126function add_compute_node() {
Srikanth Vavilapallib1c4da02016-11-17 15:24:28 -0600127 echo add_compute_node: $1 $2
Andy Bavier2505f592016-11-11 15:58:55 -0500128
Andy Bavier99c11d32016-09-14 17:21:20 -0400129 cd $CORDDIR/build
Srikanth Vavilapallib1c4da02016-11-17 15:24:28 -0600130 sudo su $USER -c "vagrant up $1 --provider libvirt"
Andy Bavier99c11d32016-09-14 17:21:20 -0400131
Andy Bavier2505f592016-11-11 15:58:55 -0500132 # Change MAC address of bridge to match cord-pod service profile
133 # This change won't survive a reboot
134 BRIDGE=$( route -n | grep 10.6.1.0 | awk '{print $8}' )
135 sudo ifconfig $BRIDGE hw ether 02:42:0a:06:01:01
Andy Bavierb5796fb2016-10-20 12:06:47 -0400136
Andy Bavier2505f592016-11-11 15:58:55 -0500137 ip addr list | grep 10.6.1.129 || sudo ip address add 10.6.1.129 dev $BRIDGE
138 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 -0600139
Andy Bavier2505f592016-11-11 15:58:55 -0500140 # Set up power cycling for the compute node and wait for it to be provisioned
141 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 -0400142
143 echo ""
144 echo "compute_node is fully provisioned!"
145}
146
147function run_e2e_test () {
148 cd $CORDDIR/build
149
Andy Bavier2505f592016-11-11 15:58:55 -0500150 # User has been added to the lbvirtd group, but su $USER to be safe
Andy Baviera69ee722016-11-17 07:26:01 -0800151 ssh corddev "cd /cord/build; ./gradlew -PdeployConfig=$VMDIR/$CONFIG postDeployTests"
Andy Bavier99c11d32016-09-14 17:21:20 -0400152}
153
154function run_diagnostics() {
155 echo "*** COLLECTING DIAGNOSTIC INFO NOT CURRENTLY IMPLEMENTED"
156 # Need to fix up inventory to collect info from compute nodes
157 # Using juju-ansible is one possibility
158 #echo "*** COLLECTING DIAGNOSTIC INFO - check ~/diag-* on the head node"
159 #ansible-playbook -i $INVENTORY cord-diag-playbook.yml
160}
161
162# Parse options
Zack Williamsdae7ff62016-11-14 15:20:06 -0700163GERRIT_BRANCHES=
Andy Bavier99c11d32016-09-14 17:21:20 -0400164RUN_TEST=0
Andy Bavier5c2e4fa2016-10-31 13:50:52 -0400165SETUP_ONLY=0
Andy Bavier99c11d32016-09-14 17:21:20 -0400166DIAGNOSTICS=0
167CLEANUP=0
Srikanth Vavilapallib1c4da02016-11-17 15:24:28 -0600168#By default, cord-in-a-box creates 1 compute node. If more than one compute is
169#needed, use -n option
170NUM_COMPUTE_NODES=1
Andy Bavier99c11d32016-09-14 17:21:20 -0400171
Srikanth Vavilapallib1c4da02016-11-17 15:24:28 -0600172while getopts "b:cdhn:st" opt; do
Andy Bavier99c11d32016-09-14 17:21:20 -0400173 case ${opt} in
Zack Williamsdae7ff62016-11-14 15:20:06 -0700174 b ) GERRIT_BRANCHES+=("$OPTARG")
Andy Bavier99c11d32016-09-14 17:21:20 -0400175 ;;
176 c ) CLEANUP=1
177 ;;
178 d ) DIAGNOSTICS=1
179 ;;
180 h ) echo "Usage:"
181 echo " $0 install OpenStack and prep XOS and ONOS VMs [default]"
Zack Williamsdae7ff62016-11-14 15:20:06 -0700182 echo " $0 -b <project:changeset/revision> checkout a changesets from gerrit. Can"
183 echo " be used multiple times."
Andy Bavier99c11d32016-09-14 17:21:20 -0400184 echo " $0 -c cleanup from previous test"
185 echo " $0 -d run diagnostic collector"
186 echo " $0 -h display this help message"
Srikanth Vavilapallib1c4da02016-11-17 15:24:28 -0600187 echo " $0 -n number of compute nodes to setup. currently max 2 nodes can be supported"
Andy Bavier5c2e4fa2016-10-31 13:50:52 -0400188 echo " $0 -s run initial setup phase only (don't start building CORD)"
Andy Bavier99c11d32016-09-14 17:21:20 -0400189 echo " $0 -t do install, bring up cord-pod configuration, run E2E test"
190 exit 0
191 ;;
Srikanth Vavilapallib1c4da02016-11-17 15:24:28 -0600192 n ) NUM_COMPUTE_NODES=$OPTARG
193 ;;
Andy Bavier5c2e4fa2016-10-31 13:50:52 -0400194 s ) SETUP_ONLY=1
195 ;;
Andy Bavier99c11d32016-09-14 17:21:20 -0400196 t ) RUN_TEST=1
197 ;;
198 \? ) echo "Invalid option: -$OPTARG"
199 exit 1
200 ;;
201 esac
202done
203
204# What to do
205if [[ $CLEANUP -eq 1 ]]
206then
207 cleanup_from_previous_test
208fi
209
Andy Bavier99c11d32016-09-14 17:21:20 -0400210bootstrap
211cloudlab_setup
Andy Bavier2505f592016-11-11 15:58:55 -0500212vagrant_vms_up
Andy Bavier5c2e4fa2016-10-31 13:50:52 -0400213
214if [[ $SETUP_ONLY -ne 0 ]]
215then
216 echo "Finished build environment setup, exiting..."
217 exit 0
218fi
219
Andy Bavier99c11d32016-09-14 17:21:20 -0400220install_head_node
221set_up_maas_user
Srikanth Vavilapallib1c4da02016-11-17 15:24:28 -0600222
Andy Bavier2505f592016-11-11 15:58:55 -0500223#Limiting the maximum number of compute nodes that can be supported to 2. If
Srikanth Vavilapallib1c4da02016-11-17 15:24:28 -0600224#more than 2 compute nodes are needed, this script need to be enhanced to set
225#the environment variable NUM_COMPUTE_NODES before invoking the Vagrant commands
226if [[ $NUM_COMPUTE_NODES -gt 2 ]]
227then
228 echo "currently max only two compute nodes can be supported..."
229 NUM_COMPUTE_NODES=2
230fi
231
232for i in `seq 1 $NUM_COMPUTE_NODES`;
233do
234 echo adding the compute node: compute-node-$i
235 add_compute_node compute_node-$i build_compute_node-$i
236done
Andy Bavier99c11d32016-09-14 17:21:20 -0400237
238if [[ $RUN_TEST -eq 1 ]]
239then
240 run_e2e_test
241fi
242
243if [[ $DIAGNOSTICS -eq 1 ]]
244then
245 run_diagnostics
246fi
247
248exit 0