Andy Bavier | c4a4051 | 2017-08-10 07:06:25 -0700 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | # |
Matteo Scandolo | 60b640f | 2017-08-08 13:05:22 -0700 | [diff] [blame] | 3 | # 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 Williams | ce63eb0 | 2017-02-28 10:46:22 -0700 | [diff] [blame] | 17 | # cord-bootstrap.sh |
| 18 | # Bootstraps a dev system for CORD, downloads source |
| 19 | |
Zack Williams | 32295d5 | 2017-08-28 22:49:37 -0700 | [diff] [blame] | 20 | set -e -u -o pipefail |
Zack Williams | ce63eb0 | 2017-02-28 10:46:22 -0700 | [diff] [blame] | 21 | |
| 22 | # start time, used to name logfiles |
| 23 | START_T=$(date -u "+%Y%m%d%H%M%SZ") |
| 24 | |
| 25 | # Location of 'cord' directory checked out on the local system |
| 26 | CORDDIR="${CORDDIR:-${HOME}/cord}" |
| 27 | |
| 28 | # Commands |
| 29 | MAKECMD="${MAKECMD:-make -j4}" |
| 30 | |
| 31 | # CORD versioning |
| 32 | REPO_BRANCH="${REPO_BRANCH:-master}" |
| 33 | |
Zack Williams | 75a1df3 | 2017-11-14 14:48:45 -0700 | [diff] [blame] | 34 | # Tool/OS versioning |
Zack Williams | 75a1df3 | 2017-11-14 14:48:45 -0700 | [diff] [blame] | 35 | HOST_RELEASE=`lsb_release -c -s` |
| 36 | |
| 37 | if [ ${HOST_RELEASE} = "trusty" ]; then |
| 38 | VAGRANT_LIBVIRT_VERSION="0.0.35" |
| 39 | else |
| 40 | VAGRANT_LIBVIRT_VERSION="0.0.40" |
| 41 | fi |
| 42 | |
Zack Williams | ce63eb0 | 2017-02-28 10:46:22 -0700 | [diff] [blame] | 43 | # Functions |
| 44 | function run_stage { |
| 45 | echo "==> "$1": Starting" |
| 46 | $1 |
| 47 | echo "==> "$1": Complete" |
| 48 | } |
| 49 | |
| 50 | function bootstrap_ansible() { |
| 51 | |
| 52 | if [ ! -x "/usr/bin/ansible" ] |
| 53 | then |
| 54 | echo "Installing Ansible..." |
| 55 | sudo apt-get update |
| 56 | sudo apt-get -y install apt-transport-https build-essential curl git python-dev \ |
| 57 | python-netaddr python-pip software-properties-common sshpass |
| 58 | sudo apt-add-repository -y ppa:ansible/ansible # latest supported version |
| 59 | sudo apt-get update |
| 60 | sudo apt-get install -y ansible |
| 61 | sudo pip install gitpython graphviz |
| 62 | fi |
| 63 | } |
| 64 | |
| 65 | function bootstrap_repo() { |
| 66 | |
| 67 | if [ ! -x "/usr/local/bin/repo" ] |
| 68 | then |
| 69 | echo "Installing repo..." |
| 70 | # v1.23, per https://source.android.com/source/downloading |
Luca Prete | b02161b | 2017-11-30 14:31:21 -0800 | [diff] [blame] | 71 | REPO_SHA256SUM="394d93ac7261d59db58afa49bb5f88386fea8518792491ee3db8baab49c3ecda" |
| 72 | curl -o /tmp/repo 'https://gerrit.opencord.org/gitweb?p=repo.git;a=blob_plain;f=repo;hb=refs/heads/stable' |
Zack Williams | ce63eb0 | 2017-02-28 10:46:22 -0700 | [diff] [blame] | 73 | echo "$REPO_SHA256SUM /tmp/repo" | sha256sum -c - |
| 74 | sudo mv /tmp/repo /usr/local/bin/repo |
| 75 | sudo chmod a+x /usr/local/bin/repo |
| 76 | fi |
| 77 | |
Zack Williams | 6924bed | 2017-09-26 18:31:41 -0700 | [diff] [blame] | 78 | if [ ! -d "$CORDDIR/build" ] |
Zack Williams | ce63eb0 | 2017-02-28 10:46:22 -0700 | [diff] [blame] | 79 | then |
| 80 | # make sure we can find gerrit.opencord.org as DNS failures will fail the build |
| 81 | dig +short gerrit.opencord.org || (echo "ERROR: gerrit.opencord.org can't be looked up in DNS" && exit 1) |
| 82 | |
| 83 | echo "Downloading CORD/XOS, branch:'${REPO_BRANCH}'..." |
| 84 | |
| 85 | if [ ! -e "${HOME}/.gitconfig" ] |
| 86 | then |
| 87 | echo "No ${HOME}/.gitconfig, setting testing defaults" |
| 88 | git config --global user.name 'Test User' |
| 89 | git config --global user.email 'test@null.com' |
| 90 | git config --global color.ui false |
| 91 | fi |
| 92 | |
Zack Williams | 6924bed | 2017-09-26 18:31:41 -0700 | [diff] [blame] | 93 | mkdir -p $CORDDIR && cd $CORDDIR |
Zack Williams | ce63eb0 | 2017-02-28 10:46:22 -0700 | [diff] [blame] | 94 | repo init -u https://gerrit.opencord.org/manifest -b $REPO_BRANCH |
| 95 | repo sync |
| 96 | |
| 97 | # download gerrit patches using repo |
| 98 | if [[ ! -z ${GERRIT_PATCHES[@]-} ]] |
| 99 | then |
| 100 | for gerrit_patch in "${GERRIT_PATCHES[@]-}" |
| 101 | do |
| 102 | echo "Checking out gerrit changeset: '$gerrit_patch'" |
| 103 | repo download ${gerrit_patch/:/ } |
| 104 | done |
| 105 | fi |
| 106 | fi |
| 107 | } |
| 108 | |
| 109 | function bootstrap_vagrant() { |
| 110 | |
| 111 | if [ ! -x "/usr/bin/vagrant" ] |
| 112 | then |
| 113 | echo "Installing vagrant and associated tools..." |
Zack Williams | 6924bed | 2017-09-26 18:31:41 -0700 | [diff] [blame] | 114 | sudo apt-get -y install qemu-kvm libvirt-bin libvirt-dev nfs-kernel-server |
| 115 | sudo adduser $USER libvirtd |
| 116 | |
Zack Williams | 75a1df3 | 2017-11-14 14:48:45 -0700 | [diff] [blame] | 117 | VAGRANT_SHA256SUM="2f9498a83b3d650fcfcfe0ec7971070fcd3803fad6470cf7da871caf2564d84f" # version 2.0.1 |
| 118 | curl -o /tmp/vagrant.deb https://releases.hashicorp.com/vagrant/2.0.1/vagrant_2.0.1_x86_64.deb |
Zack Williams | ce63eb0 | 2017-02-28 10:46:22 -0700 | [diff] [blame] | 119 | echo "$VAGRANT_SHA256SUM /tmp/vagrant.deb" | sha256sum -c - |
| 120 | sudo dpkg -i /tmp/vagrant.deb |
Zack Williams | 6924bed | 2017-09-26 18:31:41 -0700 | [diff] [blame] | 121 | fi |
Zack Williams | ce63eb0 | 2017-02-28 10:46:22 -0700 | [diff] [blame] | 122 | |
Zack Williams | 6924bed | 2017-09-26 18:31:41 -0700 | [diff] [blame] | 123 | run_stage cloudlab_setup |
Zack Williams | ce63eb0 | 2017-02-28 10:46:22 -0700 | [diff] [blame] | 124 | |
Zack Williams | 6924bed | 2017-09-26 18:31:41 -0700 | [diff] [blame] | 125 | echo "Installing vagrant plugins if needed..." |
Zack Williams | 75a1df3 | 2017-11-14 14:48:45 -0700 | [diff] [blame] | 126 | vagrant plugin list | grep -q vagrant-libvirt || vagrant plugin install vagrant-libvirt --plugin-version ${VAGRANT_LIBVIRT_VERSION} |
Zack Williams | 6924bed | 2017-09-26 18:31:41 -0700 | [diff] [blame] | 127 | vagrant plugin list | grep -q vagrant-hosts || vagrant plugin install vagrant-hosts |
Zack Williams | 82f24ca | 2017-12-30 13:53:46 -0700 | [diff] [blame] | 128 | vagrant plugin list | grep -q vagrant-mutate || vagrant plugin install vagrant-mutate |
Zack Williams | ce63eb0 | 2017-02-28 10:46:22 -0700 | [diff] [blame] | 129 | |
Zack Williams | 3aa32f0 | 2017-12-13 19:58:19 -0700 | [diff] [blame] | 130 | echo "Obtaining libvirt image of Ubuntu" |
| 131 | |
| 132 | if [[ $XENIAL -eq 1 ]]; then |
Zack Williams | 82f24ca | 2017-12-30 13:53:46 -0700 | [diff] [blame] | 133 | UBUNTU_VERSION=${UBUNTU_VERSION:-bento/ubuntu-16.04} |
Zack Williams | 3aa32f0 | 2017-12-13 19:58:19 -0700 | [diff] [blame] | 134 | else |
Zack Williams | 82f24ca | 2017-12-30 13:53:46 -0700 | [diff] [blame] | 135 | UBUNTU_VERSION=${UBUNTU_VERSION:-ubuntu/trusty64} |
Zack Williams | ce63eb0 | 2017-02-28 10:46:22 -0700 | [diff] [blame] | 136 | fi |
Zack Williams | ce63eb0 | 2017-02-28 10:46:22 -0700 | [diff] [blame] | 137 | |
Zack Williams | 82f24ca | 2017-12-30 13:53:46 -0700 | [diff] [blame] | 138 | vagrant box list | grep ${UBUNTU_VERSION} | grep virtualbox || vagrant box add --provider virtualbox ${UBUNTU_VERSION} |
Zack Williams | 3aa32f0 | 2017-12-13 19:58:19 -0700 | [diff] [blame] | 139 | vagrant box list | grep ${UBUNTU_VERSION} | grep libvirt || vagrant mutate ${UBUNTU_VERSION} libvirt --input-provider virtualbox |
| 140 | } |
| 141 | |
Zack Williams | ce63eb0 | 2017-02-28 10:46:22 -0700 | [diff] [blame] | 142 | function cloudlab_setup() { |
| 143 | |
| 144 | # Don't do anything if not a CloudLab node |
| 145 | [ ! -d /usr/local/etc/emulab ] && return |
| 146 | |
| 147 | # The watchdog will sometimes reset groups, turn it off |
| 148 | if [ -e /usr/local/etc/emulab/watchdog ] |
| 149 | then |
| 150 | sudo /usr/bin/perl -w /usr/local/etc/emulab/watchdog stop |
| 151 | sudo mv /usr/local/etc/emulab/watchdog /usr/local/etc/emulab/watchdog-disabled |
| 152 | fi |
| 153 | |
| 154 | # Mount extra space, if haven't already |
| 155 | if [ ! -d /mnt/extra ] |
| 156 | then |
| 157 | sudo mkdir -p /mnt/extra |
| 158 | |
| 159 | # for NVME SSD on Utah Cloudlab, not supported by mkextrafs |
| 160 | if $(df | grep -q nvme0n1p1) && [ -e /usr/testbed/bin/mkextrafs ] |
| 161 | then |
| 162 | # set partition type of 4th partition to Linux, ignore errors |
| 163 | echo -e "t\n4\n82\np\nw\nq" | sudo fdisk /dev/nvme0n1 || true |
| 164 | |
| 165 | sudo mkfs.ext4 /dev/nvme0n1p4 |
| 166 | echo "/dev/nvme0n1p4 /mnt/extra/ ext4 defaults 0 0" | sudo tee -a /etc/fstab |
| 167 | sudo mount /mnt/extra |
| 168 | mount | grep nvme0n1p4 || (echo "ERROR: NVME mkfs/mount failed, exiting!" && exit 1) |
| 169 | |
| 170 | elif [ -e /usr/testbed/bin/mkextrafs ] # if on Clemson/Wisconsin Cloudlab |
| 171 | then |
| 172 | # Sometimes this command fails on the first try |
| 173 | sudo /usr/testbed/bin/mkextrafs -r /dev/sdb -qf "/mnt/extra/" || sudo /usr/testbed/bin/mkextrafs -r /dev/sdb -qf "/mnt/extra/" |
| 174 | |
| 175 | # Check that the mount succeeded (sometimes mkextrafs succeeds but device not mounted) |
| 176 | mount | grep sdb || (echo "ERROR: mkextrafs failed, exiting!" && exit 1) |
| 177 | fi |
| 178 | fi |
| 179 | |
| 180 | # replace /var/lib/libvirt/images with a symlink |
| 181 | [ -d /var/lib/libvirt/images/ ] && [ ! -h /var/lib/libvirt/images ] && sudo rmdir /var/lib/libvirt/images |
| 182 | sudo mkdir -p /mnt/extra/libvirt_images |
| 183 | |
| 184 | if [ ! -e /var/lib/libvirt/images ] |
| 185 | then |
| 186 | sudo ln -s /mnt/extra/libvirt_images /var/lib/libvirt/images |
| 187 | fi |
| 188 | } |
| 189 | |
| 190 | function bootstrap_docker() { |
| 191 | |
| 192 | if [ ! -x "/usr/bin/docker" ] |
| 193 | then |
| 194 | echo "Installing Devel Tools (docker)..." |
| 195 | cd ${CORDDIR}/build/platform-install |
| 196 | ansible-playbook -i inventory/localhost devel-tools-playbook.yml |
| 197 | fi |
| 198 | } |
| 199 | |
| 200 | # Parse options |
| 201 | GERRIT_PATCHES=() |
| 202 | MAKE_TARGETS=() |
| 203 | GROUP_LIST=() |
| 204 | DOCKER=0 |
| 205 | VAGRANT=0 |
Zack Williams | 3aa32f0 | 2017-12-13 19:58:19 -0700 | [diff] [blame] | 206 | XENIAL=0 |
Zack Williams | ce63eb0 | 2017-02-28 10:46:22 -0700 | [diff] [blame] | 207 | |
Zack Williams | 3aa32f0 | 2017-12-13 19:58:19 -0700 | [diff] [blame] | 208 | while getopts "dhp:t:vx" opt; do |
Zack Williams | ce63eb0 | 2017-02-28 10:46:22 -0700 | [diff] [blame] | 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." |
Zack Williams | 3aa32f0 | 2017-12-13 19:58:19 -0700 | [diff] [blame] | 219 | echo " -x Use Xenial (16.04) in Vagrant VM's." |
Zack Williams | ce63eb0 | 2017-02-28 10:46:22 -0700 | [diff] [blame] | 220 | exit 0 |
| 221 | ;; |
| 222 | p ) GERRIT_PATCHES+=("$OPTARG") |
| 223 | ;; |
| 224 | t ) MAKE_TARGETS+=("$OPTARG") |
| 225 | ;; |
| 226 | v ) VAGRANT=1 |
| 227 | GROUP_LIST+=("libvirtd") |
| 228 | ;; |
Zack Williams | 3aa32f0 | 2017-12-13 19:58:19 -0700 | [diff] [blame] | 229 | x ) XENIAL=1 |
| 230 | ;; |
Zack Williams | ce63eb0 | 2017-02-28 10:46:22 -0700 | [diff] [blame] | 231 | \? ) echo "Invalid option: -$OPTARG" |
| 232 | exit 1 |
| 233 | ;; |
| 234 | esac |
| 235 | done |
| 236 | |
| 237 | # Start process |
| 238 | run_stage bootstrap_ansible |
| 239 | run_stage bootstrap_repo |
| 240 | |
| 241 | if [[ $VAGRANT -ne 0 ]] |
| 242 | then |
| 243 | run_stage bootstrap_vagrant |
| 244 | fi |
| 245 | |
| 246 | if [[ $DOCKER -ne 0 ]] |
| 247 | then |
| 248 | run_stage bootstrap_docker |
| 249 | fi |
| 250 | |
| 251 | # Check for group membership when needed |
| 252 | if [[ ! -z ${GROUP_LIST[@]-} ]] |
| 253 | then |
| 254 | HAS_NEEDED_GROUPS=0 |
| 255 | for group_item in "${GROUP_LIST[@]-}"; do |
| 256 | if ! $(groups | grep -q "$group_item") |
| 257 | then |
| 258 | echo "You are not in the group: "$group_item", please logout/login." |
| 259 | HAS_NEEDED_GROUPS=1 |
| 260 | fi |
| 261 | done |
| 262 | |
| 263 | if [[ $HAS_NEEDED_GROUPS -ne 0 ]]; |
| 264 | then |
| 265 | exit 1 |
| 266 | fi |
| 267 | fi |
| 268 | |
| 269 | # run make targets, if specified |
| 270 | if [[ ! -z ${MAKE_TARGETS[@]-} ]] |
| 271 | then |
| 272 | for make_target in "${MAKE_TARGETS[@]-}"; do |
| 273 | makelog=${HOME}/${START_T}_make_`echo $make_target | sed 's/[^[:alnum:]-]/_/g'` |
| 274 | echo "Logging to: $makelog" |
| 275 | echo "Running '$MAKECMD $make_target'" | tee -a $makelog |
| 276 | cd ${CORDDIR}/build/ |
| 277 | $MAKECMD $make_target 2>&1 | tee -a $makelog |
| 278 | done |
| 279 | fi |
| 280 | |
| 281 | exit 0 |