Matteo Scandolo | 60b640f | 2017-08-08 13:05:22 -0700 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
Zack Williams | ce63eb0 | 2017-02-28 10:46:22 -0700 | [diff] [blame] | 17 | #!/usr/bin/env bash |
| 18 | # cord-bootstrap.sh |
| 19 | # Bootstraps a dev system for CORD, downloads source |
| 20 | |
| 21 | set -e -u |
| 22 | |
| 23 | # start time, used to name logfiles |
| 24 | START_T=$(date -u "+%Y%m%d%H%M%SZ") |
| 25 | |
| 26 | # Location of 'cord' directory checked out on the local system |
| 27 | CORDDIR="${CORDDIR:-${HOME}/cord}" |
| 28 | |
| 29 | # Commands |
| 30 | MAKECMD="${MAKECMD:-make -j4}" |
| 31 | |
| 32 | # CORD versioning |
| 33 | REPO_BRANCH="${REPO_BRANCH:-master}" |
| 34 | |
| 35 | # Functions |
| 36 | function run_stage { |
| 37 | echo "==> "$1": Starting" |
| 38 | $1 |
| 39 | echo "==> "$1": Complete" |
| 40 | } |
| 41 | |
| 42 | function bootstrap_ansible() { |
| 43 | |
| 44 | if [ ! -x "/usr/bin/ansible" ] |
| 45 | then |
| 46 | echo "Installing Ansible..." |
| 47 | sudo apt-get update |
| 48 | sudo apt-get -y install apt-transport-https build-essential curl git python-dev \ |
| 49 | python-netaddr python-pip software-properties-common sshpass |
| 50 | sudo apt-add-repository -y ppa:ansible/ansible # latest supported version |
| 51 | sudo apt-get update |
| 52 | sudo apt-get install -y ansible |
| 53 | sudo pip install gitpython graphviz |
| 54 | fi |
| 55 | } |
| 56 | |
| 57 | function bootstrap_repo() { |
| 58 | |
| 59 | if [ ! -x "/usr/local/bin/repo" ] |
| 60 | then |
| 61 | echo "Installing repo..." |
| 62 | # v1.23, per https://source.android.com/source/downloading |
| 63 | REPO_SHA256SUM="e147f0392686c40cfd7d5e6f332c6ee74c4eab4d24e2694b3b0a0c037bf51dc5" |
| 64 | curl -o /tmp/repo https://storage.googleapis.com/git-repo-downloads/repo |
| 65 | echo "$REPO_SHA256SUM /tmp/repo" | sha256sum -c - |
| 66 | sudo mv /tmp/repo /usr/local/bin/repo |
| 67 | sudo chmod a+x /usr/local/bin/repo |
| 68 | fi |
| 69 | |
| 70 | if [ ! -d "$CORDDIR" ] |
| 71 | then |
| 72 | # make sure we can find gerrit.opencord.org as DNS failures will fail the build |
| 73 | dig +short gerrit.opencord.org || (echo "ERROR: gerrit.opencord.org can't be looked up in DNS" && exit 1) |
| 74 | |
| 75 | echo "Downloading CORD/XOS, branch:'${REPO_BRANCH}'..." |
| 76 | |
| 77 | if [ ! -e "${HOME}/.gitconfig" ] |
| 78 | then |
| 79 | echo "No ${HOME}/.gitconfig, setting testing defaults" |
| 80 | git config --global user.name 'Test User' |
| 81 | git config --global user.email 'test@null.com' |
| 82 | git config --global color.ui false |
| 83 | fi |
| 84 | |
| 85 | mkdir $CORDDIR && cd $CORDDIR |
| 86 | repo init -u https://gerrit.opencord.org/manifest -b $REPO_BRANCH |
| 87 | repo sync |
| 88 | |
| 89 | # download gerrit patches using repo |
| 90 | if [[ ! -z ${GERRIT_PATCHES[@]-} ]] |
| 91 | then |
| 92 | for gerrit_patch in "${GERRIT_PATCHES[@]-}" |
| 93 | do |
| 94 | echo "Checking out gerrit changeset: '$gerrit_patch'" |
| 95 | repo download ${gerrit_patch/:/ } |
| 96 | done |
| 97 | fi |
| 98 | fi |
| 99 | } |
| 100 | |
| 101 | function bootstrap_vagrant() { |
| 102 | |
| 103 | if [ ! -x "/usr/bin/vagrant" ] |
| 104 | then |
| 105 | echo "Installing vagrant and associated tools..." |
| 106 | VAGRANT_SHA256SUM="faff6befacc7eed3978b4b71f0dbb9c135c01d8a4d13236bda2f9ed53482d2c4" # version 1.9.3 |
| 107 | curl -o /tmp/vagrant.deb https://releases.hashicorp.com/vagrant/1.9.3/vagrant_1.9.3_x86_64.deb |
| 108 | echo "$VAGRANT_SHA256SUM /tmp/vagrant.deb" | sha256sum -c - |
| 109 | sudo dpkg -i /tmp/vagrant.deb |
| 110 | sudo apt-get -y install qemu-kvm libvirt-bin libvirt-dev nfs-kernel-server ruby2.0 |
| 111 | sudo adduser $USER libvirtd |
| 112 | |
| 113 | run_stage cloudlab_setup |
| 114 | |
| 115 | echo "Installing vagrant plugins..." |
| 116 | # FIXME: fix for vagrant-libvirt dependency issue that arose on 2017-04-28 - zdw |
| 117 | # vagrant plugin list | grep vagrant-libvirt || vagrant plugin install vagrant-libvirt --plugin-version 0.0.35 |
| 118 | if ! vagrant plugin list | grep vagrant-libvirt |
| 119 | then |
| 120 | git clone -b remove_xmlrpc_dep https://github.com/zdw/vagrant-libvirt.git ${HOME}/vagrant-libvirt |
| 121 | cd ~/vagrant-libvirt |
| 122 | gem2.0 build vagrant-libvirt.gemspec |
| 123 | vagrant plugin install vagrant-libvirt-0.0.35.gem |
| 124 | cd ~ |
| 125 | fi |
| 126 | vagrant plugin list | grep vagrant-mutate || vagrant plugin install vagrant-mutate |
| 127 | |
| 128 | add_box ubuntu/trusty64 |
| 129 | fi |
| 130 | } |
| 131 | |
| 132 | function 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 | |
| 137 | function 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 | |
| 185 | function 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 |
| 196 | GERRIT_PATCHES=() |
| 197 | MAKE_TARGETS=() |
| 198 | GROUP_LIST=() |
| 199 | DOCKER=0 |
| 200 | VAGRANT=0 |
| 201 | |
| 202 | while 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 |
| 226 | done |
| 227 | |
| 228 | # Start process |
| 229 | run_stage bootstrap_ansible |
| 230 | run_stage bootstrap_repo |
| 231 | |
| 232 | if [[ $VAGRANT -ne 0 ]] |
| 233 | then |
| 234 | run_stage bootstrap_vagrant |
| 235 | fi |
| 236 | |
| 237 | if [[ $DOCKER -ne 0 ]] |
| 238 | then |
| 239 | run_stage bootstrap_docker |
| 240 | fi |
| 241 | |
| 242 | # Check for group membership when needed |
| 243 | if [[ ! -z ${GROUP_LIST[@]-} ]] |
| 244 | then |
| 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 |
| 258 | fi |
| 259 | |
| 260 | # run make targets, if specified |
| 261 | if [[ ! -z ${MAKE_TARGETS[@]-} ]] |
| 262 | then |
| 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 |
| 270 | fi |
| 271 | |
| 272 | exit 0 |