blob: 9ddc21cf3d2a89009a0f16fac6eedf129878963c [file] [log] [blame]
Andy Bavierc4a40512017-08-10 07:06:25 -07001#!/usr/bin/env bash
2#
Matteo Scandolo60b640f2017-08-08 13:05:22 -07003# Copyright 2017-present Open Networking Foundation
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
Zack Williamsce63eb02017-02-28 10:46:22 -070017# cord-bootstrap.sh
18# Bootstraps a dev system for CORD, downloads source
19
Zack Williams32295d52017-08-28 22:49:37 -070020set -e -u -o pipefail
Zack Williamsce63eb02017-02-28 10:46:22 -070021
22# start time, used to name logfiles
23START_T=$(date -u "+%Y%m%d%H%M%SZ")
24
25# Location of 'cord' directory checked out on the local system
26CORDDIR="${CORDDIR:-${HOME}/cord}"
27
28# Commands
29MAKECMD="${MAKECMD:-make -j4}"
30
31# CORD versioning
32REPO_BRANCH="${REPO_BRANCH:-master}"
33
34# Functions
35function run_stage {
36 echo "==> "$1": Starting"
37 $1
38 echo "==> "$1": Complete"
39}
40
41function bootstrap_ansible() {
42
43 if [ ! -x "/usr/bin/ansible" ]
44 then
45 echo "Installing Ansible..."
46 sudo apt-get update
47 sudo apt-get -y install apt-transport-https build-essential curl git python-dev \
48 python-netaddr python-pip software-properties-common sshpass
49 sudo apt-add-repository -y ppa:ansible/ansible # latest supported version
50 sudo apt-get update
51 sudo apt-get install -y ansible
52 sudo pip install gitpython graphviz
53 fi
54}
55
56function bootstrap_repo() {
57
58 if [ ! -x "/usr/local/bin/repo" ]
59 then
60 echo "Installing repo..."
61 # v1.23, per https://source.android.com/source/downloading
62 REPO_SHA256SUM="e147f0392686c40cfd7d5e6f332c6ee74c4eab4d24e2694b3b0a0c037bf51dc5"
63 curl -o /tmp/repo https://storage.googleapis.com/git-repo-downloads/repo
64 echo "$REPO_SHA256SUM /tmp/repo" | sha256sum -c -
65 sudo mv /tmp/repo /usr/local/bin/repo
66 sudo chmod a+x /usr/local/bin/repo
67 fi
68
69 if [ ! -d "$CORDDIR" ]
70 then
71 # make sure we can find gerrit.opencord.org as DNS failures will fail the build
72 dig +short gerrit.opencord.org || (echo "ERROR: gerrit.opencord.org can't be looked up in DNS" && exit 1)
73
74 echo "Downloading CORD/XOS, branch:'${REPO_BRANCH}'..."
75
76 if [ ! -e "${HOME}/.gitconfig" ]
77 then
78 echo "No ${HOME}/.gitconfig, setting testing defaults"
79 git config --global user.name 'Test User'
80 git config --global user.email 'test@null.com'
81 git config --global color.ui false
82 fi
83
84 mkdir $CORDDIR && cd $CORDDIR
85 repo init -u https://gerrit.opencord.org/manifest -b $REPO_BRANCH
86 repo sync
87
88 # download gerrit patches using repo
89 if [[ ! -z ${GERRIT_PATCHES[@]-} ]]
90 then
91 for gerrit_patch in "${GERRIT_PATCHES[@]-}"
92 do
93 echo "Checking out gerrit changeset: '$gerrit_patch'"
94 repo download ${gerrit_patch/:/ }
95 done
96 fi
97 fi
98}
99
100function bootstrap_vagrant() {
101
102 if [ ! -x "/usr/bin/vagrant" ]
103 then
104 echo "Installing vagrant and associated tools..."
105 VAGRANT_SHA256SUM="faff6befacc7eed3978b4b71f0dbb9c135c01d8a4d13236bda2f9ed53482d2c4" # version 1.9.3
106 curl -o /tmp/vagrant.deb https://releases.hashicorp.com/vagrant/1.9.3/vagrant_1.9.3_x86_64.deb
107 echo "$VAGRANT_SHA256SUM /tmp/vagrant.deb" | sha256sum -c -
108 sudo dpkg -i /tmp/vagrant.deb
Andy Bavier40a36632017-09-07 08:25:20 -0700109 sudo apt-get -y install qemu-kvm libvirt-bin libvirt-dev nfs-kernel-server
Zack Williamsce63eb02017-02-28 10:46:22 -0700110 sudo adduser $USER libvirtd
111
112 run_stage cloudlab_setup
113
114 echo "Installing vagrant plugins..."
Andy Bavier40a36632017-09-07 08:25:20 -0700115 vagrant plugin list | grep vagrant-libvirt || vagrant plugin install vagrant-libvirt --plugin-version 0.0.35
Zack Williamsce63eb02017-02-28 10:46:22 -0700116 vagrant plugin list | grep vagrant-mutate || vagrant plugin install vagrant-mutate
Gopinath Taget40d57b32017-08-16 17:05:09 -0700117 vagrant plugin list | grep vagrant-hosts || vagrant plugin install vagrant-hosts
Zack Williamsce63eb02017-02-28 10:46:22 -0700118
119 add_box ubuntu/trusty64
120 fi
121}
122
123function add_box() {
124 vagrant box list | grep $1 | grep virtualbox || vagrant box add $1
125 vagrant box list | grep $1 | grep libvirt || vagrant mutate $1 libvirt --input-provider virtualbox
126}
127
128function cloudlab_setup() {
129
130 # Don't do anything if not a CloudLab node
131 [ ! -d /usr/local/etc/emulab ] && return
132
133 # The watchdog will sometimes reset groups, turn it off
134 if [ -e /usr/local/etc/emulab/watchdog ]
135 then
136 sudo /usr/bin/perl -w /usr/local/etc/emulab/watchdog stop
137 sudo mv /usr/local/etc/emulab/watchdog /usr/local/etc/emulab/watchdog-disabled
138 fi
139
140 # Mount extra space, if haven't already
141 if [ ! -d /mnt/extra ]
142 then
143 sudo mkdir -p /mnt/extra
144
145 # for NVME SSD on Utah Cloudlab, not supported by mkextrafs
146 if $(df | grep -q nvme0n1p1) && [ -e /usr/testbed/bin/mkextrafs ]
147 then
148 # set partition type of 4th partition to Linux, ignore errors
149 echo -e "t\n4\n82\np\nw\nq" | sudo fdisk /dev/nvme0n1 || true
150
151 sudo mkfs.ext4 /dev/nvme0n1p4
152 echo "/dev/nvme0n1p4 /mnt/extra/ ext4 defaults 0 0" | sudo tee -a /etc/fstab
153 sudo mount /mnt/extra
154 mount | grep nvme0n1p4 || (echo "ERROR: NVME mkfs/mount failed, exiting!" && exit 1)
155
156 elif [ -e /usr/testbed/bin/mkextrafs ] # if on Clemson/Wisconsin Cloudlab
157 then
158 # Sometimes this command fails on the first try
159 sudo /usr/testbed/bin/mkextrafs -r /dev/sdb -qf "/mnt/extra/" || sudo /usr/testbed/bin/mkextrafs -r /dev/sdb -qf "/mnt/extra/"
160
161 # Check that the mount succeeded (sometimes mkextrafs succeeds but device not mounted)
162 mount | grep sdb || (echo "ERROR: mkextrafs failed, exiting!" && exit 1)
163 fi
164 fi
165
166 # replace /var/lib/libvirt/images with a symlink
167 [ -d /var/lib/libvirt/images/ ] && [ ! -h /var/lib/libvirt/images ] && sudo rmdir /var/lib/libvirt/images
168 sudo mkdir -p /mnt/extra/libvirt_images
169
170 if [ ! -e /var/lib/libvirt/images ]
171 then
172 sudo ln -s /mnt/extra/libvirt_images /var/lib/libvirt/images
173 fi
174}
175
176function bootstrap_docker() {
177
178 if [ ! -x "/usr/bin/docker" ]
179 then
180 echo "Installing Devel Tools (docker)..."
181 cd ${CORDDIR}/build/platform-install
182 ansible-playbook -i inventory/localhost devel-tools-playbook.yml
183 fi
184}
185
186# Parse options
187GERRIT_PATCHES=()
188MAKE_TARGETS=()
189GROUP_LIST=()
190DOCKER=0
191VAGRANT=0
192
193while getopts "dhp:t:v" opt; do
194 case ${opt} in
195 d ) DOCKER=1
196 GROUP_LIST+=("docker")
197 ;;
198 h ) echo "Usage for $0:"
199 echo " -d Install Docker for local scenario."
200 echo " -h Display this help message."
201 echo " -p <project:change/revision> Download a patch from gerrit. Can be repeated."
202 echo " -t <target> Run '$MAKECMD <target>' in cord/build/. Can be repeated."
203 echo " -v Install Vagrant for mock/virtual/physical scenarios."
204 exit 0
205 ;;
206 p ) GERRIT_PATCHES+=("$OPTARG")
207 ;;
208 t ) MAKE_TARGETS+=("$OPTARG")
209 ;;
210 v ) VAGRANT=1
211 GROUP_LIST+=("libvirtd")
212 ;;
213 \? ) echo "Invalid option: -$OPTARG"
214 exit 1
215 ;;
216 esac
217done
218
219# Start process
220run_stage bootstrap_ansible
221run_stage bootstrap_repo
222
223if [[ $VAGRANT -ne 0 ]]
224then
225 run_stage bootstrap_vagrant
226fi
227
228if [[ $DOCKER -ne 0 ]]
229then
230 run_stage bootstrap_docker
231fi
232
233# Check for group membership when needed
234if [[ ! -z ${GROUP_LIST[@]-} ]]
235then
236 HAS_NEEDED_GROUPS=0
237 for group_item in "${GROUP_LIST[@]-}"; do
238 if ! $(groups | grep -q "$group_item")
239 then
240 echo "You are not in the group: "$group_item", please logout/login."
241 HAS_NEEDED_GROUPS=1
242 fi
243 done
244
245 if [[ $HAS_NEEDED_GROUPS -ne 0 ]];
246 then
247 exit 1
248 fi
249fi
250
251# run make targets, if specified
252if [[ ! -z ${MAKE_TARGETS[@]-} ]]
253then
254 for make_target in "${MAKE_TARGETS[@]-}"; do
255 makelog=${HOME}/${START_T}_make_`echo $make_target | sed 's/[^[:alnum:]-]/_/g'`
256 echo "Logging to: $makelog"
257 echo "Running '$MAKECMD $make_target'" | tee -a $makelog
258 cd ${CORDDIR}/build/
259 $MAKECMD $make_target 2>&1 | tee -a $makelog
260 done
261fi
262
263exit 0