Make it so that we can run setup-kudu-demo-vm.sh multiple times

Previously, running it multiple times would result in a few problems:
* Repeated creation of new host-only networks
* Failure to re-import the VM
* Stale IP of the VM left in /etc/hosts


Project: http://git-wip-us.apache.org/repos/asf/kudu/repo
Commit: http://git-wip-us.apache.org/repos/asf/kudu/commit/e8065efc
Tree: http://git-wip-us.apache.org/repos/asf/kudu/tree/e8065efc
Diff: http://git-wip-us.apache.org/repos/asf/kudu/diff/e8065efc

Branch: refs/heads/master
Commit: e8065efc54e29c4c34cff77d08344c5bb7c8959a
Parents: e4fc37a
Author: Mike Percy <[email protected]>
Authored: Sun Sep 27 21:07:10 2015 -0400
Committer: Mike Percy <[email protected]>
Committed: Sun Sep 27 21:36:39 2015 -0400

----------------------------------------------------------------------
 demo-vm-setup/setup-kudu-demo-vm.sh | 56 +++++++++++++++++---------------
 1 file changed, 29 insertions(+), 27 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/kudu/blob/e8065efc/demo-vm-setup/setup-kudu-demo-vm.sh
----------------------------------------------------------------------
diff --git a/demo-vm-setup/setup-kudu-demo-vm.sh 
b/demo-vm-setup/setup-kudu-demo-vm.sh
index aacff92..540581d 100755
--- a/demo-vm-setup/setup-kudu-demo-vm.sh
+++ b/demo-vm-setup/setup-kudu-demo-vm.sh
@@ -31,29 +31,33 @@ else
   curl -O ${VIRTUALBOX_URL}
 fi
 
-# Create a host only network interface
-VBoxManage hostonlyif create
+# Set up the VM for the first time if it doesn't already exist.
+if ! VBoxManage list vms | grep -q '"kudu-demo"'; then
+  # Create a host only network interface
+  VBoxManage hostonlyif create
 
-# Find the last one created
-last_if=`VBoxManage list -l hostonlyifs | grep "^Name:" | tail -n 1 | tr " " 
"\n" | tail -n 1`
-host_ip=`VBoxManage list -l hostonlyifs | grep "^IPAddress:" | tail -n 1 | tr 
" " "\n" | tail -n 1`
+  # Find the last one created
+  last_if=`VBoxManage list -l hostonlyifs | grep "^Name:" | tail -n 1 | tr " " 
"\n" | tail -n 1`
+  host_ip=`VBoxManage list -l hostonlyifs | grep "^IPAddress:" | tail -n 1 | 
tr " " "\n" | tail -n 1`
 
-lower_ip=`echo $host_ip | sed 
's/\([0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\)\.[0-9]\{1,3\}/\1/g'`
+  lower_ip=`echo $host_ip | sed 
's/\([0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\)\.[0-9]\{1,3\}/\1/g'`
 
-VBoxManage hostonlyif ipconfig $last_if --ip $host_ip
-VBoxManage dhcpserver add --ifname $last_if --ip $host_ip --netmask 
255.255.255.0 --lowerip $lower_ip.100 --upperip $lower_ip.200 || :
-VBoxManage dhcpserver modify --ifname $last_if --enable
+  VBoxManage hostonlyif ipconfig $last_if --ip $host_ip
+  VBoxManage dhcpserver add --ifname $last_if --ip $host_ip --netmask 
255.255.255.0 --lowerip $lower_ip.100 --upperip $lower_ip.200 || :
+  VBoxManage dhcpserver modify --ifname $last_if --enable
 
-# Import the ovf
-VBoxManage import ${OVF} --vsys 0 --cpus ${VM_NUM_CPUS} --memory ${VM_MEM_MB} 
--vmname ${VM_NAME} --options keepallmacs
-VBoxManage modifyvm ${VM_NAME} --nic1 hostonly
-VBoxManage modifyvm ${VM_NAME} --hostonlyadapter1 $last_if
-VBoxManage modifyvm ${VM_NAME} --nic2 nat
+  # Import the ovf
+  echo "Importing VM ${OVF}..."
+  VBoxManage import ${OVF} --vsys 0 --cpus ${VM_NUM_CPUS} --memory 
${VM_MEM_MB} --vmname ${VM_NAME} --options keepallmacs
+  VBoxManage modifyvm ${VM_NAME} --nic1 hostonly
+  VBoxManage modifyvm ${VM_NAME} --hostonlyadapter1 $last_if
+  VBoxManage modifyvm ${VM_NAME} --nic2 nat
 
-# Create a shared folder with the current checkout available to the VM
-REL_PATH=`pwd`/../
-SHARED_FOLDER_PATH=`dir_resolve $REL_PATH`
-VBoxManage sharedfolder add ${VM_NAME} --name examples --hostpath 
$SHARED_FOLDER_PATH --automount
+  # Create a shared folder with the current checkout available to the VM
+  REL_PATH=`pwd`/../
+  SHARED_FOLDER_PATH=`dir_resolve $REL_PATH`
+  VBoxManage sharedfolder add ${VM_NAME} --name examples --hostpath 
$SHARED_FOLDER_PATH --automount
+fi
 
 # Start the VM
 VBoxManage startvm ${VM_NAME}
@@ -72,20 +76,18 @@ while true; do
     sleep 5
 done
 
-if ! grep -q quickstart.cloudera /etc/hosts ; then
 echo "Updating the /etc/hosts file requires sudo rights."
-sudo bash -e -c 'echo "#Kudu Quickstart VM" >> /etc/hosts'
-sudo bash -c "echo $ip quickstart.cloudera >> /etc/hosts"
-else
-echo "Hostname setup already done, check if the IP address of the VM"
-echo "matches the hosts entry."
-echo "IP VM: $ip"
-cat /etc/hosts
+if grep -q quickstart.cloudera /etc/hosts ; then
+  # Strip out the old entry since it's probably wrong now.
+  grep -v quickstart.cloudera /etc/hosts > ./etc-hosts.new
+  sudo mv ./etc-hosts.new /etc/hosts
 fi
+sudo bash -c "echo '$ip quickstart.cloudera # Kudu quickstart VM' >> 
/etc/hosts"
 
 echo 
"========================================================================="
 echo "Kudu Quickstart VM installed successfully"
 echo "To use the C++ and Python examples from this repository, you have to SSH"
-echo "to the VM using the user 'demo' with the password 'demo'. "
+echo "to the VM using the user 'demo' with the password 'demo'."
+echo "You can use this command: ssh [email protected]"
 echo ""
 echo "You'll find the examples mounted as a shared folder at 
/media/sf_examples"

Reply via email to