Carmelo Cascone | d661507 | 2017-12-15 00:53:43 -0800 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | set -xe |
| 4 | |
Carmelo Cascone | ada7b5b | 2019-04-23 13:50:03 -0700 | [diff] [blame] | 5 | VM_TYPE=${1:-dev} |
| 6 | USE_STRATUM=${USE_STRATUM:-false} |
| 7 | STRATUM_BMV2_TAR=${STRATUM_BMV2_TAR-unknown} |
| 8 | |
| 9 | DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" |
Carmelo Cascone | c5569c2 | 2018-04-18 10:09:15 +0900 | [diff] [blame] | 10 | |
Carmelo Cascone | 57defd3 | 2018-05-11 14:34:01 -0700 | [diff] [blame] | 11 | function wait_vm_shutdown { |
| 12 | set +x |
Carmelo Cascone | ef71549 | 2018-05-14 19:48:46 -0700 | [diff] [blame] | 13 | while vboxmanage showvminfo $1 | grep -c "running (since"; do |
Carmelo Cascone | 57defd3 | 2018-05-11 14:34:01 -0700 | [diff] [blame] | 14 | echo "Waiting for VM to shutdown..." |
| 15 | sleep 1 |
| 16 | done |
| 17 | sleep 2 |
| 18 | set -x |
| 19 | } |
| 20 | |
Carmelo Cascone | ef71549 | 2018-05-14 19:48:46 -0700 | [diff] [blame] | 21 | function wait_for_tcp_port { |
| 22 | set +x |
| 23 | while ! nc -z $1 $2; do |
| 24 | echo "Waiting for TCP port $2 on $1 to be open..." |
| 25 | sleep 1 |
| 26 | done |
| 27 | sleep 2 |
| 28 | set -x |
| 29 | } |
| 30 | |
Carmelo Cascone | ada7b5b | 2019-04-23 13:50:03 -0700 | [diff] [blame] | 31 | rm -rf ./tmp |
| 32 | if [[ ${VM_TYPE} = "tutorial" ]] |
| 33 | then |
| 34 | bazel build //:onos |
| 35 | bazel build //:onos-package-admin |
| 36 | rm -rf ~/.m2/repository/org/onosproject |
| 37 | cd ${ONOS_ROOT} |
| 38 | onos-publish -l |
| 39 | cd ${DIR} |
| 40 | mkdir -p ./tmp |
| 41 | cp ../../../bazel-bin/onos.tar.gz ./tmp/onos.tar.gz |
| 42 | cp ../../../bazel-bin/onos-admin.tar.gz ./tmp/onos-admin.tar.gz |
| 43 | cp ../mininet/bmv2.py ./tmp/bmv2.py |
| 44 | mv ~/.m2/repository/org/onosproject ./tmp/artifacts |
| 45 | if [[ ${USE_STRATUM} = true ]] |
| 46 | then |
| 47 | cp ${STRATUM_BMV2_TAR} ./tmp/stratum_bmv2.tar.gz |
| 48 | fi |
| 49 | fi |
Carmelo Cascone | d661507 | 2017-12-15 00:53:43 -0800 | [diff] [blame] | 50 | |
Carmelo Cascone | ada7b5b | 2019-04-23 13:50:03 -0700 | [diff] [blame] | 51 | # Initial provisioning if necessary. |
| 52 | USE_STRATUM=${USE_STRATUM} vagrant up ${VM_TYPE} |
Carmelo Cascone | d661507 | 2017-12-15 00:53:43 -0800 | [diff] [blame] | 53 | |
Carmelo Cascone | ada7b5b | 2019-04-23 13:50:03 -0700 | [diff] [blame] | 54 | rm -rf ./tmp |
| 55 | |
| 56 | SSH_PORT=`vagrant port --guest 22 ${VM_TYPE}` |
| 57 | VB_UUID=`cat .vagrant/machines/${VM_TYPE}/virtualbox/id` |
| 58 | |
| 59 | if [[ ${VM_TYPE} = "dev" ]] |
| 60 | then |
| 61 | # Take snapshot before cleanup for local use |
| 62 | # e.g. to avoid re-building P4 tools from scratch |
| 63 | vboxmanage controlvm ${VB_UUID} acpipowerbutton |
| 64 | wait_vm_shutdown ${VB_UUID} |
| 65 | VBoxManage snapshot ${VB_UUID} take "pre-cleanup" |
| 66 | vagrant up ${VM_TYPE} |
| 67 | # SSH port forwarding might change after vagrant up. |
| 68 | SSH_PORT=`vagrant port --guest 22 ${VM_TYPE}` |
| 69 | wait_for_tcp_port 127.0.0.1 ${SSH_PORT} |
| 70 | fi |
Carmelo Cascone | 57defd3 | 2018-05-11 14:34:01 -0700 | [diff] [blame] | 71 | |
| 72 | # Cleanup |
cmo | 4ab984b | 2019-06-14 16:06:12 +0800 | [diff] [blame] | 73 | vagrant ssh -c 'bash /vagrant/ova-cleanup.sh' ${VM_TYPE} |
Carmelo Cascone | d661507 | 2017-12-15 00:53:43 -0800 | [diff] [blame] | 74 | sleep 5 |
Carmelo Cascone | 4f985cd | 2018-02-11 17:36:42 -0800 | [diff] [blame] | 75 | vboxmanage controlvm ${VB_UUID} acpipowerbutton |
Carmelo Cascone | 57defd3 | 2018-05-11 14:34:01 -0700 | [diff] [blame] | 76 | wait_vm_shutdown ${VB_UUID} |
Carmelo Cascone | d661507 | 2017-12-15 00:53:43 -0800 | [diff] [blame] | 77 | |
| 78 | # Remove vagrant shared folder |
| 79 | vboxmanage sharedfolder remove ${VB_UUID} -name "vagrant" |
| 80 | |
Carmelo Cascone | 57defd3 | 2018-05-11 14:34:01 -0700 | [diff] [blame] | 81 | rm -f onos-p4-${VM_TYPE}.ova |
Carmelo Cascone | c5569c2 | 2018-04-18 10:09:15 +0900 | [diff] [blame] | 82 | vboxmanage export ${VB_UUID} -o onos-p4-${VM_TYPE}.ova |
Carmelo Cascone | 57defd3 | 2018-05-11 14:34:01 -0700 | [diff] [blame] | 83 | |
Carmelo Cascone | ada7b5b | 2019-04-23 13:50:03 -0700 | [diff] [blame] | 84 | if [[ ${VM_TYPE} = "dev" ]] |
| 85 | then |
| 86 | sleep 1 |
| 87 | vboxmanage snapshot ${VB_UUID} restore pre-cleanup |
| 88 | sleep 1 |
| 89 | vboxmanage snapshot ${VB_UUID} delete pre-cleanup |
| 90 | fi |