- #!/bin/bash
- source "$(pwd)/secho.sh"
- source "$(pwd)/spinner.sh"
- SELECTED_VM=""
- DOT_FOLDER=~/.vmh
- CONF_FILE=~/.vmh/conf
- SELECTED_VM_FILE=~/.vmh/selected_vm
- function connect() # Connects to the selected Virtual Machine, starting it if needed
- {
- local STATE=$(_state)
- local SLEEP=15
- if [ "$STATE" == 'running' ]; then
- echo "Starting a secure shell connection to $SELECTED_VM"
- shell
- fi
- if [ "$STATE" != 'running' ]; then
- read -n1 -s -p "The Virtual machine $VM is not started, do you want to start it? (Y/n)" ANSWER
- case "$ANSWER" in
- "" | [yY] | [yY][Ee][Ss] )
- start
- echo "Starting a secure shell connection to $SELECTED_VM"
- shell
- ;;
- [nN] | [n|N][O|o] )
- echo ""
- echo "Exiting";
- $(exit 1)
- ;;
- * )
- echo ""
- echo "y/n"
- $(exit 1)
- ;;
- esac
- fi
- }
- function start() # Starts the selected Virtual Machine
- {
- local STATE=$(_state)
- local SLEEP=15
- if [ "$STATE" == 'running' ]; then
- echo "The VM $VM is already powered on!"
- fi
- if [ "$STATE" != 'running' ]; then
- echo "Powering $VM on will take a few seconds"
- VBoxManage startvm $SELECTED_VM --type headless
- start_spinner "Booting up..."
- #trap "kill -9 $! > /dev/null 2>&1;trap 1 2 3 8 15" 1 2 3 8 15
- trap "kill -9 $! > /dev/null 2>&1;trap INT TERM KILL" INT TERM KILL
- local READY=""
- local IP="value"
- while [[ "$READY" != "succeeded" ]]
- do
- sleep 1
- IP=$(_ip)
- if [ "$IP" != "value" ]; then
- READY=$(nc -zn $IP 22 | grep -o "succeeded")
- fi
- done
- stop_spinner $?
- fi
- }
- function stop() # Stops the selected Virtual Machine
- {
- local STATE=$(_state)
- local SLEEP=15
- if [ "$STATE" != 'running' ]; then
- echo "The VM $SELECTED_VM is already powered off!"
- fi
- if [ "$STATE" == 'running' ]; then
- echo "Powering $SELECTED_VM off will take a few seconds"
- VBoxManage controlvm $SELECTED_VM acpipowerbutton
- #start_spinner "Shutting down..."
- #trap "kill -9 $! > /dev/null 2>&1;trap 1 2 3 8 15" 1 2 3 8 15
- trap "kill -9 $! > /dev/null 2>&1;trap INT TERM KILL" INT TERM KILL
- local ELAPSED=0
- while [[ "$STATE" == 'running' ]]
- do
- sleep 2
- let ELAPSED+=2
- STATE=$(_state)
- if [ "$ELAPSED" -gt 15 ]; then
- VBoxManage controlvm $SELECTED_VM acpipowerbutton
- ELAPSED=0
- fi
- done
- stop_spinner $?
- fi
- }
- function restart() # Restarts the selected Virtual Machine
- {
- VBoxManage controlvm $SELECTED_VM reset
- }
- function shell() # Establishes a secure shell connection to the selected Virtual Machine
- {
- ssh $(_ip)
- }
- function properties() # Enumerates properties for the selected Virtual Machine
- {
- VBoxManage guestproperty enumerate $SELECTED_VM
- }
- function status() # Shows the status for the selected Virtual Machine
- {
- VBoxManage showvminfo $SELECTED_VM | grep State
- }
- function list() # Lists Virtual Machines available for selection
- {
- VBoxManage list vms | awk '{ print $1 }'
- }
- function use() # Select a Virtual Machine from the available VMs
- {
- VMS=($(list))
- if [ "$1" == "" ]; then
- secho "${#VMS[*]} Virtual machines found:" green bold
- for i in ${!VMS[*]}; do
- string="${VMS[$i]:1:${#VMS[$i]}-2}"
- echo "[$i] $string"
- done
- read -e -p "Which VM do you want to use? " ANSWER
- else
- ANSWER=$1
- fi
- # No answer
- if [ "$ANSWER" == "" ]; then
- secho "You must select a VM, either by typing number or name..." red bold
- use
- else
- # $ANSWER is a digit
- if [[ $ANSWER = *[[:digit:]]* ]]; then
- VM=${VMS[$ANSWER]}
- VM="${VM:1:${#VM}-2}"
- else
- # Serach for $ANSWER in $VMS array
- for item in ${VMS[*]}; do
- item="${item:1:${#item}-2}"
- if [ "$item" == "$ANSWER" ]; then
- VM=$ANSWER
- fi
- done;
- fi
- if [ "$VM" == "" ]; then
- secho "The Virtual Machine your selecting doesn't exist, please select another" red bold
- use
- else
- SELECTED_VM="$VM"
- fi
- fi
- _select_vm
- }
- function _help
- {
- secho "Usage: " green bold
- grep "^function" $0 \
- | grep -v "^function _" \
- | sed s/function/vmhelper/ \
- | sed s/\(\)/\ \<Virtual\ Machine\>\ /
- }
- function _interactive
- {
- actions="list use status properties restart start stop help _help"
- interactive=0;
- for action in ${actions[*]}; do
- if [ "$action" == "$1" ]; then
- interactive=1;
- fi
- done;
- if [ "$interactive" -eq 1 ]; then
- echo ""
- message=$(secho "What else? [help] # " blue negative)
- read -e -p "$message " ANSWER
- echo ""
- _dispatch $ANSWER
- fi
- }
- function _dispatch
- {
- if [ "$(type -t $1)" == "function" ]; then
- if [ "$1" == "help" ]; then
- _help
- else
- $(echo "$1")
- fi
- _interactive $1
- exit
- else
- if [ -n "$1" ]; then
- secho "Unrecognized command <$1> " red bold
- fi
- _help
- _interactive "_help"
- exit
- fi
- }
- function _select_vm
- {
- echo "SELECTED_VM=${SELECTED_VM}" > $SELECTED_VM_FILE
- secho " ${SELECTED_VM} is selected " black bold cyan
- }
- function _state
- {
- VBoxManage showvminfo $SELECTED_VM | grep State | awk '{ print $2 }' | egrep -v ^'\('
- }
- function _ip
- {
- VBoxManage guestproperty get $SELECTED_VM "/VirtualBox/GuestInfo/Net/0/V4/IP" | awk '{ print $2 }'
- }
- function _dot_files
- {
- if [ ! -d $DOT_FOLDER ]; then
- mkdir $DOT_FOLDER
- fi
- if [ -f $CONF_FILE ]; then
- source $CONF_FILE
- else
- touch $CONF_FILE
- fi
- if [ -f $SELECTED_VM_FILE ]; then
- source $SELECTED_VM_FILE
- else
- touch $SELECTED_VM_FILE
- fi
- }
- _dot_files
- # Check for selected Virtual Machine
- if [ -n "$2" ]; then
- use $2
- if [ "$1" == "use" ]; then
- exit
- fi
- else
- if [ "$SELECTED_VM" == "" ]; then
- use
- fi
- fi
- _select_vm
- _dispatch $1