From f091d763234d0cc14a29409f9d4474af722051c7 Mon Sep 17 00:00:00 2001 From: Silvio Rhatto Date: Thu, 16 Mar 2017 15:55:10 -0300 Subject: Use APP_BASE variable via __kvmx_set_app_base --- TODO.md | 1 - kvmx | 39 +++++++++++++++++++++++++++++++++++---- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/TODO.md b/TODO.md index 82608be..3a912b2 100644 --- a/TODO.md +++ b/TODO.md @@ -3,7 +3,6 @@ TODO * Shared folder and mountpoint pairs. * Default image image location so putting that info on kvmxfile is optional. -* Use APP_BASE variable like the Hydra Suite (symlink handling, etc). * More kvmx-create params (ssh, serial console, etc). * Production mode (systemd service, serial console, no passwordless ssh, etc). * Support for more distros at kvmx-create (archlinux, gentoo, slackware, etc). diff --git a/kvmx b/kvmx index 15b6468..44800f1 100755 --- a/kvmx +++ b/kvmx @@ -26,11 +26,42 @@ ACTION="$1" VM="$2" GLOBAL_USER_CONFIG_FOLDER="$HOME/.config/kvmx" -# Alias to be used in config files -KVMX_BASE="$DIRNAME" +# Set application base +function __kvmx_set_app_base { + local dest + local base + + # Determine if we are in a local or system-wide install. + if [ -h "$0" ]; then + dest="$(readlink $0)" + + # Check again as the caller might be a symlink as well + if [ -h "$dest" ]; then + base="`dirname $dest`" + dest="$(dirname $(readlink $dest))" + else + base="`dirname $0`" + dest="`dirname $dest`" + fi + + # Deal with relative or absolute links + if [ "`basename $dest`" == "$dest" ]; then + export APP_BASE="$base" + else + export APP_BASE="$dest" + fi + else + export APP_BASE="`dirname $0`" + fi +} # Initialize function __kvmx_initialize { + __kvmx_set_app_base + + # Alias to be used in config files + KVMX_BASE="$APP_BASE" + if [ "$ACTION" == "init" ] || [ "$ACTION" == "list" ]; then return fi @@ -42,7 +73,7 @@ function __kvmx_initialize { # Default parameters PORT="$(($RANDOM + 1024))" SSH="$(($PORT + 22))" - SSH_COMMAND="ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o LogLevel=FATAL -o ProxyCommand=none -i $DIRNAME/ssh/insecure_private_key" + SSH_COMMAND="ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o LogLevel=FATAL -o ProxyCommand=none -i $APP_BASE/ssh/insecure_private_key" SSH_LOGIN="user" # Initalize @@ -408,7 +439,7 @@ function kvmx_init { # Copy config from template if [ ! -e "$FOLDER/kvmxfile" ]; then - cp $DIRNAME/kvmxfile $FOLDER/ + cp $APP_BASE/kvmxfile $FOLDER/ sed -i -e "s|hostname=\"machine\"|hostname=\"$VM\"|g" $FOLDER/kvmxfile fi -- cgit v1.2.3