From 06fe3a3e84d80be5576329cb5b7905989b4e8781 Mon Sep 17 00:00:00 2001 From: Roger Luethi Date: Sat, 9 Aug 2014 17:06:15 +0200 Subject: [PATCH] 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: 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 --- labs/lib/functions | 25 ------------------- labs/lib/osbash/functions.host | 34 ++++++++++++++++++++++---- labs/lib/osbash/virtualbox.functions | 3 --- labs/scripts/vagrant/run_scripts.sh | 36 ++++++++++++++++++++++------ 4 files changed, 58 insertions(+), 40 deletions(-) diff --git a/labs/lib/functions b/labs/lib/functions index 8a3b4a63..0657714d 100644 --- a/labs/lib/functions +++ b/labs/lib/functions @@ -113,30 +113,5 @@ function src_dir_code_to_dir { ;; esac } -# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -# Returns list of enabled scripts (to disable, comment out with #) - -function get_script_paths_from_config { - local CONFIG_FILE=$1 - local CONFIG_PATH=$CONFIG_DIR/$CONFIG_FILE - - if [ ! -f "$CONFIG_PATH" ]; then - echo >&2 "Config file not found: $CONFIG_FILE" - return 1 - fi - local DIR_CODE="" - local NAME="" - while read -r DIR_CODE NAME; do - if [[ $DIR_CODE =~ ^# ]]; then - # Skip lines that are commented out - continue - else - local DIR="$(src_dir_code_to_dir "$DIR_CODE")" - local SCR_PATH=$DIR/$NAME - echo "$SCR_PATH" - fi - done < "$CONFIG_PATH" -} # vim: set ai ts=4 sw=4 et ft=sh: diff --git a/labs/lib/osbash/functions.host b/labs/lib/osbash/functions.host index 649a9fa3..e205280d 100644 --- a/labs/lib/osbash/functions.host +++ b/labs/lib/osbash/functions.host @@ -251,13 +251,37 @@ function autostart { done } +# Parse config/scripts.* configuration files function autostart_from_config { - local CONFIG_FILE=$1 + local config_file=$1 + local config_path=$CONFIG_DIR/$config_file - log_autostart_source "$CONFIG_FILE" - get_script_paths_from_config "$CONFIG_FILE" | while read SCR_PATH; do - _autostart_queue "$SCR_PATH" - done + if [ ! -f "$config_path" ]; then + echo >&2 "Config file not found: $config_file" + return 1 + fi + + log_autostart_source "$config_file" + while read -r field_1 field_2; do + if [[ $field_1 =~ ^# ]]; then + # Skip lines that are commented out + continue + elif [ "$field_1" == "boot" ]; then + # Boot with queued autostart files now, wait for shutdown + echo >&2 _vbox_boot_with_autostart "$NODE_NAME" "$VM_SSH_PORT" + _vbox_boot_with_autostart "$NODE_NAME" "$VM_SSH_PORT" + elif [ "$field_1" == "snapshot" ]; then + # Format: snapshot "Description for snapshot" + echo >&2 vm_snapshot "$NODE_NAME" "$field_2" + vm_snapshot "$NODE_NAME" "$field_2" + else + # Queue a script for autostart + # Format: + # Note: _autostart_queue takes care of dircode + echo >&2 _autostart_queue "$field_1/$field_2" + _autostart_queue "$field_1/$field_2" + fi + done < "$config_path" } #------------------------------------------------------------------------------- diff --git a/labs/lib/osbash/virtualbox.functions b/labs/lib/osbash/virtualbox.functions index f73687aa..9dff0d8a 100644 --- a/labs/lib/osbash/virtualbox.functions +++ b/labs/lib/osbash/virtualbox.functions @@ -82,9 +82,6 @@ function vm_snapshot { local VM_NAME=$1 local SHOT_NAME=$2 - # Blanks would fail in Windows batch files; space becomes underscore - SHOT_NAME="${SHOT_NAME// /_}" - $VBM snapshot "$VM_NAME" take "$SHOT_NAME" # VirtualBox VM needs a break before taking new commands vbox_sleep 1 diff --git a/labs/scripts/vagrant/run_scripts.sh b/labs/scripts/vagrant/run_scripts.sh index 20f67a0f..9be19b3a 100755 --- a/labs/scripts/vagrant/run_scripts.sh +++ b/labs/scripts/vagrant/run_scripts.sh @@ -11,11 +11,33 @@ clean_dir "$LOG_DIR" exec_logpath "$LOG_DIR/$HOSTNAME.log" -# The Vagrantfile uses precise, so we know it's Ubuntu -for CONFIG_FILE in "scripts.nodeinit_vagrant" "scripts.ubuntu" "scripts.$HOSTNAME"; do - echo "Config file $CONFIG_FILE" - get_script_paths_from_config "$CONFIG_FILE" | while read SCR_PATH; do - echo "$SCR_PATH" - as_root_exec_script "$SCR_PATH" - done +function vagrant_start_from_config { + local config_file=$1 + local config_path=$CONFIG_DIR/$config_file + + if [ ! -f "$config_path" ]; then + echo >&2 "Config file not found: $config_file" + return 1 + fi + + while read -r field_1 field_2; do + if [[ $field_1 =~ ^# ]]; then + # Skip lines that are commented out + continue + elif [[ "$field_1" == "boot" || "$field_1" == "snapshot" ]]; then + # Skip osbash commands, Vagrant ignores them + continue + else + local dir="$(src_dir_code_to_dir "$field_1")" + local scr_path=$dir/$field_2 + echo "$scr_path" + as_root_exec_script "$scr_path" + fi + done < "$config_path" +} + +# The Vagrantfile uses Ubuntu +for config_file in "scripts.nodeinit_vagrant" "scripts.ubuntu" "scripts.$HOSTNAME"; do + echo "Config file $config_file" + vagrant_start_from_config "$config_file" done