blob: f34aaba5ddd592b1b30b998a9b00dc100a228ffc [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
Andy Bavierd1165102017-01-13 16:38:57 -050010# For CORD version
11REPO_BRANCH="master"
12VERSION_STRING="CiaB development version"
13
Andy Bavier99c11d32016-09-14 17:21:20 -040014function cleanup_from_previous_test() {
Andy Bavier99c11d32016-09-14 17:21:20 -040015 echo "## Cleanup ##"
16
Andy Baviera4883ba2016-12-21 15:46:58 -050017 if [ -d $CORDDIR/build ]
18 then
19 echo "Destroying all Vagrant VMs"
20 cd $CORDDIR/build
21 sudo su $USER -c 'vagrant destroy'
22 fi
Andy Bavier99c11d32016-09-14 17:21:20 -040023
Andy Bavier2505f592016-11-11 15:58:55 -050024 echo "Removing $CORDDIR"
25 cd ~
Andy Bavier99c11d32016-09-14 17:21:20 -040026 rm -rf $CORDDIR
Andy Bavier99c11d32016-09-14 17:21:20 -040027}
28
29function bootstrap() {
30 cd ~
31 sudo apt-get update
32 [ -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 -050033 dpkg -l vagrant || sudo dpkg -i vagrant_1.8.5_x86_64.deb
Andy Bavier1a35ae62016-11-15 19:58:13 -080034 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 -040035
36 [ -e ~/.ssh/id_rsa ] || ssh-keygen -t rsa -N "" -f ~/.ssh/id_rsa
Andy Bavier99c11d32016-09-14 17:21:20 -040037
Andy Bavier99c11d32016-09-14 17:21:20 -040038 sudo adduser $USER libvirtd
39
40 sudo curl -o /usr/local/bin/repo https://storage.googleapis.com/git-repo-downloads/repo
41 sudo chmod a+x /usr/local/bin/repo
42
43 if [ ! -d "$CORDDIR" ]
44 then
45 mkdir $CORDDIR && cd $CORDDIR
46 git config --global user.name 'Test User'
47 git config --global user.email 'test@null.com'
48 git config --global color.ui false
49
Andy Bavierd1165102017-01-13 16:38:57 -050050 repo init -u https://gerrit.opencord.org/manifest -b $REPO_BRANCH -g build,onos,orchestration
Andy Bavier99c11d32016-09-14 17:21:20 -040051 repo sync
52
Zack Williamsdae7ff62016-11-14 15:20:06 -070053 # check out gerrit branches using repo
54 for gerrit_branch in ${GERRIT_BRANCHES[@]}; do
55 echo "checking out opencord gerrit branch: $gerrit_branch"
56 repo download ${gerrit_branch/:/ }
57 done
Andy Bavier99c11d32016-09-14 17:21:20 -040058 fi
59
60 cd $CORDDIR/build
Andy Bavierad3ea842016-12-21 16:19:07 -050061 vagrant plugin list | grep vagrant-libvirt || vagrant plugin install vagrant-libvirt --plugin-version 0.0.35
62 vagrant plugin list | grep vagrant-mutate || vagrant plugin install vagrant-mutate
Andy Bavier99c11d32016-09-14 17:21:20 -040063 vagrant box list ubuntu/trusty64 | grep virtualbox || vagrant box add ubuntu/trusty64
64 vagrant box list ubuntu/trusty64 | grep libvirt || vagrant mutate ubuntu/trusty64 libvirt --input-provider virtualbox
65}
66
67function cloudlab_setup() {
68 if [ -e /usr/testbed/bin/mkextrafs ]
69 then
Andy Bavier9c0835e2016-11-11 15:56:47 -050070 sudo mkdir -p /mnt/extra
Scott Baker8b9182d2016-10-27 17:53:52 -070071
Andy Bavier8ce395f2016-10-14 12:51:30 -040072 # Sometimes this command fails on the first try
Andy Bavier9c0835e2016-11-11 15:56:47 -050073 sudo /usr/testbed/bin/mkextrafs -r /dev/sdb -qf "/mnt/extra/" || sudo /usr/testbed/bin/mkextrafs -r /dev/sdb -qf "/mnt/extra/"
74
75 # Check that the mount succeeded (sometimes mkextrafs succeeds but device not mounted)
Andy Baviera69ee722016-11-17 07:26:01 -080076 mount | grep sdb || (echo "ERROR: mkextrafs failed, exiting!" && exit 1)
Scott Baker8b9182d2016-10-27 17:53:52 -070077
78 # we'll replace /var/lib/libvirt/images with a symlink below
79 [ -d /var/lib/libvirt/images/ ] && [ ! -h /var/lib/libvirt/images ] && sudo rmdir /var/lib/libvirt/images
80
Andy Bavier9c0835e2016-11-11 15:56:47 -050081 sudo mkdir -p /mnt/extra/libvirt_images
Andy Bavier2505f592016-11-11 15:58:55 -050082 if [ ! -e /var/lib/libvirt/images ]
83 then
84 sudo ln -s /mnt/extra/libvirt_images /var/lib/libvirt/images
85 fi
Andy Bavier99c11d32016-09-14 17:21:20 -040086 fi
87}
88
Andy Bavier2505f592016-11-11 15:58:55 -050089function vagrant_vms_up() {
Andy Bavier99c11d32016-09-14 17:21:20 -040090 cd $CORDDIR/build
91
Andy Bavier65c8e152016-12-22 16:01:27 -050092 sudo su $USER -c 'vagrant up corddev prod --provider libvirt'
Andy Baviera69ee722016-11-17 07:26:01 -080093
94 # This is a workaround for a weird issue with ARP cache timeout breaking 'vagrant ssh'
95 # It allows SSH'ing to the machine via 'ssh corddev'
Andy Bavier2505f592016-11-11 15:58:55 -050096 sudo su $USER -c "vagrant ssh-config corddev prod > $SSHCONFIG"
97
98 scp ~/.ssh/id_rsa* corddev:.ssh
99 ssh corddev "chmod go-r ~/.ssh/id_rsa"
Andy Bavier99c11d32016-09-14 17:21:20 -0400100}
101
102function install_head_node() {
103 cd $CORDDIR/build
104
Andy Baviera69ee722016-11-17 07:26:01 -0800105 # SSH config saved earlier allows us to connect to VM without running 'vagrant'
Andy Baviera69ee722016-11-17 07:26:01 -0800106 ssh corddev "cd /cord/build; ./gradlew fetch"
107 ssh corddev "cd /cord/build; ./gradlew buildImages"
Andy Bavier2505f592016-11-11 15:58:55 -0500108 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 -0800109 ssh corddev "cd /cord/build; ./gradlew -PdeployConfig=$VMDIR/$CONFIG deploy"
Andy Bavier99c11d32016-09-14 17:21:20 -0400110}
111
112function set_up_maas_user() {
Andy Bavier2505f592016-11-11 15:58:55 -0500113 # Set up MAAS user on server to restart nodes via libvirt
114 grep maas /etc/passwd || sudo useradd -m maas
Andy Bavier99c11d32016-09-14 17:21:20 -0400115 sudo adduser maas libvirtd
116
Andy Bavier2505f592016-11-11 15:58:55 -0500117 # Copy generated public key to maas user's authorized_keys
118 sudo su maas -c "mkdir -p ~/.ssh"
119 sudo cp $HOME/.ssh/id_rsa.pub ~maas/.ssh/authorized_keys
120 sudo chown maas:maas ~maas/.ssh/authorized_keys
Andy Bavier99c11d32016-09-14 17:21:20 -0400121
Andy Bavier2505f592016-11-11 15:58:55 -0500122 # Copy generated private key to maas user's home dir in prod VM
123 scp $HOME/.ssh/id_rsa prod:/tmp
124 ssh prod "sudo mkdir -p ~maas/.ssh"
125 ssh prod "sudo cp /tmp/id_rsa ~maas/.ssh/id_rsa"
126 ssh prod "sudo chown -R maas:maas ~maas/.ssh"
127}
Srikanth Vavilapallib1c4da02016-11-17 15:24:28 -0600128
Andy Bavier99c11d32016-09-14 17:21:20 -0400129function add_compute_node() {
Srikanth Vavilapallib1c4da02016-11-17 15:24:28 -0600130 echo add_compute_node: $1 $2
Andy Bavier2505f592016-11-11 15:58:55 -0500131
Andy Bavier99c11d32016-09-14 17:21:20 -0400132 cd $CORDDIR/build
Srikanth Vavilapallib1c4da02016-11-17 15:24:28 -0600133 sudo su $USER -c "vagrant up $1 --provider libvirt"
Andy Bavier99c11d32016-09-14 17:21:20 -0400134
Andy Bavier2505f592016-11-11 15:58:55 -0500135 # Change MAC address of bridge to match cord-pod service profile
136 # This change won't survive a reboot
137 BRIDGE=$( route -n | grep 10.6.1.0 | awk '{print $8}' )
138 sudo ifconfig $BRIDGE hw ether 02:42:0a:06:01:01
Andy Bavierb5796fb2016-10-20 12:06:47 -0400139
Andy Bavier2505f592016-11-11 15:58:55 -0500140 ip addr list | grep 10.6.1.129 || sudo ip address add 10.6.1.129 dev $BRIDGE
141 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 -0600142
Andy Bavier2505f592016-11-11 15:58:55 -0500143 # Set up power cycling for the compute node and wait for it to be provisioned
144 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 -0400145
146 echo ""
147 echo "compute_node is fully provisioned!"
148}
149
150function run_e2e_test () {
151 cd $CORDDIR/build
152
Andy Bavier2505f592016-11-11 15:58:55 -0500153 # User has been added to the lbvirtd group, but su $USER to be safe
Andy Baviera69ee722016-11-17 07:26:01 -0800154 ssh corddev "cd /cord/build; ./gradlew -PdeployConfig=$VMDIR/$CONFIG postDeployTests"
Andy Bavier99c11d32016-09-14 17:21:20 -0400155}
156
157function run_diagnostics() {
158 echo "*** COLLECTING DIAGNOSTIC INFO NOT CURRENTLY IMPLEMENTED"
159 # Need to fix up inventory to collect info from compute nodes
160 # Using juju-ansible is one possibility
161 #echo "*** COLLECTING DIAGNOSTIC INFO - check ~/diag-* on the head node"
162 #ansible-playbook -i $INVENTORY cord-diag-playbook.yml
163}
164
165# Parse options
Zack Williamsdae7ff62016-11-14 15:20:06 -0700166GERRIT_BRANCHES=
Andy Bavier99c11d32016-09-14 17:21:20 -0400167RUN_TEST=0
Andy Bavier5c2e4fa2016-10-31 13:50:52 -0400168SETUP_ONLY=0
Andy Bavier99c11d32016-09-14 17:21:20 -0400169DIAGNOSTICS=0
170CLEANUP=0
Srikanth Vavilapallib1c4da02016-11-17 15:24:28 -0600171#By default, cord-in-a-box creates 1 compute node. If more than one compute is
172#needed, use -n option
173NUM_COMPUTE_NODES=1
Andy Bavier99c11d32016-09-14 17:21:20 -0400174
Andy Bavierd1165102017-01-13 16:38:57 -0500175while getopts "b:cdhn:stv" opt; do
Andy Bavier99c11d32016-09-14 17:21:20 -0400176 case ${opt} in
Zack Williamsdae7ff62016-11-14 15:20:06 -0700177 b ) GERRIT_BRANCHES+=("$OPTARG")
Andy Bavier99c11d32016-09-14 17:21:20 -0400178 ;;
179 c ) CLEANUP=1
180 ;;
181 d ) DIAGNOSTICS=1
182 ;;
183 h ) echo "Usage:"
184 echo " $0 install OpenStack and prep XOS and ONOS VMs [default]"
Zack Williamsdae7ff62016-11-14 15:20:06 -0700185 echo " $0 -b <project:changeset/revision> checkout a changesets from gerrit. Can"
186 echo " be used multiple times."
Andy Bavier99c11d32016-09-14 17:21:20 -0400187 echo " $0 -c cleanup from previous test"
188 echo " $0 -d run diagnostic collector"
189 echo " $0 -h display this help message"
Srikanth Vavilapallib1c4da02016-11-17 15:24:28 -0600190 echo " $0 -n number of compute nodes to setup. currently max 2 nodes can be supported"
Andy Bavier5c2e4fa2016-10-31 13:50:52 -0400191 echo " $0 -s run initial setup phase only (don't start building CORD)"
Andy Bavier99c11d32016-09-14 17:21:20 -0400192 echo " $0 -t do install, bring up cord-pod configuration, run E2E test"
Andy Bavierd1165102017-01-13 16:38:57 -0500193 echo " $0 -v print CiaB version and exit"
Andy Bavier99c11d32016-09-14 17:21:20 -0400194 exit 0
195 ;;
Srikanth Vavilapallib1c4da02016-11-17 15:24:28 -0600196 n ) NUM_COMPUTE_NODES=$OPTARG
197 ;;
Andy Bavier5c2e4fa2016-10-31 13:50:52 -0400198 s ) SETUP_ONLY=1
199 ;;
Andy Bavier99c11d32016-09-14 17:21:20 -0400200 t ) RUN_TEST=1
201 ;;
Andy Bavierd1165102017-01-13 16:38:57 -0500202 v ) echo "$VERSION_STRING ($REPO_BRANCH branch)"
203 exit 0
204 ;;
Andy Bavier99c11d32016-09-14 17:21:20 -0400205 \? ) echo "Invalid option: -$OPTARG"
206 exit 1
207 ;;
208 esac
209done
210
211# What to do
212if [[ $CLEANUP -eq 1 ]]
213then
214 cleanup_from_previous_test
215fi
216
Andy Bavierd1165102017-01-13 16:38:57 -0500217echo ""
218echo "Preparing to install $VERSION_STRING ($REPO_BRANCH branch)"
219echo ""
220
Andy Bavier99c11d32016-09-14 17:21:20 -0400221bootstrap
222cloudlab_setup
Andy Bavier2505f592016-11-11 15:58:55 -0500223vagrant_vms_up
Andy Bavier5c2e4fa2016-10-31 13:50:52 -0400224
225if [[ $SETUP_ONLY -ne 0 ]]
226then
227 echo "Finished build environment setup, exiting..."
228 exit 0
229fi
230
Andy Bavier99c11d32016-09-14 17:21:20 -0400231install_head_node
232set_up_maas_user
Srikanth Vavilapallib1c4da02016-11-17 15:24:28 -0600233
Andy Bavier2505f592016-11-11 15:58:55 -0500234#Limiting the maximum number of compute nodes that can be supported to 2. If
Srikanth Vavilapallib1c4da02016-11-17 15:24:28 -0600235#more than 2 compute nodes are needed, this script need to be enhanced to set
236#the environment variable NUM_COMPUTE_NODES before invoking the Vagrant commands
237if [[ $NUM_COMPUTE_NODES -gt 2 ]]
238then
239 echo "currently max only two compute nodes can be supported..."
240 NUM_COMPUTE_NODES=2
241fi
242
243for i in `seq 1 $NUM_COMPUTE_NODES`;
244do
245 echo adding the compute node: compute-node-$i
246 add_compute_node compute_node-$i build_compute_node-$i
247done
Andy Bavier99c11d32016-09-14 17:21:20 -0400248
249if [[ $RUN_TEST -eq 1 ]]
250then
251 run_e2e_test
252fi
253
254if [[ $DIAGNOSTICS -eq 1 ]]
255then
256 run_diagnostics
257fi
258
259exit 0