blob: bc0e5c47b2e5c0c1de52084fbc1c0ea8243377af [file] [log] [blame]
Carmelo Casconed6615072017-12-15 00:53:43 -08001#!/usr/bin/env bash
2
3set -xe
4
Carmelo Casconec5569c22018-04-18 10:09:15 +09005VM_TYPE=${P4_VM_TYPE:-dev}
6
Carmelo Cascone57defd32018-05-11 14:34:01 -07007function wait_vm_shutdown {
8 set +x
Carmelo Casconeef715492018-05-14 19:48:46 -07009 while vboxmanage showvminfo $1 | grep -c "running (since"; do
Carmelo Cascone57defd32018-05-11 14:34:01 -070010 echo "Waiting for VM to shutdown..."
11 sleep 1
12 done
13 sleep 2
14 set -x
15}
16
Carmelo Casconeef715492018-05-14 19:48:46 -070017function 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 Casconed6615072017-12-15 00:53:43 -080027# 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.
29rm -rf .vagrant/
Carmelo Casconea4dc3c12019-02-12 17:30:00 -080030vagrant box update
Carmelo Casconed6615072017-12-15 00:53:43 -080031vagrant up
32
33SSH_PORT=`vagrant port --guest 22`
34VB_UUID=`cat .vagrant/machines/default/virtualbox/id`
35
Carmelo Cascone57defd32018-05-11 14:34:01 -070036# Take snapshot before cleanup for local use
37# e.g. to avoid re-building P4 tools from scratch
38vboxmanage controlvm ${VB_UUID} acpipowerbutton
39wait_vm_shutdown ${VB_UUID}
40VBoxManage snapshot ${VB_UUID} take "pre-cleanup"
41
42# Cleanup
43vagrant up
Carmelo Casconeef715492018-05-14 19:48:46 -070044# SSH port forwarding might change after vagrant up.
45SSH_PORT=`vagrant port --guest 22`
46wait_for_tcp_port 127.0.0.1 ${SSH_PORT}
Carmelo Casconed6615072017-12-15 00:53:43 -080047sshpass -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 Casconed6615072017-12-15 00:53:43 -080050sleep 5
Carmelo Cascone4f985cd2018-02-11 17:36:42 -080051vboxmanage controlvm ${VB_UUID} acpipowerbutton
Carmelo Cascone57defd32018-05-11 14:34:01 -070052wait_vm_shutdown ${VB_UUID}
Carmelo Casconed6615072017-12-15 00:53:43 -080053
54# Remove vagrant shared folder
55vboxmanage sharedfolder remove ${VB_UUID} -name "vagrant"
56
Carmelo Cascone57defd32018-05-11 14:34:01 -070057rm -f onos-p4-${VM_TYPE}.ova
Carmelo Casconec5569c22018-04-18 10:09:15 +090058vboxmanage export ${VB_UUID} -o onos-p4-${VM_TYPE}.ova
Carmelo Cascone57defd32018-05-11 14:34:01 -070059
60sleep 1
61vboxmanage snapshot ${VB_UUID} restore pre-cleanup
62sleep 1
63vboxmanage snapshot ${VB_UUID} delete pre-cleanup