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