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