Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 20th, 2012  |  syntax: None  |  size: 5.34 KB  |  hits: 5  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/bin/bash
  2.  
  3. source "$(pwd)/secho.sh"
  4. source "$(pwd)/spinner.sh"
  5.  
  6. SELECTED_VM=""
  7.  
  8. DOT_FOLDER=~/.vmh
  9. CONF_FILE=~/.vmh/conf
  10. SELECTED_VM_FILE=~/.vmh/selected_vm
  11.  
  12. function connect() # Connects to the selected Virtual Machine, starting it if needed
  13. {
  14.         local STATE=$(_state)
  15.         local SLEEP=15
  16.  
  17.         if [ "$STATE" == 'running' ]; then
  18.                 echo "Starting a secure shell connection to $SELECTED_VM"
  19.                 shell
  20.         fi
  21.        
  22.         if [ "$STATE" != 'running' ]; then
  23.                 read -n1 -s -p "The Virtual machine $VM is not started, do you want to start it? (Y/n)" ANSWER
  24.                 case "$ANSWER" in
  25.                         "" | [yY] | [yY][Ee][Ss] )
  26.                                 start
  27.                                 echo "Starting a secure shell connection to $SELECTED_VM"
  28.                                 shell
  29.                         ;;
  30.  
  31.                         [nN] | [n|N][O|o] )
  32.                                 echo ""
  33.                                 echo "Exiting";
  34.                                 $(exit 1)
  35.                         ;;
  36.  
  37.                         * )
  38.                                 echo ""
  39.                                 echo "y/n"
  40.                                 $(exit 1)
  41.                         ;;
  42.                 esac
  43.         fi
  44.  
  45. }
  46.  
  47. function start() # Starts the selected Virtual Machine
  48. {
  49.         local STATE=$(_state)
  50.         local SLEEP=15
  51.  
  52.         if [ "$STATE" == 'running' ]; then
  53.                 echo "The VM $VM is already powered on!"
  54.         fi
  55.        
  56.         if [ "$STATE" != 'running' ]; then
  57.                 echo "Powering $VM on will take a few seconds"
  58.                 VBoxManage startvm $SELECTED_VM --type headless
  59.  
  60.                 start_spinner "Booting up..."  
  61.                 #trap "kill -9 $! > /dev/null 2>&1;trap 1 2 3 8 15" 1 2 3 8 15
  62.                 trap "kill -9 $! > /dev/null 2>&1;trap INT TERM KILL" INT TERM KILL
  63.                
  64.                 local READY=""
  65.                 local IP="value"       
  66.                 while [[ "$READY" != "succeeded" ]]
  67.                 do
  68.                         sleep 1
  69.                         IP=$(_ip)
  70.                         if [ "$IP" != "value" ]; then
  71.                                 READY=$(nc -zn $IP 22 | grep -o "succeeded")
  72.                         fi
  73.                 done
  74.                 stop_spinner $?
  75.                
  76.         fi
  77. }
  78.  
  79. function stop() # Stops the selected Virtual Machine
  80. {
  81.         local STATE=$(_state)
  82.         local SLEEP=15
  83.        
  84.         if [ "$STATE" != 'running' ]; then
  85.                 echo "The VM $SELECTED_VM is already powered off!"
  86.         fi
  87.  
  88.         if [ "$STATE" == 'running' ]; then
  89.                 echo "Powering $SELECTED_VM off will take a few seconds"
  90.                 VBoxManage controlvm $SELECTED_VM acpipowerbutton
  91.  
  92.                 #start_spinner "Shutting down..."
  93.                 #trap "kill -9 $! > /dev/null 2>&1;trap 1 2 3 8 15" 1 2 3 8 15
  94.                 trap "kill -9 $! > /dev/null 2>&1;trap INT TERM KILL" INT TERM KILL
  95.                
  96.                 local ELAPSED=0
  97.                 while [[ "$STATE" == 'running' ]]
  98.                 do
  99.                         sleep 2
  100.                         let ELAPSED+=2
  101.                         STATE=$(_state)
  102.                         if [ "$ELAPSED" -gt 15 ]; then 
  103.                                 VBoxManage controlvm $SELECTED_VM acpipowerbutton
  104.                                 ELAPSED=0
  105.                         fi
  106.                 done
  107.                 stop_spinner $?
  108.         fi
  109. }
  110.  
  111. function restart() # Restarts the selected Virtual Machine
  112. {
  113.         VBoxManage controlvm $SELECTED_VM reset
  114. }
  115.  
  116. function shell() # Establishes a secure shell connection to the selected Virtual Machine
  117. {
  118.         ssh $(_ip)
  119. }
  120.  
  121. function properties() # Enumerates properties for the selected Virtual Machine
  122. {
  123.         VBoxManage guestproperty enumerate $SELECTED_VM
  124. }
  125.  
  126. function status() # Shows the status for the selected Virtual Machine
  127. {
  128.         VBoxManage showvminfo $SELECTED_VM | grep State
  129. }
  130.  
  131. function list() # Lists Virtual Machines available for selection
  132. {
  133.         VBoxManage list vms | awk '{ print $1 }'
  134. }
  135.  
  136. function use() # Select a Virtual Machine from the available VMs
  137. {
  138.         VMS=($(list))
  139.  
  140.         if [ "$1" == "" ]; then
  141.                 secho "${#VMS[*]} Virtual machines found:" green bold
  142.                 for i in ${!VMS[*]}; do
  143.                         string="${VMS[$i]:1:${#VMS[$i]}-2}"
  144.                         echo "[$i] $string"
  145.                 done
  146.                 read -e -p "Which VM do you want to use? " ANSWER
  147.         else
  148.                 ANSWER=$1
  149.         fi
  150.  
  151.         # No answer
  152.         if [ "$ANSWER" == "" ]; then
  153.                 secho "You must select a VM, either by typing number or name..." red bold
  154.                 use
  155.         else
  156.  
  157.                 # $ANSWER is a digit
  158.                 if [[ $ANSWER = *[[:digit:]]* ]]; then
  159.                         VM=${VMS[$ANSWER]}
  160.                         VM="${VM:1:${#VM}-2}"
  161.                 else
  162.                         # Serach for $ANSWER in $VMS array
  163.                         for item in ${VMS[*]}; do
  164.                                 item="${item:1:${#item}-2}"
  165.                                 if [ "$item" == "$ANSWER" ]; then
  166.                                         VM=$ANSWER
  167.                                 fi
  168.                         done;
  169.                 fi
  170.  
  171.                 if [ "$VM" == "" ]; then
  172.                         secho "The Virtual Machine your selecting doesn't exist, please select another" red bold
  173.                         use
  174.                 else
  175.                         SELECTED_VM="$VM"
  176.                 fi
  177.         fi
  178.  
  179.         _select_vm
  180. }
  181.  
  182. function _help
  183. {
  184.         secho "Usage: " green bold
  185.         grep "^function" $0 \
  186.                 | grep -v "^function _" \
  187.                 | sed s/function/vmhelper/ \
  188.                 | sed s/\(\)/\  \<Virtual\ Machine\>\   /
  189. }
  190.  
  191. function _interactive
  192. {
  193.         actions="list use status properties restart start stop help _help"
  194.         interactive=0;
  195.         for action in ${actions[*]}; do
  196.                 if [ "$action" == "$1" ]; then
  197.                         interactive=1;
  198.                 fi
  199.         done;
  200.  
  201.         if [ "$interactive" -eq 1 ]; then
  202.                 echo ""
  203.                 message=$(secho "What else? [help] # " blue negative)
  204.                 read -e -p "$message " ANSWER
  205.                 echo ""
  206.                 _dispatch $ANSWER
  207.         fi
  208. }
  209.  
  210. function _dispatch
  211. {
  212.         if [ "$(type -t $1)" == "function" ]; then
  213.                 if [ "$1" == "help" ]; then
  214.                         _help
  215.                 else
  216.                         $(echo "$1")
  217.                 fi
  218.  
  219.                 _interactive $1
  220.                 exit
  221.         else
  222.                 if [ -n "$1" ]; then
  223.                         secho "Unrecognized command <$1> " red bold
  224.                 fi
  225.  
  226.                 _help
  227.                 _interactive "_help"
  228.                 exit
  229.         fi
  230. }
  231.  
  232. function _select_vm
  233. {
  234.         echo "SELECTED_VM=${SELECTED_VM}" > $SELECTED_VM_FILE
  235.         secho " ${SELECTED_VM} is selected " black bold cyan
  236. }
  237.  
  238. function _state
  239. {
  240.         VBoxManage showvminfo $SELECTED_VM | grep State | awk '{ print $2 }' | egrep -v ^'\('
  241. }
  242.  
  243. function _ip
  244. {
  245.         VBoxManage guestproperty get $SELECTED_VM "/VirtualBox/GuestInfo/Net/0/V4/IP" | awk '{ print $2 }'
  246. }
  247.  
  248. function _dot_files
  249. {
  250.         if [ ! -d $DOT_FOLDER ]; then
  251.                 mkdir $DOT_FOLDER
  252.         fi
  253.  
  254.         if [ -f $CONF_FILE ]; then
  255.                 source $CONF_FILE
  256.         else
  257.                 touch $CONF_FILE
  258.         fi
  259.  
  260.         if [ -f $SELECTED_VM_FILE ]; then
  261.                 source $SELECTED_VM_FILE
  262.         else
  263.                 touch $SELECTED_VM_FILE
  264.         fi
  265. }
  266.  
  267. _dot_files
  268.  
  269. # Check for selected Virtual Machine
  270. if [ -n "$2" ]; then
  271.         use $2
  272.         if [ "$1" == "use" ]; then
  273.                 exit
  274.         fi
  275. else
  276.         if [ "$SELECTED_VM" == "" ]; then
  277.                 use
  278.         fi
  279. fi
  280.  
  281. _select_vm
  282. _dispatch $1