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