blob: cbfcfc0e178562eac4e4b75d29fa439cd63f29e0 [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
Zack Williams75a1df32017-11-14 14:48:45 -070034# Tool/OS versioning
Zack Williams75a1df32017-11-14 14:48:45 -070035HOST_RELEASE=`lsb_release -c -s`
36
37if [ ${HOST_RELEASE} = "trusty" ]; then
38 VAGRANT_LIBVIRT_VERSION="0.0.35"
39else
40 VAGRANT_LIBVIRT_VERSION="0.0.40"
41fi
42
Zack Williamsce63eb02017-02-28 10:46:22 -070043# Functions
44function run_stage {
45 echo "==> "$1": Starting"
46 $1
47 echo "==> "$1": Complete"
48}
49
50function bootstrap_ansible() {
51
52 if [ ! -x "/usr/bin/ansible" ]
53 then
54 echo "Installing Ansible..."
55 sudo apt-get update
56 sudo apt-get -y install apt-transport-https build-essential curl git python-dev \
57 python-netaddr python-pip software-properties-common sshpass
58 sudo apt-add-repository -y ppa:ansible/ansible # latest supported version
59 sudo apt-get update
60 sudo apt-get install -y ansible
61 sudo pip install gitpython graphviz
62 fi
63}
64
65function bootstrap_repo() {
66
67 if [ ! -x "/usr/local/bin/repo" ]
68 then
69 echo "Installing repo..."
70 # v1.23, per https://source.android.com/source/downloading
Luca Preteb02161b2017-11-30 14:31:21 -080071 REPO_SHA256SUM="394d93ac7261d59db58afa49bb5f88386fea8518792491ee3db8baab49c3ecda"
72 curl -o /tmp/repo 'https://gerrit.opencord.org/gitweb?p=repo.git;a=blob_plain;f=repo;hb=refs/heads/stable'
Zack Williamsce63eb02017-02-28 10:46:22 -070073 echo "$REPO_SHA256SUM /tmp/repo" | sha256sum -c -
74 sudo mv /tmp/repo /usr/local/bin/repo
75 sudo chmod a+x /usr/local/bin/repo
76 fi
77
Zack Williams6924bed2017-09-26 18:31:41 -070078 if [ ! -d "$CORDDIR/build" ]
Zack Williamsce63eb02017-02-28 10:46:22 -070079 then
80 # make sure we can find gerrit.opencord.org as DNS failures will fail the build
81 dig +short gerrit.opencord.org || (echo "ERROR: gerrit.opencord.org can't be looked up in DNS" && exit 1)
82
83 echo "Downloading CORD/XOS, branch:'${REPO_BRANCH}'..."
84
85 if [ ! -e "${HOME}/.gitconfig" ]
86 then
87 echo "No ${HOME}/.gitconfig, setting testing defaults"
88 git config --global user.name 'Test User'
89 git config --global user.email 'test@null.com'
90 git config --global color.ui false
91 fi
92
Zack Williams6924bed2017-09-26 18:31:41 -070093 mkdir -p $CORDDIR && cd $CORDDIR
Zack Williamsce63eb02017-02-28 10:46:22 -070094 repo init -u https://gerrit.opencord.org/manifest -b $REPO_BRANCH
95 repo sync
96
97 # download gerrit patches using repo
98 if [[ ! -z ${GERRIT_PATCHES[@]-} ]]
99 then
100 for gerrit_patch in "${GERRIT_PATCHES[@]-}"
101 do
102 echo "Checking out gerrit changeset: '$gerrit_patch'"
103 repo download ${gerrit_patch/:/ }
104 done
105 fi
106 fi
107}
108
109function bootstrap_vagrant() {
110
111 if [ ! -x "/usr/bin/vagrant" ]
112 then
113 echo "Installing vagrant and associated tools..."
Zack Williams6924bed2017-09-26 18:31:41 -0700114 sudo apt-get -y install qemu-kvm libvirt-bin libvirt-dev nfs-kernel-server
115 sudo adduser $USER libvirtd
116
Zack Williams75a1df32017-11-14 14:48:45 -0700117 VAGRANT_SHA256SUM="2f9498a83b3d650fcfcfe0ec7971070fcd3803fad6470cf7da871caf2564d84f" # version 2.0.1
118 curl -o /tmp/vagrant.deb https://releases.hashicorp.com/vagrant/2.0.1/vagrant_2.0.1_x86_64.deb
Zack Williamsce63eb02017-02-28 10:46:22 -0700119 echo "$VAGRANT_SHA256SUM /tmp/vagrant.deb" | sha256sum -c -
120 sudo dpkg -i /tmp/vagrant.deb
Zack Williams6924bed2017-09-26 18:31:41 -0700121 fi
Zack Williamsce63eb02017-02-28 10:46:22 -0700122
Zack Williams6924bed2017-09-26 18:31:41 -0700123 run_stage cloudlab_setup
Zack Williamsce63eb02017-02-28 10:46:22 -0700124
Zack Williams6924bed2017-09-26 18:31:41 -0700125 echo "Installing vagrant plugins if needed..."
Zack Williams75a1df32017-11-14 14:48:45 -0700126 vagrant plugin list | grep -q vagrant-libvirt || vagrant plugin install vagrant-libvirt --plugin-version ${VAGRANT_LIBVIRT_VERSION}
Zack Williams6924bed2017-09-26 18:31:41 -0700127 vagrant plugin list | grep -q vagrant-hosts || vagrant plugin install vagrant-hosts
Zack Williamsce63eb02017-02-28 10:46:22 -0700128
Zack Williams3aa32f02017-12-13 19:58:19 -0700129 echo "Obtaining libvirt image of Ubuntu"
130
131 if [[ $XENIAL -eq 1 ]]; then
132 add_xenial
133 else
134 add_trusty
Zack Williamsce63eb02017-02-28 10:46:22 -0700135 fi
136}
137
Zack Williams3aa32f02017-12-13 19:58:19 -0700138function add_trusty() {
139 UBUNTU_VERSION=${UBUNTU_VERSION:-ubuntu/trusty64}
140 vagrant plugin list | grep -q vagrant-mutate || vagrant plugin install vagrant-mutate
141 vagrant box list | grep ${UBUNTU_VERSION} | grep virtualbox || vagrant box add ${UBUNTU_VERSION}
142 vagrant box list | grep ${UBUNTU_VERSION} | grep libvirt || vagrant mutate ${UBUNTU_VERSION} libvirt --input-provider virtualbox
143}
144
145function add_xenial() {
146 UBUNTU_VERSION=${UBUNTU_VERSION:-generic/ubuntu1604}
147 vagrant box list | grep ${UBUNTU_VERSION} | grep libvirt || vagrant box add --provider libvirt ${UBUNTU_VERSION}
Zack Williamsce63eb02017-02-28 10:46:22 -0700148}
149
150function cloudlab_setup() {
151
152 # Don't do anything if not a CloudLab node
153 [ ! -d /usr/local/etc/emulab ] && return
154
155 # The watchdog will sometimes reset groups, turn it off
156 if [ -e /usr/local/etc/emulab/watchdog ]
157 then
158 sudo /usr/bin/perl -w /usr/local/etc/emulab/watchdog stop
159 sudo mv /usr/local/etc/emulab/watchdog /usr/local/etc/emulab/watchdog-disabled
160 fi
161
162 # Mount extra space, if haven't already
163 if [ ! -d /mnt/extra ]
164 then
165 sudo mkdir -p /mnt/extra
166
167 # for NVME SSD on Utah Cloudlab, not supported by mkextrafs
168 if $(df | grep -q nvme0n1p1) && [ -e /usr/testbed/bin/mkextrafs ]
169 then
170 # set partition type of 4th partition to Linux, ignore errors
171 echo -e "t\n4\n82\np\nw\nq" | sudo fdisk /dev/nvme0n1 || true
172
173 sudo mkfs.ext4 /dev/nvme0n1p4
174 echo "/dev/nvme0n1p4 /mnt/extra/ ext4 defaults 0 0" | sudo tee -a /etc/fstab
175 sudo mount /mnt/extra
176 mount | grep nvme0n1p4 || (echo "ERROR: NVME mkfs/mount failed, exiting!" && exit 1)
177
178 elif [ -e /usr/testbed/bin/mkextrafs ] # if on Clemson/Wisconsin Cloudlab
179 then
180 # Sometimes this command fails on the first try
181 sudo /usr/testbed/bin/mkextrafs -r /dev/sdb -qf "/mnt/extra/" || sudo /usr/testbed/bin/mkextrafs -r /dev/sdb -qf "/mnt/extra/"
182
183 # Check that the mount succeeded (sometimes mkextrafs succeeds but device not mounted)
184 mount | grep sdb || (echo "ERROR: mkextrafs failed, exiting!" && exit 1)
185 fi
186 fi
187
188 # replace /var/lib/libvirt/images with a symlink
189 [ -d /var/lib/libvirt/images/ ] && [ ! -h /var/lib/libvirt/images ] && sudo rmdir /var/lib/libvirt/images
190 sudo mkdir -p /mnt/extra/libvirt_images
191
192 if [ ! -e /var/lib/libvirt/images ]
193 then
194 sudo ln -s /mnt/extra/libvirt_images /var/lib/libvirt/images
195 fi
196}
197
198function bootstrap_docker() {
199
200 if [ ! -x "/usr/bin/docker" ]
201 then
202 echo "Installing Devel Tools (docker)..."
203 cd ${CORDDIR}/build/platform-install
204 ansible-playbook -i inventory/localhost devel-tools-playbook.yml
205 fi
206}
207
208# Parse options
209GERRIT_PATCHES=()
210MAKE_TARGETS=()
211GROUP_LIST=()
212DOCKER=0
213VAGRANT=0
Zack Williams3aa32f02017-12-13 19:58:19 -0700214XENIAL=0
Zack Williamsce63eb02017-02-28 10:46:22 -0700215
Zack Williams3aa32f02017-12-13 19:58:19 -0700216while getopts "dhp:t:vx" opt; do
Zack Williamsce63eb02017-02-28 10:46:22 -0700217 case ${opt} in
218 d ) DOCKER=1
219 GROUP_LIST+=("docker")
220 ;;
221 h ) echo "Usage for $0:"
222 echo " -d Install Docker for local scenario."
223 echo " -h Display this help message."
224 echo " -p <project:change/revision> Download a patch from gerrit. Can be repeated."
225 echo " -t <target> Run '$MAKECMD <target>' in cord/build/. Can be repeated."
226 echo " -v Install Vagrant for mock/virtual/physical scenarios."
Zack Williams3aa32f02017-12-13 19:58:19 -0700227 echo " -x Use Xenial (16.04) in Vagrant VM's."
Zack Williamsce63eb02017-02-28 10:46:22 -0700228 exit 0
229 ;;
230 p ) GERRIT_PATCHES+=("$OPTARG")
231 ;;
232 t ) MAKE_TARGETS+=("$OPTARG")
233 ;;
234 v ) VAGRANT=1
235 GROUP_LIST+=("libvirtd")
236 ;;
Zack Williams3aa32f02017-12-13 19:58:19 -0700237 x ) XENIAL=1
238 ;;
Zack Williamsce63eb02017-02-28 10:46:22 -0700239 \? ) echo "Invalid option: -$OPTARG"
240 exit 1
241 ;;
242 esac
243done
244
245# Start process
246run_stage bootstrap_ansible
247run_stage bootstrap_repo
248
249if [[ $VAGRANT -ne 0 ]]
250then
251 run_stage bootstrap_vagrant
252fi
253
254if [[ $DOCKER -ne 0 ]]
255then
256 run_stage bootstrap_docker
257fi
258
259# Check for group membership when needed
260if [[ ! -z ${GROUP_LIST[@]-} ]]
261then
262 HAS_NEEDED_GROUPS=0
263 for group_item in "${GROUP_LIST[@]-}"; do
264 if ! $(groups | grep -q "$group_item")
265 then
266 echo "You are not in the group: "$group_item", please logout/login."
267 HAS_NEEDED_GROUPS=1
268 fi
269 done
270
271 if [[ $HAS_NEEDED_GROUPS -ne 0 ]];
272 then
273 exit 1
274 fi
275fi
276
277# run make targets, if specified
278if [[ ! -z ${MAKE_TARGETS[@]-} ]]
279then
280 for make_target in "${MAKE_TARGETS[@]-}"; do
281 makelog=${HOME}/${START_T}_make_`echo $make_target | sed 's/[^[:alnum:]-]/_/g'`
282 echo "Logging to: $makelog"
283 echo "Running '$MAKECMD $make_target'" | tee -a $makelog
284 cd ${CORDDIR}/build/
285 $MAKECMD $make_target 2>&1 | tee -a $makelog
286 done
287fi
288
289exit 0