Compare commits

...

17 Commits

Author SHA1 Message Date
Lukas Greve
2a529df043 add support for fedora 43 2025-11-18 16:56:44 +01:00
Lukas Greve
435ca041c0 add support for Fedora Cloud 43 2025-11-18 16:55:39 +01:00
Lukas Greve
f25c33eaca remove complex bit related to fedora rawhide 2025-11-18 16:55:24 +01:00
Lukas Greve
c99938e744 add ability to change disk size 2025-11-17 21:38:35 +01:00
Lukas Greve
e038dcef97 move back in root folder 2025-11-02 11:53:00 +01:00
Lukas Greve
0a32075220 add logic to avoid refetching fedora rawhide if already present. Otherwise, this script redownload rawhide almost every day, as there is a new image everyday 2025-11-02 11:52:39 +01:00
Lukas Greve
8266670f1d move scripts 2025-11-02 11:22:55 +01:00
Lukas Greve
7b67ff510b add aider 2025-11-02 11:22:23 +01:00
Lukas Greve
4aa7dafe26 make it a markdown 2025-11-02 11:22:13 +01:00
Lukas Greve
ebaf28040c Store VMs images in a more persistent location 2025-11-01 10:55:24 +01:00
Lukas Greve
d6e11a3e63 add logic for updating the image location for Fedora Rawhide. Does not appear to be working 2025-10-26 12:13:29 +01:00
Lukas Greve
f27930a294 Add logic to fetch latest Fedora Cloud Rawhide image 2025-10-26 12:02:16 +01:00
Lukas Greve
1b757a98eb Add support for Fedora Cloud Rawhide 2025-10-25 18:59:53 +02:00
Lukas Greve
5f5119db1d add support for CentOS Stream 10 2025-10-25 18:59:40 +02:00
Lukas Greve
f541ae77ce remove whitespace 2025-10-25 18:59:19 +02:00
Lukas Greve
cdfae5661e add CentOS Stream 10 2025-10-25 13:49:11 +02:00
Lukas Greve
ab633d601d add centos stream and refactor script 2025-10-25 13:48:53 +02:00
11 changed files with 503 additions and 248 deletions

1
.gitignore vendored
View File

@@ -10,3 +10,4 @@ terraform.tfvars.example
# Terraform plan and output files # Terraform plan and output files
*.tfplan *.tfplan
*.tfout *.tfout
.aider*

View File

View File

