
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: enable-web-station-websocket.sh
204 lines
5.7 KiB
Bash
204 lines
5.7 KiB
Bash
#!/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
|
|
|
|
nginxServiceTemplate='/var/packages/WebStation/target/misc/nginx_service_template.mustache'
|
|
|
|
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"
|
|
|
|
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
|
|
|
|
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)
|
|
if [[ $dockerContainerCount == 0 ]]; then
|
|
echo -e $RED"not found Containers exit"$END
|
|
fi
|
|
|
|
echo -e "found docker container count: $RED$dockerContainerCount"$END
|
|
|
|
dispalyServices() {
|
|
jq -r 'map(.display_name) | to_entries | map((.key + 1 | tostring) + ") " + .value) | .[]' <<<$webStationService
|
|
}
|
|
|
|
selectService() {
|
|
dispalyServices
|
|
while read -p "请输入启动Websocket的服务序号# " index; do
|
|
if ((index - 1 < 0)); then
|
|
dispalyServices
|
|
continue
|
|
fi
|
|
service=$(jq -e -c ".[$((index - 1))]" <<<$webStationService)
|
|
if [[ $? == 0 ]]; then
|
|
break
|
|
fi
|
|
dispalyServices
|
|
done <&1
|
|
}
|
|
|
|
modifyNginxServiceTemplate() {
|
|
local backup="$nginxServiceTemplate.$(date +%Y%m%d%H%M).bak"
|
|
if [[ ! -f $backup ]]; then
|
|
echo "备份 nginx service template文件到: $backup"
|
|
cp $nginxServiceTemplate $backup
|
|
fi
|
|
sed -i 's/{{> @proxy_rule@ }}/{{> @proxy_rule@ }}\n\n \/usr\/local\/etc\/nginx\/conf.d\/{{service_id}}\/proxy.conf*;\n\n/' $nginxServiceTemplate
|
|
checkNginxServiceTemplate
|
|
}
|
|
|
|
checkNginxServiceTemplate() {
|
|
if [[ $(grep '/usr/local/etc/nginx/conf.d/{{service_id}}/proxy.conf' $nginxServiceTemplate) == '' ]]; then
|
|
echo "nginx service 自定义location不存在 添加指令到模版"
|
|
modifyNginxServiceTemplate
|
|
else
|
|
echo "nginx service 自定义location已经存在"
|
|
fi
|
|
}
|
|
|
|
modifyCustomNginxProxy() {
|
|
local proxyConfig="/usr/local/etc/nginx/conf.d/$1/proxy.conf"
|
|
local backup="/usr/local/etc/nginx/conf.d/$1/proxy.conf.$(date +%Y%m%d%H%M).bak"
|
|
if [[ -f $proxyConfig ]]; then
|
|
|
|
if [[ $(cat $proxyConfig | grep proxy_set_header | grep Connection | grep '$connection_upgrade') == '' ]]; then
|
|
echo "自定义 proxy.conf 中没有启用websocket\n开始启用websocket"
|
|
|
|
if [[ ! -f $backup ]]; then
|
|
cp $proxyConfig $backup
|
|
echo "备份 proxy.conf 到 $backup"
|
|
fi
|
|
|
|
cat >>$proxyConfig <<EOF
|
|
|
|
# 启用websocket脚本自动生成 请勿删除
|
|
proxy_set_header Connection \$connection_upgrade;
|
|
|
|
EOF
|
|
|
|
if [[ $? == 0 ]]; then
|
|
echo "写出配置文件成功 配置文件如下: "
|
|
cat $proxyConfig
|
|
return 0
|
|
else
|
|
echo "写出配置文件失败 请检查错误信息"
|
|
exit 2
|
|
fi
|
|
|
|
else
|
|
echo "自定义 proxy.conf 已启用websocket 不需要操作"
|
|
return 1
|
|
fi
|
|
else
|
|
echo "自定义 proxy.conf 不存在生成文件"
|
|
mkdir -p "/usr/local/etc/nginx/conf.d/$1"
|
|
|
|
cat >$proxyConfig <<EOF
|
|
|
|
# 启用websocket脚本自动生成 请勿删除
|
|
proxy_set_header Connection \$connection_upgrade;
|
|
|
|
EOF
|
|
|
|
if [[ $? == 0 ]]; then
|
|
echo "写出文件成功文件内容如下: "
|
|
cat $proxyConfig
|
|
return 0
|
|
else
|
|
echo "写出配置文件失败 请检查错误信息"
|
|
exit 2
|
|
fi
|
|
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"
|
|
}
|
|
|
|
selectService
|
|
|
|
serviceId=$(jq -r '.service' <<<$service)
|
|
serviceName=$(jq -r "map(select(.service.id == \"$serviceId\")) | .[] | .name" <<<$dockerContainer)
|
|
|
|
if [[ -f $nginxServiceTemplate ]]; then
|
|
checkNginxServiceTemplate
|
|
modifyCustomNginxProxy $serviceId
|
|
if [[ $? == 0 ]]; then
|
|
echo "配置信息已修改 重启容器中"
|
|
restartDockerContainer $serviceName
|
|
fi
|
|
else
|
|
echo "nginx template 文件不存在 请检查WebStation套件是否正常"
|
|
exit 1
|
|
fi
|