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 | d661507 | 2017-12-15 00:53:43 -0800 | [diff] [blame] | 30 | vagrant up |
| 31 | |
| 32 | SSH_PORT=`vagrant port --guest 22` |
| 33 | VB_UUID=`cat .vagrant/machines/default/virtualbox/id` |
| 34 | |
Carmelo Cascone | 57defd3 | 2018-05-11 14:34:01 -0700 | [diff] [blame] | 35 | # Take snapshot before cleanup for local use |
| 36 | # e.g. to avoid re-building P4 tools from scratch |
| 37 | vboxmanage controlvm ${VB_UUID} acpipowerbutton |
| 38 | wait_vm_shutdown ${VB_UUID} |
| 39 | VBoxManage snapshot ${VB_UUID} take "pre-cleanup" |
| 40 | |
| 41 | # Cleanup |
| 42 | vagrant up |
Carmelo Cascone | ef71549 | 2018-05-14 19:48:46 -0700 | [diff] [blame] | 43 | # SSH port forwarding might change after vagrant up. |
| 44 | SSH_PORT=`vagrant port --guest 22` |
| 45 | wait_for_tcp_port 127.0.0.1 ${SSH_PORT} |
Carmelo Cascone | d661507 | 2017-12-15 00:53:43 -0800 | [diff] [blame] | 46 | sshpass -p 'rocks' \ |
| 47 | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no \ |
| 48 | -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] | 49 | sleep 5 |
Carmelo Cascone | 4f985cd | 2018-02-11 17:36:42 -0800 | [diff] [blame] | 50 | vboxmanage controlvm ${VB_UUID} acpipowerbutton |
Carmelo Cascone | 57defd3 | 2018-05-11 14:34:01 -0700 | [diff] [blame] | 51 | wait_vm_shutdown ${VB_UUID} |
Carmelo Cascone | d661507 | 2017-12-15 00:53:43 -0800 | [diff] [blame] | 52 | |
| 53 | # Remove vagrant shared folder |
| 54 | vboxmanage sharedfolder remove ${VB_UUID} -name "vagrant" |
| 55 | |
Carmelo Cascone | 57defd3 | 2018-05-11 14:34:01 -0700 | [diff] [blame] | 56 | rm -f onos-p4-${VM_TYPE}.ova |
Carmelo Cascone | c5569c2 | 2018-04-18 10:09:15 +0900 | [diff] [blame] | 57 | vboxmanage export ${VB_UUID} -o onos-p4-${VM_TYPE}.ova |
Carmelo Cascone | 57defd3 | 2018-05-11 14:34:01 -0700 | [diff] [blame] | 58 | |
| 59 | sleep 1 |
| 60 | vboxmanage snapshot ${VB_UUID} restore pre-cleanup |
| 61 | sleep 1 |
| 62 | vboxmanage snapshot ${VB_UUID} delete pre-cleanup |