|
1 | 1 | #!/bin/bash |
2 | | -cd "$ORCHESTRATOR_HOME" || exit 1 |
3 | 2 |
|
4 | | -# Find all sonar-application-* JAR files, sort them by version, and list them |
5 | | -files=$(find . -name "sonarqube-enterprise-*" | sort --version-sort --field-separator=- --key=3 --reverse) |
| 3 | +# Function to clean up old versions of files based on a prefix |
| 4 | +# Parameters: |
| 5 | +# $1: The file prefix (e.g., "sonarqube-enterprise", "sonar-application") |
| 6 | +cleanup_old_versions() { |
| 7 | + local file_prefix="$1" |
| 8 | + # Ensure a prefix was provided |
| 9 | + if [ -z "$file_prefix" ]; then |
| 10 | + echo "Error: No file prefix provided to cleanup_old_versions function." |
| 11 | + return 1 # Indicate error |
| 12 | + fi |
6 | 13 |
|
7 | | -# Print the files that will be kept (the latest one) |
8 | | -echo "Files that won't be deleted:" |
9 | | -echo "$files" | head -n 1 |
| 14 | + local search_pattern="${file_prefix}-*" |
10 | 15 |
|
11 | | -# Get the files that will be deleted (all except the latest one) |
12 | | -files_to_delete=$(echo "$files" | tail -n +2) |
| 16 | + echo "--- Processing files starting with '${file_prefix}' ---" |
13 | 17 |
|
14 | | -echo "" |
15 | | -# Check if there are files to delete |
16 | | -if [ -z "$files_to_delete" ]; then |
17 | | - echo "No files will be deleted." |
18 | | -else |
19 | | - # Print the files that will be deleted |
20 | | - echo "Files that will be deleted:" |
21 | | - echo "$files_to_delete" |
22 | | - |
23 | | - # Delete the files that will be deleted |
24 | | - echo "$files_to_delete" | xargs -I {} sh -c 'rm -f "{}" && rmdir "$(dirname "{}")" 2>/dev/null || true' |
| 18 | + # Find all matching files, sort them by version (descending), and list them |
| 19 | + # Assuming version is the 3rd field when splitting by '-' |
| 20 | + # Using process substitution and mapfile for safer handling of filenames |
| 21 | + local -a all_files |
| 22 | + mapfile -t all_files < <(find . -name "$search_pattern" | sort --version-sort --field-separator=- --key=3 --reverse) |
| 23 | + |
| 24 | + # Check if any files were found |
| 25 | + if [ ${#all_files[@]} -eq 0 ]; then |
| 26 | + echo "No files found matching '${search_pattern}'." |
| 27 | + echo "--- Finished processing '${file_prefix}' ---" |
| 28 | + echo "" |
| 29 | + return 0 # Nothing to do, success |
| 30 | + fi |
| 31 | + |
| 32 | + # The first file in the sorted list is the latest one to keep |
| 33 | + local latest_file="${all_files[0]}" |
| 34 | + echo "Latest version (will be kept):" |
| 35 | + echo "$latest_file" |
| 36 | + |
| 37 | + # Get the files to delete (all except the latest one) |
| 38 | + local -a files_to_delete=("${all_files[@]:1}") # Slice the array starting from the second element |
| 39 | + |
| 40 | + echo "" |
| 41 | + echo "Files to delete:" |
| 42 | + for file in "${files_to_delete[@]}"; do |
| 43 | + echo "$file" |
| 44 | + rm -f "$file" && rmdir "$(dirname "$file")" 2>/dev/null || true |
| 45 | + done |
| 46 | + |
| 47 | + echo "--- Finished processing '${file_prefix}' ---" |
| 48 | + echo "" |
| 49 | + return 0 # Indicate success |
| 50 | +} |
| 51 | + |
| 52 | +# --- Main Script Logic --- |
| 53 | + |
| 54 | +# Check if ORCHESTRATOR_HOME is set and is a directory |
| 55 | +if [ -z "$ORCHESTRATOR_HOME" ]; then |
| 56 | + echo "Error: ORCHESTRATOR_HOME environment variable is not set." |
| 57 | + exit 1 |
| 58 | +elif [ ! -d "$ORCHESTRATOR_HOME" ]; then |
| 59 | + echo "Error: ORCHESTRATOR_HOME ('$ORCHESTRATOR_HOME') is not a valid directory." |
| 60 | + exit 1 |
25 | 61 | fi |
| 62 | + |
| 63 | +# Change to the target directory. Exit if failed. |
| 64 | +cd "$ORCHESTRATOR_HOME" || { |
| 65 | + echo "Error: Could not change directory to '$ORCHESTRATOR_HOME'" |
| 66 | + exit 1 |
| 67 | +} |
| 68 | +echo "Changed directory to $ORCHESTRATOR_HOME" |
| 69 | +echo "" |
| 70 | + |
| 71 | +# Call the cleanup function for each desired prefix |
| 72 | +cleanup_old_versions "sonarqube-enterprise" |
| 73 | +cleanup_old_versions "sonar-application" |
| 74 | + |
| 75 | +echo "All cleanup tasks finished." |
| 76 | +exit 0 |
0 commit comments