一点牢骚
harbor 2.x 之后有了仓库代理功能确实方便许多,但为啥不能直接指定存到本地仓库呢?(或许是我没发现…),本着少动一点是一点的原则,于是有了这篇“水文”,haha
准备工作
- harbor部署,省略…500字
- 本地访问域名:nexus.j-test.com
- 创建两个镜像代理仓库
- 再创建两个镜像代理项目,docker163、dockerhub,分别关联上镜像代理仓库
- 创建三个本地仓库(按自己需求来)
- k8s
- kolla
- library
imgctl脚本
cat > /usr/bin/imgctl << EOF
#/bin/bash
#######################################################################
# imgctl - quick pull docker image and push local harbor repository
# author - RunnerJxc
#######################################################################
harbor_repo=harbor.j-test.com
project1=docker163
project2=dockerhub
harbor_proxy=($project1 $project2)
dest_project="library"
#是否已登录仓库
[[ -f ~/.docker/config.json ]] || { docker login $harbor_repo;}
# getopt的版本是增强版吗
getopt -T &>/dev/null;[ $? -ne 4 ] && { echo -e "\e[41mthe getopt is not enhanced version\e[0m";exit 1;}
ARGS=$(getopt -o i:d:h -l img:,dest:,help,pull,push -n "please see -h or --help" -- "$@")
eval set -- $ARGS
help_info='imgctl - quick pull docker image and push local harbor repository
Usage: imgctl [options] xx
options:
-i|--img the docker image you want to pull or push
-d|--dest the local project you want to push,default library
-h|--help the help message
--pull only pull image from remote.-i xx --pull
--push only push local image to harbor. --push or -i xx --push or -i xx -d xx --push
'
function error(){
echo -e "\e[41m[error] $1 \e[0m"
exit 1
}
function success(){
echo -e "\e[42m[success] $1 \e[0m"
}
function gethelp(){
echo -e "$help_info"
exit 1
}
function pull(){
case $1 in
*"/"*)
dest=$1
;;
*)
dest=library/$1
;;
esac
docker pull $harbor_repo/$project1/$dest
[[ $? -eq 0 ]] && { success "pull $1 finished"; } || { docker pull $harbor_repo/$project2/$dest;}
}
function push(){
[[ -n $2 ]] && { dest_project=$2; }
local_imgs=$(docker images | awk '{printf "%s:%s\n",$1,$2}')
[[ -n $1 && `echo "$local_imgs" | grep -E "$1"` ]] && { docker tag $1 $harbor_repo/$dest_project/$1 && docker push $harbor_repo/$dest_project/$1 && docker rmi $1 && success "push $1 finished";} || { pull $1;}
for hub in ${harbor_proxy[@]};
do
for img in `echo "$local_imgs" | grep $hub`;
do
case $img in
*"library"*)
new_img=`echo $img | sed "s/$hub\///" | sed "s/library/$dest_project/"`
;;
*)
new_img=`echo $img | sed "s/$hub/$dest_project/"`
;;
esac
docker tag $img $new_img
docker push $new_img
[[ $? -eq 0 ]] && { docker rmi $img; success "push $new_img finished";} || { error "push $new_img failed";}
done
done
}
ping -c 1 -w 2 $harbor_repo > /dev/null
[[ $? -eq 0 ]] || { error "harbor repository unreached";}
#只要有-h或--help就退出
[[ `echo $ARGS | grep -E '\-h|\-\-help'` ]] && { gethelp; }
case "$1" in
-i|--img)
[[ `echo $ARGS | grep -E '\-\-pull'` ]] && { pull $2 && exit 0;}
[[ $3 == "-d" || $3 == "--dest" ]] && { push $2 $4 ;exit 0;}
[[ `echo $ARGS | grep -E '\-\-push'` ]] && { push $2 && exit 0;}
pull $2
push
;;
-d|--dest)
#如果同时使用-d/--dest及--pull则退出
[[ `echo $ARGS | grep -E '\-\-pull'` ]] && { error "cant't use -d/--dest and --pull together";}
[[ $3 == "-i" || $3 == "--img" ]] && { push $4 $2; }
;;
--pull)
#如果没有指定-i/--img
[[ `echo $ARGS | grep -E '\-i|\-\-img'` ]] || { error "there's no any img to pull,you neet to use -i/--img point one"; }
;;
--push)
push
;;
*)
gethelp
;;
esac
EOF
chmod +x /usr/bin/imgctl
测试
-
帮助信息
-
-i/–img 指定镜像自动pull并push到 -d/–dest 指定的项目,不指定 -d/–dest 默认push到library。当指定为本地镜像时直接push
不指定-d/–dest
指定本地镜像push到指定项目
-
-i/–img xx --pull 将镜像仅下载到本地
-
–push 将含有代理仓库(docker163、dockerhub)标识的都push到-d/–dest指定的项目,不指定默认为library