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