refactor(update-setup): Enhance configuration update process with error handling and summary reporting
Some checks failed
/ validate-docker-compose (push) Has been cancelled

This commit is contained in:
Jose Daniel G. Percy 2025-04-26 00:51:17 +08:00
parent ca4c3e92f0
commit 2217377ae8

View File

@ -280,50 +280,234 @@ update_arr_config() {
local container=$1 local container=$1
local path=$2 local path=$2
echo -e "${BLUE}Updating ${container} configuration...${NC}" echo -e "${BLUE}Configuring $container URL base and extracting API key...${NC}"
until [ -f "${CONFIG_ROOT:-.}"/"$container"/config.xml ]; do sleep 1; done
sed -i.bak "s/<UrlBase><\/UrlBase>/<UrlBase>\/$path<\/UrlBase>/" "${CONFIG_ROOT:-.}"/"$container"/config.xml && rm "${CONFIG_ROOT:-.}"/"$container"/config.xml.bak
CONTAINER_NAME_UPPER=$(echo "$container" | tr '[:lower:]' '[:upper:]') # Check if config file exists
sed -i.bak 's/^'"${CONTAINER_NAME_UPPER}"'_API_KEY=.*/'"${CONTAINER_NAME_UPPER}"'_API_KEY='"$(sed -n 's/.*<ApiKey>\(.*\)<\/ApiKey>.*/\1/p' "${CONFIG_ROOT:-.}"/"$container"/config.xml)"'/' .env && rm .env.bak config_file="${CONFIG_ROOT:-.}/$container/config.xml"
timeout=10
count=0
while [ ! -f "$config_file" ] && [ $count -lt $timeout ]; do
echo -e "${YELLOW}Waiting for $container config file to be available... ($(($timeout-$count)) seconds remaining)${NC}"
sleep 1
count=$((count+1))
done
echo -e "${GREEN}Update of ${container} configuration complete, restarting...${NC}" if [ ! -f "$config_file" ]; then
docker compose restart "$container" echo -e "${RED}Error: Config file for $container not found after $timeout seconds${NC}"
echo -e "${YELLOW}Is $container configured properly? Check the container logs${NC}"
return 1
fi
# Update URL base
if ! grep -q "<UrlBase>/$path</UrlBase>" "$config_file"; then
echo -e "${BLUE}Setting URL base to /$path${NC}"
sed -i.bak "s|<UrlBase>.*</UrlBase>|<UrlBase>/$path</UrlBase>|" "$config_file" 2>/dev/null || sed -i.bak "s|<UrlBase />|<UrlBase>/$path</UrlBase>|" "$config_file" 2>/dev/null
if [ $? -ne 0 ]; then
echo -e "${RED}Failed to update URL base in $config_file${NC}"
return 1
fi
rm -f "${config_file}.bak"
else
echo -e "${GREEN}URL base already set to /$path${NC}"
fi
# Extract API key
api_key=$(sed -n 's/.*<ApiKey>\(.*\)<\/ApiKey>.*/\1/p' "$config_file")
if [ -z "$api_key" ]; then
echo -e "${YELLOW}Could not find API key in $container configuration${NC}"
else
CONTAINER_NAME_UPPER=$(echo "$container" | tr '[:lower:]' '[:upper:]')
current_key=$(grep "^${CONTAINER_NAME_UPPER}_API_KEY=" .env | cut -d'=' -f2)
if [ "$current_key" != "$api_key" ]; then
echo -e "${BLUE}Updating ${CONTAINER_NAME_UPPER}_API_KEY in .env file${NC}"
sed -i.bak "s|^${CONTAINER_NAME_UPPER}_API_KEY=.*|${CONTAINER_NAME_UPPER}_API_KEY=$api_key|" .env
rm -f .env.bak
else
echo -e "${GREEN}${CONTAINER_NAME_UPPER}_API_KEY already up to date in .env file${NC}"
fi
fi
echo -e "${GREEN}Restarting $container to apply changes...${NC}"
if ! docker compose restart "$container"; then
echo -e "${RED}Failed to restart $container${NC}"
return 1
fi
echo -e "${GREEN}Successfully configured $container${NC}"
return 0
} }
update_qbittorrent_config() { update_qbittorrent_config() {
local container=$1 local container=$1
echo -e "${BLUE}Updating ${container} configuration...${NC}" echo -e "${BLUE}Configuring $container WebUI password...${NC}"
docker compose stop "$container"
until [ -f "${CONFIG_ROOT:-.}"/"$container"/qBittorrent/qBittorrent.conf ]; do sleep 1; done
sed -i.bak '/WebUI\\ServerDomains=*/a WebUI\\Password_PBKDF2="@ByteArray(ARQ77eY1NUZaQsuDHbIMCA==:0WMRkYTUWVT9wVvdDtHAjU9b3b7uB8NR1Gur2hmQCvCDpm39Q+PsJRJPaCU51dEiz+dTzh8qbPsL8WkFljQYFQ==)"' "${CONFIG_ROOT:-.}"/"$container"/qBittorrent/qBittorrent.conf && rm "${CONFIG_ROOT:-.}"/"$container"/qBittorrent/qBittorrent.conf.bak
echo -e "${GREEN}Update of ${container} configuration complete, restarting...${NC}" # Stop the container to ensure we can modify the config
docker compose start "$container" echo -e "${BLUE}Stopping $container to update configuration...${NC}"
if ! docker compose stop "$container"; then
echo -e "${RED}Failed to stop $container${NC}"
return 1
fi
# Check if config file exists
config_file="${CONFIG_ROOT:-.}/$container/qBittorrent/qBittorrent.conf"
timeout=10
count=0
while [ ! -f "$config_file" ] && [ $count -lt $timeout ]; do
echo -e "${YELLOW}Waiting for $container config file to be available... ($(($timeout-$count)) seconds remaining)${NC}"
sleep 1
count=$((count+1))
done
if [ ! -f "$config_file" ]; then
echo -e "${RED}Error: Config file for $container not found after $timeout seconds${NC}"
echo -e "${YELLOW}Is $container configured properly? Check the container logs${NC}"
# Restart the container anyway
docker compose start "$container"
return 1
fi
# Check if password is already set
if grep -q "WebUI\\\\Password_PBKDF2=" "$config_file"; then
echo -e "${GREEN}Password already configured in $container${NC}"
else
echo -e "${BLUE}Setting default password for $container WebUI${NC}"
sed -i.bak '/WebUI\\\\ServerDomains=*/a WebUI\\\\Password_PBKDF2="@ByteArray(ARQ77eY1NUZaQsuDHbIMCA==:0WMRkYTUWVT9wVvdDtHAjU9b3b7uB8NR1Gur2hmQCvCDpm39Q+PsJRJPaCU51dEiz+dTzh8qbPsL8WkFljQYFQ==)"' "$config_file"
if [ $? -ne 0 ]; then
echo -e "${RED}Failed to update password in $config_file${NC}"
docker compose start "$container"
return 1
fi
rm -f "${config_file}.bak"
echo -e "${GREEN}Default password set. Username: admin / Password: adminadmin${NC}"
echo -e "${YELLOW}You should change this password in the WebUI settings${NC}"
fi
echo -e "${GREEN}Starting $container with updated configuration...${NC}"
if ! docker compose start "$container"; then
echo -e "${RED}Failed to start $container${NC}"
return 1
fi
echo -e "${GREEN}Successfully configured $container${NC}"
return 0
} }
update_bazarr_config() { update_bazarr_config() {
local container=$1 local container=$1
echo -e "${BLUE}Updating ${container} configuration...${NC}" echo -e "${BLUE}Configuring $container URL base and service connections...${NC}"
until [ -f "${CONFIG_ROOT:-.}"/"$container"/config/config/config.yaml ]; do sleep 1; done
sed -i.bak "s/base_url: ''/base_url: '\/$container'/" "${CONFIG_ROOT:-.}"/"$container"/config/config/config.yaml && rm "${CONFIG_ROOT:-.}"/"$container"/config/config/config.yaml.bak
sed -i.bak "s/use_radarr: false/use_radarr: true/" "${CONFIG_ROOT:-.}"/"$container"/config/config/config.yaml && rm "${CONFIG_ROOT:-.}"/"$container"/config/config/config.yaml.bak
sed -i.bak "s/use_sonarr: false/use_sonarr: true/" "${CONFIG_ROOT:-.}"/"$container"/config/config/config.yaml && rm "${CONFIG_ROOT:-.}"/"$container"/config/config/config.yaml.bak
until [ -f "${CONFIG_ROOT:-.}"/sonarr/config.xml ]; do sleep 1; done # Check if config file exists
SONARR_API_KEY=$(sed -n 's/.*<ApiKey>\(.*\)<\/ApiKey>.*/\1/p' "${CONFIG_ROOT:-.}"/sonarr/config.xml) config_file="${CONFIG_ROOT:-.}/$container/config/config/config.yaml"
sed -i.bak "/sonarr:/,/^radarr:/ { s/apikey: .*/apikey: $SONARR_API_KEY/; s/base_url: .*/base_url: \/sonarr/; s/ip: .*/ip: sonarr/ }" "${CONFIG_ROOT:-.}"/"$container"/config/config/config.yaml && rm "${CONFIG_ROOT:-.}"/"$container"/config/config/config.yaml.bak timeout=15
count=0
while [ ! -f "$config_file" ] && [ $count -lt $timeout ]; do
echo -e "${YELLOW}Waiting for $container config file to be available... ($(($timeout-$count)) seconds remaining)${NC}"
sleep 1
count=$((count+1))
done
until [ -f "${CONFIG_ROOT:-.}"/radarr/config.xml ]; do sleep 1; done if [ ! -f "$config_file" ]; then
RADARR_API_KEY=$(sed -n 's/.*<ApiKey>\(.*\)<\/ApiKey>.*/\1/p' "${CONFIG_ROOT:-.}"/radarr/config.xml) echo -e "${RED}Error: Config file for $container not found after $timeout seconds${NC}"
sed -i.bak "/radarr:/,/^sonarr:/ { s/apikey: .*/apikey: $RADARR_API_KEY/; s/base_url: .*/base_url: \/radarr/; s/ip: .*/ip: radarr/ }" "${CONFIG_ROOT:-.}"/"$container"/config/config/config.yaml && rm "${CONFIG_ROOT:-.}"/"$container"/config/config/config.yaml.bak echo -e "${YELLOW}Is $container configured properly? Check the container logs${NC}"
return 1
fi
sed -i.bak 's/^BAZARR_API_KEY=.*/BAZARR_API_KEY='"$(sed -n 's/.*apikey: \(.*\)*/\1/p' "${CONFIG_ROOT:-.}"/"$container"/config/config/config.yaml | head -n 1)"'/' .env && rm .env.bak # Update URL base
if grep -q "base_url: '/$container'" "$config_file"; then
echo -e "${GREEN}URL base already set to /$container${NC}"
else
echo -e "${BLUE}Setting URL base to /$container${NC}"
sed -i.bak "s|base_url: .*|base_url: '/$container'|" "$config_file"
if [ $? -ne 0 ]; then
echo -e "${RED}Failed to update URL base in $config_file${NC}"
return 1
fi
rm -f "${config_file}.bak"
fi
echo -e "${GREEN}Update of ${container} configuration complete, restarting...${NC}" # Configure Radarr integration
docker compose restart "$container" updates_needed=false
if grep -q "use_radarr: false" "$config_file"; then
echo -e "${BLUE}Enabling Radarr integration${NC}"
sed -i.bak "s/use_radarr: false/use_radarr: true/" "$config_file"
rm -f "${config_file}.bak"
updates_needed=true
else
echo -e "${GREEN}Radarr integration already enabled${NC}"
fi
# Configure Sonarr integration
if grep -q "use_sonarr: false" "$config_file"; then
echo -e "${BLUE}Enabling Sonarr integration${NC}"
sed -i.bak "s/use_sonarr: false/use_sonarr: true/" "$config_file"
rm -f "${config_file}.bak"
updates_needed=true
else
echo -e "${GREEN}Sonarr integration already enabled${NC}"
fi
# Get Sonarr API key if needed
sonarr_config="${CONFIG_ROOT:-.}/sonarr/config.xml"
if [ -f "$sonarr_config" ]; then
sonarr_api_key=$(sed -n 's/.*<ApiKey>\(.*\)<\/ApiKey>.*/\1/p' "$sonarr_config")
if [ -n "$sonarr_api_key" ]; then
echo -e "${BLUE}Setting Sonarr API key and URL in Bazarr config${NC}"
sed -i.bak "/sonarr:/,/^radarr:/ { s/apikey: .*/apikey: $sonarr_api_key/; s|base_url: .*|base_url: /sonarr|; s/ip: .*/ip: sonarr/ }" "$config_file"
rm -f "${config_file}.bak"
updates_needed=true
else
echo -e "${YELLOW}Could not find Sonarr API key${NC}"
fi
else
echo -e "${YELLOW}Sonarr config not found, skipping Sonarr API configuration${NC}"
fi
# Get Radarr API key if needed
radarr_config="${CONFIG_ROOT:-.}/radarr/config.xml"
if [ -f "$radarr_config" ]; then
radarr_api_key=$(sed -n 's/.*<ApiKey>\(.*\)<\/ApiKey>.*/\1/p' "$radarr_config")
if [ -n "$radarr_api_key" ]; then
echo -e "${BLUE}Setting Radarr API key and URL in Bazarr config${NC}"
sed -i.bak "/radarr:/,/^sonarr:/ { s/apikey: .*/apikey: $radarr_api_key/; s|base_url: .*|base_url: /radarr|; s/ip: .*/ip: radarr/ }" "$config_file"
rm -f "${config_file}.bak"
updates_needed=true
else
echo -e "${YELLOW}Could not find Radarr API key${NC}"
fi
else
echo -e "${YELLOW}Radarr config not found, skipping Radarr API configuration${NC}"
fi
# Extract Bazarr API key for .env
bazarr_api_key=$(sed -n 's/.*apikey: \(.*\)*/\1/p' "$config_file" | head -n 1)
if [ -n "$bazarr_api_key" ]; then
current_key=$(grep "^BAZARR_API_KEY=" .env | cut -d'=' -f2)
if [ "$current_key" != "$bazarr_api_key" ]; then
echo -e "${BLUE}Updating BAZARR_API_KEY in .env file${NC}"
sed -i.bak "s|^BAZARR_API_KEY=.*|BAZARR_API_KEY=$bazarr_api_key|" .env
rm -f .env.bak
else
echo -e "${GREEN}BAZARR_API_KEY already up to date in .env file${NC}"
fi
else
echo -e "${YELLOW}Could not find Bazarr API key${NC}"
fi
if [ "$updates_needed" = true ]; then
echo -e "${GREEN}Restarting $container to apply changes...${NC}"
if ! docker compose restart "$container"; then
echo -e "${RED}Failed to restart $container${NC}"
return 1
fi
else
echo -e "${GREEN}No configuration changes needed for $container${NC}"
fi
echo -e "${GREEN}Successfully configured $container${NC}"
return 0
} }
update_service_configs() { update_service_configs() {
@ -345,18 +529,86 @@ update_service_configs() {
return 1 return 1
fi fi
echo -e "${BLUE}Checking for running containers to update...${NC}" # Keep track of what we updated
for container in $(docker ps --format '{{.Names}}'); do updated_containers=()
if [[ "$container" =~ ^(radarr|sonarr|lidarr|prowlarr)$ ]]; then not_running_containers=()
update_arr_config "$container" "$container" skipped_containers=()
elif [[ "$container" =~ ^(bazarr)$ ]]; then
update_bazarr_config "$container" echo -e "${BLUE}Checking for containers to update...${NC}"
elif [[ "$container" =~ ^(qbittorrent)$ ]]; then
update_qbittorrent_config "$container" # Define the list of containers we can update
declare -a configurable_containers=("radarr" "sonarr" "lidarr" "prowlarr" "bazarr" "qbittorrent")
# First check which containers we should be able to update
for container in "${configurable_containers[@]}"; do
if ! docker ps --format '{{.Names}}' | grep -q "^${container}$"; then
not_running_containers+=("$container")
fi fi
done done
# Now update the running containers
for container in $(docker ps --format '{{.Names}}'); do
if [[ "$container" =~ ^(radarr|sonarr|lidarr|prowlarr)$ ]]; then
echo -e "\n${CYAN}Updating ${BOLD}$container${NC} configuration..."
if update_arr_config "$container" "$container"; then
updated_containers+=("$container")
else
skipped_containers+=("$container (error during update)")
fi
elif [[ "$container" =~ ^(bazarr)$ ]]; then
echo -e "\n${CYAN}Updating ${BOLD}$container${NC} configuration..."
if update_bazarr_config "$container"; then
updated_containers+=("$container")
else
skipped_containers+=("$container (error during update)")
fi
elif [[ "$container" =~ ^(qbittorrent)$ ]]; then
echo -e "\n${CYAN}Updating ${BOLD}$container${NC} configuration..."
if update_qbittorrent_config "$container"; then
updated_containers+=("$container")
else
skipped_containers+=("$container (error during update)")
fi
fi
done
# Print summary
echo -e "\n${GREEN}${BOLD}Service Configuration Update Complete!${NC}" echo -e "\n${GREEN}${BOLD}Service Configuration Update Complete!${NC}"
echo -e "${BLUE}Summary:${NC}"
if [[ ${#updated_containers[@]} -gt 0 ]]; then
echo -e " ${GREEN}Successfully updated configurations for:${NC}"
for container in "${updated_containers[@]}"; do
echo -e " - ${CYAN}$container${NC}"
done
else
echo -e " ${YELLOW}No container configurations were updated${NC}"
fi
if [[ ${#not_running_containers[@]} -gt 0 ]]; then
echo -e " ${YELLOW}The following containers were not running (skipped):${NC}"
for container in "${not_running_containers[@]}"; do
echo -e " - ${YELLOW}$container${NC}"
done
echo -e " ${BLUE}Start these containers and run the script again to configure them${NC}"
fi
if [[ ${#skipped_containers[@]} -gt 0 ]]; then
echo -e " ${RED}Failed to update configurations for:${NC}"
for container in "${skipped_containers[@]}"; do
echo -e " - ${RED}$container${NC}"
done
fi
# If we've extracted any API keys, show them
if grep -q "_API_KEY=" .env; then
echo -e "\n${BLUE}${BOLD}API Keys:${NC}"
echo -e "${BLUE}The following API keys were extracted and updated in your .env file:${NC}"
grep "_API_KEY=" .env | sort | while read -r line; do
key=$(echo "$line" | cut -d'=' -f1)
echo -e " - ${CYAN}$key${NC}"
done
fi
} }
################################################## ##################################################