-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup-watchtower.sh
214 lines (191 loc) · 6.47 KB
/
setup-watchtower.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/bin/bash
# 定义变量
ROOT_DIR="/root"
WATCHTOWER_DIR="$ROOT_DIR/watchtower"
COMPOSE_FILE="$WATCHTOWER_DIR/docker-compose.yml"
SERVICE_NAME="watchtower"
DEFAULT_COMMAND="--interval 300" # 默认监控所有容器
DOCKER_INSTALL_SCRIPT="https://get.docker.com/"
DOCKER_COMPOSE_VERSION=$(curl -s https://api.github.com/repos/docker/compose/releases/latest | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
# 检查 Docker 是否安装
install_docker() {
if ! command -v docker &>/dev/null; then
echo "Docker 未安装,正在从官方源安装..."
curl -fsSL "$DOCKER_INSTALL_SCRIPT" | bash
systemctl start docker
systemctl enable docker
echo "Docker 已成功安装。"
else
echo "Docker 已安装。"
fi
}
# 检查 Docker Compose 是否安装
install_docker_compose() {
if ! command -v docker-compose &>/dev/null; then
echo "Docker Compose 未安装,正在从官方源安装..."
curl -L "https://github.com/docker/compose/releases/download/$DOCKER_COMPOSE_VERSION/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
echo "Docker Compose $DOCKER_COMPOSE_VERSION 已成功安装。"
else
echo "Docker Compose 已安装。"
fi
}
# 创建 Watchtower 目录
create_watchtower_dir() {
if [[ ! -d "$WATCHTOWER_DIR" ]]; then
echo "创建 Watchtower 文件夹:$WATCHTOWER_DIR"
mkdir -p "$WATCHTOWER_DIR"
fi
cd "$WATCHTOWER_DIR" || exit
}
# 创建 Docker Compose 文件
create_compose_file() {
echo "创建默认 Docker Compose 配置文件..."
cat <<EOF >"$COMPOSE_FILE"
services:
$SERVICE_NAME:
image: containrrr/watchtower
container_name: $SERVICE_NAME
volumes:
- /var/run/docker.sock:/var/run/docker.sock
environment:
- WATCHTOWER_CLEANUP=true
command: $DEFAULT_COMMAND
restart: unless-stopped
EOF
echo "默认配置已保存到:$COMPOSE_FILE"
}
# 列出当前所有容器
list_all_containers() {
echo "当前 Docker 容器列表:"
docker ps -a --format "table {{.Names}}\t{{.Status}}"
}
# 验证容器名称是否存在
validate_container_names() {
local invalid_containers=()
for container in $1; do
if ! docker ps -a --format "{{.Names}}" | grep -wq "$container"; then
invalid_containers+=("$container")
fi
done
echo "${invalid_containers[@]}"
}
# 读取现有监控的配置
read_current_configuration() {
if [[ -f "$COMPOSE_FILE" ]]; then
current_command=$(grep "^ command:" "$COMPOSE_FILE" | awk '{$1=""; print $0}')
echo "当前监控配置为:$current_command"
else
echo "未检测到现有配置。"
fi
}
# 检测并移除同名容器(首次执行时)
remove_existing_container() {
echo "检测是否有手动启动的 Watchtower 容器..."
if docker ps -a --format "{{.Names}}" | grep -qw "$SERVICE_NAME"; then
# 检查容器是否由 docker-compose 管理
is_compose_managed=$(docker inspect --format '{{index .Config.Labels "com.docker.compose.project"}}' "$SERVICE_NAME" 2>/dev/null)
if [[ -z "$is_compose_managed" ]]; then
echo "检测到同名容器 $SERVICE_NAME,为手动启动,正在停止并移除..."
docker stop "$SERVICE_NAME"
docker rm "$SERVICE_NAME"
echo "手动启动的 $SERVICE_NAME 容器已成功移除。"
else
echo "检测到同名容器 $SERVICE_NAME,由 docker-compose 管理,跳过移除操作。"
fi
else
echo "未检测到运行中的 $SERVICE_NAME 容器。"
fi
}
# 更新或新增监控容器
update_or_add_containers() {
echo "请选择操作:"
echo "1) 新增监控容器(保留现有配置)"
echo "2) 覆盖监控容器(重新定义配置)"
echo "3) 恢复监控所有容器(需要用户确认)"
read -r choice
case $choice in
1)
# 新增容器
list_all_containers
echo "请输入要新增监控的容器名称,多个容器用空格分隔:"
read -r new_containers
invalid_containers=$(validate_container_names "$new_containers")
if [[ -n "$invalid_containers" ]]; then
echo "以下容器名称无效或不存在:$invalid_containers"
echo "请检查输入并重新运行脚本。"
return
fi
if [[ -n "$new_containers" ]]; then
updated_command=$(grep "^ command:" "$COMPOSE_FILE" | awk '{$1=""; print $0}')
for container in $new_containers; do
updated_command="$updated_command $container"
done
sed -i "/^ command:/c\ command: $updated_command" "$COMPOSE_FILE"
echo "已新增监控的容器:$new_containers"
else
echo "未新增任何容器,保持现有配置。"
fi
;;
2)
# 覆盖配置
list_all_containers
echo "请输入要监控的容器名称,多个容器用空格分隔(留空表示监控所有容器):"
read -r containers
invalid_containers=$(validate_container_names "$containers")
if [[ -n "$invalid_containers" ]]; then
echo "以下容器名称无效或不存在:$invalid_containers"
echo "请检查输入并重新运行脚本。"
return
fi
specific_command="$DEFAULT_COMMAND"
for container in $containers; do
specific_command="$specific_command $container"
done
sed -i "/^ command:/c\ command: $specific_command" "$COMPOSE_FILE"
echo "已覆盖监控的容器配置为:${containers:-所有容器}"
;;
3)
# 恢复监控所有容器
echo "确认恢复为监控所有容器?这将覆盖现有配置。[y/N]"
read -r confirm
if [[ "$confirm" =~ ^[Yy]$ ]]; then
sed -i "/^ command:/c\ command: $DEFAULT_COMMAND" "$COMPOSE_FILE"
echo "已恢复为默认监控所有容器。"
else
echo "操作已取消。"
fi
;;
*)
echo "无效选择,退出操作。"
;;
esac
}
# 启动或重启 Watchtower 容器
start_or_restart_watchtower() {
echo "启动或重启 Watchtower 容器..."
remove_existing_container
docker-compose -f "$COMPOSE_FILE" up -d
echo "Watchtower 已启动或更新配置生效。"
}
# 主流程
main() {
install_docker
install_docker_compose
create_watchtower_dir
if [[ ! -f "$COMPOSE_FILE" ]]; then
echo "未检测到 Watchtower 配置文件,正在初始化默认配置..."
create_compose_file
else
echo "检测到现有 Watchtower 配置文件。"
read_current_configuration
echo "是否需要修改当前配置?[y/N]"
read -r modify
if [[ "$modify" =~ ^[Yy]$ ]]; then
update_or_add_containers
fi
fi
start_or_restart_watchtower
}
# 执行主流程
main