blob: 525518cd751bb4955ab45ed84e7c839f0bb8583a [file] [log] [blame]
Zack Williamsa1f55082017-02-28 22:41:36 -07001#!/usr/bin/env bash
2# opencloud-in-a-box.sh
3
4set -e -x
5
6# start time, used to name logfiles
7START_T=$(date -u "+%Y%m%d_%H%M%SZ")
8
9# Paths
10CORDDIR=~/cord
11CONFIG=${CORDDIR}/config/opencloud_in_a_box.yml
12SSHCONFIG=~/.ssh/config
13VAGRANT_CWD=${CORDDIR}/build/targets/opencloud-in-a-box
14
15# CORD versioning
16REPO_BRANCH="master"
17VERSION_STRING="OpenCloud Devel"
18
19function add_box() {
20 echo "Downloading image: $1"
21 vagrant box list | grep $1 | grep virtualbox || vagrant box add $1
22 vagrant box list | grep $1 | grep libvirt || vagrant mutate $1 libvirt --input-provider virtualbox
23}
24
25function run_stage {
26 echo "==> "$1": Starting"
27 $1
28 echo "==> "$1": Complete"
29}
30
31function cleanup_from_previous_test() {
32 echo "## Cleanup ##"
33
34 if [ -d $CORDDIR/build ]
35 then
36 echo "Destroying all Vagrant VMs"
37 cd $CORDDIR/build
38 sudo VAGRANT_CWD=$VAGRANT_CWD vagrant destroy
39 fi
40
41 echo "Removing $CORDDIR"
42 cd ~
43 rm -rf $CORDDIR
44}
45
46function bootstrap() {
47
48 if [ ! -x "/usr/bin/ansible" ]
49 then
50 echo "Installing Ansible..."
51 sudo apt-get update
52 sudo apt-get install -y software-properties-common apt-transport-https
Sapan Bhatia35246092017-04-13 19:42:02 -070053 #sudo apt-add-repository -y ppa:ansible/ansible # latest supported version
54 sudo apt-get -y install python-dev libffi-dev python-pip libssl-dev sshpass
55 sudo pip install ansible==2.2.2.0
Zack Williamsa1f55082017-02-28 22:41:36 -070056 sudo apt-get update
Sapan Bhatia35246092017-04-13 19:42:02 -070057 sudo apt-get install -y python-netaddr
Zack Williamsa1f55082017-02-28 22:41:36 -070058 fi
59
60 if [ ! -x "/usr/local/bin/repo" ]
61 then
62 echo "Installing repo..."
63 REPO_SHA256SUM="e147f0392686c40cfd7d5e6f332c6ee74c4eab4d24e2694b3b0a0c037bf51dc5" # not versioned...
64 curl -o /tmp/repo https://storage.googleapis.com/git-repo-downloads/repo
65 echo "$REPO_SHA256SUM /tmp/repo" | sha256sum -c -
66 sudo mv /tmp/repo /usr/local/bin/repo
67 sudo chmod a+x /usr/local/bin/repo
68 fi
69
70 if [ ! -x "/usr/bin/vagrant" ]
71 then
72 echo "Installing vagrant and associated tools..."
73 VAGRANT_SHA256SUM="52ebd6aa798582681d0f1e008982c709136eeccd668a8e8be4d48a1f632de7b8" # version 1.9.2
74 curl -o /tmp/vagrant.deb https://releases.hashicorp.com/vagrant/1.9.2/vagrant_1.9.2_x86_64.deb
75 echo "$VAGRANT_SHA256SUM /tmp/vagrant.deb" | sha256sum -c -
76 sudo dpkg -i /tmp/vagrant.deb
77 sudo apt-get -y install qemu-kvm libvirt-bin libvirt-dev curl nfs-kernel-server git build-essential python-pip
78 sudo adduser $USER libvirtd
79 sudo pip install pyparsing python-logstash
80
81 run_stage cloudlab_setup
82
83 echo "Installing vagrant plugins..."
84 vagrant plugin list | grep vagrant-libvirt || vagrant plugin install vagrant-libvirt --plugin-version 0.0.35
85 vagrant plugin list | grep vagrant-mutate || vagrant plugin install vagrant-mutate
86
87 add_box ubuntu/trusty64
88 fi
89
90 if [ ! -d "$CORDDIR" ]
91 then
92 echo "Downloading CORD/XOS..."
93
94 if [ ! -e "~/.gitconfig" ]
95 then
96 echo "No ~/.gitconfig, setting testing defaults"
97 git config --global user.name 'Test User'
98 git config --global user.email 'test@null.com'
99 git config --global color.ui false
100 fi
101
102 # make sure we can find gerrit.opencord.org as DNS failures will fail the build
103 dig +short gerrit.opencord.org || (echo "ERROR: gerrit.opencord.org can't be looked up in DNS" && exit 1)
104
105 mkdir $CORDDIR && cd $CORDDIR
106 repo init -u https://gerrit.opencord.org/manifest -b master -g build,onos,orchestration,voltha
107 repo sync
108
109 # check out gerrit branches using repo
110 for gerrit_branch in ${GERRIT_BRANCHES[@]}; do
111 echo "Checking out opencord gerrit branch: $gerrit_branch"
112 repo download ${gerrit_branch/:/ }
113 done
114 fi
115}
116
117function cloudlab_setup() {
118
119 # Don't do anything if not a CloudLab node
120 [ ! -d /usr/local/etc/emulab ] && return
121
122 # The watchdog will sometimes reset groups, turn it off
123 if [ -e /usr/local/etc/emulab/watchdog ]
124 then
125 sudo /usr/bin/perl -w /usr/local/etc/emulab/watchdog stop
126 sudo mv /usr/local/etc/emulab/watchdog /usr/local/etc/emulab/watchdog-disabled
127 fi
128
129 # Mount extra space, if haven't already
130 if [ ! -d /mnt/extra ]
131 then
132 sudo mkdir -p /mnt/extra
133
134 # for NVME SSD on Utah Cloudlab, not supported by mkextrafs
135 if $(df | grep -q nvme0n1p1) && [ -e /usr/testbed/bin/mkextrafs ]
136 then
137 # set partition type of 4th partition to Linux, ignore errors
138 echo -e "t\n4\n82\np\nw\nq" | sudo fdisk /dev/nvme0n1 || true
139
140 sudo mkfs.ext4 /dev/nvme0n1p4
141 echo "/dev/nvme0n1p4 /mnt/extra/ ext4 defaults 0 0" | sudo tee -a /etc/fstab
142 sudo mount /mnt/extra
143 mount | grep nvme0n1p4 || (echo "ERROR: NVME mkfs/mount failed, exiting!" && exit 1)
144
145 elif [ -e /usr/testbed/bin/mkextrafs ] # if on Clemson/Wisconsin Cloudlab
146 then
147 # Sometimes this command fails on the first try
148 sudo /usr/testbed/bin/mkextrafs -r /dev/sdb -qf "/mnt/extra/" || sudo /usr/testbed/bin/mkextrafs -r /dev/sdb -qf "/mnt/extra/"
149
150 # Check that the mount succeeded (sometimes mkextrafs succeeds but device not mounted)
151 mount | grep sdb || (echo "ERROR: mkextrafs failed, exiting!" && exit 1)
152 fi
153 fi
154
155 # replace /var/lib/libvirt/images with a symlink
156 [ -d /var/lib/libvirt/images/ ] && [ ! -h /var/lib/libvirt/images ] && sudo rmdir /var/lib/libvirt/images
157 sudo mkdir -p /mnt/extra/libvirt_images
158
159 if [ ! -e /var/lib/libvirt/images ]
160 then
161 sudo ln -s /mnt/extra/libvirt_images /var/lib/libvirt/images
162 fi
163}
164
165function vagrant_vms_up() {
166
167 # vagrant-libvirt 0.0.35 fails with libvirt networks created by others
168 # echo "Configuring libvirt networking..."
169 # cd ${CORDDIR}/build/ansible
170 # ansible-playbook opencloud-in-a-box-playbook.yml
171
172 echo "Bringing up OpenCloud-in-a-Box Vagrant VM's..."
173 cd $CORDDIR/build
174 sudo VAGRANT_CWD=$VAGRANT_CWD vagrant up head1 compute1 compute2 --provider libvirt
175
176 # This is a workaround for a weird issue with ARP cache timeout breaking 'vagrant ssh'
177 # It allows SSH'ing to the machine via 'ssh <machinename>'
178 echo "Configuring SSH for VM's..."
179 sudo VAGRANT_CWD=$VAGRANT_CWD vagrant ssh-config head1 compute1 compute2 > $SSHCONFIG
180
181 sudo chown -R ${USER} ${VAGRANT_CWD}/.vagrant
182}
183
184function opencloud_buildhost_prep() {
185
186 echo "Preparing build tools..."
187 cd ${CORDDIR}/build/platform-install
188 ansible-playbook -i inventory/opencloud opencloud-prep-playbook.yml
189}
190
191# Parse options
192GERRIT_BRANCHES=
193RUN_TEST=0
194SETUP_ONLY=0
195CLEANUP=0
196
197while getopts "b:chsv" opt; do
198 case ${opt} in
199 b ) GERRIT_BRANCHES+=("$OPTARG")
200 ;;
201 c ) CLEANUP=1
202 ;;
203 h ) echo "Usage:"
204 echo " $0 install OpenCloud-in-a-Box [default]"
205 echo " $0 -b <project:changeset/revision> checkout a changesets from gerrit. Can"
206 echo " be used multiple times."
207 echo " $0 -c cleanup from previous test"
208 echo " $0 -h display this help message"
209 echo " $0 -s run initial setup phase only (don't start building CORD)"
210 echo " $0 -v print CiaB version and exit"
211 exit 0
212 ;;
213 s ) SETUP_ONLY=1
214 ;;
215 v ) echo "$VERSION_STRING ($REPO_BRANCH branch)"
216 exit 0
217 ;;
218 \? ) echo "Invalid option: -$OPTARG"
219 exit 1
220 ;;
221 esac
222done
223
224# What to do
225if [[ $CLEANUP -eq 1 ]]
226then
227 run_stage cleanup_from_previous_test
228fi
229
230echo ""
231echo "Preparing to install $VERSION_STRING ($REPO_BRANCH branch)"
232echo ""
233
234run_stage bootstrap
235
236
237if [[ $SETUP_ONLY -ne 0 ]]
238then
239 echo "Finished build environment setup, exiting..."
240 exit 0
241fi
242
243run_stage vagrant_vms_up
244run_stage opencloud_buildhost_prep
245
246exit 0