blob: 4f848ad66272b08cb09574f8ef099bb4369550a4 [file] [log] [blame]
Zack Williamsce63eb02017-02-28 10:46:22 -07001#!/usr/bin/env bash
2# cord-bootstrap.sh
3# Bootstraps a dev system for CORD, downloads source
4
5set -e -u
6
7# start time, used to name logfiles
8START_T=$(date -u "+%Y%m%d%H%M%SZ")
9
10# Location of 'cord' directory checked out on the local system
11CORDDIR="${CORDDIR:-${HOME}/cord}"
12
13# Commands
14MAKECMD="${MAKECMD:-make -j4}"
15
16# CORD versioning
17REPO_BRANCH="${REPO_BRANCH:-master}"
18
19# Functions
20function run_stage {
21 echo "==> "$1": Starting"
22 $1
23 echo "==> "$1": Complete"
24}
25
26function bootstrap_ansible() {
27
28 if [ ! -x "/usr/bin/ansible" ]
29 then
30 echo "Installing Ansible..."
31 sudo apt-get update
32 sudo apt-get -y install apt-transport-https build-essential curl git python-dev \
33 python-netaddr python-pip software-properties-common sshpass
34 sudo apt-add-repository -y ppa:ansible/ansible # latest supported version
35 sudo apt-get update
36 sudo apt-get install -y ansible
37 sudo pip install gitpython graphviz
38 fi
39}
40
41function bootstrap_repo() {
42
43 if [ ! -x "/usr/local/bin/repo" ]
44 then
45 echo "Installing repo..."
46 # v1.23, per https://source.android.com/source/downloading
47 REPO_SHA256SUM="e147f0392686c40cfd7d5e6f332c6ee74c4eab4d24e2694b3b0a0c037bf51dc5"
48 curl -o /tmp/repo https://storage.googleapis.com/git-repo-downloads/repo
49 echo "$REPO_SHA256SUM /tmp/repo" | sha256sum -c -
50 sudo mv /tmp/repo /usr/local/bin/repo
51 sudo chmod a+x /usr/local/bin/repo
52 fi
53
54 if [ ! -d "$CORDDIR" ]
55 then
56 # make sure we can find gerrit.opencord.org as DNS failures will fail the build
57 dig +short gerrit.opencord.org || (echo "ERROR: gerrit.opencord.org can't be looked up in DNS" && exit 1)
58
59 echo "Downloading CORD/XOS, branch:'${REPO_BRANCH}'..."
60
61 if [ ! -e "${HOME}/.gitconfig" ]
62 then
63 echo "No ${HOME}/.gitconfig, setting testing defaults"
64 git config --global user.name 'Test User'
65 git config --global user.email 'test@null.com'
66 git config --global color.ui false
67 fi
68
69 mkdir $CORDDIR && cd $CORDDIR
70 repo init -u https://gerrit.opencord.org/manifest -b $REPO_BRANCH
71 repo sync
72
73 # download gerrit patches using repo
74 if [[ ! -z ${GERRIT_PATCHES[@]-} ]]
75 then
76 for gerrit_patch in "${GERRIT_PATCHES[@]-}"
77 do
78 echo "Checking out gerrit changeset: '$gerrit_patch'"
79 repo download ${gerrit_patch/:/ }
80 done
81 fi
82 fi
83}
84
85function bootstrap_vagrant() {
86
87 if [ ! -x "/usr/bin/vagrant" ]
88 then
89 echo "Installing vagrant and associated tools..."
90 VAGRANT_SHA256SUM="faff6befacc7eed3978b4b71f0dbb9c135c01d8a4d13236bda2f9ed53482d2c4" # version 1.9.3
91 curl -o /tmp/vagrant.deb https://releases.hashicorp.com/vagrant/1.9.3/vagrant_1.9.3_x86_64.deb
92 echo "$VAGRANT_SHA256SUM /tmp/vagrant.deb" | sha256sum -c -
93 sudo dpkg -i /tmp/vagrant.deb
94 sudo apt-get -y install qemu-kvm libvirt-bin libvirt-dev nfs-kernel-server ruby2.0
95 sudo adduser $USER libvirtd
96
97 run_stage cloudlab_setup
98
99 echo "Installing vagrant plugins..."
100 # FIXME: fix for vagrant-libvirt dependency issue that arose on 2017-04-28 - zdw
101 # vagrant plugin list | grep vagrant-libvirt || vagrant plugin install vagrant-libvirt --plugin-version 0.0.35
102 if ! vagrant plugin list | grep vagrant-libvirt
103 then
104 git clone -b remove_xmlrpc_dep https://github.com/zdw/vagrant-libvirt.git ${HOME}/vagrant-libvirt
105 cd ~/vagrant-libvirt
106 gem2.0 build vagrant-libvirt.gemspec
107 vagrant plugin install vagrant-libvirt-0.0.35.gem
108 cd ~
109 fi
110 vagrant plugin list | grep vagrant-mutate || vagrant plugin install vagrant-mutate
111
112 add_box ubuntu/trusty64
113 fi
114}
115
116function add_box() {
117 vagrant box list | grep $1 | grep virtualbox || vagrant box add $1
118 vagrant box list | grep $1 | grep libvirt || vagrant mutate $1 libvirt --input-provider virtualbox
119}
120
121function cloudlab_setup() {
122
123 # Don't do anything if not a CloudLab node
124 [ ! -d /usr/local/etc/emulab ] && return
125
126 # The watchdog will sometimes reset groups, turn it off
127 if [ -e /usr/local/etc/emulab/watchdog ]
128 then
129 sudo /usr/bin/perl -w /usr/local/etc/emulab/watchdog stop
130 sudo mv /usr/local/etc/emulab/watchdog /usr/local/etc/emulab/watchdog-disabled
131 fi
132
133 # Mount extra space, if haven't already
134 if [ ! -d /mnt/extra ]
135 then
136 sudo mkdir -p /mnt/extra
137
138 # for NVME SSD on Utah Cloudlab, not supported by mkextrafs
139 if $(df | grep -q nvme0n1p1) && [ -e /usr/testbed/bin/mkextrafs ]
140 then
141 # set partition type of 4th partition to Linux, ignore errors
142 echo -e "t\n4\n82\np\nw\nq" | sudo fdisk /dev/nvme0n1 || true
143
144 sudo mkfs.ext4 /dev/nvme0n1p4
145 echo "/dev/nvme0n1p4 /mnt/extra/ ext4 defaults 0 0" | sudo tee -a /etc/fstab
146 sudo mount /mnt/extra
147 mount | grep nvme0n1p4 || (echo "ERROR: NVME mkfs/mount failed, exiting!" && exit 1)
148
149 elif [ -e /usr/testbed/bin/mkextrafs ] # if on Clemson/Wisconsin Cloudlab
150 then
151 # Sometimes this command fails on the first try
152 sudo /usr/testbed/bin/mkextrafs -r /dev/sdb -qf "/mnt/extra/" || sudo /usr/testbed/bin/mkextrafs -r /dev/sdb -qf "/mnt/extra/"
153
154 # Check that the mount succeeded (sometimes mkextrafs succeeds but device not mounted)
155 mount | grep sdb || (echo "ERROR: mkextrafs failed, exiting!" && exit 1)
156 fi
157 fi
158
159 # replace /var/lib/libvirt/images with a symlink
160 [ -d /var/lib/libvirt/images/ ] && [ ! -h /var/lib/libvirt/images ] && sudo rmdir /var/lib/libvirt/images
161 sudo mkdir -p /mnt/extra/libvirt_images
162
163 if [ ! -e /var/lib/libvirt/images ]
164 then
165 sudo ln -s /mnt/extra/libvirt_images /var/lib/libvirt/images
166 fi
167}
168
169function bootstrap_docker() {
170
171 if [ ! -x "/usr/bin/docker" ]
172 then
173 echo "Installing Devel Tools (docker)..."
174 cd ${CORDDIR}/build/platform-install
175 ansible-playbook -i inventory/localhost devel-tools-playbook.yml
176 fi
177}
178
179# Parse options
180GERRIT_PATCHES=()
181MAKE_TARGETS=()
182GROUP_LIST=()
183DOCKER=0
184VAGRANT=0
185
186while getopts "dhp:t:v" opt; do
187 case ${opt} in
188 d ) DOCKER=1
189 GROUP_LIST+=("docker")
190 ;;
191 h ) echo "Usage for $0:"
192 echo " -d Install Docker for local scenario."
193 echo " -h Display this help message."
194 echo " -p <project:change/revision> Download a patch from gerrit. Can be repeated."
195 echo " -t <target> Run '$MAKECMD <target>' in cord/build/. Can be repeated."
196 echo " -v Install Vagrant for mock/virtual/physical scenarios."
197 exit 0
198 ;;
199 p ) GERRIT_PATCHES+=("$OPTARG")
200 ;;
201 t ) MAKE_TARGETS+=("$OPTARG")
202 ;;
203 v ) VAGRANT=1
204 GROUP_LIST+=("libvirtd")
205 ;;
206 \? ) echo "Invalid option: -$OPTARG"
207 exit 1
208 ;;
209 esac
210done
211
212# Start process
213run_stage bootstrap_ansible
214run_stage bootstrap_repo
215
216if [[ $VAGRANT -ne 0 ]]
217then
218 run_stage bootstrap_vagrant
219fi
220
221if [[ $DOCKER -ne 0 ]]
222then
223 run_stage bootstrap_docker
224fi
225
226# Check for group membership when needed
227if [[ ! -z ${GROUP_LIST[@]-} ]]
228then
229 HAS_NEEDED_GROUPS=0
230 for group_item in "${GROUP_LIST[@]-}"; do
231 if ! $(groups | grep -q "$group_item")
232 then
233 echo "You are not in the group: "$group_item", please logout/login."
234 HAS_NEEDED_GROUPS=1
235 fi
236 done
237
238 if [[ $HAS_NEEDED_GROUPS -ne 0 ]];
239 then
240 exit 1
241 fi
242fi
243
244# run make targets, if specified
245if [[ ! -z ${MAKE_TARGETS[@]-} ]]
246then
247 for make_target in "${MAKE_TARGETS[@]-}"; do
248 makelog=${HOME}/${START_T}_make_`echo $make_target | sed 's/[^[:alnum:]-]/_/g'`
249 echo "Logging to: $makelog"
250 echo "Running '$MAKECMD $make_target'" | tee -a $makelog
251 cd ${CORDDIR}/build/
252 $MAKECMD $make_target 2>&1 | tee -a $makelog
253 done
254fi
255
256exit 0