Zack Williams | a1f5508 | 2017-02-28 22:41:36 -0700 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | # opencloud-in-a-box.sh |
| 3 | |
| 4 | set -e -x |
| 5 | |
| 6 | # start time, used to name logfiles |
| 7 | START_T=$(date -u "+%Y%m%d_%H%M%SZ") |
| 8 | |
| 9 | # Paths |
| 10 | CORDDIR=~/cord |
| 11 | CONFIG=${CORDDIR}/config/opencloud_in_a_box.yml |
| 12 | SSHCONFIG=~/.ssh/config |
| 13 | VAGRANT_CWD=${CORDDIR}/build/targets/opencloud-in-a-box |
| 14 | |
| 15 | # CORD versioning |
| 16 | REPO_BRANCH="master" |
| 17 | VERSION_STRING="OpenCloud Devel" |
| 18 | |
| 19 | function add_box() { |
| 20 | echo "Downloading image: $1" |
| 21 | vagrant box list | grep $1 | grep virtualbox || vagrant box add $1 |
| 22 | vagrant box list | grep $1 | grep libvirt || vagrant mutate $1 libvirt --input-provider virtualbox |
| 23 | } |
| 24 | |
| 25 | function run_stage { |
| 26 | echo "==> "$1": Starting" |
| 27 | $1 |
| 28 | echo "==> "$1": Complete" |
| 29 | } |
| 30 | |
| 31 | function cleanup_from_previous_test() { |
| 32 | echo "## Cleanup ##" |
| 33 | |
| 34 | if [ -d $CORDDIR/build ] |
| 35 | then |
| 36 | echo "Destroying all Vagrant VMs" |
| 37 | cd $CORDDIR/build |
| 38 | sudo VAGRANT_CWD=$VAGRANT_CWD vagrant destroy |
| 39 | fi |
| 40 | |
| 41 | echo "Removing $CORDDIR" |
| 42 | cd ~ |
| 43 | rm -rf $CORDDIR |
| 44 | } |
| 45 | |
| 46 | function bootstrap() { |
| 47 | |
| 48 | if [ ! -x "/usr/bin/ansible" ] |
| 49 | then |
| 50 | echo "Installing Ansible..." |
| 51 | sudo apt-get update |
| 52 | sudo apt-get install -y software-properties-common apt-transport-https |
| 53 | sudo apt-add-repository -y ppa:ansible/ansible # latest supported version |
| 54 | sudo apt-get update |
| 55 | sudo apt-get install -y ansible python-netaddr |
| 56 | fi |
| 57 | |
| 58 | if [ ! -x "/usr/local/bin/repo" ] |
| 59 | then |
| 60 | echo "Installing repo..." |
| 61 | REPO_SHA256SUM="e147f0392686c40cfd7d5e6f332c6ee74c4eab4d24e2694b3b0a0c037bf51dc5" # not versioned... |
| 62 | curl -o /tmp/repo https://storage.googleapis.com/git-repo-downloads/repo |
| 63 | echo "$REPO_SHA256SUM /tmp/repo" | sha256sum -c - |
| 64 | sudo mv /tmp/repo /usr/local/bin/repo |
| 65 | sudo chmod a+x /usr/local/bin/repo |
| 66 | fi |
| 67 | |
| 68 | if [ ! -x "/usr/bin/vagrant" ] |
| 69 | then |
| 70 | echo "Installing vagrant and associated tools..." |
| 71 | VAGRANT_SHA256SUM="52ebd6aa798582681d0f1e008982c709136eeccd668a8e8be4d48a1f632de7b8" # version 1.9.2 |
| 72 | curl -o /tmp/vagrant.deb https://releases.hashicorp.com/vagrant/1.9.2/vagrant_1.9.2_x86_64.deb |
| 73 | echo "$VAGRANT_SHA256SUM /tmp/vagrant.deb" | sha256sum -c - |
| 74 | sudo dpkg -i /tmp/vagrant.deb |
| 75 | sudo apt-get -y install qemu-kvm libvirt-bin libvirt-dev curl nfs-kernel-server git build-essential python-pip |
| 76 | sudo adduser $USER libvirtd |
| 77 | sudo pip install pyparsing python-logstash |
| 78 | |
| 79 | run_stage cloudlab_setup |
| 80 | |
| 81 | echo "Installing vagrant plugins..." |
| 82 | vagrant plugin list | grep vagrant-libvirt || vagrant plugin install vagrant-libvirt --plugin-version 0.0.35 |
| 83 | vagrant plugin list | grep vagrant-mutate || vagrant plugin install vagrant-mutate |
| 84 | |
| 85 | add_box ubuntu/trusty64 |
| 86 | fi |
| 87 | |
| 88 | if [ ! -d "$CORDDIR" ] |
| 89 | then |
| 90 | echo "Downloading CORD/XOS..." |
| 91 | |
| 92 | if [ ! -e "~/.gitconfig" ] |
| 93 | then |
| 94 | echo "No ~/.gitconfig, setting testing defaults" |
| 95 | git config --global user.name 'Test User' |
| 96 | git config --global user.email 'test@null.com' |
| 97 | git config --global color.ui false |
| 98 | fi |
| 99 | |
| 100 | # make sure we can find gerrit.opencord.org as DNS failures will fail the build |
| 101 | dig +short gerrit.opencord.org || (echo "ERROR: gerrit.opencord.org can't be looked up in DNS" && exit 1) |
| 102 | |
| 103 | mkdir $CORDDIR && cd $CORDDIR |
| 104 | repo init -u https://gerrit.opencord.org/manifest -b master -g build,onos,orchestration,voltha |
| 105 | repo sync |
| 106 | |
| 107 | # check out gerrit branches using repo |
| 108 | for gerrit_branch in ${GERRIT_BRANCHES[@]}; do |
| 109 | echo "Checking out opencord gerrit branch: $gerrit_branch" |
| 110 | repo download ${gerrit_branch/:/ } |
| 111 | done |
| 112 | fi |
| 113 | } |
| 114 | |
| 115 | function cloudlab_setup() { |
| 116 | |
| 117 | # Don't do anything if not a CloudLab node |
| 118 | [ ! -d /usr/local/etc/emulab ] && return |
| 119 | |
| 120 | # The watchdog will sometimes reset groups, turn it off |
| 121 | if [ -e /usr/local/etc/emulab/watchdog ] |
| 122 | then |
| 123 | sudo /usr/bin/perl -w /usr/local/etc/emulab/watchdog stop |
| 124 | sudo mv /usr/local/etc/emulab/watchdog /usr/local/etc/emulab/watchdog-disabled |
| 125 | fi |
| 126 | |
| 127 | # Mount extra space, if haven't already |
| 128 | if [ ! -d /mnt/extra ] |
| 129 | then |
| 130 | sudo mkdir -p /mnt/extra |
| 131 | |
| 132 | # for NVME SSD on Utah Cloudlab, not supported by mkextrafs |
| 133 | if $(df | grep -q nvme0n1p1) && [ -e /usr/testbed/bin/mkextrafs ] |
| 134 | then |
| 135 | # set partition type of 4th partition to Linux, ignore errors |
| 136 | echo -e "t\n4\n82\np\nw\nq" | sudo fdisk /dev/nvme0n1 || true |
| 137 | |
| 138 | sudo mkfs.ext4 /dev/nvme0n1p4 |
| 139 | echo "/dev/nvme0n1p4 /mnt/extra/ ext4 defaults 0 0" | sudo tee -a /etc/fstab |
| 140 | sudo mount /mnt/extra |
| 141 | mount | grep nvme0n1p4 || (echo "ERROR: NVME mkfs/mount failed, exiting!" && exit 1) |
| 142 | |
| 143 | elif [ -e /usr/testbed/bin/mkextrafs ] # if on Clemson/Wisconsin Cloudlab |
| 144 | then |
| 145 | # Sometimes this command fails on the first try |
| 146 | sudo /usr/testbed/bin/mkextrafs -r /dev/sdb -qf "/mnt/extra/" || sudo /usr/testbed/bin/mkextrafs -r /dev/sdb -qf "/mnt/extra/" |
| 147 | |
| 148 | # Check that the mount succeeded (sometimes mkextrafs succeeds but device not mounted) |
| 149 | mount | grep sdb || (echo "ERROR: mkextrafs failed, exiting!" && exit 1) |
| 150 | fi |
| 151 | fi |
| 152 | |
| 153 | # replace /var/lib/libvirt/images with a symlink |
| 154 | [ -d /var/lib/libvirt/images/ ] && [ ! -h /var/lib/libvirt/images ] && sudo rmdir /var/lib/libvirt/images |
| 155 | sudo mkdir -p /mnt/extra/libvirt_images |
| 156 | |
| 157 | if [ ! -e /var/lib/libvirt/images ] |
| 158 | then |
| 159 | sudo ln -s /mnt/extra/libvirt_images /var/lib/libvirt/images |
| 160 | fi |
| 161 | } |
| 162 | |
| 163 | function vagrant_vms_up() { |
| 164 | |
| 165 | # vagrant-libvirt 0.0.35 fails with libvirt networks created by others |
| 166 | # echo "Configuring libvirt networking..." |
| 167 | # cd ${CORDDIR}/build/ansible |
| 168 | # ansible-playbook opencloud-in-a-box-playbook.yml |
| 169 | |
| 170 | echo "Bringing up OpenCloud-in-a-Box Vagrant VM's..." |
| 171 | cd $CORDDIR/build |
| 172 | sudo VAGRANT_CWD=$VAGRANT_CWD vagrant up head1 compute1 compute2 --provider libvirt |
| 173 | |
| 174 | # This is a workaround for a weird issue with ARP cache timeout breaking 'vagrant ssh' |
| 175 | # It allows SSH'ing to the machine via 'ssh <machinename>' |
| 176 | echo "Configuring SSH for VM's..." |
| 177 | sudo VAGRANT_CWD=$VAGRANT_CWD vagrant ssh-config head1 compute1 compute2 > $SSHCONFIG |
| 178 | |
| 179 | sudo chown -R ${USER} ${VAGRANT_CWD}/.vagrant |
| 180 | } |
| 181 | |
| 182 | function opencloud_buildhost_prep() { |
| 183 | |
| 184 | echo "Preparing build tools..." |
| 185 | cd ${CORDDIR}/build/platform-install |
| 186 | ansible-playbook -i inventory/opencloud opencloud-prep-playbook.yml |
| 187 | } |
| 188 | |
| 189 | # Parse options |
| 190 | GERRIT_BRANCHES= |
| 191 | RUN_TEST=0 |
| 192 | SETUP_ONLY=0 |
| 193 | CLEANUP=0 |
| 194 | |
| 195 | while getopts "b:chsv" opt; do |
| 196 | case ${opt} in |
| 197 | b ) GERRIT_BRANCHES+=("$OPTARG") |
| 198 | ;; |
| 199 | c ) CLEANUP=1 |
| 200 | ;; |
| 201 | h ) echo "Usage:" |
| 202 | echo " $0 install OpenCloud-in-a-Box [default]" |
| 203 | echo " $0 -b <project:changeset/revision> checkout a changesets from gerrit. Can" |
| 204 | echo " be used multiple times." |
| 205 | echo " $0 -c cleanup from previous test" |
| 206 | echo " $0 -h display this help message" |
| 207 | echo " $0 -s run initial setup phase only (don't start building CORD)" |
| 208 | echo " $0 -v print CiaB version and exit" |
| 209 | exit 0 |
| 210 | ;; |
| 211 | s ) SETUP_ONLY=1 |
| 212 | ;; |
| 213 | v ) echo "$VERSION_STRING ($REPO_BRANCH branch)" |
| 214 | exit 0 |
| 215 | ;; |
| 216 | \? ) echo "Invalid option: -$OPTARG" |
| 217 | exit 1 |
| 218 | ;; |
| 219 | esac |
| 220 | done |
| 221 | |
| 222 | # What to do |
| 223 | if [[ $CLEANUP -eq 1 ]] |
| 224 | then |
| 225 | run_stage cleanup_from_previous_test |
| 226 | fi |
| 227 | |
| 228 | echo "" |
| 229 | echo "Preparing to install $VERSION_STRING ($REPO_BRANCH branch)" |
| 230 | echo "" |
| 231 | |
| 232 | run_stage bootstrap |
| 233 | |
| 234 | |
| 235 | if [[ $SETUP_ONLY -ne 0 ]] |
| 236 | then |
| 237 | echo "Finished build environment setup, exiting..." |
| 238 | exit 0 |
| 239 | fi |
| 240 | |
| 241 | run_stage vagrant_vms_up |
| 242 | run_stage opencloud_buildhost_prep |
| 243 | |
| 244 | exit 0 |