blob: 81f6d038aa057deccc5a907613158ba16a948756 [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 Casconed6615072017-12-15 00:53:43 -080030vagrant up
31
32SSH_PORT=`vagrant port --guest 22`
33VB_UUID=`cat .vagrant/machines/default/virtualbox/id`
34
Carmelo Cascone57defd32018-05-11 14:34:01 -070035# Take snapshot before cleanup for local use
36# e.g. to avoid re-building P4 tools from scratch
37vboxmanage controlvm ${VB_UUID} acpipowerbutton
38wait_vm_shutdown ${VB_UUID}
39VBoxManage snapshot ${VB_UUID} take "pre-cleanup"
40
41# Cleanup
42vagrant up
Carmelo Casconeef715492018-05-14 19:48:46 -070043# SSH port forwarding might change after vagrant up.
44SSH_PORT=`vagrant port --guest 22`
45wait_for_tcp_port 127.0.0.1 ${SSH_PORT}
Carmelo Casconed6615072017-12-15 00:53:43 -080046sshpass -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 Casconed6615072017-12-15 00:53:43 -080049sleep 5
Carmelo Cascone4f985cd2018-02-11 17:36:42 -080050vboxmanage controlvm ${VB_UUID} acpipowerbutton
Carmelo Cascone57defd32018-05-11 14:34:01 -070051wait_vm_shutdown ${VB_UUID}
Carmelo Casconed6615072017-12-15 00:53:43 -080052
53# Remove vagrant shared folder
54vboxmanage sharedfolder remove ${VB_UUID} -name "vagrant"
55
Carmelo Cascone57defd32018-05-11 14:34:01 -070056rm -f onos-p4-${VM_TYPE}.ova
Carmelo Casconec5569c22018-04-18 10:09:15 +090057vboxmanage export ${VB_UUID} -o onos-p4-${VM_TYPE}.ova
Carmelo Cascone57defd32018-05-11 14:34:01 -070058
59sleep 1
60vboxmanage snapshot ${VB_UUID} restore pre-cleanup
61sleep 1
62vboxmanage snapshot ${VB_UUID} delete pre-cleanup