blob: 34ac8855e1b27f78059773e7a30c2475c009824b [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 Williams82f24ca2017-12-30 13:53:46 -0700128 vagrant plugin list | grep -q vagrant-mutate || vagrant plugin install vagrant-mutate
Zack Williamsce63eb02017-02-28 10:46:22 -0700129
Zack Williams3aa32f02017-12-13 19:58:19 -0700130 echo "Obtaining libvirt image of Ubuntu"
131
132 if [[ $XENIAL -eq 1 ]]; then
Zack Williams82f24ca2017-12-30 13:53:46 -0700133 UBUNTU_VERSION=${UBUNTU_VERSION:-bento/ubuntu-16.04}
Zack Williams3aa32f02017-12-13 19:58:19 -0700134 else
Zack Williams82f24ca2017-12-30 13:53:46 -0700135 UBUNTU_VERSION=${UBUNTU_VERSION:-ubuntu/trusty64}
Zack Williamsce63eb02017-02-28 10:46:22 -0700136 fi
Zack Williamsce63eb02017-02-28 10:46:22 -0700137
Zack Williams82f24ca2017-12-30 13:53:46 -0700138 vagrant box list | grep ${UBUNTU_VERSION} | grep virtualbox || vagrant box add --provider virtualbox ${UBUNTU_VERSION}
Zack Williams3aa32f02017-12-13 19:58:19 -0700139 vagrant box list | grep ${UBUNTU_VERSION} | grep libvirt || vagrant mutate ${UBUNTU_VERSION} libvirt --input-provider virtualbox
140}
141
Zack Williamsce63eb02017-02-28 10:46:22 -0700142function cloudlab_setup() {
143
144 # Don't do anything if not a CloudLab node
145 [ ! -d /usr/local/etc/emulab ] && return
146
147 # The watchdog will sometimes reset groups, turn it off
148 if [ -e /usr/local/etc/emulab/watchdog ]
149 then
150 sudo /usr/bin/perl -w /usr/local/etc/emulab/watchdog stop
151 sudo mv /usr/local/etc/emulab/watchdog /usr/local/etc/emulab/watchdog-disabled
152 fi
153
154 # Mount extra space, if haven't already
155 if [ ! -d /mnt/extra ]
156 then
157 sudo mkdir -p /mnt/extra
158
159 # for NVME SSD on Utah Cloudlab, not supported by mkextrafs
160 if $(df | grep -q nvme0n1p1) && [ -e /usr/testbed/bin/mkextrafs ]
161 then
162 # set partition type of 4th partition to Linux, ignore errors
163 echo -e "t\n4\n82\np\nw\nq" | sudo fdisk /dev/nvme0n1 || true
164
165 sudo mkfs.ext4 /dev/nvme0n1p4
166 echo "/dev/nvme0n1p4 /mnt/extra/ ext4 defaults 0 0" | sudo tee -a /etc/fstab
167 sudo mount /mnt/extra
168 mount | grep nvme0n1p4 || (echo "ERROR: NVME mkfs/mount failed, exiting!" && exit 1)
169
170 elif [ -e /usr/testbed/bin/mkextrafs ] # if on Clemson/Wisconsin Cloudlab
171 then
172 # Sometimes this command fails on the first try
173 sudo /usr/testbed/bin/mkextrafs -r /dev/sdb -qf "/mnt/extra/" || sudo /usr/testbed/bin/mkextrafs -r /dev/sdb -qf "/mnt/extra/"
174
175 # Check that the mount succeeded (sometimes mkextrafs succeeds but device not mounted)
176 mount | grep sdb || (echo "ERROR: mkextrafs failed, exiting!" && exit 1)
177 fi
178 fi
179
180 # replace /var/lib/libvirt/images with a symlink
181 [ -d /var/lib/libvirt/images/ ] && [ ! -h /var/lib/libvirt/images ] && sudo rmdir /var/lib/libvirt/images
182 sudo mkdir -p /mnt/extra/libvirt_images
183
184 if [ ! -e /var/lib/libvirt/images ]
185 then
186 sudo ln -s /mnt/extra/libvirt_images /var/lib/libvirt/images
187 fi
188}
189
190function bootstrap_docker() {
191
192 if [ ! -x "/usr/bin/docker" ]
193 then
194 echo "Installing Devel Tools (docker)..."
195 cd ${CORDDIR}/build/platform-install
196 ansible-playbook -i inventory/localhost devel-tools-playbook.yml
197 fi
198}
199
200# Parse options
201GERRIT_PATCHES=()
202MAKE_TARGETS=()
203GROUP_LIST=()
204DOCKER=0
205VAGRANT=0
Zack Williams3aa32f02017-12-13 19:58:19 -0700206XENIAL=0
Zack Williamsce63eb02017-02-28 10:46:22 -0700207
Zack Williams3aa32f02017-12-13 19:58:19 -0700208while getopts "dhp:t:vx" opt; do
Zack Williamsce63eb02017-02-28 10:46:22 -0700209 case ${opt} in
210 d ) DOCKER=1
211 GROUP_LIST+=("docker")
212 ;;
213 h ) echo "Usage for $0:"
214 echo " -d Install Docker for local scenario."
215 echo " -h Display this help message."
216 echo " -p <project:change/revision> Download a patch from gerrit. Can be repeated."
217 echo " -t <target> Run '$MAKECMD <target>' in cord/build/. Can be repeated."
218 echo " -v Install Vagrant for mock/virtual/physical scenarios."
Zack Williams3aa32f02017-12-13 19:58:19 -0700219 echo " -x Use Xenial (16.04) in Vagrant VM's."
Zack Williamsce63eb02017-02-28 10:46:22 -0700220 exit 0
221 ;;
222 p ) GERRIT_PATCHES+=("$OPTARG")
223 ;;
224 t ) MAKE_TARGETS+=("$OPTARG")
225 ;;
226 v ) VAGRANT=1
227 GROUP_LIST+=("libvirtd")
228 ;;
Zack Williams3aa32f02017-12-13 19:58:19 -0700229 x ) XENIAL=1
230 ;;
Zack Williamsce63eb02017-02-28 10:46:22 -0700231 \? ) echo "Invalid option: -$OPTARG"
232 exit 1
233 ;;
234 esac
235done
236
237# Start process
238run_stage bootstrap_ansible
239run_stage bootstrap_repo
240
241if [[ $VAGRANT -ne 0 ]]
242then
243 run_stage bootstrap_vagrant
244fi
245
246if [[ $DOCKER -ne 0 ]]
247then
248 run_stage bootstrap_docker
249fi
250
251# Check for group membership when needed
252if [[ ! -z ${GROUP_LIST[@]-} ]]
253then
254 HAS_NEEDED_GROUPS=0
255 for group_item in "${GROUP_LIST[@]-}"; do
256 if ! $(groups | grep -q "$group_item")
257 then
258 echo "You are not in the group: "$group_item", please logout/login."
259 HAS_NEEDED_GROUPS=1
260 fi
261 done
262
263 if [[ $HAS_NEEDED_GROUPS -ne 0 ]];
264 then
265 exit 1
266 fi
267fi
268
269# run make targets, if specified
270if [[ ! -z ${MAKE_TARGETS[@]-} ]]
271then
272 for make_target in "${MAKE_TARGETS[@]-}"; do
273 makelog=${HOME}/${START_T}_make_`echo $make_target | sed 's/[^[:alnum:]-]/_/g'`
274 echo "Logging to: $makelog"
275 echo "Running '$MAKECMD $make_target'" | tee -a $makelog
276 cd ${CORDDIR}/build/
277 $MAKECMD $make_target 2>&1 | tee -a $makelog
278 done
279fi
280
281exit 0