blob: c37fb98f787d643c17f6449b8d7f6a2a6208bab5 [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
Andy Bavierb22860a2016-12-06 12:05:02 -050015 sudo su $USER -c 'vagrant destroy'
Andy Bavier99c11d32016-09-14 17:21:20 -040016
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
Andy Bavier99c11d32016-09-14 17:21:20 -040031 sudo adduser $USER libvirtd
32
33 sudo curl -o /usr/local/bin/repo https://storage.googleapis.com/git-repo-downloads/repo
34 sudo chmod a+x /usr/local/bin/repo
35
36 if [ ! -d "$CORDDIR" ]
37 then
38 mkdir $CORDDIR && cd $CORDDIR
39 git config --global user.name 'Test User'
40 git config --global user.email 'test@null.com'
41 git config --global color.ui false
42
Scott Baker101b37c2016-10-24 16:22:43 -070043 repo init -u https://gerrit.opencord.org/manifest -b master -g build,onos,orchestration
Andy Bavier99c11d32016-09-14 17:21:20 -040044 repo sync
45
Zack Williamsdae7ff62016-11-14 15:20:06 -070046 # check out gerrit branches using repo
47 for gerrit_branch in ${GERRIT_BRANCHES[@]}; do
48 echo "checking out opencord gerrit branch: $gerrit_branch"
49 repo download ${gerrit_branch/:/ }
50 done
Andy Bavier99c11d32016-09-14 17:21:20 -040051 fi
52
53 cd $CORDDIR/build
Andy Bavier45e30bb2016-10-07 15:45:18 -040054 vagrant plugin install vagrant-libvirt --plugin-version 0.0.35
Andy Bavier99c11d32016-09-14 17:21:20 -040055 vagrant plugin install vagrant-mutate
56 vagrant box list ubuntu/trusty64 | grep virtualbox || vagrant box add ubuntu/trusty64
57 vagrant box list ubuntu/trusty64 | grep libvirt || vagrant mutate ubuntu/trusty64 libvirt --input-provider virtualbox
58}
59
60function cloudlab_setup() {
61 if [ -e /usr/testbed/bin/mkextrafs ]
62 then
Andy Bavier9c0835e2016-11-11 15:56:47 -050063 sudo mkdir -p /mnt/extra
Scott Baker8b9182d2016-10-27 17:53:52 -070064
Andy Bavier8ce395f2016-10-14 12:51:30 -040065 # Sometimes this command fails on the first try
Andy Bavier9c0835e2016-11-11 15:56:47 -050066 sudo /usr/testbed/bin/mkextrafs -r /dev/sdb -qf "/mnt/extra/" || sudo /usr/testbed/bin/mkextrafs -r /dev/sdb -qf "/mnt/extra/"
67
68 # Check that the mount succeeded (sometimes mkextrafs succeeds but device not mounted)
Andy Baviera69ee722016-11-17 07:26:01 -080069 mount | grep sdb || (echo "ERROR: mkextrafs failed, exiting!" && exit 1)
Scott Baker8b9182d2016-10-27 17:53:52 -070070
71 # we'll replace /var/lib/libvirt/images with a symlink below
72 [ -d /var/lib/libvirt/images/ ] && [ ! -h /var/lib/libvirt/images ] && sudo rmdir /var/lib/libvirt/images
73
Andy Bavier9c0835e2016-11-11 15:56:47 -050074 sudo mkdir -p /mnt/extra/libvirt_images
Andy Bavier2505f592016-11-11 15:58:55 -050075 if [ ! -e /var/lib/libvirt/images ]
76 then
77 sudo ln -s /mnt/extra/libvirt_images /var/lib/libvirt/images
78 fi
Andy Bavier99c11d32016-09-14 17:21:20 -040079 fi
80}
81
Andy Bavier2505f592016-11-11 15:58:55 -050082function vagrant_vms_up() {
Andy Bavier99c11d32016-09-14 17:21:20 -040083 cd $CORDDIR/build
84
85 sudo su $USER -c 'vagrant up corddev --provider libvirt'
Andy Bavier2505f592016-11-11 15:58:55 -050086 sudo su $USER -c 'vagrant up prod --provider libvirt'
Andy Baviera69ee722016-11-17 07:26:01 -080087
88 # This is a workaround for a weird issue with ARP cache timeout breaking 'vagrant ssh'
89 # It allows SSH'ing to the machine via 'ssh corddev'
Andy Bavier2505f592016-11-11 15:58:55 -050090 sudo su $USER -c "vagrant ssh-config corddev prod > $SSHCONFIG"
91
92 scp ~/.ssh/id_rsa* corddev:.ssh
93 ssh corddev "chmod go-r ~/.ssh/id_rsa"
Andy Bavier99c11d32016-09-14 17:21:20 -040094}
95
96function install_head_node() {
97 cd $CORDDIR/build
98
Andy Baviera69ee722016-11-17 07:26:01 -080099 # SSH config saved earlier allows us to connect to VM without running 'vagrant'
Andy Baviera69ee722016-11-17 07:26:01 -0800100 ssh corddev "cd /cord/build; ./gradlew fetch"
101 ssh corddev "cd /cord/build; ./gradlew buildImages"
Andy Bavier2505f592016-11-11 15:58:55 -0500102 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 -0800103 ssh corddev "cd /cord/build; ./gradlew -PdeployConfig=$VMDIR/$CONFIG deploy"
Andy Bavier99c11d32016-09-14 17:21:20 -0400104}
105
106function set_up_maas_user() {
Andy Bavier2505f592016-11-11 15:58:55 -0500107 # Set up MAAS user on server to restart nodes via libvirt
108 grep maas /etc/passwd || sudo useradd -m maas
Andy Bavier99c11d32016-09-14 17:21:20 -0400109 sudo adduser maas libvirtd
110
Andy Bavier2505f592016-11-11 15:58:55 -0500111 # Copy generated public key to maas user's authorized_keys
112 sudo su maas -c "mkdir -p ~/.ssh"
113 sudo cp $HOME/.ssh/id_rsa.pub ~maas/.ssh/authorized_keys
114 sudo chown maas:maas ~maas/.ssh/authorized_keys
Andy Bavier99c11d32016-09-14 17:21:20 -0400115
Andy Bavier2505f592016-11-11 15:58:55 -0500116 # Copy generated private key to maas user's home dir in prod VM
117 scp $HOME/.ssh/id_rsa prod:/tmp
118 ssh prod "sudo mkdir -p ~maas/.ssh"
119 ssh prod "sudo cp /tmp/id_rsa ~maas/.ssh/id_rsa"
120 ssh prod "sudo chown -R maas:maas ~maas/.ssh"
121}
Srikanth Vavilapallib1c4da02016-11-17 15:24:28 -0600122
Andy Bavier99c11d32016-09-14 17:21:20 -0400123function add_compute_node() {
Srikanth Vavilapallib1c4da02016-11-17 15:24:28 -0600124 echo add_compute_node: $1 $2
Andy Bavier2505f592016-11-11 15:58:55 -0500125
Andy Bavier99c11d32016-09-14 17:21:20 -0400126 cd $CORDDIR/build
Srikanth Vavilapallib1c4da02016-11-17 15:24:28 -0600127 sudo su $USER -c "vagrant up $1 --provider libvirt"
Andy Bavier99c11d32016-09-14 17:21:20 -0400128
Andy Bavier2505f592016-11-11 15:58:55 -0500129 # Change MAC address of bridge to match cord-pod service profile
130 # This change won't survive a reboot
131 BRIDGE=$( route -n | grep 10.6.1.0 | awk '{print $8}' )
132 sudo ifconfig $BRIDGE hw ether 02:42:0a:06:01:01
Andy Bavierb5796fb2016-10-20 12:06:47 -0400133
Andy Bavier2505f592016-11-11 15:58:55 -0500134 ip addr list | grep 10.6.1.129 || sudo ip address add 10.6.1.129 dev $BRIDGE
135 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 -0600136
Andy Bavier2505f592016-11-11 15:58:55 -0500137 # Set up power cycling for the compute node and wait for it to be provisioned
138 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 -0400139
140 echo ""
141 echo "compute_node is fully provisioned!"
142}
143
144function run_e2e_test () {
145 cd $CORDDIR/build
146
Andy Bavier2505f592016-11-11 15:58:55 -0500147 # User has been added to the lbvirtd group, but su $USER to be safe
Andy Baviera69ee722016-11-17 07:26:01 -0800148 ssh corddev "cd /cord/build; ./gradlew -PdeployConfig=$VMDIR/$CONFIG postDeployTests"
Andy Bavier99c11d32016-09-14 17:21:20 -0400149}
150
151function run_diagnostics() {
152 echo "*** COLLECTING DIAGNOSTIC INFO NOT CURRENTLY IMPLEMENTED"
153 # Need to fix up inventory to collect info from compute nodes
154 # Using juju-ansible is one possibility
155 #echo "*** COLLECTING DIAGNOSTIC INFO - check ~/diag-* on the head node"
156 #ansible-playbook -i $INVENTORY cord-diag-playbook.yml
157}
158
159# Parse options
Zack Williamsdae7ff62016-11-14 15:20:06 -0700160GERRIT_BRANCHES=
Andy Bavier99c11d32016-09-14 17:21:20 -0400161RUN_TEST=0
Andy Bavier5c2e4fa2016-10-31 13:50:52 -0400162SETUP_ONLY=0
Andy Bavier99c11d32016-09-14 17:21:20 -0400163DIAGNOSTICS=0
164CLEANUP=0
Srikanth Vavilapallib1c4da02016-11-17 15:24:28 -0600165#By default, cord-in-a-box creates 1 compute node. If more than one compute is
166#needed, use -n option
167NUM_COMPUTE_NODES=1
Andy Bavier99c11d32016-09-14 17:21:20 -0400168
Srikanth Vavilapallib1c4da02016-11-17 15:24:28 -0600169while getopts "b:cdhn:st" opt; do
Andy Bavier99c11d32016-09-14 17:21:20 -0400170 case ${opt} in
Zack Williamsdae7ff62016-11-14 15:20:06 -0700171 b ) GERRIT_BRANCHES+=("$OPTARG")
Andy Bavier99c11d32016-09-14 17:21:20 -0400172 ;;
173 c ) CLEANUP=1
174 ;;
175 d ) DIAGNOSTICS=1
176 ;;
177 h ) echo "Usage:"
178 echo " $0 install OpenStack and prep XOS and ONOS VMs [default]"
Zack Williamsdae7ff62016-11-14 15:20:06 -0700179 echo " $0 -b <project:changeset/revision> checkout a changesets from gerrit. Can"
180 echo " be used multiple times."
Andy Bavier99c11d32016-09-14 17:21:20 -0400181 echo " $0 -c cleanup from previous test"
182 echo " $0 -d run diagnostic collector"
183 echo " $0 -h display this help message"
Srikanth Vavilapallib1c4da02016-11-17 15:24:28 -0600184 echo " $0 -n number of compute nodes to setup. currently max 2 nodes can be supported"
Andy Bavier5c2e4fa2016-10-31 13:50:52 -0400185 echo " $0 -s run initial setup phase only (don't start building CORD)"
Andy Bavier99c11d32016-09-14 17:21:20 -0400186 echo " $0 -t do install, bring up cord-pod configuration, run E2E test"
187 exit 0
188 ;;
Srikanth Vavilapallib1c4da02016-11-17 15:24:28 -0600189 n ) NUM_COMPUTE_NODES=$OPTARG
190 ;;
Andy Bavier5c2e4fa2016-10-31 13:50:52 -0400191 s ) SETUP_ONLY=1
192 ;;
Andy Bavier99c11d32016-09-14 17:21:20 -0400193 t ) RUN_TEST=1
194 ;;
195 \? ) echo "Invalid option: -$OPTARG"
196 exit 1
197 ;;
198 esac
199done
200
201# What to do
202if [[ $CLEANUP -eq 1 ]]
203then
204 cleanup_from_previous_test
205fi
206
Andy Bavier99c11d32016-09-14 17:21:20 -0400207bootstrap
208cloudlab_setup
Andy Bavier2505f592016-11-11 15:58:55 -0500209vagrant_vms_up
Andy Bavier5c2e4fa2016-10-31 13:50:52 -0400210
211if [[ $SETUP_ONLY -ne 0 ]]
212then
213 echo "Finished build environment setup, exiting..."
214 exit 0
215fi
216
Andy Bavier99c11d32016-09-14 17:21:20 -0400217install_head_node
218set_up_maas_user
Srikanth Vavilapallib1c4da02016-11-17 15:24:28 -0600219
Andy Bavier2505f592016-11-11 15:58:55 -0500220#Limiting the maximum number of compute nodes that can be supported to 2. If
Srikanth Vavilapallib1c4da02016-11-17 15:24:28 -0600221#more than 2 compute nodes are needed, this script need to be enhanced to set
222#the environment variable NUM_COMPUTE_NODES before invoking the Vagrant commands
223if [[ $NUM_COMPUTE_NODES -gt 2 ]]
224then
225 echo "currently max only two compute nodes can be supported..."
226 NUM_COMPUTE_NODES=2
227fi
228
229for i in `seq 1 $NUM_COMPUTE_NODES`;
230do
231 echo adding the compute node: compute-node-$i
232 add_compute_node compute_node-$i build_compute_node-$i
233done
Andy Bavier99c11d32016-09-14 17:21:20 -0400234
235if [[ $RUN_TEST -eq 1 ]]
236then
237 run_e2e_test
238fi
239
240if [[ $DIAGNOSTICS -eq 1 ]]
241then
242 run_diagnostics
243fi
244
245exit 0