Carmelo Cascone | d661507 | 2017-12-15 00:53:43 -0800 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | set -xe |
| 4 | |
Carmelo Cascone | c5569c2 | 2018-04-18 10:09:15 +0900 | [diff] [blame] | 5 | VM_TYPE=${P4_VM_TYPE:-dev} |
| 6 | |
Carmelo Cascone | 57defd3 | 2018-05-11 14:34:01 -0700 | [diff] [blame] | 7 | function wait_vm_shutdown { |
| 8 | set +x |
Carmelo Cascone | ef71549 | 2018-05-14 19:48:46 -0700 | [diff] [blame] | 9 | while vboxmanage showvminfo $1 | grep -c "running (since"; do |
Carmelo Cascone | 57defd3 | 2018-05-11 14:34:01 -0700 | [diff] [blame] | 10 | echo "Waiting for VM to shutdown..." |
| 11 | sleep 1 |
| 12 | done |
| 13 | sleep 2 |
| 14 | set -x |
| 15 | } |
| 16 | |
Carmelo Cascone | ef71549 | 2018-05-14 19:48:46 -0700 | [diff] [blame] | 17 | function wait_for_tcp_port { |
| 18 | set +x |
| 19 | while ! nc -z $1 $2; do |
| 20 | echo "Waiting for TCP port $2 on $1 to be open..." |
| 21 | sleep 1 |
| 22 | done |
| 23 | sleep 2 |
| 24 | set -x |
| 25 | } |
| 26 | |
Carmelo Cascone | d661507 | 2017-12-15 00:53:43 -0800 | [diff] [blame] | 27 | # Remove references to the existing vagrant-built VM (if any). |
| 28 | # We want to build a new one from scratch, not start an existing one. |
| 29 | rm -rf .vagrant/ |
Carmelo Cascone | a4dc3c1 | 2019-02-12 17:30:00 -0800 | [diff] [blame] | 30 | vagrant box update |
Carmelo Cascone | d661507 | 2017-12-15 00:53:43 -0800 | [diff] [blame] | 31 | vagrant up |
| 32 | |
| 33 | SSH_PORT=`vagrant port --guest 22` |
| 34 | VB_UUID=`cat .vagrant/machines/default/virtualbox/id` |
| 35 | |
Carmelo Cascone | 57defd3 | 2018-05-11 14:34:01 -0700 | [diff] [blame] | 36 | # Take snapshot before cleanup for local use |
| 37 | # e.g. to avoid re-building P4 tools from scratch |
| 38 | vboxmanage controlvm ${VB_UUID} acpipowerbutton |
| 39 | wait_vm_shutdown ${VB_UUID} |
| 40 | VBoxManage snapshot ${VB_UUID} take "pre-cleanup" |
| 41 | |
| 42 | # Cleanup |
| 43 | vagrant up |
Carmelo Cascone | ef71549 | 2018-05-14 19:48:46 -0700 | [diff] [blame] | 44 | # SSH port forwarding might change after vagrant up. |
| 45 | SSH_PORT=`vagrant port --guest 22` |
| 46 | wait_for_tcp_port 127.0.0.1 ${SSH_PORT} |
Carmelo Cascone | d661507 | 2017-12-15 00:53:43 -0800 | [diff] [blame] | 47 | sshpass -p 'rocks' \ |
| 48 | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no \ |
| 49 | -p ${SSH_PORT} sdn@127.0.0.1 "bash /vagrant/pre-ova-cleanup.sh" |
Carmelo Cascone | d661507 | 2017-12-15 00:53:43 -0800 | [diff] [blame] | 50 | sleep 5 |
Carmelo Cascone | 4f985cd | 2018-02-11 17:36:42 -0800 | [diff] [blame] | 51 | vboxmanage controlvm ${VB_UUID} acpipowerbutton |
Carmelo Cascone | 57defd3 | 2018-05-11 14:34:01 -0700 | [diff] [blame] | 52 | wait_vm_shutdown ${VB_UUID} |
Carmelo Cascone | d661507 | 2017-12-15 00:53:43 -0800 | [diff] [blame] | 53 | |
| 54 | # Remove vagrant shared folder |
| 55 | vboxmanage sharedfolder remove ${VB_UUID} -name "vagrant" |
| 56 | |
Carmelo Cascone | 57defd3 | 2018-05-11 14:34:01 -0700 | [diff] [blame] | 57 | rm -f onos-p4-${VM_TYPE}.ova |
Carmelo Cascone | c5569c2 | 2018-04-18 10:09:15 +0900 | [diff] [blame] | 58 | vboxmanage export ${VB_UUID} -o onos-p4-${VM_TYPE}.ova |
Carmelo Cascone | 57defd3 | 2018-05-11 14:34:01 -0700 | [diff] [blame] | 59 | |
| 60 | sleep 1 |
| 61 | vboxmanage snapshot ${VB_UUID} restore pre-cleanup |
| 62 | sleep 1 |
| 63 | vboxmanage snapshot ${VB_UUID} delete pre-cleanup |