- run 手动修改脚本内变量后执行
- harbor 通过harbor v2.0接口,获取镜像域名及信息后修改标签同步至其他镜像仓库中
如图所示,登录后可通过如下文件查看当前宿主机上登录的镜像仓库的信息
#################################################
#手动同步,需要提前修改,且镜像名称一一对应
#################################################
domain_old="registry.aliyuncs.com/google_containers" #待同步镜像的域名
image_name="pause:3.6 kube-apiserver:v1.19.0" #镜像名称及版本,以空格为分隔符
#################################################################################
domain_new="registry.harbor.com:8081/test" #通过镜像仓库的域名及项目名称,用于docker push
image_name_new="pause:3.6 kube-apiserver:v1.19.0" #镜像名称及版本,以空格为分隔符,与上面一一对应
#Harbor信息
Harbor_Address=http://registry.harbor.com:8081 #Harbor主机地址,需要本地/etc/hosts解析
Harbor_domain="registry.harbor.com:8081" #镜像域名,用于docker pull
Harbor_User=admin #登录Harbor的用户
Harbor_Passwd=Harbor12345 #登录Harbor的用户密码
Harbor_Project_List="bcbx" #Harbor项目名称,以空格未分隔,填写多个项目名称
sh Syncimages.sh harbor
sh Syncimages.sh run
#!/bin/bash
#所有者:北城半夏
#
#当前版本
#v1.0.1
#################################################################################
# 定义变量 #
#################################################################################
#日志文件
bashpath="$(cd `dirname $0`;pwd)"
#bashpath="/root/run/log/docker"
[ -d ${bashpath} ] || mkdir -p ${bashpath}
Images_File=${bashpath}/harbor-images-`date '+%Y-%m-%d'`.txt
#################################################
#手动同步,需要提前修改,且镜像名称一一对应
domain_old="registry.aliyuncs.com/google_containers"
image_name="pause:3.6 kube-apiserver:v1.19.0"
#################################################################################
domain_new="registry.harbor.com:8081/test"
image_name_new="pause:3.6 kube-apiserver:v1.19.0"
#################################################
#Harbor信息
Harbor_Address=http://registry.harbor.com:8081 #Harbor主机地址,需要本地/etc/hosts解析
Harbor_domain="registry.harbor.com:8081"
Harbor_User=admin #登录Harbor的用户
Harbor_Passwd=Harbor12345 #登录Harbor的用户密码
Harbor_Project_List="bcbx" #Harbor项目名称,以空格未分隔,填写多个项目名称
#################################################################################
# 定义函数 #
#################################################################################
info(){
echo -e "\033[35m 【`hostname` `date '+%Y-%m-%d %H:%M:%S'`】\033[0m" "\033[36m$1\033[0m "
}
info2(){
echo -e "\033[32m 【`hostname` `date '+%Y-%m-%d %H:%M:%S'`】\033[0m" "\033[91m$1\033[0m "
}
info4(){
echo -e "\033[35m 【`hostname` `date '+%Y-%m-%d %H:%M:%S'`】\033[0m" "\033[94m$1\033[0m "
}
info3(){
echo -e "\033[36m------------------------------------------------------------------------------\033[0m"
}
#pull旧的镜像到镜像仓库中
docker_pull(){
local domain=$1 image_name=${image_old_name[*]}
for i in ${image_name}
do
image_old_lable=`docker images --format {{.Repository}}:{{.Tag}}|grep ${domain}/${i}`
if [ -z ${image_old_lable} ] ;then
docker pull ${domain}/${i}
#echo "docker pull ${domain}/${i}"
fi
done
[ $? -eq 0 ] && info "镜像pull成功" && info3
}
#tag修改标签
docker_tag(){
local domain_old=$1 domain_new=$2
for i in `seq 0 ${new_num}`
do
docker tag ${domain_old}/${image_old_name[i]} ${domain_new}/${image_new_name[i]}
#echo "docker tag ${domain_old}/${image_old_name[i]} ${domain_new}/${image_new_name[i]}"
[ $? -eq 0 ] && info "镜像tag ${domain_old}/${image_old_name[i]} ${domain_new}/${image_new_name[i]} 成功"
done
[ $? -eq 0 ] && info3
}
#tag修改标签
docker_push(){
local domain_new=$1 image_name=${image_name_new}
for i in ${image_name}
do
docker push ${domain_new}/${i}
# echo "docker push ${domain_new}/${i}"
[ $? -eq 0 ] && info "镜像push ${domain_new}/${i} 成功"
done
[ $? -eq 0 ] && info3
}
docker_rmi(){
local domain_new=$1 image_name=${image_name_new}
for i in ${image_name}
do
docker rmi ${domain_new}/${i}
#echo "docker rmi ${domain_new}/${i}"
[ $? -eq 0 ] && info2 "RMI image ${domain_new}/${i}成功"
done
[ $? -eq 0 ] && info3
}
#定义数组元素及变量
array_run(){
#定义数组(old image name)
for i in ${image_name};do image_old_name[${#image_old_name[*]}]=$i ;done
for i in ${image_name_new};do image_new_name[${#image_new_name[*]}]=$i ;done
old_num=$(( `echo ${#image_old_name[*]}` -1 ))
new_num=$(( `echo ${#image_new_name[*]}` -1 ))
if [ ${old_num} -eq ${new_num} ];then
info "image name load success"
fi
}
#################################################################################
harbor_get_list(){
if [ -f ${Images_File} ];then
rm -rf ${Images_File}
fi
for Project in $Harbor_Project_List;do
# 循环获取项目下所有的镜像
Image_Names=$(curl -u ${Harbor_User}:${Harbor_Passwd} -H "Content-Type: application/json" -X GET ${Harbor_Address}/api/v2.0/projects/$Project/repositories -k | python -m json.tool | grep name | awk '/"name": /' | awk -F '"' '{print $4}')
for Image in $Image_Names;do
# 循环获取镜像的版本(tag)
Image_Tags=$(curl -u ${Harbor_User}:${Harbor_Passwd} -H "Content-Type: application/json" -X GET ${Harbor_Address}/v2/$Image/tags/list -k | awk -F '"' '{print $8,$10,$12}')
for Tag in $Image_Tags;do
# 格式化输出镜像信息
echo "${Harbor_domain}/$Image:$Tag" >> ${Images_File}
done
done
done
}
harbor_pull(){
#pull镜像
info "Pull image to local machine"
for i in ${image_name}
do
docker pull ${i}
done
}
harbor_tag(){
#修改tag镜像
info3
info "Modify mirror label"
for i in `seq 0 ${old_num}`
do
name=`echo ${image_old_name[i]} |awk -F "/" '{print $NF}'`
docker tag ${image_old_name[i]} ${domain_new}/${name}
done
}
harbor_push(){
#推送镜像到仓库中
info3
info "Push image to warehouse"
for i in `seq 0 ${old_num}`
do
name=`echo ${image_old_name[i]} |awk -F "/" '{print $NF}'`
docker push ${domain_new}/${name}
done
}
harbor_rmi(){
#删除本地镜像
info3
info "Delete local image"
for i in `seq 0 ${old_num}`
do
name=`echo ${image_old_name[i]} |awk -F "/" '{print $NF}'`
docker rmi ${domain_new}/${name}
docker rmi ${image_old_name[i]}
done
}
#################################################################################
#通过harbor v2.0的接口返回镜像信息后自动同步
harbor(){
harbor_get_list
info3
image_name=`cat ${Images_File}`
for i in ${image_name};do image_old_name[${#image_old_name[*]}]=$i ;done
old_num=$(( `echo ${#image_old_name[*]}` -1 ))
harbor_pull
harbor_tag
harbor_push
harbor_rmi
}
#################################################################################
#手动修改变量后执行的方式
run(){
#加载数据元素
array_run
docker_pull ${domain_old}
docker_tag ${domain_old} ${domain_new}
docker_push ${domain_new}
echo -ne "\033[31m是否删除修改前后的镜像,节约空间(yes/no)\033[0m"
read -p "" test
if [ yes == ${test} ];then
info4 "删除${domain_old}镜像"
docker_rmi ${domain_old}
info4 "删除${domain_new}镜像"
docker_rmi ${domain_new}
fi
}
log(){
info2 "sh $0 harbor 通过harbor v2.0接口"
info2 "sh $0 run 通过添加镜像域名、标签等方式实现镜像通过"
}
#rmi=1
#run
start_image(){
local install=$1
if [ -z ${install} ];then
log
echo -ne "\033[31m请输入执行方式:(harbor/run)\033[0m"
read -p "" install
start_image ${install}
else
case $install in
'harbor')
harbor;;
'run')
run;;
esac
fi
}
start_image $1