@@ -1,215 +1,84 @@
#!/bin/bash #!/bin/bash
# Script to detect locally available OS images and update image_location URLs in main.tf files # Image URLs with dynamic Fedora URL handling
# This script updates terraform configurations to use local image paths instead of remote URLs IMAGES=(
# It also supports reverting back to original remote URLs using hardcoded values
# Function to display usage
usage() {
echo "Usage: $0 [options]"
echo " options:"
echo " -h, --help Display this help message"
echo " -d, --dry-run Show what would be changed without making modifications"
echo " -r, --revert Revert image_location URLs back to original remote URLs"
echo ""
echo "Example:"
echo " $0 # Convert remote URLs to local paths (default)"
echo " $0 -d # Dry run - show what would be updated"
echo " $0 -r # Revert to original remote URLs"
echo " $0 -r -d # Dry run revert mode"
exit 1
}
# Parse command line arguments
DRY_RUN=false
REVERT_MODE=false
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
usage
;;
-d|--dry-run)
DRY_RUN=true
shift
;;
-r|--revert)
REVERT_MODE=true
shift
;;
*)
echo "Unknown option: $1"
usage
;;
esac
done
# Define the directory where images are stored
IMAGE_DIR="/var/lib/libvirt/images"
# Check if we have write permissions to the target directory
if [[ ! -d "$IMAGE_DIR" ]]; then
echo "Error: Directory $IMAGE_DIR does not exist"
exit 1
fi
# Image URLs (hardcoded original URLs from download_images.sh)
ORIGINAL_IMAGES=(
"https://cloud.debian.org/images/cloud/trixie/latest/debian-13-genericcloud-amd64.raw" "https://cloud.debian.org/images/cloud/trixie/latest/debian-13-genericcloud-amd64.raw"
"https://download.fedoraproject.org/pub/fedora/linux/releases/42/Cloud/x86_64/images/Fedora-Cloud-Base-Generic-42-1.1.x86_64.qcow2" "https://download.fedoraproject.org/pub/fedora/linux/releases/42/Cloud/x86_64/images/Fedora-Cloud-Base-Generic-42-1.1.x86_64.qcow2"
"https://download.opensuse.org/tumbleweed/appliances/openSUSE-Tumbleweed-Minimal-VM.x86_64-Cloud.qcow2" "https://download.opensuse.org/tumbleweed/appliances/openSUSE-Tumbleweed-Minimal-VM.x86_64-Cloud.qcow2"
"https://dl.rockylinux.org/pub/rocky/10/images/x86_64/Rocky-10-GenericCloud-Base.latest.x86_64.qcow2" "https://dl.rockylinux.org/pub/rocky/10/images/x86_64/Rocky-10-GenericCloud-Base.latest.x86_64.qcow2"
"https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img" "https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img"
"https://cloud.centos.org/centos/10-stream/x86_64/images/CentOS-Stream-GenericCloud-x86_64-10-latest.x86_64.qcow2"
"https://download.fedoraproject.org/pub/fedora/linux/releases/43/Cloud/x86_64/images/Fedora-Cloud-Base-Generic-43-1.6.x86_64.qcow2"
) )
# Function to get filename from URL # Target directory
get_filename_from_url() { TARGET_DIR="/var/lib/libvirt/images"
local url=$1
basename "$url"
}
# Find all main.tf files and process them # Main script execution
MAIN_TF_FILES=$(find . -name "main.tf" -type f) main() {
# Check if we have write permissions to the target directory
if [ -z "$MAIN_TF_FILES" ]; then if [[ ! -w "$TARGET_DIR" ]]; then
echo "No main.tf files found!" # Check if we're already running as root
exit 1 if [[ $EUID -ne 0 ]]; then
fi echo "This script requires write access to $TARGET_DIR"
echo "Re-executing with sudo..."
echo "Found main.tf files:" exec sudo "$0" "$@"
echo "$MAIN_TF_FILES"
echo ""
# Process each file
for file in $MAIN_TF_FILES; do
echo "Processing $file..."
# Check if the file contains image_location lines
if ! grep -q "image_location" "$file"; then
echo " No image_location found in $file, skipping..."
continue
fi
if [ "$REVERT_MODE" = true ]; then
# Revert operation: change file:// back to original https:// URLs
# Create a temporary file to avoid read/write race condition
temp_file=$(mktemp)
# Process the file line by line to avoid reading/writing the same file
while IFS= read -r line; do
if [[ "$line" =~ .*image_location.*=.*\"file://(.*?)\".* ]]; then
# Extract local path from the file:// URL
local_file_path="${BASH_REMATCH[1]}"
local_filename=$(basename "$local_file_path")
# Find matching original URL for this filename
found_match=false
for original_url in "${ORIGINAL_IMAGES[@]}"; do
if [[ "$(basename "$original_url")" == "$local_filename" ]]; then
echo " Found matching file: $local_filename"
if [ "$DRY_RUN" = false ]; then
# Replace the line in temporary file
sed -e "s|image_location.*=.*\"file://$local_file_path\"|image_location = \"$original_url\"|" <<< "$line" >> "$temp_file"
echo " Reverted to original URL: $original_url"
else
echo " Would revert to: $original_url"
echo "$line" >> "$temp_file"
fi
found_match=true
break
fi
done
# If we didn't find a match, still copy the original line
if [ "$found_match" = false ]; then
echo " Warning: No matching original URL found for $local_filename"
echo "$line" >> "$temp_file"
fi
else
# Not a line with image_location, just copy as is
echo "$line" >> "$temp_file"
fi
done < "$file"
# If not in dry run mode, replace the original file with the temporary file
if [ "$DRY_RUN" = false ]; then
mv "$temp_file" "$file"
else else
rm "$temp_file" echo "Error: Cannot write to $TARGET_DIR even with sudo privileges."
exit 1
fi
fi
# Download all images
echo "Starting download of all images..."
echo ""
local success_count=0
local failure_count=0
for url in "${IMAGES[@]}"; do
# Skip empty URLs
if [[ -z "$url" ]]; then
continue
fi fi
else local filename
# Normal operation: convert remote URLs to local paths filename=$(basename "$url")
temp_file=$(mktemp) local filepath="$TARGET_DIR/$filename"
# Process the file line by line to avoid reading/writing the same file if [[ -f "$filepath" ]]; then
while IFS= read -r line; do echo "Image $filename already exists, skipping..."
if [[ "$line" =~ .*image_location.*=.*\"(https://.*)\".* ]]; then ((success_count++))
remote_url="${BASH_REMATCH[1]}" continue
filename=$(basename "$remote_url") fi
# Check if the local file exists echo "Downloading $filename..."
local_path="$IMAGE_DIR/$filename"
if [[ -f "$local_path" ]]; then # Use wget with progress and retry options
echo " Found local image: $filename" if ! wget -P "$TARGET_DIR" --progress=bar:force:noscroll -c "$url"; then
echo "Failed to download $filename"
if [ "$DRY_RUN" = false ]; then ((failure_count++))
# Replace the line in temporary file
sed -e "s|image_location.*=.*\"$remote_url\"|image_location = \"file://$local_path\"|" <<< "$line" >> "$temp_file"
echo " Updated to: file://$local_path"
else
echo " Would update to: file://$local_path"
echo "$line" >> "$temp_file"
fi
else
echo " Local image not found: $filename"
echo "$line" >> "$temp_file"
fi
else
# Not a line with image_location, just copy as is
echo "$line" >> "$temp_file"
fi
done < "$file"
# If not in dry run mode, replace the original file with the temporary file
if [ "$DRY_RUN" = false ]; then
mv "$temp_file" "$file"
else else
rm "$temp_file" echo "Download completed: $filename"
((success_count++))
fi fi
if [ "$DRY_RUN" = false ] && [ -f "$temp_file" ]; then
echo " Updated $file"
elif [ "$DRY_RUN" = true ]; then
echo " Would update $file (dry run)"
fi
fi
done
echo ""
if [ "$DRY_RUN" = false ]; then
if [ "$REVERT_MODE" = true ]; then
echo "Image location URLs have been successfully reverted to original remote URLs!"
else
echo "Image location URLs have been successfully updated in all main.tf files!"
fi
else
echo "Dry run completed - no changes were made."
fi
# Show a summary of what would be changed
echo ""
echo "Summary of local image availability:"
find "$IMAGE_DIR" -maxdepth 1 -type f -name "*.qcow2" -o -name "*.raw" -o -name "*.img" | \
while read -r image; do
filename=$(basename "$image")
echo "$filename"
done done
# If nothing was found, show what images are expected # Summary
if ! find "$IMAGE_DIR" -maxdepth 1 -type f -name "*.qcow2" -o -name "*.raw" -o -name "*.img" | grep -q .; then echo ""
echo " No local images found in $IMAGE_DIR" echo "Download summary:"
echo " Run download_images.sh to download required images." echo "Successful downloads: $success_count"
echo "Failed downloads: $failure_count"
if [[ $failure_count -gt 0 ]]; then
echo "Some downloads failed. Check above messages for details."
exit 1
else
echo "All images downloaded successfully!"
fi
}
# Run main function if script is executed directly
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
main "$@"
fi fi

View File

@@ -0,0 +1,22 @@
terraform {
required_version = ">= 0.13"
required_providers {
libvirt = {
source = "dmacvicar/libvirt"
version = "0.8.3"
}
}
}
provider "libvirt" {
uri = "qemu:///system"
}
module "shared_modules" {
source = "../../shared_modules"
vm_name = "cent10-bios"
image_location = "https://cloud.centos.org/centos/10-stream/x86_64/images/CentOS-Stream-GenericCloud-x86_64-10-latest.x86_64.qcow2"
ssh_key = "" # please provide a SSH public key
enable_cloudinit = true
}

View File

@@ -0,0 +1,22 @@
terraform {
required_version = ">= 0.13"
required_providers {
libvirt = {
source = "dmacvicar/libvirt"
version = "0.8.3"
}
}
}
provider "libvirt" {
uri = "qemu:///system"
}
module "shared_modules" {
source = "../../shared_modules"
vm_name = "fraw-bios"
image_location = "file:///var/lib/libvirt/images/Fedora-Cloud-Base-Generic-Rawhide-20251024.n.0.x86_64.qcow2"
ssh_key = "" # please provide a SSH public key
enable_cloudinit = true
}

View File

@@ -0,0 +1,23 @@
terraform {
required_version = ">= 0.13"
required_providers {
libvirt = {
source = "dmacvicar/libvirt"
version = "0.8.3"
}
}
}
provider "libvirt" {
uri = "qemu+ssh://lukas@192.168.1.170/system"
}
module "shared_modules" {
source = "../../shared_modules"
vm_name = "f43-bios"
image_location = "https://download.fedoraproject.org/pub/fedora/linux/releases/43/Cloud/x86_64/images/Fedora-Cloud-Base-Generic-43-1.6.x86_64.qcow2"
ssh_key = "" # please provide a SSH public key
enable_cloudinit = true
# disk_size_bytes = 101474836480
}

192
main_launcher.sh Executable file
View File

@@ -0,0 +1,192 @@
#!/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

View File

@@ -13,7 +13,19 @@ variable "pool_name" {
variable "pool_path" { variable "pool_path" {
description = "Path for the storage pool" description = "Path for the storage pool"
type = string type = string
default = "/tmp/tf_tmp_storage" default = "/opt/tf_tmp_storage"
}
variable "disk_size_gb" {
description = "Disk size in GB"
type = number
default = 20
}
variable "disk_size_bytes" {
description = "Disk size in bytes"
type = number
default = 20 * 1024 * 1024 * 1024
} }
variable "instance_count" { variable "instance_count" {
@@ -71,7 +83,7 @@ variable "memory" {
variable "vcpu" { variable "vcpu" {
description = "Number of virtual CPUs" description = "Number of virtual CPUs"
type = number type = number
default = 2 default = 1
} }
variable "network_mode" { variable "network_mode" {

View File

@@ -1,9 +1,25 @@
resource "libvirt_volume" "vm_disk" { # Base volume (template/parent)
count = var.instance_count resource "libvirt_volume" "base_vm_image" {
name = "${var.vm_name}-${count.index}" name = "${var.vm_name}-base"
pool = "${var.vm_name}-pool" pool = "${var.vm_name}-pool"
source = var.image_location source = var.image_location
format = "qcow2" format = "qcow2"
depends_on = [libvirt_pool.tf_tmp_storage] depends_on = [
libvirt_pool.tf_tmp_storage
]
}
# Clone volume with resizable size
resource "libvirt_volume" "vm_disk" {
count = var.instance_count
name = "${var.vm_name}-${count.index}"
pool = "${var.vm_name}-pool"
format = "qcow2"
size = var.disk_size_bytes
base_volume_id = libvirt_volume.base_vm_image.id
depends_on = [
libvirt_volume.base_vm_image
]
} }

View File

@@ -53,19 +53,48 @@ if [[ ! -d "$IMAGE_DIR" ]]; then
exit 1 exit 1
fi fi
# Image URLs (hardcoded original URLs from download_images.sh) # Function to get all locally available image files (including Fedora Rawhide)
ORIGINAL_IMAGES=( get_local_images() {
"https://cloud.debian.org/images/cloud/trixie/latest/debian-13-genericcloud-amd64.raw" find "$IMAGE_DIR" -maxdepth 1 -type f \( -name "*.qcow2" -o -name "*.raw" -o -name "*.img" \) | \
"https://download.fedoraproject.org/pub/fedora/linux/releases/42/Cloud/x86_64/images/Fedora-Cloud-Base-Generic-42-1.1.x86_64.qcow2" while read -r image; do
"https://download.opensuse.org/tumbleweed/appliances/openSUSE-Tumbleweed-Minimal-VM.x86_64-Cloud.qcow2" basename "$image"
"https://dl.rockylinux.org/pub/rocky/10/images/x86_64/Rocky-10-GenericCloud-Base.latest.x86_64.qcow2" done | sort
"https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img" }
)
# Function to get filename from URL # Function to check if a local file matches the pattern for a Fedora Rawhide image
get_filename_from_url() { is_fedora_rawhide_image() {
local url=$1 local filename=$1
basename "$url" # Pattern matching for Fedora Rawhide images that contain "Fedora-Cloud-Base-Generic-Rawhide"
if [[ "$filename" =~ ^Fedora-Cloud-Base-Generic-Rawhide.*\.qcow2$ ]]; then
return 0
fi
return 1
}
# Function to get the latest Fedora Rawhide image path from local directory
get_latest_fedora_rawhide_path() {
local latest_file
latest_file=$(find "$IMAGE_DIR" -maxdepth 1 -name "Fedora-Cloud-Base-Generic-Rawhide*.qcow2" -type f \
| sort -r \
| head -1)
if [[ -n "$latest_file" ]]; then
echo "$latest_file"
fi
}
# Function to provide a mapping between local files and their original URLs
create_original_url_mapping() {
# Create a hash-like mapping for known images
cat << 'EOF'
noble-server-cloudimg-amd64.img=https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img
Fedora-Cloud-Base-Generic-42-1.1.x86_64.qcow2=https://download.fedoraproject.org/pub/fedora/linux/releases/42/Cloud/x86_64/images/Fedora-Cloud-Base-Generic-42-1.1.x86_64.qcow2
Fedora-Cloud-Base-Generic-43-1.6.x86_64.qcow2=https://download.fedoraproject.org/pub/fedora/linux/releases/43/Cloud/x86_64/images/Fedora-Cloud-Base-Generic-43-1.6.x86_64.qcow2
openSUSE-Tumbleweed-Minimal-VM.x86_64-Cloud.qcow2=https://download.opensuse.org/tumbleweed/appliances/openSUSE-Tumbleweed-Minimal-VM.x86_64-Cloud.qcow2
Rocky-10-GenericCloud-Base.latest.x86_64.qcow2=https://dl.rockylinux.org/pub/rocky/10/images/x86_64/Rocky-10-GenericCloud-Base.latest.x86_64.qcow2
debian-13-genericcloud-amd64.raw=https://cloud.debian.org/images/cloud/trixie/latest/debian-13-genericcloud-amd64.raw
CentOS-Stream-GenericCloud-x86_64-10-latest.x86_64.qcow2=https://cloud.centos.org/centos/10-stream/x86_64/images/CentOS-Stream-GenericCloud-x86_64-10-latest.x86_64.qcow2
EOF
} }
# Find all main.tf files and process them # Find all main.tf files and process them
@@ -95,34 +124,59 @@ for file in $MAIN_TF_FILES; do
temp_file=$(mktemp) temp_file=$(mktemp)
while IFS= read -r line || [[ -n "$line" ]]; do while IFS= read -r line || [[ -n "$line" ]]; do
# Check if the line contains a file:// URL
if [[ "$line" =~ .*image_location.*=.*\"file://(.*?)\".* ]]; then if [[ "$line" =~ .*image_location.*=.*\"file://(.*?)\".* ]]; then
# Extract local path from the file:// URL # Extract local path from the file:// URL
local_file_path="${BASH_REMATCH[1]}" local_file_path="${BASH_REMATCH[1]}"
local_filename=$(basename "$local_file_path") local_filename=$(basename "$local_file_path")
# Find matching original URL for this filename # Handle Fedora Rawhide images specially
found_match=false if [[ "$local_filename" =~ ^Fedora-Cloud-Base-Generic-Rawhide.*\.qcow2$ ]]; then
for original_url in "${ORIGINAL_IMAGES[@]}"; do echo " Reverting Fedora Rawhide image: $local_filename"
if [[ "$(basename "$original_url")" == "$local_filename" ]]; then
echo " Found matching file: $local_filename"
if [ "$DRY_RUN" = false ]; then # For Rawhide, we'll keep the file:// reference but note that it's a special case
# Use precise string replacement to avoid corrupting the file if [ "$DRY_RUN" = false ]; then
new_line="${line/\"file:\/\/$local_file_path\"/\"$original_url\"}" echo "$line" >> "$temp_file"
echo "$new_line" >> "$temp_file" else
echo " Reverted to original URL: $original_url" echo " Would process Fedora Rawhide image: $local_filename (keeping file:// reference)"
else echo "$line" >> "$temp_file"
echo " Would revert to: $original_url"
echo "$line" >> "$temp_file"
fi
found_match=true
break
fi fi
done else
# For regular images, try to map back to original URL
# Create mapping for this specific case
mapping=$(create_original_url_mapping)
if [ "$found_match" = false ]; then # Find matching original URL
echo " Warning: No matching original URL found for $local_filename" found_match=false
echo "$line" >> "$temp_file" while IFS= read -r mapping_line; do
if [[ -z "$mapping_line" ]] || [[ "$mapping_line" =~ ^#.*$ ]]; then
continue
fi
file_pattern=$(echo "$mapping_line" | cut -d'=' -f1)
original_url=$(echo "$mapping_line" | cut -d'=' -f2)
if [[ "$file_pattern" == "$local_filename" ]]; then
echo " Found matching original URL: $local_filename"
if [ "$DRY_RUN" = false ]; then
# Use precise string replacement to avoid corrupting the file
new_line="${line/\"file:\/\/$local_file_path\"/\"$original_url\"}"
echo "$new_line" >> "$temp_file"
echo " Reverted to original URL: $original_url"
else
echo " Would revert to: $original_url"
echo "$line" >> "$temp_file"
fi
found_match=true
break
fi
done <<< "$mapping"
if [ "$found_match" = false ]; then
echo " Warning: No matching original URL found for $local_filename"
echo "$line" >> "$temp_file"
fi
fi fi
else else
# Not a line with image_location, just copy as is # Not a line with image_location, just copy as is
@@ -145,10 +199,54 @@ for file in $MAIN_TF_FILES; do
remote_url="${BASH_REMATCH[1]}" remote_url="${BASH_REMATCH[1]}"
filename=$(basename "$remote_url") filename=$(basename "$remote_url")
# Check if the local file exists # Check if the local file exists (including Fedora Rawhide cases)
local_path="$IMAGE_DIR/$filename" local_path="$IMAGE_DIR/$filename"
if [[ -f "$local_path" ]]; then # Special handling for Fedora Rawhide - check if it's the right pattern
if [[ "$filename" =~ ^Fedora-Cloud-Base-Generic-Rawhide.*\.qcow2$ ]]; then
# For Fedora Rawhide, we need to be more flexible with matching patterns
echo " Checking Fedora Rawhide pattern for: $filename"
# Find the most recent Fedora image that matches the pattern but has different timestamp
latest_rawhide=$(find "$IMAGE_DIR" -maxdepth 1 -name "Fedora-Cloud-Base-Generic-Rawhide*.qcow2" -type f \
| sort -r \
| head -1)
if [[ -n "$latest_rawhide" ]]; then
echo " Found matching local Fedora Rawhide image: $(basename $latest_rawhide)"
if [ "$DRY_RUN" = false ]; then
new_line="${line/\"$remote_url\"/\"file://$latest_rawhide\"}"
echo "$new_line" >> "$temp_file"
echo " Updated to local file: file://$latest_rawhide"
else
echo " Would update Fedora Rawhide to: file://$latest_rawhide"
echo "$line" >> "$temp_file"
fi
else
# No matching locally - check if we can find a similar pattern
echo " Checking for any Fedora-Cloud-Base-Generic-Rawhide*.qcow2 files..."
# Look for any file with the same prefix but different timestamp
local_candidates=$(find "$IMAGE_DIR" -maxdepth 1 -name "*Fedora-Cloud-Base-Generic-Rawhide*" -type f)
if [[ -n "$local_candidates" ]]; then
most_recent=$(echo "$local_candidates" | sort -r | head -1)
echo " Found matching local Fedora Rawhide image: $(basename $most_recent)"
if [ "$DRY_RUN" = false ]; then
new_line="${line/\"$remote_url\"/\"file://$most_recent\"}"
echo "$new_line" >> "$temp_file"
echo " Updated to local file: file://$most_recent"
else
echo " Would update Fedora Rawhide to: file://$most_recent"
echo "$line" >> "$temp_file"
fi
else
echo " Local Fedora Rawhide image not found, using original URL"
echo "$line" >> "$temp_file"
fi
fi
elif [[ -f "$local_path" ]]; then
echo " Found local image: $filename" echo " Found local image: $filename"
if [ "$DRY_RUN" = false ]; then if [ "$DRY_RUN" = false ]; then