blob: 4a410eac44bd02c9f4d6bddb28adae69885df852 [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
Andy Bavier65c8e152016-12-22 16:01:27 -050088 sudo su $USER -c 'vagrant up corddev prod --provider libvirt'
Andy Baviera69ee722016-11-17 07:26:01 -080089
90 # This is a workaround for a weird issue with ARP cache timeout breaking 'vagrant ssh'
91 # It allows SSH'ing to the machine via 'ssh corddev'
Andy Bavier2505f592016-11-11 15:58:55 -050092 sudo su $USER -c "vagrant ssh-config corddev prod > $SSHCONFIG"
93
94 scp ~/.ssh/id_rsa* corddev:.ssh
95 ssh corddev "chmod go-r ~/.ssh/id_rsa"
Andy Bavier99c11d32016-09-14 17:21:20 -040096}
97
98function install_head_node() {
99 cd $CORDDIR/build
100
Andy Baviera69ee722016-11-17 07:26:01 -0800101 # SSH config saved earlier allows us to connect to VM without running 'vagrant'
Andy Baviera69ee722016-11-17 07:26:01 -0800102 ssh corddev "cd /cord/build; ./gradlew fetch"
103 ssh corddev "cd /cord/build; ./gradlew buildImages"
Andy Bavier2505f592016-11-11 15:58:55 -0500104 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 -0800105 ssh corddev "cd /cord/build; ./gradlew -PdeployConfig=$VMDIR/$CONFIG deploy"
Andy Bavier99c11d32016-09-14 17:21:20 -0400106}
107
108function set_up_maas_user() {
Andy Bavier2505f592016-11-11 15:58:55 -0500109 # Set up MAAS user on server to restart nodes via libvirt
110 grep maas /etc/passwd || sudo useradd -m maas
Andy Bavier99c11d32016-09-14 17:21:20 -0400111 sudo adduser maas libvirtd
112
Andy Bavier2505f592016-11-11 15:58:55 -0500113 # Copy generated public key to maas user's authorized_keys
114 sudo su maas -c "mkdir -p ~/.ssh"
115 sudo cp $HOME/.ssh/id_rsa.pub ~maas/.ssh/authorized_keys
116 sudo chown maas:maas ~maas/.ssh/authorized_keys
Andy Bavier99c11d32016-09-14 17:21:20 -0400117
Andy Bavier2505f592016-11-11 15:58:55 -0500118 # Copy generated private key to maas user's home dir in prod VM
119 scp $HOME/.ssh/id_rsa prod:/tmp
120 ssh prod "sudo mkdir -p ~maas/.ssh"
121 ssh prod "sudo cp /tmp/id_rsa ~maas/.ssh/id_rsa"
122 ssh prod "sudo chown -R maas:maas ~maas/.ssh"
123}
Srikanth Vavilapallib1c4da02016-11-17 15:24:28 -0600124
Andy Bavier99c11d32016-09-14 17:21:20 -0400125function add_compute_node() {
Srikanth Vavilapallib1c4da02016-11-17 15:24:28 -0600126 echo add_compute_node: $1 $2
Andy Bavier2505f592016-11-11 15:58:55 -0500127
Andy Bavier99c11d32016-09-14 17:21:20 -0400128 cd $CORDDIR/build
Srikanth Vavilapallib1c4da02016-11-17 15:24:28 -0600129 sudo su $USER -c "vagrant up $1 --provider libvirt"
Andy Bavier99c11d32016-09-14 17:21:20 -0400130
Andy Bavier2505f592016-11-11 15:58:55 -0500131 # Change MAC address of bridge to match cord-pod service profile
132 # This change won't survive a reboot
133 BRIDGE=$( route -n | grep 10.6.1.0 | awk '{print $8}' )
134 sudo ifconfig $BRIDGE hw ether 02:42:0a:06:01:01
Andy Bavierb5796fb2016-10-20 12:06:47 -0400135
Andy Bavier2505f592016-11-11 15:58:55 -0500136 ip addr list | grep 10.6.1.129 || sudo ip address add 10.6.1.129 dev $BRIDGE
137 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 -0600138
Andy Bavier2505f592016-11-11 15:58:55 -0500139 # Set up power cycling for the compute node and wait for it to be provisioned
140 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 -0400141
142 echo ""
143 echo "compute_node is fully provisioned!"
144}
145
146function run_e2e_test () {
147 cd $CORDDIR/build
148
Andy Bavier2505f592016-11-11 15:58:55 -0500149 # User has been added to the lbvirtd group, but su $USER to be safe
Andy Baviera69ee722016-11-17 07:26:01 -0800150 ssh corddev "cd /cord/build; ./gradlew -PdeployConfig=$VMDIR/$CONFIG postDeployTests"
Andy Bavier99c11d32016-09-14 17:21:20 -0400151}
152
153function run_diagnostics() {
154 echo "*** COLLECTING DIAGNOSTIC INFO NOT CURRENTLY IMPLEMENTED"
155 # Need to fix up inventory to collect info from compute nodes
156 # Using juju-ansible is one possibility
157 #echo "*** COLLECTING DIAGNOSTIC INFO - check ~/diag-* on the head node"
158 #ansible-playbook -i $INVENTORY cord-diag-playbook.yml
159}
160
161# Parse options
Zack Williamsdae7ff62016-11-14 15:20:06 -0700162GERRIT_BRANCHES=
Andy Bavier99c11d32016-09-14 17:21:20 -0400163RUN_TEST=0
Andy Bavier5c2e4fa2016-10-31 13:50:52 -0400164SETUP_ONLY=0
Andy Bavier99c11d32016-09-14 17:21:20 -0400165DIAGNOSTICS=0
166CLEANUP=0
Srikanth Vavilapallib1c4da02016-11-17 15:24:28 -0600167#By default, cord-in-a-box creates 1 compute node. If more than one compute is
168#needed, use -n option
169NUM_COMPUTE_NODES=1
Andy Bavier99c11d32016-09-14 17:21:20 -0400170
Srikanth Vavilapallib1c4da02016-11-17 15:24:28 -0600171while getopts "b:cdhn:st" opt; do
Andy Bavier99c11d32016-09-14 17:21:20 -0400172 case ${opt} in
Zack Williamsdae7ff62016-11-14 15:20:06 -0700173 b ) GERRIT_BRANCHES+=("$OPTARG")
Andy Bavier99c11d32016-09-14 17:21:20 -0400174 ;;
175 c ) CLEANUP=1
176 ;;
177 d ) DIAGNOSTICS=1
178 ;;
179 h ) echo "Usage:"
180 echo " $0 install OpenStack and prep XOS and ONOS VMs [default]"
Zack Williamsdae7ff62016-11-14 15:20:06 -0700181 echo " $0 -b <project:changeset/revision> checkout a changesets from gerrit. Can"
182 echo " be used multiple times."
Andy Bavier99c11d32016-09-14 17:21:20 -0400183 echo " $0 -c cleanup from previous test"
184 echo " $0 -d run diagnostic collector"
185 echo " $0 -h display this help message"
Srikanth Vavilapallib1c4da02016-11-17 15:24:28 -0600186 echo " $0 -n number of compute nodes to setup. currently max 2 nodes can be supported"
Andy Bavier5c2e4fa2016-10-31 13:50:52 -0400187 echo " $0 -s run initial setup phase only (don't start building CORD)"
Andy Bavier99c11d32016-09-14 17:21:20 -0400188 echo " $0 -t do install, bring up cord-pod configuration, run E2E test"
189 exit 0
190 ;;
Srikanth Vavilapallib1c4da02016-11-17 15:24:28 -0600191 n ) NUM_COMPUTE_NODES=$OPTARG
192 ;;
Andy Bavier5c2e4fa2016-10-31 13:50:52 -0400193 s ) SETUP_ONLY=1
194 ;;
Andy Bavier99c11d32016-09-14 17:21:20 -0400195 t ) RUN_TEST=1
196 ;;
197 \? ) echo "Invalid option: -$OPTARG"
198 exit 1
199 ;;
200 esac
201done
202
203# What to do
204if [[ $CLEANUP -eq 1 ]]
205then
206 cleanup_from_previous_test
207fi
208
Andy Bavier99c11d32016-09-14 17:21:20 -0400209bootstrap
210cloudlab_setup
Andy Bavier2505f592016-11-11 15:58:55 -0500211vagrant_vms_up
Andy Bavier5c2e4fa2016-10-31 13:50:52 -0400212
213if [[ $SETUP_ONLY -ne 0 ]]
214then
215 echo "Finished build environment setup, exiting..."
216 exit 0
217fi
218
Andy Bavier99c11d32016-09-14 17:21:20 -0400219install_head_node
220set_up_maas_user
Srikanth Vavilapallib1c4da02016-11-17 15:24:28 -0600221
Andy Bavier2505f592016-11-11 15:58:55 -0500222#Limiting the maximum number of compute nodes that can be supported to 2. If
Srikanth Vavilapallib1c4da02016-11-17 15:24:28 -0600223#more than 2 compute nodes are needed, this script need to be enhanced to set
224#the environment variable NUM_COMPUTE_NODES before invoking the Vagrant commands
225if [[ $NUM_COMPUTE_NODES -gt 2 ]]
226then
227 echo "currently max only two compute nodes can be supported..."
228 NUM_COMPUTE_NODES=2
229fi
230
231for i in `seq 1 $NUM_COMPUTE_NODES`;
232do
233 echo adding the compute node: compute-node-$i
234 add_compute_node compute_node-$i build_compute_node-$i
235done
Andy Bavier99c11d32016-09-14 17:21:20 -0400236
237if [[ $RUN_TEST -eq 1 ]]
238then
239 run_e2e_test
240fi
241
242if [[ $DIAGNOSTICS -eq 1 ]]
243then
244 run_diagnostics
245fi
246
247exit 0