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