synology-scripts/auto-reload-web-station-docker-reverse-proxy.sh
nap.liu 189129bd10 优化判断使用 [[
Please enter the commit message for your changes. Lines starting
 with '' will be ignored, and an empty message aborts the commit.

 On branch master
 Changes to be committed:
	modified:   auto-add-bt-trackerlist.sh
	modified:   auto-reload-web-station-docker-reverse-proxy.sh
	modified:   enable-web-station-websocket.sh
2023-07-01 12:28:50 +08:00

178 lines
5.6 KiB
Bash
Executable File

#!/bin/bash
DISABLE_COLOR=true
BLACK="\e[30m"
RED="\e[31m"
GREEN="\e[32m"
YELLOW="\e[33m"
BLUE="\e[34m"
MAGENTA="\e[35m"
CYAN="\e[36m"
LIGHT_GRAY="\e[37m"
DARK_GRAY="\e[90m"
LIGHT_RED="\e[91m"
LIGHT_GREEN="\e[92m"
LIGHT_YELLOW="\e[93m"
LIGHT_BLUE="\e[94m"
LIGHT_MAGENTA="\e[95m"
LIGHT_CYAN="\e[96m"
WHITE="\e[97m"
END="\e[0m"
if [[ $DISABLE_COLOR == true ]]; then
BLACK=""
RED=""
GREEN=""
YELLOW=""
BLUE=""
MAGENTA=""
CYAN=""
LIGHT_GRAY=""
DARK_GRAY=""
LIGHT_RED=""
LIGHT_GREEN=""
LIGHT_YELLOW=""
LIGHT_BLUE=""
LIGHT_MAGENTA=""
LIGHT_CYAN=""
WHITE=""
END=""
fi
restartDockerContainer() {
stop=$(synowebapi --exec api=SYNO.Docker.Container method=stop version=1 name=$1 2>/dev/null)
status=$(jq '.success' <<<$stop)
if [[ $status == false ]]; then
echo -e $RED"stop container: $1 error: $stop"$END
fi
start=$(synowebapi --exec api=SYNO.Docker.Container method=start version=1 name=$1 2>/dev/null)
status=$(jq '.success' <<<$start)
if [[ $status == false ]]; then
echo -e $RED"start container: $1 error: $stop"$END
fi
echo "restart container finished: $1"
}
checkServiceIsOffline() {
echo -e "checking container: $GREEN$1$END url: $GREEN$2$END"
result=$(curl -I $2 2>/dev/null | grep 200)
if [[ ${#result} -eq 0 ]]; then
echo -e "conatiner need restarted: $RED$1$END"
return 1
else
echo -e "container is$BLUE online$END"
return 0
fi
}
pakcageList=$(synowebapi --exec api=SYNO.Core.Package method=list version=2 additional='["status"]' 2>/dev/null | jq -c '.data.packages | map(select(.id == "WebStation" or .id == "ContainerManager"))')
isRunning=$(jq -r 'map(select(.id == "WebStation") | .additional.status) | .[]' <<<$pakcageList)
if [[ $isRunning != "running" ]]; then
echo -e "Web Station$RED not running$END exit"
exit 0
fi
echo "Web Station is running go next step"
isRunning=$(jq -r 'map(select(.id == "ContainerManager") | .additional.status) | .[]' <<<$pakcageList)
if [[ $isRunning != "running" ]]; then
echo -e "Container Manager$RED not running$END exit"
exit 0
fi
echo "Container Manager is running go next step"
webStationPortal=$(synowebapi --exec api=SYNO.WebStation.WebService.Portal method=list version=1 2>/dev/null | jq -c '.data.portals | map({host: .fqdn, http: .http_port[0], https: .https_port[0], service: .service, display_name: .shortcut.display_name})')
webStationPortalCount=$(jq 'length' <<<$webStationPortal)
if [[ $webStationPortalCount -eq 0 ]]; then
echo -e $RED"not found Web Station portal"$END
exit 0
fi
echo -e "found Web Station portal count: $RED$webStationPortalCount"$END
webStationService=$(synowebapi --exec api=SYNO.WebStation.WebService.Service method=list version=1 2>/dev/null | jq -c '.data.services | map(select(.category == "Docker"))')
webStationServiceCount=$(jq 'length' <<<$webStationService)
if [[ $webStationServiceCount -eq 0 ]]; then
echo -e $RED"not found Web Station services exit"$END
exit 0
fi
echo -e "found Web Station services count: $RED$webStationServiceCount"$END
dockerContainer=$(synowebapi --exec api=SYNO.Docker.Container method=list version=1 limit=-1 offset=0 type=all 2>/dev/null | jq -c '.data.containers | map(. | select(.status=="running" and .services[0] != null) | {name: .name, status: .status, id: .id, service: .services[0]})')
dockerContainerCount=$(jq 'length' <<<$dockerContainer)
echo -e "found docker container count: $RED$dockerContainerCount"$END
for idx in $(seq 0 $(($webStationServiceCount - 1))); do
echo "-------------------[$((idx + 1))]---------------------"
service=$(jq -c ".[$idx]" <<<$webStationService)
serviceName=$(jq -r '.display_name' <<<$service)
serviceId=$(jq '.service' <<<$service)
serviceEnable=$(jq '.enable' <<<$service)
portal=$(jq -c "map(select(.service == $serviceId)) | .[]" <<<$webStationPortal)
portalHost=$(jq -r '.host' <<<$portal)
echo -e "service: $GREEN$serviceName$END enable: $BLUE$serviceEnable$END"
if [[ $serviceEnable == true ]]; then
echo -e "reverse proxy: $GREEN$portalHost$END ➡️ $GREEN$serviceName$END is$BLUE running$END skip"
continue
fi
if [[ ${#portal} -ne 0 ]]; then
container=$(jq -c "map(select(.service.service == $serviceId)) | .[]" <<<$dockerContainer)
if [[ ${#container} -eq 0 ]]; then
echo "service: $GREEN$serviceName$END container$RED is not found skip$END"
continue
fi
containerName=$(jq -r '.name' <<<$container)
containerStatus=$(jq -r '.status' <<<$container)
echo -e "service container: $GREEN$containerName$END status: $GREEN$containerStatus$END"
if [[ $containerStatus != 'running' ]]; then
echo -e "contianer$GREEN $containerName$END is$RED not running$END skip"
continue
fi
httpPort=$(jq '.http' <<<$portal)
httpsPort=$(jq '.https' <<<$portal)
proxyTarget=$(jq -r '.proxy_target' <<<$service)
httpOffline=0
httpsOffline=0
if [[ ${#httpPort} -ne 0 ]]; then
echo -e "reverse proxy found$GREEN http://$portalHost:$httpsPort $END➡️ $GREEN $proxyTarget $END"
checkServiceIsOffline $containerName "http://$portalHost:$httpPort"
httpOffline=$?
fi
if [[ ${#httpsPort} -ne 0 ]]; then
echo -e "reverse proxy found$GREEN https://$portalHost:$httpsPort $END➡️ $GREEN $proxyTarget $END"
checkServiceIsOffline $containerName "https://$portalHost:$httpsPort"
httpsOffline=$?
fi
if [[ $httpOffline -eq 1 || $httpsOffline -eq 1 ]]; then
restartDockerContainer $containerName
else
echo "service: $RED$serviceName is$BLUE online$END proccess next"
fi
else
echo -e "service:$RED $serviceName is not found $END"
fi
done
echo "script finished"