-
Notifications
You must be signed in to change notification settings - Fork 1
/
jenkins_sync_docker_images.sh
267 lines (239 loc) · 7.37 KB
/
jenkins_sync_docker_images.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#!/usr/bin/env bash
# set -x
cd "$(dirname "$0")" || return 1
SH_DIR=$(pwd)
ME=$0
PARAMETERS=$*
config_file="$1"
dest_registry="${DEST_HARBOR_REGISTRY:-library}"
dest_repo="${DEST_HARBOR_URL}/${dest_registry}" # 包含仓库项目的名字
thread=3 # 此处定义线程数
faillog="./failure.log" # 此处定义失败列表,注意失败列表会先被删除再重新写入
echo >>"$config_file" # 加行空行
#定义输出颜色函数
function red_echo() {
#用法: red_echo "内容"
local what="$*"
echo -e "$(date +%F-%T) \e[1;31m ${what} \e[0m"
}
function green_echo() {
#用法: green_echo "内容"
local what="$*"
echo -e "$(date +%F-%T) \e[1;32m ${what} \e[0m"
}
function yellow_echo() {
#用法: yellow_echo "内容"
local what="$*"
echo -e "$(date +%F-%T) \e[1;33m ${what} \e[0m"
}
function blue_echo() {
#用法: blue_echo "内容"
local what="$*"
echo -e "$(date +%F-%T) \e[1;34m ${what} \e[0m"
}
function twinkle_echo() {
#用法: twinkle_echo $(red_echo "内容") ,此处例子为红色闪烁输出
local twinkle='\e[05m'
local what="${twinkle} $*"
echo -e "$(date +%F-%T) ${what}"
}
function return_echo() {
if [ $? -eq 0 ]; then
echo -n "$* " && green_echo "成功"
return 0
else
echo -n "$* " && red_echo "失败"
return 1
fi
}
function return_error_exit() {
[ $? -eq 0 ] && local REVAL="0"
local what=$*
if [ "$REVAL" = "0" ]; then
[ ! -z "$what" ] && { echo -n "$* " && green_echo "成功"; }
else
red_echo "$* 失败,脚本退出"
exit 1
fi
}
# 定义确认函数
function user_verify_function() {
while true; do
echo ""
read -p "是否确认?[Y/N]:" Y
case $Y in
[yY] | [yY][eE][sS])
echo -e "answer: \\033[20G [ \e[1;32m是\e[0m ] \033[0m"
break
;;
[nN] | [nN][oO])
echo -e "answer: \\033[20G [ \e[1;32m否\e[0m ] \033[0m"
exit 1
;;
*)
continue
;;
esac
done
}
# 定义跳过函数
function user_pass_function() {
while true; do
echo ""
read -p "是否确认?[Y/N]:" Y
case $Y in
[yY] | [yY][eE][sS])
echo -e "answer: \\033[20G [ \e[1;32m是\e[0m ] \033[0m"
break
;;
[nN] | [nN][oO])
echo -e "answer: \\033[20G [ \e[1;32m否\e[0m ] \033[0m"
return 1
;;
*)
continue
;;
esac
done
}
function check_image() {
local image_name=$1
local image_tag=$2
local encoded
# encoded=$(curl -s -o /dev/null -w %{url_effective} --get --data-urlencode "$image_name" "http://localhost")
# # 移除前缀部分,只保留编码后的结果
# encoded=$(echo $encoded | sed 's@http://localhost/?@@')
# 二次编码 e.g. a/b -> a%2Fb -> a%252Fb
encoded=$(echo $image_name | sed 's@/@%252F@g')
curl -s -i --connect-timeout 10 -m 20 -u "$DEST_HARBOR_CRE_USR:$DEST_HARBOR_CRE_PSW" -k -X GET \
-H "accept: application/json" \
"https://$DEST_HARBOR_URL/api/v2.0/projects/$dest_registry/repositories/$encoded/artifacts/$image_tag/tags?page=1&page_size=10&with_signature=false&with_immutable_status=false" |
grep '"name":' >/dev/null
return $?
}
function check_skopeo() {
command -v skopeo &>/dev/null
}
function skopeo_sync_image() {
local line=$1
local image_name=$2
local image_tag=$3
skopeo -v && skopeo copy -a \
--dest-creds=${DEST_HARBOR_CRE_USR}:${DEST_HARBOR_CRE_PSW} \
${BUILD_ARGS} \
docker://${line} \
docker://$dest_repo/$image_name:$image_tag
return $?
}
function docker_login() {
# echo "${SRC_HARBOR_CRE_PSW}" | docker login --username "${SRC_HARBOR_CRE_USR}" --password-stdin $SRC_HARBOR_URL
echo "${DEST_HARBOR_CRE_PSW}" | docker login --username "${DEST_HARBOR_CRE_USR}" --password-stdin $DEST_HARBOR_URL
}
function docker_sync_image() {
local line=$1
local image_name=$2
local image_tag=$3
docker pull $line &&
docker tag $line $dest_repo/$image_name:$image_tag &&
docker push $dest_repo/$image_name:$image_tag &&
docker rmi $line &&
docker rmi $dest_repo/$image_name:$image_tag ||
{
red_echo "同步镜像[ $line ]"
echo "$line" | tee -a $faillog
}
}
function sync_image() {
local line=$*
local image_name
local image_tag
line=$(echo "$line" | sed '[email protected]/@@')
if [[ ! -z $(echo "$line" | grep '/') ]]; then
case $dest_registry in
library)
image_name=$(echo $line | awk -F':|/' '{print $(NF-2)"/"$(NF-1)}')
;;
*)
image_name=$(echo $line | awk -F':|/' '{print $(NF-1)}')
;;
esac
if [[ ! -z $(echo "$image_name" | grep -w "$dest_registry") ]]; then
image_name=$(basename $image_name)
fi
else
image_name=$(echo ${line%:*})
fi
image_name=$(echo $image_name | sed '[email protected]/@@' | sed '[email protected]/@@')
image_tag=$(echo $line | awk -F: '{print $2}')
check_image $image_name $image_tag
return_echo "检测镜像 [$image_name] 存在 "
if [ $? -ne 0 ]; then
echo
yellow_echo "同步镜像[ $line ]"
if [ "$have_skopeo" -eq 0 ]; then
skopeo_sync_image "$line" "$image_name" "$image_tag" || docker_sync_image "$line" "$image_name" "$image_tag"
else
docker_sync_image "$line" "$image_name" "$image_tag"
fi
else
green_echo "已存在镜像,不需要推送[$dest_repo/$image_name:$image_tag]"
return 0
fi
}
function usage() {
echo "sh $ME config.txt"
}
if [ -z "$PARAMETERS" ]; then
usage
exit 55
fi
function trap_exit() {
kill -9 0
}
function multi_process() {
trap 'trap_exit;exit 2' 1 2 3 15
if [ -f $faillog ]; then
rm -f $faillog
fi
tmp_fifofile="./$$.fifo"
mkfifo $tmp_fifofile # 新建一个fifo类型的文件
exec 6<>$tmp_fifofile # 将fd6指向fifo类型
rm $tmp_fifofile
for ((i = 0; i < $thread; i++)); do
echo
done >&6 # 事实上就是在fd6中放置了$thread个回车符
exec 5<$config_file
while read line <&5; do
excute_line=$(echo "$line" | grep -E -v "^#")
if [ -z "$excute_line" ]; then
continue
fi
read -u6
# 一个read -u6命令执行一次,就从fd6中减去一个回车符,然后向下执行,
# fd6中没有回车符的时候,就停在这了,从而实现了线程数量控制
{ # 此处子进程开始执行,被放到后台
sync_image $excute_line
echo >&6 # 当进程结束以后,再向fd6中加上一个回车符,即补上了read -u6减去的那个
} &
done
wait # 等待所有的后台子进程结束
exec 6>&- # 关闭df6
if [ -f $faillog ]; then
echo "#############################"
red_echo "Has failure job list:"
echo
cat $faillog
echo "#############################"
exit 1
else
green_echo "All finish"
echo "#############################"
fi
}
check_skopeo
have_skopeo=$?
if [ "$have_skopeo" -ne 0 ]; then
docker_login
fi
multi_process
exit 0