|
| 1 | +#!/bin/bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# Script to update RELATED_IMAGE_ environment variables in ClusterServiceVersion file |
| 5 | +# with values from the current bash environment |
| 6 | + |
| 7 | +CSV_FILE="bundle/manifests/openstack-operator.clusterserviceversion.yaml" |
| 8 | + |
| 9 | +# Check if CSV file exists |
| 10 | +if [[ ! -f "$CSV_FILE" ]]; then |
| 11 | + echo "Error: ClusterServiceVersion file not found at $CSV_FILE" |
| 12 | + echo "Please run 'make bundle' first to generate the bundle directory" |
| 13 | + exit 1 |
| 14 | +fi |
| 15 | + |
| 16 | +# Create a backup of the original file |
| 17 | +cp "$CSV_FILE" "${CSV_FILE}.backup" |
| 18 | + |
| 19 | +echo "Updating RELATED_IMAGE_ environment variables in $CSV_FILE..." |
| 20 | + |
| 21 | +# Extract all RELATED_IMAGE_ env var names from the CSV file |
| 22 | +RELATED_IMAGE_VARS=$(grep -o 'RELATED_IMAGE_[A-Z_]*' "$CSV_FILE" | sort -u) |
| 23 | + |
| 24 | +# Track if any errors occurred |
| 25 | +ERRORS=0 |
| 26 | + |
| 27 | +# Process each RELATED_IMAGE_ variable |
| 28 | +for var_name in $RELATED_IMAGE_VARS; do |
| 29 | + # Check if the environment variable exists in the current bash environment |
| 30 | + if [[ -n "${!var_name:-}" ]]; then |
| 31 | + current_value="${!var_name}" |
| 32 | + echo "Updating $var_name with value: $current_value" |
| 33 | + |
| 34 | + # Use sed to replace all occurrences of the current value in the CSV file |
| 35 | + # First, we need to get the current value from the CSV file |
| 36 | + current_csv_value=$(grep -A1 "name: $var_name" "$CSV_FILE" | grep "value:" | sed 's/.*value: //' | tr -d '"') |
| 37 | + |
| 38 | + if [[ -n "$current_csv_value" ]]; then |
| 39 | + # Escape special characters for sed |
| 40 | + escaped_current=$(printf '%s\n' "$current_csv_value" | sed 's/[[\.*^$()+?{|]/\\&/g') |
| 41 | + escaped_new=$(printf '%s\n' "$current_value" | sed 's/[[\.*^$()+?{|]/\\&/g') |
| 42 | + |
| 43 | + # Replace all occurrences of the current value with the new value |
| 44 | + sed -i "s|$escaped_current|$escaped_new|g" "$CSV_FILE" |
| 45 | + else |
| 46 | + echo "Warning: Could not find current value for $var_name in CSV file" |
| 47 | + fi |
| 48 | + else |
| 49 | + echo "Error: Environment variable $var_name is not set" |
| 50 | + ERRORS=$((ERRORS + 1)) |
| 51 | + fi |
| 52 | +done |
| 53 | + |
| 54 | +if [[ $ERRORS -gt 0 ]]; then |
| 55 | + echo "" |
| 56 | + echo "Error: $ERRORS environment variable(s) were not found" |
| 57 | + echo "Please set all required RELATED_IMAGE_ environment variables before running this script" |
| 58 | + echo "Restoring original file from backup..." |
| 59 | + mv "${CSV_FILE}.backup" "$CSV_FILE" |
| 60 | + exit 1 |
| 61 | +else |
| 62 | + echo "" |
| 63 | + echo "Successfully updated all RELATED_IMAGE_ environment variables" |
| 64 | + echo "Backup saved as ${CSV_FILE}.backup" |
| 65 | + rm -f "${CSV_FILE}.backup" |
| 66 | +fi |
0 commit comments