blob: 969ffc268e0dac6fb41734c1b00cc2cddd347c12 [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 Williamsa57ea8c2017-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
Wei Yu Chen28034392017-10-03 15:55:45 +080032REPO_BRANCH="${REPO_BRANCH:-cord-4.1}"
Zack Williamsce63eb02017-02-28 10:46:22 -070033
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
Luca Preteaa5b23b2017-11-30 14:31:21 -080062 REPO_SHA256SUM="394d93ac7261d59db58afa49bb5f88386fea8518792491ee3db8baab49c3ecda"
63 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 -070064 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
Zack Williams881ece82017-09-26 18:31:41 -070069 if [ ! -d "$CORDDIR/build" ]
Zack Williamsce63eb02017-02-28 10:46:22 -070070 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
Zack Williams881ece82017-09-26 18:31:41 -070084 mkdir -p $CORDDIR && cd $CORDDIR
Zack Williamsce63eb02017-02-28 10:46:22 -070085 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..."
Zack Williams881ece82017-09-26 18:31:41 -0700105 sudo apt-get -y install qemu-kvm libvirt-bin libvirt-dev nfs-kernel-server
106 sudo adduser $USER libvirtd
107
Zack Williamsce63eb02017-02-28 10:46:22 -0700108 VAGRANT_SHA256SUM="faff6befacc7eed3978b4b71f0dbb9c135c01d8a4d13236bda2f9ed53482d2c4" # version 1.9.3
109 curl -o /tmp/vagrant.deb https://releases.hashicorp.com/vagrant/1.9.3/vagrant_1.9.3_x86_64.deb
110 echo "$VAGRANT_SHA256SUM /tmp/vagrant.deb" | sha256sum -c -
111 sudo dpkg -i /tmp/vagrant.deb
Zack Williams881ece82017-09-26 18:31:41 -0700112 fi
Zack Williamsce63eb02017-02-28 10:46:22 -0700113
Zack Williams881ece82017-09-26 18:31:41 -0700114 run_stage cloudlab_setup
Zack Williamsce63eb02017-02-28 10:46:22 -0700115
Zack Williams881ece82017-09-26 18:31:41 -0700116 echo "Installing vagrant plugins if needed..."
117 vagrant plugin list | grep -q vagrant-libvirt || vagrant plugin install vagrant-libvirt --plugin-version 0.0.35
118 vagrant plugin list | grep -q vagrant-mutate || vagrant plugin install vagrant-mutate
119 vagrant plugin list | grep -q vagrant-hosts || vagrant plugin install vagrant-hosts
Zack Williamsce63eb02017-02-28 10:46:22 -0700120
Zack Williams881ece82017-09-26 18:31:41 -0700121 if ! vagrant box list | grep -q ubuntu/trusty64.*libvirt
122 then
Zack Williamsce63eb02017-02-28 10:46:22 -0700123 add_box ubuntu/trusty64
124 fi
125}
126
127function add_box() {
128 vagrant box list | grep $1 | grep virtualbox || vagrant box add $1
129 vagrant box list | grep $1 | grep libvirt || vagrant mutate $1 libvirt --input-provider virtualbox
130}
131
132function cloudlab_setup() {
133
134 # Don't do anything if not a CloudLab node
135 [ ! -d /usr/local/etc/emulab ] && return
136
137 # The watchdog will sometimes reset groups, turn it off
138 if [ -e /usr/local/etc/emulab/watchdog ]
139 then
140 sudo /usr/bin/perl -w /usr/local/etc/emulab/watchdog stop
141 sudo mv /usr/local/etc/emulab/watchdog /usr/local/etc/emulab/watchdog-disabled
142 fi
143
144 # Mount extra space, if haven't already
145 if [ ! -d /mnt/extra ]
146 then
147 sudo mkdir -p /mnt/extra
148
149 # for NVME SSD on Utah Cloudlab, not supported by mkextrafs
150 if $(df | grep -q nvme0n1p1) && [ -e /usr/testbed/bin/mkextrafs ]
151 then
152 # set partition type of 4th partition to Linux, ignore errors
153 echo -e "t\n4\n82\np\nw\nq" | sudo fdisk /dev/nvme0n1 || true
154
155 sudo mkfs.ext4 /dev/nvme0n1p4
156 echo "/dev/nvme0n1p4 /mnt/extra/ ext4 defaults 0 0" | sudo tee -a /etc/fstab
157 sudo mount /mnt/extra
158 mount | grep nvme0n1p4 || (echo "ERROR: NVME mkfs/mount failed, exiting!" && exit 1)
159
160 elif [ -e /usr/testbed/bin/mkextrafs ] # if on Clemson/Wisconsin Cloudlab
161 then
162 # Sometimes this command fails on the first try
163 sudo /usr/testbed/bin/mkextrafs -r /dev/sdb -qf "/mnt/extra/" || sudo /usr/testbed/bin/mkextrafs -r /dev/sdb -qf "/mnt/extra/"
164
165 # Check that the mount succeeded (sometimes mkextrafs succeeds but device not mounted)
166 mount | grep sdb || (echo "ERROR: mkextrafs failed, exiting!" && exit 1)
167 fi
168 fi
169
170 # replace /var/lib/libvirt/images with a symlink
171 [ -d /var/lib/libvirt/images/ ] && [ ! -h /var/lib/libvirt/images ] && sudo rmdir /var/lib/libvirt/images
172 sudo mkdir -p /mnt/extra/libvirt_images
173
174 if [ ! -e /var/lib/libvirt/images ]
175 then
176 sudo ln -s /mnt/extra/libvirt_images /var/lib/libvirt/images
177 fi
178}
179
180function bootstrap_docker() {
181
182 if [ ! -x "/usr/bin/docker" ]
183 then
184 echo "Installing Devel Tools (docker)..."
185 cd ${CORDDIR}/build/platform-install
186 ansible-playbook -i inventory/localhost devel-tools-playbook.yml
187 fi
188}
189
190# Parse options
191GERRIT_PATCHES=()
192MAKE_TARGETS=()
193GROUP_LIST=()
194DOCKER=0
195VAGRANT=0
196
197while getopts "dhp:t:v" opt; do
198 case ${opt} in
199 d ) DOCKER=1
200 GROUP_LIST+=("docker")
201 ;;
202 h ) echo "Usage for $0:"
203 echo " -d Install Docker for local scenario."
204 echo " -h Display this help message."
205 echo " -p <project:change/revision> Download a patch from gerrit. Can be repeated."
206 echo " -t <target> Run '$MAKECMD <target>' in cord/build/. Can be repeated."
207 echo " -v Install Vagrant for mock/virtual/physical scenarios."
208 exit 0
209 ;;
210 p ) GERRIT_PATCHES+=("$OPTARG")
211 ;;
212 t ) MAKE_TARGETS+=("$OPTARG")
213 ;;
214 v ) VAGRANT=1
215 GROUP_LIST+=("libvirtd")
216 ;;
217 \? ) echo "Invalid option: -$OPTARG"
218 exit 1
219 ;;
220 esac
221done
222
223# Start process
224run_stage bootstrap_ansible
225run_stage bootstrap_repo
226
227if [[ $VAGRANT -ne 0 ]]
228then
229 run_stage bootstrap_vagrant
230fi
231
232if [[ $DOCKER -ne 0 ]]
233then
234 run_stage bootstrap_docker
235fi
236
237# Check for group membership when needed
238if [[ ! -z ${GROUP_LIST[@]-} ]]
239then
240 HAS_NEEDED_GROUPS=0
241 for group_item in "${GROUP_LIST[@]-}"; do
242 if ! $(groups | grep -q "$group_item")
243 then
244 echo "You are not in the group: "$group_item", please logout/login."
245 HAS_NEEDED_GROUPS=1
246 fi
247 done
248
249 if [[ $HAS_NEEDED_GROUPS -ne 0 ]];
250 then
251 exit 1
252 fi
253fi
254
255# run make targets, if specified
256if [[ ! -z ${MAKE_TARGETS[@]-} ]]
257then
258 for make_target in "${MAKE_TARGETS[@]-}"; do
259 makelog=${HOME}/${START_T}_make_`echo $make_target | sed 's/[^[:alnum:]-]/_/g'`
260 echo "Logging to: $makelog"
261 echo "Running '$MAKECMD $make_target'" | tee -a $makelog
262 cd ${CORDDIR}/build/
263 $MAKECMD $make_target 2>&1 | tee -a $makelog
264 done
265fi
266
267exit 0