Roger Luethi 06fe3a3e84 labs: add snapshot/boot commands to config/scripts.* handler
This patch adds the ability for osbash to understand boot and
snapshot commands in config/scripts.* files.

The syntax for running scripts within the VM remains:

<dircode> <script_name>

For the new commands, it is:
boot
snapshot "Description for snapshot"

The Vagrant-side of the code will simply ignore these commands.

get_script_paths_from_config has been removed and merged into the
execution routine for osbash and Vagrant, respectively.

This changeset puts only the functionality in place. It is going to be
used by a subsequent patch.

Implements: blueprint openstack-training-labs
Change-Id: I2213125715e4cdf4cd2b4f501f3c216e2474b465
2014-08-09 17:48:52 +02:00

118 lines
2.8 KiB
Bash

# This file contains bash functions that may be used by both guest and host
# systems.
# Non-recursive removal of all files except README.*
function clean_dir {
local CLEAN_DIR=$1
if [ ! -e "$CLEAN_DIR" ]; then
mkdir -pv "$CLEAN_DIR"
elif [ ! -d "$CLEAN_DIR" ]; then
echo >&2 "Not a directory: $CLEAN_DIR"
return 1
fi
shopt -s nullglob
local ENTRIES=("$CLEAN_DIR"/*)
if [ -n "${ENTRIES[0]-}" ]; then
for f in "${ENTRIES[@]}"; do
# Skip directories
if [ ! -f "$f" ]; then
continue
fi
# Skip README.*
if [[ $f =~ /README\. ]]; then
continue
fi
rm -f "$f"
done
fi
}
function is_root {
if [ $EUID -eq 0 ]; then
return 0
else
return 1
fi
}
function yes_or_no {
local prompt=$1
local input=""
while [ : ]; do
read -p "$prompt (Y/n): " input
case "$input" in
N|n)
return 1
;;
""|Y|y)
return 0
;;
*)
echo "Invalid input: $input"
;;
esac
done
}
#-------------------------------------------------------------------------------
# Helpers to incrementally number files via name prefixes
#-------------------------------------------------------------------------------
function get_next_file_number {
local DIR=$1
local EXT=${2:-""}
# Get number of *.log files in directory
shopt -s nullglob
if [ -n "$EXT" ]; then
# Count files with specific extension
local FILES=("$DIR/"*".$EXT")
else
# Count all files
local FILES=("$DIR/"*)
fi
echo "${#FILES[*]}"
}
function get_next_prefix {
local DIR=$1
local EXT=$2
# Number of digits in prefix string (default 3)
local DIGITS=${3:-3}
# Get number of *.$EXT files in $DIR
local CNT="$(get_next_file_number "$DIR" "$EXT")"
printf "%0${DIGITS}d" "$CNT"
}
#-------------------------------------------------------------------------------
# Helpers for scripts configuration files (config/scripts.*)
#-------------------------------------------------------------------------------
# Configuration files use codes for directories rather than full paths (to make
# them easier to read and write)
function src_dir_code_to_dir {
local SRC_DIR_CODE=$1
case "$SRC_DIR_CODE" in
osbash)
echo "$OSBASH_SCRIPTS_DIR"
;;
scripts)
echo "$SCRIPTS_DIR"
;;
config)
echo "$CONFIG_DIR"
;;
*)
# No code, it must be a path already
echo "$SRC_DIR_CODE"
;;
esac
}
# vim: set ai ts=4 sw=4 et ft=sh: