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