From 3b606e398e0ae95708be37e015d6d5fd0ff6ad0c Mon Sep 17 00:00:00 2001 From: Silvio Rhatto Date: Fri, 31 Mar 2017 10:18:33 -0300 Subject: Library support --- kvmx | 25 +++++++++++++++++-------- kvmx-create | 6 +++++- kvmx-keygen | 27 --------------------------- lib/kvmx/functions | 17 +++++++++++++++++ 4 files changed, 39 insertions(+), 36 deletions(-) delete mode 100755 kvmx-keygen create mode 100755 lib/kvmx/functions diff --git a/kvmx b/kvmx index 7a5b3ed..4ee1055 100755 --- a/kvmx +++ b/kvmx @@ -27,8 +27,8 @@ VM="$2" GLOBAL_USER_CONFIG_FOLDER="$HOME/.config/kvmx" GLOBAL_USER_CONFIG_FILE="$HOME/.config/kvmxconfig" -# Set application base -function __kvmx_set_app_base { +# Get the application base +function kvmx_app_base { local dest local base @@ -47,13 +47,15 @@ function __kvmx_set_app_base { # Deal with relative or absolute links if [ "`basename $dest`" == "$dest" ]; then - export APP_BASE="$base" + APP_BASE="$base" else - export APP_BASE="$dest" + APP_BASE="$dest" fi else - export APP_BASE="`dirname $0`" + APP_BASE="`dirname $0`" fi + + echo $APP_BASE } # Build a SSH command @@ -72,11 +74,18 @@ function __kvmx_create_config_entry { # Initialize function __kvmx_initialize { - __kvmx_set_app_base + if [ "$ACTION" == "app_base" ]; then + return + fi + + # Load basic functions + export APP_BASE="`$DIRNAME/kvmx app_base`" + source $APP_BASE/lib/kvmx/functions || exit 1 # Alias to be used in config files KVMX_BASE="$APP_BASE" + # Stop processing here for some actions if [ "$ACTION" == "init" ] || [ "$ACTION" == "list" ]; then return fi @@ -98,7 +107,7 @@ function __kvmx_initialize { fi # Load and check guest config - if [ "$ACTION" != "init" ] && [ "$ACTION" != "list" ] && [ "$ACTION" != "ls" ] && [ "$ACTION" != "edit" ] && [ "$ACTION" != "usage" ]; then + if [ "$ACTION" != "ls" ] && [ "$ACTION" != "edit" ] && [ "$ACTION" != "usage" ]; then if [ ! -e "$GLOBAL_USER_CONFIG_FOLDER/$VM" ]; then if [ -e "kvmxfile" ]; then # Existing kvmxfile but not registered at the global user config @@ -720,7 +729,7 @@ function kvmx_rotate_sshkeys { # Generate new keypair mkdir -p "$STORAGE/ssh" SSHKEY="$STORAGE/ssh/$VM.key" - $DIRNAME/kvmx-keygen $SSHKEY.new "$user@`basename $image .img`" + __kvmx_ssh_keygen $SSHKEY.new "$user@`basename $image .img`" # Replace pubkey on server echo "touch ~/.ssh/authorized_keys.new && chmod 600 ~/.ssh/authorized_keys.new" | kvmx_ssh diff --git a/kvmx-create b/kvmx-create index 4ea8755..5fb2ca4 100755 --- a/kvmx-create +++ b/kvmx-create @@ -23,6 +23,10 @@ BASENAME="`basename $0`" DIRNAME="`dirname $0`" GLOBAL_USER_CONFIG_FILE="$HOME/.config/kvmxconfig" +# Load basic functions +export APP_BASE="`$DIRNAME/kvmx app_base`" +source $APP_BASE/lib/kvmx/functions || exit 1 + # Load configuration function kvmx_config_load { if [ ! -z "$1" ] && [ -e "$1" ]; then @@ -261,7 +265,7 @@ function kvmx_create_custom { privkey="`dirname $image`/ssh/`basename $image .img`.key" pubkey="${privkey}.pub" mkdir -p "`dirname $privkey`" - $DIRNAME/kvmx-keygen $privkey "$user@`basename $image .img`" + __kvmx_ssh_keygen $privkey "$user@`basename $image .img`" else pubkey="$DIRNAME/share/ssh/insecure_private_key.pub" fi diff --git a/kvmx-keygen b/kvmx-keygen deleted file mode 100755 index 3784e52..0000000 --- a/kvmx-keygen +++ /dev/null @@ -1,27 +0,0 @@ -#!/usr/bin/env bash -# -# kvmx-keygen -- ssh-keygen wrapper for kvmx -# -# Copyright (C) 2017 Silvio Rhatto - rhatto at riseup.net -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published -# by the Free Software Foundation, either version 3 of the License, -# or any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . -# - -# Parameters -BASENAME="`basename $0`" -PRIVKEY="$1" -COMMENT="$2" - -# Generate a keypair -ssh-keygen -t rsa -b 4096 -f $PRIVKEY -N '' -C $COMMENT diff --git a/lib/kvmx/functions b/lib/kvmx/functions new file mode 100755 index 0000000..df1bd7a --- /dev/null +++ b/lib/kvmx/functions @@ -0,0 +1,17 @@ +#!/bin/bash +# +# Common functions. +# + +# Source required functions +#source $APP_BASE/lib/kvmx/misc + +# Setup environment +#kvmx_set_env $* + +# Generate a keypair +function __kvmx_ssh_keygen { + if [ ! -z "$2" ]; then + ssh-keygen -t rsa -b 4096 -f $1 -N '' -C $2 + fi +} -- cgit v1.2.3