Andy Bavier | 99c11d3 | 2016-09-14 17:21:20 -0400 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | set -e |
| 4 | set -x |
| 5 | |
| 6 | CORDDIR=~/opencord |
| 7 | VMDIR=/cord/build/ |
| 8 | CONFIG=config/cord_in_a_box.yml |
| 9 | |
| 10 | function cleanup_from_previous_test() { |
| 11 | set +e |
| 12 | |
| 13 | echo "## Cleanup ##" |
| 14 | |
| 15 | echo "Shutting down all Vagrant VMs" |
| 16 | cd $CORDDIR/build |
| 17 | vagrant destroy |
| 18 | |
| 19 | echo "Destroying juju environment" |
| 20 | juju destroy-environment --force -y manual |
| 21 | |
| 22 | VMS=$( sudo uvt-kvm list ) |
| 23 | for VM in $VMS |
| 24 | do |
| 25 | echo "Destroying $VM" |
| 26 | sudo uvt-kvm destroy $VM |
| 27 | done |
| 28 | |
| 29 | echo "Cleaning up files" |
| 30 | rm -rf ~/.juju |
| 31 | rm -f ~/.ssh/known_hosts |
| 32 | rm -rf ~/platform-install |
| 33 | rm -rf ~/cord_apps |
| 34 | rm -rf ~/.ansible_async |
| 35 | |
| 36 | echo "Removing MAAS" |
| 37 | [ -e /usr/local/bin/remove-maas-components ] && /usr/local/bin/remove-maas-components |
| 38 | |
| 39 | echo "Remove apt-cacher-ng" |
| 40 | sudo apt-get remove -y apt-cacher-ng |
| 41 | sudo rm -f /etc/apt/apt.conf.d/02apt-cacher-ng |
| 42 | |
| 43 | echo "Removing mgmtbr" |
| 44 | ifconfig mgmtbr && sudo ip link set dev mgmtbr down && sudo brctl delbr mgmtbr |
| 45 | |
| 46 | echo "Removing Juju packages" |
| 47 | sudo apt-get remove --purge -y $(dpkg --get-selections | grep "juju\|nova\|neutron\|keystone\|glance" | awk '{print $1}') |
| 48 | sudo apt-get autoremove -y |
| 49 | |
| 50 | rm -rf $CORDDIR |
| 51 | |
| 52 | set -e |
| 53 | } |
| 54 | |
| 55 | function bootstrap() { |
| 56 | cd ~ |
| 57 | sudo apt-get update |
| 58 | [ -e vagrant_1.8.5_x86_64.deb ] || wget https://releases.hashicorp.com/vagrant/1.8.5/vagrant_1.8.5_x86_64.deb |
| 59 | sudo dpkg -i vagrant_1.8.5_x86_64.deb |
Andy Bavier | 8ce395f | 2016-10-14 12:51:30 -0400 | [diff] [blame] | 60 | sudo apt-get -y install qemu-kvm libvirt-bin libvirt-dev curl nfs-kernel-server |
Andy Bavier | 99c11d3 | 2016-09-14 17:21:20 -0400 | [diff] [blame] | 61 | |
| 62 | [ -e ~/.ssh/id_rsa ] || ssh-keygen -t rsa -N "" -f ~/.ssh/id_rsa |
| 63 | cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys |
| 64 | |
| 65 | # Log into the local node once to get host key |
| 66 | ssh -o StrictHostKeyChecking=no localhost "ls > /dev/null" |
| 67 | |
| 68 | USER=$(whoami) |
| 69 | sudo adduser $USER libvirtd |
| 70 | |
| 71 | sudo curl -o /usr/local/bin/repo https://storage.googleapis.com/git-repo-downloads/repo |
| 72 | sudo chmod a+x /usr/local/bin/repo |
| 73 | |
| 74 | if [ ! -d "$CORDDIR" ] |
| 75 | then |
| 76 | mkdir $CORDDIR && cd $CORDDIR |
| 77 | git config --global user.name 'Test User' |
| 78 | git config --global user.email 'test@null.com' |
| 79 | git config --global color.ui false |
| 80 | |
Scott Baker | 101b37c | 2016-10-24 16:22:43 -0700 | [diff] [blame] | 81 | repo init -u https://gerrit.opencord.org/manifest -b master -g build,onos,orchestration |
Andy Bavier | 99c11d3 | 2016-09-14 17:21:20 -0400 | [diff] [blame] | 82 | repo sync |
| 83 | |
| 84 | cd $CORDDIR/build |
| 85 | sed -i "s/user: 'ubuntu'/user: \"$USER\"/" $CONFIG |
| 86 | |
| 87 | # Set external interface in config file |
| 88 | IFACE=$(route | grep default | awk '{print $8}' ) |
| 89 | sed -i "s/eth0/$IFACE/" $CONFIG |
| 90 | fi |
| 91 | |
| 92 | cd $CORDDIR/build |
Andy Bavier | 45e30bb | 2016-10-07 15:45:18 -0400 | [diff] [blame] | 93 | vagrant plugin install vagrant-libvirt --plugin-version 0.0.35 |
Andy Bavier | 99c11d3 | 2016-09-14 17:21:20 -0400 | [diff] [blame] | 94 | vagrant plugin install vagrant-mutate |
| 95 | vagrant box list ubuntu/trusty64 | grep virtualbox || vagrant box add ubuntu/trusty64 |
| 96 | vagrant box list ubuntu/trusty64 | grep libvirt || vagrant mutate ubuntu/trusty64 libvirt --input-provider virtualbox |
| 97 | } |
| 98 | |
| 99 | function cloudlab_setup() { |
| 100 | if [ -e /usr/testbed/bin/mkextrafs ] |
| 101 | then |
Andy Bavier | 8ce395f | 2016-10-14 12:51:30 -0400 | [diff] [blame] | 102 | # Sometimes this command fails on the first try |
| 103 | sudo /usr/testbed/bin/mkextrafs -r /dev/sdb -qf "/var/lib/libvirt/images/" || sudo /usr/testbed/bin/mkextrafs -r /dev/sdb -qf "/var/lib/libvirt/images/" |
Andy Bavier | 99c11d3 | 2016-09-14 17:21:20 -0400 | [diff] [blame] | 104 | |
| 105 | cd $CORDDIR/build |
| 106 | SRC="#- 'on_cloudlab=True'" |
| 107 | DST="- 'on_cloudlab=True'" |
| 108 | sed -i "s/$SRC/$DST/" config/cord_in_a_box.yml |
| 109 | fi |
| 110 | } |
| 111 | |
| 112 | function unfortunate_hacks() { |
| 113 | cd $CORDDIR/build |
| 114 | |
| 115 | # Disable interface rename during MAAS provision |
| 116 | sed -i 's/"INTERFACE_CONFIG=1"/"INTERFACE_CONFIG=0"/' maas/roles/maas/templates/automation-compose.yml.j2 |
| 117 | |
| 118 | # Don't require fabric_ip |
| 119 | SRC="fabric_ip | mandatory" |
| 120 | DST="fabric_ip | default('manual')" |
| 121 | sed -i "s/$SRC/$DST/" maas/roles/compute-node/vars/main.yml |
| 122 | |
| 123 | # Allow compute nodes to PXE boot from mgmtbr |
| 124 | sed -i "s/@type='udp']/@type='udp' or @type='bridge']/" \ |
| 125 | ~/.vagrant.d/gems/gems/vagrant-libvirt-0.0.35/lib/vagrant-libvirt/action/set_boot_order.rb |
| 126 | |
| 127 | # Should get these keys inside the VM in another way |
| 128 | cp ~/.ssh/id_rsa* $CORDDIR |
| 129 | } |
| 130 | |
| 131 | function corddev_up() { |
| 132 | cd $CORDDIR/build |
| 133 | |
| 134 | sudo su $USER -c 'vagrant up corddev --provider libvirt' |
| 135 | } |
| 136 | |
| 137 | function install_head_node() { |
| 138 | cd $CORDDIR/build |
| 139 | |
| 140 | # Network setup to install physical server as head node |
| 141 | ip addr list dev virbr2 | grep 10.100.198.201 || sudo ip addr add dev virbr2 10.100.198.201 |
| 142 | ifconfig mgmtbr || sudo brctl addbr mgmtbr |
| 143 | sudo ifconfig mgmtbr 10.1.0.1/24 up |
| 144 | |
| 145 | # User has been added to the libvirtd group, but su $USER to be safe |
| 146 | sudo su $USER -c "vagrant ssh corddev -c \"cp /cord/id_rsa* ~/.ssh\"" |
| 147 | sudo su $USER -c "vagrant ssh corddev -c \"cd /cord/build; ./gradlew fetch\"" |
| 148 | sudo su $USER -c "vagrant ssh corddev -c \"cd /cord/build; ./gradlew buildImages\"" |
| 149 | sudo su $USER -c "vagrant ssh corddev -c \"cd /cord/build; ./gradlew -PdeployConfig=$VMDIR/$CONFIG -PtargetReg=10.100.198.201:5000 publish\"" |
| 150 | sudo su $USER -c "vagrant ssh corddev -c \"cd /cord/build; ./gradlew -PdeployConfig=$VMDIR/$CONFIG deploy\"" |
| 151 | } |
| 152 | |
| 153 | function set_up_maas_user() { |
| 154 | # Set up MAAS user to restart nodes via libvirt |
| 155 | sudo mkdir -p /home/maas |
| 156 | sudo chown maas:maas /home/maas |
| 157 | sudo chsh -s /bin/bash maas |
| 158 | sudo adduser maas libvirtd |
| 159 | |
| 160 | sudo su maas -c 'cp ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys' |
| 161 | } |
| 162 | |
| 163 | function add_compute_node() { |
| 164 | cd $CORDDIR/build |
| 165 | sudo su $USER -c 'vagrant up compute_node --provider libvirt' |
| 166 | |
| 167 | # Sign into MAAS |
| 168 | KEY=$(sudo maas-region-admin apikey --username=cord) |
| 169 | maas login cord http://localhost/MAAS/api/1.0 $KEY |
| 170 | |
| 171 | NODEID=$(maas cord nodes list|jq -r '.[] | select(.status == 0).system_id') |
| 172 | until [ "$NODEID" ]; do |
| 173 | echo "Waiting for the compute node to transition to NEW state" |
| 174 | sleep 15 |
| 175 | NODEID=$(maas cord nodes list|jq -r '.[] | select(.status == 0).system_id') |
| 176 | done |
| 177 | |
| 178 | # Add remote power state |
| 179 | maas cord node update $NODEID power_type="virsh" \ |
| 180 | power_parameters_power_address="qemu+ssh://maas@localhost/system" \ |
| 181 | power_parameters_power_id="build_compute_node" |
| 182 | |
| 183 | STATUS=$(sudo /usr/local/bin/get-node-prov-state |jq ".[] | select(.id == \"$NODEID\").status") |
| 184 | until [ "$STATUS" == "2" ]; do |
| 185 | if [ "$STATUS" == "3" ]; then |
| 186 | echo "*** ERROR in provisioning!" |
| 187 | echo "*** Check /etc/maas/ansible/logs/$NODEID.log" |
| 188 | exit 1 |
| 189 | fi |
| 190 | echo "Waiting for the compute node to be fully provisioned" |
| 191 | sleep 60 |
| 192 | STATUS=$(sudo /usr/local/bin/get-node-prov-state |jq ".[] | select(.id == \"$NODEID\").status") |
| 193 | done |
| 194 | |
| 195 | echo "" |
| 196 | echo "compute_node is fully provisioned!" |
| 197 | } |
| 198 | |
| 199 | function run_e2e_test () { |
| 200 | cd $CORDDIR/build |
| 201 | |
| 202 | # User has been added to the libvirtd group, but su $USER to be safe |
| 203 | sudo su $USER -c "vagrant ssh corddev -c \"cd /cord/build; ./gradlew -PdeployConfig=$VMDIR/$CONFIG postDeployTests\"" |
| 204 | } |
| 205 | |
| 206 | function run_diagnostics() { |
| 207 | echo "*** COLLECTING DIAGNOSTIC INFO NOT CURRENTLY IMPLEMENTED" |
| 208 | # Need to fix up inventory to collect info from compute nodes |
| 209 | # Using juju-ansible is one possibility |
| 210 | #echo "*** COLLECTING DIAGNOSTIC INFO - check ~/diag-* on the head node" |
| 211 | #ansible-playbook -i $INVENTORY cord-diag-playbook.yml |
| 212 | } |
| 213 | |
| 214 | # Parse options |
| 215 | RUN_TEST=0 |
| 216 | SETUP_BRANCH="master" |
| 217 | DIAGNOSTICS=0 |
| 218 | CLEANUP=0 |
| 219 | |
| 220 | while getopts "b:cdehi:p:r:ts:" opt; do |
| 221 | case ${opt} in |
| 222 | b ) XOS_BRANCH=$OPTARG |
| 223 | ;; |
| 224 | c ) CLEANUP=1 |
| 225 | ;; |
| 226 | d ) DIAGNOSTICS=1 |
| 227 | ;; |
| 228 | h ) echo "Usage:" |
| 229 | echo " $0 install OpenStack and prep XOS and ONOS VMs [default]" |
| 230 | echo " $0 -b <branch> checkout <branch> of the xos git repo" |
| 231 | echo " $0 -c cleanup from previous test" |
| 232 | echo " $0 -d run diagnostic collector" |
| 233 | echo " $0 -h display this help message" |
| 234 | echo " $0 -t do install, bring up cord-pod configuration, run E2E test" |
| 235 | exit 0 |
| 236 | ;; |
| 237 | t ) RUN_TEST=1 |
| 238 | ;; |
| 239 | \? ) echo "Invalid option: -$OPTARG" |
| 240 | exit 1 |
| 241 | ;; |
| 242 | esac |
| 243 | done |
| 244 | |
| 245 | # What to do |
| 246 | if [[ $CLEANUP -eq 1 ]] |
| 247 | then |
| 248 | cleanup_from_previous_test |
| 249 | fi |
| 250 | |
| 251 | set -e |
| 252 | |
| 253 | bootstrap |
| 254 | cloudlab_setup |
| 255 | unfortunate_hacks |
| 256 | corddev_up |
| 257 | install_head_node |
| 258 | set_up_maas_user |
| 259 | add_compute_node |
| 260 | |
| 261 | if [[ $RUN_TEST -eq 1 ]] |
| 262 | then |
| 263 | run_e2e_test |
| 264 | fi |
| 265 | |
| 266 | if [[ $DIAGNOSTICS -eq 1 ]] |
| 267 | then |
| 268 | run_diagnostics |
| 269 | fi |
| 270 | |
| 271 | exit 0 |