Harbor api 操作
harbor 的版本为 1.5.2
为 Harbor 配置 swagger
官网参考: https://github.com/goharbor/harbor/blob/v1.5.2/docs/configure_swagger.md
cd /iba/software/harbor
wget https://raw.githubusercontent.com/goharbor/harbor/master/docs/prepare-swagger.sh https://raw.githubusercontent.com/goharbor/harbor/master/docs/swagger.yaml
vi prepare-swagger.sh
#修改 SERVER_IP
SERVER_IP=192.168.0.241
chmod +x prepare-swagger.sh
./prepare-swagger.sh
vi docker-compose.yml
# 在 - /data/ca_download/:/etc/ui/ca/:z 后添加下面两行
- ../src/ui/static/vendors/swagger-ui-2.1.4/dist:/harbor/static/vendors/swagger
- ../src/ui/static/resources/yaml/swagger.yaml:/harbor/static/resources/yaml/swagger.yaml
docker-compose down -v && docker-compose up -d
通过浏览器访问 http://192.168.0.241/static/vendors/swagger/index.html
# 获取镜像
curl -X GET --header 'Accept: application/json' 'http://192.168.0.241/api/search?q=library'
#获取镜像仓库的 tags,红色框框处就是 tags
curl -X GET --header 'Accept: application/json' 'http://192.168.0.241/api/repositories/library%2Ftomcat/tags'
# 删除镜像仓库的 tags
curl -X DELETE -H 'Accept: text/plain' -u admin:Harbor12345 "http://192.168.0.241/api/repositories/library/redis/tags/2018-10-29_17-39"
# 删除镜像的 tags 时,可以通过 curl 获取返回值来判断成功与否
curl -X DELETE -H 'Accept: text/plain' -u admin:Harbor12345 -o /dev/null -s -w %{http_code} "http://192.168.0.241/api/repositories/library/redis/tags/2018-10-29_17-39"
# HTTP Status Code 返回码说明
200 Delete tag successfully.
400 Invalid repo_name.
401 Unauthorized.
403 Forbidden.
404 Repository or tag not found.
附上自动清理 harbor tags 的脚本,这里删除多余的 tags,保留9个,可在下面脚本配置,打 tag 的时候可以按照日期命名,例如:2018-10-29_17-39 这可以方便用下面的脚本进行删除操作
#!/bin/bash
URL="http://192.168.0.241"
USER="admin"
PASS="Harbor12345"
PRO="library"
HARBOR_PAHT="/iba/harbor"
# 软删除 harbor tags
del_tags()
{
echo "软删除 ${rp}/${t}"
curl -X DELETE -H 'Accept: text/plain' -u ${USER}:${PASS} "${URL}/api/repositories/${rp}/tags/${t}"
}
# 硬删除 harbor tags
har_del_tags()
{
cd ${HARBOR_PAHT}
docker-compose -f docker-compose.yml -f docker-compose.clair.yml stop
docker run -it --name gc --rm --volumes-from registry vmware/registry:2.6.2-photon garbage-collect /etc/registry/config.yml
docker-compose -f docker-compose.yml -f docker-compose.clair.yml start
}
# 获取 project id
PID=$(curl -s -X GET --header 'Accept: application/json' "${URL}/api/projects"|grep -w -B 2 "${PRO}" |grep "project_id"|awk -F '[:, ]' '{print $7}')
#echo ${PID}
# 拿获取到的 projects_id 获取 repositories
REPOS=$(curl -s -X GET --header 'Accept: application/json' "${URL}/api/repositories?project_id=${PID}"|grep "name"|awk -F '"' '{print $4}')
for rp in ${REPOS}
do
echo ${rp}
TAGS=$(curl -s -X GET --header 'Accept: application/json' "${URL}/api/repositories/${rp}/tags"|grep \"name\"|awk -F '"' '{print $4}'|sort -r |awk 'NR > 9 {print $1}')
for t in ${TAGS}
do
echo ${t}
del_tags
done
echo "===================="
done
har_del_tags