From 49c42178e30d8453dbd53a5f0f04f0db751fa432 Mon Sep 17 00:00:00 2001 From: Silvio Rhatto Date: Sun, 31 Dec 2017 00:08:25 -0200 Subject: Better command-line argument handling and additional action checks --- kvmx | 109 +++++++++++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 80 insertions(+), 29 deletions(-) (limited to 'kvmx') diff --git a/kvmx b/kvmx index 20afd02..5da8fca 100755 --- a/kvmx +++ b/kvmx @@ -23,7 +23,6 @@ VERSION="0.1.0" BASENAME="`basename $0`" DIRNAME="`dirname $0`" ACTION="$1" -VM="$2" GLOBAL_USER_CONFIG_FOLDER="$HOME/.config/kvmx" GLOBAL_USER_CONFIG_FILE="$HOME/.config/kvmxconfig" @@ -96,8 +95,16 @@ function __kvmx_initialize { return fi - if [ -z "$VM" ]; then + # Check if second argument is a VM name or option + if [ -z "$2" ]; then VM="$(basename `pwd`)" + SHIFTARGS="1" + elif [ -e 'kvmxfile' ] && [ ! -e "$GLOBAL_USER_CONFIG_FOLDER/$2" ] && [ "$2" != "$(basename `pwd`)" ]; then + VM="$(basename `pwd`)" + SHIFTARGS="1" + else + VM="$2" + SHIFTARGS="2" fi # Default parameters @@ -202,12 +209,17 @@ function kvmx_spice { exit 1 fi + if [ "$spice" == "0" ]; then + echo "$BASENAME: spice is disabled for guest $VM" + exit 1 + fi + # Ensure we have the right port configuration: we can also be # running directly from command line. PORT="`cat $PORTFILE`" if [ -z "$PORT" ]; then - echo "$BASENAME: cannot get spice port for $VM." + echo "$BASENAME: cannot get spice port for $VM" exit 1 fi @@ -425,8 +437,12 @@ function kvmx_up { -S "kvmx-$VM" -t "kvmx-$VM" socat STDIO,raw,echo=0 "UNIX:${CONSOLEFILE},retry=30" & if [ "$run_spice_client" == "1" ]; then - sleep 1 - kvmx_spice + if [ "$spice" == "0" ]; then + echo "$BASENAME: spice is disabled for guest $VM" + else + sleep 1 + kvmx_spice + fi fi if [ "$ssh_support" == "y" ]; then @@ -508,6 +524,11 @@ function kvmx_hostname { exit 1 fi + if [ "$ssh_support" != "y" ]; then + echo "$BASENAME: ssh_support is disabled for guest $VM" + exit 1 + fi + echo "Setting hostname..." $SSH_COMMAND -o ConnectTimeout=2 -p $SSH 127.0.0.1 < [options]" + echo "usage: $BASENAME [vm] [options]" echo "" echo "available actions:" echo "" @@ -577,13 +598,13 @@ function kvmx_ssh { # Shift params according to how the program was called: # either "kvmx ssh" or "kvmx ssh guest". - if [ "$ACTION" == "ssh" ]; then - if [ ! -z "$2" ]; then - shift 2 - else - shift 1 - fi - fi + #if [ "$ACTION" == "ssh" ]; then + # if [ ! -z "$2" ]; then + # shift 2 + # else + # shift 1 + # fi + #fi SSH="`cat $SSHFILE`" #TERM=xterm $SSH_COMMAND -p $SSH 127.0.0.1 $* @@ -754,8 +775,13 @@ function kvmx_rsync_to { exit 1 fi - ORIG="$3" - DEST="$4" + if [ "$ssh_support" != "y" ]; then + echo "$BASENAME: ssh_support is disabled for guest $VM" + exit 1 + fi + + ORIG="$1" + DEST="$2" if [ -z "$DEST" ]; then # Error @@ -781,8 +807,13 @@ function kvmx_rsync_from { exit 1 fi - ORIG="$3" - DEST="$4" + if [ "$ssh_support" != "y" ]; then + echo "$BASENAME: ssh_support is disabled for guest $VM" + exit 1 + fi + + ORIG="$1" + DEST="$2" if [ -z "$DEST" ]; then # Error @@ -808,8 +839,13 @@ function kvmx_scp_from { exit 1 fi - ORIG="$3" - DEST="$4" + if [ "$ssh_support" != "y" ]; then + echo "$BASENAME: ssh_support is disabled for guest $VM" + exit 1 + fi + + ORIG="$1" + DEST="$2" if [ -z "$DEST" ]; then # Error @@ -835,8 +871,13 @@ function kvmx_scp_to { exit 1 fi - ORIG="$3" - DEST="$4" + if [ "$ssh_support" != "y" ]; then + echo "$BASENAME: ssh_support is disabled for guest $VM" + exit 1 + fi + + ORIG="$1" + DEST="$2" if [ -z "$DEST" ]; then # Error @@ -874,7 +915,7 @@ function kvmx_upgrade { # Initializes a new guest function kvmx_init { - FOLDER="$3" + FOLDER="$1" if [ -z "$FOLDER" ]; then if [ -z "$VM" ]; then @@ -914,7 +955,7 @@ function kvmx_clone { exit 1 fi - FOLDER="$3" + FOLDER="$1" DEST="`basename $FOLDER`" if [ -z "$FOLDER" ]; then @@ -1057,6 +1098,11 @@ function kvmx_provision { #exit 1 fi + if [ "$ssh_support" != "y" ]; then + echo "$BASENAME: ssh_support is disabled for guest $VM" + exit 1 + fi + if [ -z "$provision_command" ]; then echo "$BASENAME: error: parameter provision_command is not configured for $VM." exit 1 @@ -1168,8 +1214,8 @@ function kvmx_xpra { exit 1 fi - local action="$3" - shift 3 + local action="$1" + shift SSH="`cat $SSHFILE`" @@ -1304,7 +1350,7 @@ function kvmx_open { exit 1 fi - ORIG="$3" + ORIG="$1" if [ -z "$ORIG" ]; then echo "$BASENAME: missing file argument." @@ -1344,7 +1390,7 @@ function kvmx_rename { exit 1 fi - FOLDER="$3" + FOLDER="$1" DEST="`basename $FOLDER`" if [ -z "$FOLDER" ]; then @@ -1430,7 +1476,7 @@ function kvmx_install { exit 1 fi - local media="$3" + local media="$1" if [ -z "$media" ]; then echo "usage: $BASENAME install $VM " @@ -1469,7 +1515,12 @@ function kvmx_serial { # Dispatch if type kvmx_$ACTION 2> /dev/null | grep -q "kvmx_$ACTION ()"; then - __kvmx_initialize + __kvmx_initialize $* + + if [ "$ACTION" != "app_base" ]; then + shift $SHIFTARGS + fi + kvmx_$ACTION $* else kvmx_usage -- cgit v1.2.3