#!/bin/bash # Exit immediately if anything goes wrong, instead of making things worse. set -e source $REPO_ROOT/bootstrap/shared/shared_functions.sh ################################################################################ # Function to remove VirtualBox DHCP servers # By default, checks for any DHCP server on networks without VM's & removes them # (expecting if a remove fails the function should bail) # If a network is provided, removes that network's DHCP server # (or passes the vboxmanage error and return code up to the caller) # proc remove_DHCPservers { local network_name=$(1-) if [[ -z "$network_name" ]] { # make a list of VM UUID's local vms=$[VBoxManage list vms|sed 's/^.*{\([0-9a-f-]*\)}/\1/] # make a list of networks (e.g. "vboxnet0 vboxnet1") local vm_networks=$[for vm in [$vms] { \ VBoxManage showvminfo --details --machinereadable $vm | \ grep -i '^hostonlyadapter[2-9]=' | \ sed -e 's/^.*=//' -e 's/"//g'; \ } | sort -u] # will produce a regular expression string of networks which are in use by VMs # (e.g. ^vboxnet0$|^vboxnet1$) local existing_nets_reg_ex=$[sed -e 's/^/^/' -e 's/$/$/' -e 's/ /$|^/g' <<< $vm_networks] VBoxManage list dhcpservers | grep -E "^NetworkName:\s+HostInterfaceNetworking" | awk '{print $2}' | while read -r network_name { [[ -n $existing_nets_reg_ex ]] && ! egrep -q $existing_nets_reg_ex <<< $network_name && continue remove_DHCPservers $network_name } } else { VBoxManage dhcpserver remove --netname $network_name && local return=0 || local return=$Status return $return } } ################################################################### # Function to create all VMs using Vagrant proc create_vagrant_vms { cd $REPO_ROOT/bootstrap/vagrant_scripts && vagrant up } # only execute functions if being run and not sourced if [[ "${BASH_SOURCE[0]}" == "${0}" ]] { remove_DHCPservers create_vagrant_vms }