]> gitweb.fluxo.info Git - vbox.git/commitdiff
Refactor and exec action
authorSilvio Rhatto <rhatto@riseup.net>
Fri, 21 Aug 2015 01:50:18 +0000 (22:50 -0300)
committerSilvio Rhatto <rhatto@riseup.net>
Fri, 21 Aug 2015 01:50:18 +0000 (22:50 -0300)
vbox

diff --git a/vbox b/vbox
index 6b6935a1bc3464183df93c1acc654dbaa5febb70..1a82600339a620fbc3ddae16446b33e1eeaf4b0b 100755 (executable)
--- a/vbox
+++ b/vbox
@@ -14,48 +14,52 @@ function usage {
   exit 1
 }
 
-# TODO
 # Execute commands inside a virtual machine
-#function vbox_exec {
-#}
+function vbox_exec {
+  local bin="$1"
+  shift
+
+  local command="guestcontrol"
+  local options="execute --username vagrant --password vagrant --image $bin"
+        options="$options --verbose --wait-exit --wait-stdout --wait-stderr -- $*"
+
+  vbox_run $command $options
+}
+
+# VBoxManage wrapper
+function vbox_run {
+  local command="$1"
+  shift
+
+  VBoxManage $command $VM $*
+}
 
 # Build options
 if [ ! -z "$VM" ]; then
   if [ "$COMMAND" == "up" ]; then
-    OPTIONS=""
-    COMMAND="startvm"
+    vbox_run startvm
   elif [ "$COMMAND" == "down" ]; then
-    OPTIONS="savestate"
-    COMMAND="controlvm"
+    vbox_run controlvm savestate
   elif [ "$COMMAND" == "halt" ]; then
-    OPTIONS="poweroff"
-    COMMAND="controlvm"
+    vbox_run controlvm poweroff
   elif [ "$COMMAND" == "upgrade" ]; then
-    OPTIONS="execute --username vagrant --password vagrant --image /home/vagrant/apps/hydra/hydractl"
-    OPTIONS="$OPTIONS --verbose --wait-exit --wait-stdout --wait-stderr -- upgrade clean"
-    COMMAND="guestcontrol"
-  # TODO
-  #elif [ "$COMMAND" == "zerofree" ]; then
-  #  vbox_exec $VM apt-get install zerofree
-  #  vbox_exec $VM apt-get clean
-  #  vbox_exec $VM telinit 1
-  #  vbox_exec $VM mount -o remount,ro /
-  #  vbox_exec $VM zerofree /dev/sda1
-  #  vbox_exec $VM halt
-  #  VBoxManage modifyhd --compact /var/cache/virtualbox/$box/$box.vdi
-  #  exit
-  #elif [ "$COMMAND" == "zerofree" ]; then
-  #  shift
-  #  vbox_exec $*
+    vbox_exec /home/vagrant/apps/hydra/hydractl upgrade clean
+  elif [ "$COMMAND" == "zerofree" ]; then
+    vbox_exec /usr/bin/sudo /usr/bin/apt-get install zerofree
+    vbox_exec /usr/bin/sudo /usr/bin/apt-get clean
+    vbox_exec /usr/bin/sudo /sbin/telinit 1
+    vbox_exec /bin/mount -o remount,ro /
+    vbox_exec /usr/sbin/zerofree /dev/sda1
+    vbox_exec /sbin/halt
+    VBoxManage modifyhd --compact /var/cache/virtualbox/$box/$box.vdi
+  elif [ "$COMMAND" == "exec" ]; then
+    shift 2
+    vbox_run $*
   else
     usage
   fi
 elif [ "$COMMAND" == "status" ]; then
-  OPTIONS="runningvms"
-  COMMAND="list"
+  vbox_run runningvms list
 else
   usage
 fi
-
-# Dispatch
-VBoxManage $COMMAND $VM $OPTIONS