Files
advanced-libvirt-terraform-…/main_launcher.sh
2025-11-02 11:53:00 +01:00

193 lines
4.8 KiB
Bash
Executable File

#!/bin/bash
# Main launcher script for terraform image management tools
# This script provides a menu interface to run the other scripts in proper order
# Function to display usage
usage() {
echo "Usage: $0 [options]"
echo " options:"
echo " -h, --help Display this help message"
echo " -y, --yes Automatically answer 'yes' to all prompts"
echo " -n, --no Automatically answer 'no' to all prompts"
echo ""
echo "Example:"
echo " $0 # Interactive mode with default 'yes'"
echo " $0 -y # Non-interactive mode with auto 'yes'"
echo " $0 -n # Non-interactive mode with auto 'no'"
exit 1
}
# Parse command line arguments
AUTO_YES=false
AUTO_NO=false
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
usage
;;
-y|--yes)
AUTO_YES=true
shift
;;
-n|--no)
AUTO_NO=true
shift
;;
*)
echo "Unknown option: $1"
usage
;;
esac
done
# Function to get user confirmation
confirm() {
local prompt="$1"
local default="$2" # "yes" or "no"
if [[ "$AUTO_YES" = true ]]; then
echo "$prompt [Y/n]: yes"
return 0
elif [[ "$AUTO_NO" = true ]]; then
echo "$prompt [y/N]: no"
return 1
fi
# Default behavior - yes is default
if [[ "$default" = "no" ]]; then
echo -n "$prompt [y/N]: "
else
echo -n "$prompt [Y/n]: "
fi
read -r answer
# If empty answer, use default
if [[ -z "$answer" ]]; then
answer="$default"
fi
# Convert to lowercase for comparison
answer=$(echo "$answer" | tr '[:upper:]' '[:lower:]')
if [[ "$answer" = "y" || "$answer" = "yes" ]]; then
return 0
else
return 1
fi
}
# Function to check if a script exists and is executable
check_script() {
local script="$1"
if [[ ! -f "$script" ]] || [[ ! -x "$script" ]]; then
echo "Error: Script '$script' not found or not executable"
return 1
fi
return 0
}
# Function to run a script with proper error handling
run_script() {
local script="$1"
local description="$2"
local args="$3"
echo "=== $description ==="
if check_script "$script"; then
if [[ -n "$args" ]]; then
echo "Running: $script $args"
"$script" "$args"
else
echo "Running: $script"
"$script"
fi
local exit_code=$?
if [[ $exit_code -eq 0 ]]; then
echo "$description completed successfully"
return 0
else
echo "$description failed with exit code $exit_code"
return 1
fi
else
echo "$description skipped due to missing script"
return 1
fi
}
# Function to check if libvirt images directory exists
check_images_directory() {
local image_dir="/var/lib/libvirt/images"
if [[ ! -d "$image_dir" ]]; then
echo "Warning: Directory $image_dir does not exist"
if confirm "Would you like to create it?" "yes"; then
if ! sudo mkdir -p "$image_dir"; then
echo "Error: Could not create directory $image_dir"
return 1
fi
echo "Directory $image_dir created successfully"
else
echo "Skipping image directory check"
fi
else
echo "Directory $image_dir exists"
fi
return 0
}
# Main execution logic
main() {
echo "=== Terraform Image Management Launcher ==="
echo
# Validate image directory
if ! check_images_directory; then
echo "Exiting due to image directory issues"
exit 1
fi
echo
# Ask if user wants to download images
if confirm "Do you want to download OS images?" "yes"; then
if ! run_script "./download_images.sh" "Downloading OS Images"; then
echo "Image download failed. Proceeding to update script..."
fi
fi
echo
# Ask if user wants to update image locations
if confirm "Do you want to update image locations to use local files?" "yes"; then
if ! run_script "./update_image_locations.sh" "Updating Image Locations"; then
echo "Image location update failed"
exit 1
fi
fi
echo
# Ask if user wants to update SSH keys
if confirm "Do you want to update SSH keys in terraform files?" "yes"; then
if ! run_script "./update_ssh_keys.sh" "Updating SSH Keys"; then
echo "SSH key update failed"
exit 1
fi
fi
echo
echo "=== All operations completed ==="
}
# Run main function if script is executed directly
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
main "$@"
fi