freealberta/fix-permissions.sh

119 lines
3.9 KiB
Bash
Executable File

#!/bin/bash
# Fix Container Directory Permissions
# Run this script if you encounter EACCES (permission denied) errors
# when starting Docker containers.
cat << "EOF"
╔═══════════════════════════════════════════════════╗
║ Changemaker - Fix Container Permissions ║
╚═══════════════════════════════════════════════════╝
EOF
# Get the absolute path of the script directory
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# Get the user/group IDs from .env or use defaults
if [ -f "$SCRIPT_DIR/.env" ]; then
source <(grep -E '^(USER_ID|GROUP_ID)=' "$SCRIPT_DIR/.env")
fi
USER_ID=${USER_ID:-1000}
GROUP_ID=${GROUP_ID:-1000}
echo ""
echo "Using UID: $USER_ID, GID: $GROUP_ID"
echo ""
# Define directories that need to be writable by containers
declare -A writable_dirs=(
["$SCRIPT_DIR/configs/code-server/.config"]="Code Server config"
["$SCRIPT_DIR/configs/code-server/.local"]="Code Server local data"
["$SCRIPT_DIR/mkdocs/.cache"]="MkDocs cache (social cards, etc.)"
["$SCRIPT_DIR/mkdocs/site"]="MkDocs built site"
["$SCRIPT_DIR/assets/uploads"]="Listmonk uploads"
["$SCRIPT_DIR/assets/images"]="Shared images"
["$SCRIPT_DIR/configs/homepage/logs"]="Homepage logs"
)
errors=0
fixed=0
needs_sudo=()
echo "Checking and fixing directory permissions..."
echo ""
for dir_path in "${!writable_dirs[@]}"; do
dir_desc="${writable_dirs[$dir_path]}"
# Create directory if it doesn't exist
if [ ! -d "$dir_path" ]; then
echo " Creating: $dir_path"
mkdir -p "$dir_path"
fi
# Add .gitkeep to track empty directories in git
if [ ! -f "$dir_path/.gitkeep" ]; then
touch "$dir_path/.gitkeep" 2>/dev/null
fi
# Try to fix permissions
if chmod -R 777 "$dir_path" 2>/dev/null; then
echo "$dir_desc"
((fixed++))
else
echo " ⚠️ $dir_desc - needs sudo"
needs_sudo+=("$dir_path")
((errors++))
fi
done
echo ""
# If there are directories that need sudo, offer to fix them
if [ ${#needs_sudo[@]} -gt 0 ]; then
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "Some directories need elevated permissions to fix."
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
read -p "Would you like to fix them with sudo? [Y/n]: " use_sudo
if [[ ! "$use_sudo" =~ ^[Nn]$ ]]; then
echo ""
for dir_path in "${needs_sudo[@]}"; do
dir_desc="${writable_dirs[$dir_path]}"
echo " Fixing: $dir_desc"
# First try to change ownership, then permissions
if sudo chown -R "$USER_ID:$GROUP_ID" "$dir_path" 2>/dev/null && \
sudo chmod -R 777 "$dir_path" 2>/dev/null; then
echo " ✅ Fixed: $dir_desc"
((fixed++))
((errors--))
else
echo " ❌ Failed: $dir_desc"
fi
done
fi
fi
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "Summary"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo " Fixed: $fixed directories"
if [ $errors -gt 0 ]; then
echo " Errors: $errors directories still need attention"
echo ""
echo "To manually fix remaining issues, run:"
echo " sudo chown -R $USER_ID:$GROUP_ID $SCRIPT_DIR"
echo " sudo chmod -R 755 $SCRIPT_DIR"
exit 1
else
echo ""
echo "✅ All container directories are properly configured!"
echo ""
echo "You can now start your containers with:"
echo " docker compose up -d"
fi