Skip to content

Instantly share code, notes, and snippets.

@mintindeed
Created August 14, 2013 01:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mintindeed/6227381 to your computer and use it in GitHub Desktop.
Save mintindeed/6227381 to your computer and use it in GitHub Desktop.
Simple bash script for managing a VirtualHost VM for local web development.
#! /bin/bash
ETHERNET="en0"
WIRELESS="en1"
NGINX_CONFIGS_PATH=""
VRDEPORT=3389
VRDEAUTHTYPE="external"
VRDEENABLED="off"
if [ -z $2 ]; then
VMNAME="dev-local"
else
VMNAME=$2
fi
#
# "start"
#
if [ $1 = "start" ]; then
# Step 0) If VM is currently running, stop it and start it again. I've
# probably changed networks and forgotten it's running, so the bridge needs
# to be reset. I could probably be a little smarter here and check if the
# host IP address has changed and if the bridged connection has changed
# before just killing the current running VM, but for now it doesn't matter
# -- I'll fix that if it becomes an issue.
if [ "running" = `VBoxManage showvminfo "$VMNAME" | grep State | tr -s " " | cut -d" " -f2` ]; then
# Turn off guest
echo "Powering off current instance of \"$VMNAME\"..."
VBoxManage controlvm "$VMNAME" acpipowerbutton
fi
# Step 1) Figure out whether we're running on wireless or ethernet
# Ethernet -- check first, since if we have ethernet AND wireless, I want
# ethernet to take priority
if [ 'active' = `ifconfig $ETHERNET | grep status: | tr -s " " | cut -d' ' -f2` ]; then
echo "Bridging Ethernet connection on $ETHERNET"
INTERFACE=$ETHERNET
# Wireless
elif [ 'active' = `ifconfig $WIRELESS | grep status: | tr -s " " | cut -d' ' -f2` ]; then
echo "Bridging Wireless connection on $WIRELESS"
INTERFACE=$WIRELESS
else
echo "Could not find active interface. Bridging will not be enabled. Using hostonly adapter."
fi
# Step 2) Modify vbox to use the appropriate adaptor
# Set up VM to use bridged adaptor (if available) else hostonly interface
if [ -z $INTERFACE ]; then
VBoxManage modifyvm "$VMNAME" --nic1 hostonly --hostonlyadapter1 "vboxnet0"
else
# Get bridge adaptor name
INTERFACE_NAME="${INTERFACE}:`VBoxManage list bridgedifs | grep -v VBoxNetworkName | grep $INTERFACE | cut -d':' -f3`"
VBoxManage modifyvm "$VMNAME" --nic1 bridged --bridgeadapter1 "$INTERFACE_NAME"
fi
# Step 3) start the virtual machine
# Ensure VRDE is set up
VBoxManage modifyvm "$VMNAME" --vrdeport $VRDEPORT --vrdeauthtype $VRDEAUTHTYPE
# Start guest
echo "Starting VM"
VBoxHeadless --startvm "$VMNAME" --vrde $VRDEENABLED &
sleep 5 # Wait a second for the VM to catch up
# Step 4) Get the IP address of the virtual machine
# Get guest IP address
echo -ne "Getting the guest IP address"
# Give the VM a little time to start up, then poll for its IP address
COUNTER=0
IP_FOUND=0
until [ $IP_FOUND -gt 0 ]; do
echo -ne "."
sleep 2
let COUNTER+=1
if [ $COUNTER -gt 10 ]; then
GUEST_IP=`VBoxManage guestproperty enumerate "$VMNAME" | grep "V4/IP" | cut -d"," -f2 | cut -d":" -f2 | tr -d " "`
if [ "$GUEST_IP" != "" ]; then
let IP_FOUND+=1
fi
elif [ $COUNTER -gt 30 ]; then
echo "It's taking too long to get the guest's IP address."
echo -e "Do you want to wait a little longer? [Y/n]"
read WAIT
if [ "$WAIT" == "Y" ] || [ "$WAIT" == "y" ]; then
echo "Continuing to look for guest IP address..."
echo "If this script consistently asks \"Do you want to wait a little longer?\" you may consider editing it and increasing the polling interval: $0"
let COUNTER=0
else
echo "Please manually check on the virtual machine."
VBoxManage showvminfo "$VMNAME" | grep State
exit 2
fi
fi
done
echo ""
echo "\"$VMNAME\" is running with the IP address: $GUEST_IP"
# Step 5) Update hosts file with current IP address
# Is this a fresh hosts file (one that we haven't modified before) or one that's been previously modified that we need to update?
echo "Updating /etc/hosts using 'sudo', you will be asked for your password:"
HOSTS="\n# \"$VMNAME\" BEGIN\n"
while read DOMAIN
do
HOSTS=$HOSTS"$GUEST_IP $DOMAIN\n"
done < ~/.vm-sites
HOSTS=$HOSTS"# \"$VMNAME\" END"
HOSTS_ENTRY=`grep "$VMNAME" /etc/hosts | wc -l | tr -s " "`
if [ $HOSTS_ENTRY -gt 1 ]; then
sudo sed -ni.bak '1h;1!H;${;g;s/# "$VMNAME" BEGIN\n.*\n# "$VMNAME" END/$HOSTS/g;p;}' /etc/hosts
else
echo -e $HOSTS | sudo tee -a /etc/hosts
fi
#
# "stop"
#
elif [ $1 = "stop" ]; then
# Turn off guest
if [ "powered" != `VBoxManage showvminfo "$VMNAME" | grep State | tr -s " " | cut -d" " -f2` ]; then
echo "Powering off current instance of \"$VMNAME\"..."
VBoxManage controlvm "$VMNAME" acpipowerbutton && echo "Done!"
else
echo "\"$VMNAME\" is not currently running."
fi
#
# "ip"
#
elif [ $1 = "ip" ]; then
if [ "powered" != `VBoxManage showvminfo "$VMNAME" | grep State | tr -s " " | cut -d" " -f2` ]; then
GUEST_IP=`VBoxManage guestproperty enumerate "$VMNAME" | grep "V4/IP" | cut -d"," -f2 | cut -d":" -f2 | tr -d " "`
if [ -z $GUEST_IP ]; then
echo "No IP address for \"$VMNAME\". You should probably restart the machine."
else
echo $GUEST_IP
fi
else
echo "\"$VMNAME\" is not currently running."
fi
#
# default / command not found
#
else
echo "Available commands:"
echo "start | stop | ip"
fi
# If there are more than 1 VMs running then only add a hosts entry for the current VM
#RUNNING_VMS=`VBoxManage list runningvms | wc -l`
#if [ $RUNNING_VMS -gt 1 ]; then
# echo "$RUNNING_VMS VMs are running"
#elif [ $RUNNING_VMS -eq 1 ]; then
# echo "1 VM is running"
#fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment