diff --git a/mocha-suv/common/libexec/mocha-suv b/mocha-suv/common/libexec/mocha-suv index 360a738..2f40902 100755 --- a/mocha-suv/common/libexec/mocha-suv +++ b/mocha-suv/common/libexec/mocha-suv @@ -6,12 +6,140 @@ # mocha suv new USE_VAGRANT="false" -VAGRANT_MOCHADIR=/home/beckermj/Documents/Vagrant/mocha +VAGRANT_MOCHADIR=~/Documents/Vagrant/mocha VAGRANT_MACHINESDIR=$VAGRANT_MOCHADIR/machines +VAGRANT_VMS="" +if [ -d $VAGRANT_MACHINESDIR ]; then + VAGRANT_VMS=$(ls $VAGRANT_MACHINESDIR) +fi + +VBOX_VMS=$(VBoxManage list vms | grep 'mochasuv-') +LXC_VMS=$(lxc list -c n -f csv | grep i-0) +# DOCKER_VMS=$(docker container ls --filter network=mocha-docker --format "{{.Names}}/") + +DOCKER_VMS_STOPPED=$(docker container ls --filter network=mocha-docker --filter status=exited --format "{{.Names}}") +DOCKER_VMS_RUNNING=$(docker container ls --filter network=mocha-docker --filter status=running --format "{{.Names}}") + if [ "$1" == "list" ]; then - ls $VAGRANT_MACHINESDIR + # first list all the Vagrant machines (ancient, unsupported... but for backwards compatibility) + if [ "$VAGRANT_VMS" != "" ]; then + echo $VAGRANT_VMS + fi + # then list all the LXC machines + if [ "$LXC_VMS" != "" ]; then + echo $LXC_VMS + fi + # finally, list all the Docker machines + if [ "$DOCKER_VMS_RUNNING" != "" ]; then + echo $DOCKER_VMS_RUNNING + fi + if [ "$DOCKER_VMS_STOPPED" != "" ]; then + echo $DOCKER_VMS_STOPPED + fi + +elif [ "$1" == "status" ]; then + + FORMAT="table" + if [ "$2" == "-f" ]; then + FORMAT=$3 + shift; shift + fi + + if [ "$2" == "" ]; then + + if [ "$FORMAT" == "table" ]; then + echo "+----------------------------+-----------+---------+" + echo "| NAME | TYPE | STATUS |" + echo "+----------------------------+-----------+---------+" + fi + + for name in $(mocha suv list); do + + mocha suv status -f $FORMAT $name + + if [ "$FORMAT" == "table" ]; then + echo "+----------------------------+-----------+---------+" + fi + + done + + # if [ "$FORMAT" == "table" ]; then + # echo "+----------------------------+--------+---------+" + # fi + + else + + EXISTS_VAGRANT=$(echo "$VAGRANT_VMS" | grep $2) + EXISTS_VBOX=$(echo "$VBOX_VMS" | grep $2) + EXISTS_LXC=$(echo "$LXC_VMS" | grep $2) + + EXISTS_DOCKER_STOPPED=$(echo "$DOCKER_VMS_STOPPED" | grep $2) + EXISTS_DOCKER_RUNNING=$(echo "$DOCKER_VMS_RUNNING" | grep $2) + + if [ "$EXISTS_VAGRANT" != "" ]; then + + if [ "$FORMAT" == "csv" ]; then + + echo "$2,vagrant,unknown" + + else + + echo "| $2 | vagrant | unknown |" + fi + + elif [ "$EXISTS_VBOX" != "" ]; then + + if [ "$FORMAT" == "csv" ]; then + + echo "$2,vbox,unknown" + + else + + echo "| $2 | vbox | unknown |" + fi + + elif [ "$EXISTS_LXC" != "" ]; then + + EXISTS_LXC_STOPPED=$(lxc list -f csv | grep STOPPED | grep $2) + if [ "$EXISTS_LXC_STOPPED" ]; then + if [ "$FORMAT" == "csv" ]; then + echo "$2,lxc,stopped" + else + # i-00000000000000000 docker + echo "| $2 | lxc | stopped |" + fi + else + if [ "$FORMAT" == "csv" ]; then + echo "$2,lxc,running" + else + # i-00000000000000000 docker + echo "| $2 | lxc | running |" + fi + fi + + elif [ "$EXISTS_DOCKER_STOPPED" != "" ]; then + + if [ "$FORMAT" == "csv" ]; then + echo "$2,docker,stopped" + else + # i-00000000000000000 docker + echo "| $2 | docker | stopped |" + fi + + elif [ "$EXISTS_DOCKER_RUNNING" != "" ]; then + + if [ "$FORMAT" == "csv" ]; then + echo "$2,docker,running" + else + # i-00000000000000000 docker + echo "| $2 | docker | running |" + fi + + fi + + fi elif [ "$1" == "up" ]; then @@ -28,7 +156,36 @@ elif [ "$1" == "up" ]; then else - VBoxHeadless --startvm "mochasuv-$2" & + EXISTS=$(VBoxManage list vms | grep 'mochasuv-$2') + if [ "$EXISTS" != "" ]; then + + # is VBox VM + VBoxHeadless --startvm "mochasuv-$2" & + + else + + EXISTS=$(echo "$LXC_VMS" | grep $2) + if [ "$EXISTS" != "" ]; then + + # LXC container found, start it + lxc start $2 + + else + + # last chance, maybe we used Docker + EXISTS=$(echo "$DOCKER_VMS_STOPPED" | grep $2) + if [ "$EXISTS" != "" ]; then + + docker container start $2 + + else + + exit 127 + + fi + + fi + fi fi else @@ -75,19 +232,28 @@ elif [ "$1" == "shell" ]; then else - if [ "$#" == "2" ]; then - # only display the banner if we aren't executing a command - echo "If you are migrating from Vagrant, the password is 'vagrant'" - echo "Use 'ssh-copy-id vagrant@$2.privatesuv.com' to set up asymmetric key login" - echo "" + EXISTS_DOCKER=$(echo "$DOCKER_VMS_RUNNING" | grep $2) + if [ "$EXISTS_DOCKER" != "" ]; then + + docker container exec -it $2 bash + + else + + if [ "$#" == "2" ]; then + # only display the banner if we aren't executing a command + echo "If you are migrating from Vagrant, the password is 'vagrant'" + echo "Use 'ssh-copy-id vagrant@$2.privatesuv.com' to set up asymmetric key login" + echo "" + fi + + MACHINENAME=$2 + shift + shift + + ssh vagrant@$MACHINENAME.privatesuv.com ${1+"$@"} + fi - MACHINENAME=$2 - shift - shift - - ssh vagrant@$MACHINENAME.privatesuv.com ${1+"$@"} - fi else diff --git a/mocha-suv/desktop-integration/bash/etc/bash_completion.d/mocha-suv b/mocha-suv/desktop-integration/bash/etc/bash_completion.d/mocha-suv index 7d73b05..bfb79e5 100644 --- a/mocha-suv/desktop-integration/bash/etc/bash_completion.d/mocha-suv +++ b/mocha-suv/desktop-integration/bash/etc/bash_completion.d/mocha-suv @@ -24,7 +24,7 @@ _mocha() return 0 ;; up|down|delete|shell|status) - COMPREPLY=( $( compgen -W "$(ls /home/beckermj/Documents/Vagrant/mocha/machines)" -- "$cur" ) ) + COMPREPLY=( $( compgen -W "$(mocha suv list)" -- "$cur" ) ) return 0 ;; esac