Skip to content

Commit

Permalink
Rewrote to provide more feedback and checks.
Browse files Browse the repository at this point in the history
  • Loading branch information
DonRichards committed Nov 20, 2024
1 parent cd0a0f3 commit 70d504d
Showing 1 changed file with 83 additions and 45 deletions.
128 changes: 83 additions & 45 deletions scripts/patch_views.sh
Original file line number Diff line number Diff line change
@@ -1,63 +1,101 @@
#!/bin/bash
set -e
set -euo pipefail

# To be precise on the error message that matches the error this should address.
ERROR_MESSAGE=$(drush watchdog:show --severity=Error --filter="InvalidArgumentException: A valid cache entry key is required" | awk '{print $6}')
# Log file for tracking script operations
LOG_FILE="/tmp/drupal_troubleshoot_$(date +%Y%m%d_%H%M%S).log"

# If error message equals to "InvalidArgumentException", then exit.
if [[ $ERROR_MESSAGE == *'InvalidArgumentException'* ]]; then
# Function to log messages
log() {
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $*" | tee -a "$LOG_FILE"
}

# Check if drush is installed (Drupal Console replacement).
drush_installed() {
composer show 'drush/drush' | grep -q '/var/www/drupal/vendor/drush/drush'
}
if drush_installed; then
echo 'Drush installed'
else
# Verify required commands exist
check_dependencies() {
local deps=("drush" "composer" "awk" "grep")
for cmd in "${deps[@]}"; do
if ! command -v "$cmd" &> /dev/null; then
log "Error: $cmd is not installed"
exit 1
fi
done
}

# Check Drush installation
ensure_drush() {
if ! composer show 'drush/drush' &>/dev/null; then
log "Installing Drush..."
composer require drush/drush
else
log "Drush is already installed"
fi
}

# Retrieve the list of enabled views by isolating the first column (view names) where the status is "Enabled"
VIEWS_FILE="enabled_views.txt"
# Main troubleshooting function
troubleshoot_drupal() {
local ERROR_MESSAGE
ERROR_MESSAGE=$(drush watchdog:show --severity=Error --filter="InvalidArgumentException: A valid cache entry key is required" | awk '{print $6}')
log "Detected Error Message: '$ERROR_MESSAGE'"

# Get the list of enabled views and store it in the file without truncation
# Force drush to output in CSV format to avoid terminal width truncation issues
# Retrieve enabled views
local VIEWS_FILE="/tmp/enabled_views.txt"
drush views:list --status=enabled --format=csv | grep -v "Machine name" | awk -F',' '{print $1}' > "$VIEWS_FILE"

# Check if the file exists and is not empty
if [[ ! -s "$VIEWS_FILE" ]]; then
printf "Error: No enabled views found or unable to retrieve views list.\n" >&2
exit 1
log "Views file contents:"
log "$VIEWS_FILE"
echo "---------------------"

# Always process views, regardless of error message
# while IFS= read -r dis_view; do
# log "Processing view: $dis_view"
# drush views:disable "$dis_view" || log "Error disabling view: $dis_view"
# sleep 1
# drush views:enable "$dis_view" || log "Error enabling view: $dis_view"
# done < "$VIEWS_FILE"

# Ensure Devel module
DEVEL_INITIALLY_ENABLED=$(drush pm:list | grep devel | grep -F 'Devel (devel)' | grep -q "Enabled" && echo "Enabled" || echo "Disabled")
log "Devel module initial state: $DEVEL_INITIALLY_ENABLED"

if ! composer show 'drupal/devel' &>/dev/null; then
log "Installing Devel module..."
composer require 'drupal/devel' -W || log "Error: Devel module installation failed"
fi

# Read the file line by line to process each view
while IFS= read -r dis_view; do
printf "Reloading view: %s\n" "$dis_view"
drush views:disable "$dis_view"
sleep 1
drush views:enable "$dis_view"
done < "$VIEWS_FILE"

# Install devel.
devel_installed() {
composer show 'drupal/devel' | grep -q '/var/www/drupal/web/modules/contrib/devel'
}
if devel_installed; then
echo 'Devel module installed'
else
composer require 'drupal/devel' -W
# If it wasn't initially enabled, enable it.
if [[ "$DEVEL_INITIALLY_ENABLED" == "Disabled" ]]; then
drush pm:enable -y devel || log "Error: Devel module enabling failed"
fi
drush pm:enable -y devel

echo -e "nnThis will likely throw an error, but that's okay. It's just a patch.nn"
{ # try
drush pm:uninstall -y islandora
} || { # catch
echo -e "nIgnore these errors. This will fail if any content is already created.nn"
}
# Attempt to reinstall Islandora (with error suppression)
log "Attempting Islandora module uninstallation..."
drush devel:reinstall -y islandora || log "Islandora uninstall may have partial failure. This can be ignored."

# Clear caches
log "Rebuilding caches..."
drush cache:rebuild
drush cr
drush cron
# fi

log "Troubleshooting complete."
}

# Main script execution
main() {
check_dependencies
ensure_drush
troubleshoot_drupal
}

# Execute main function with error trapping
if main; then
log "Script completed successfully"
else
log "Script encountered errors"
exit 1
fi

# If it wasn't initially enabled, disable it at the end
if [[ "$DEVEL_INITIALLY_ENABLED" == "Disabled" ]]; then
drush pm:uninstall -y devel
fi

echo "Check LOG at $LOG_FILE"

0 comments on commit 70d504d

Please sign in to comment.