Harbor api 操作

一、harbor版本

[注释:镜像ID相同的不同版本镜像tag,删除时会同时删除]

harbor-offline-installer-v1.5.0.tgz

 Harbor api 操作

 

 

 

 

二、获取 nginx项目 及 项目下仓库 信息

# curl -u "admin:12345" -X GET --header ‘Accept: application/json‘ ‘http://192.168.56.21/api/search?q=项目名‘

[root@node ~]# curl -u "admin:Harbor12345" -X GET --header Accept: application/json http://192.168.56.21/api/search?q=nginx { "project": [ { "project_id": 2, "owner_id": 1, "name": "nginx", "creation_time": "2020-01-10T06:26:55Z", "update_time": "2020-01-10T06:26:55Z", "deleted": 0, "owner_name": "", "togglable": true, "current_user_role_id": 1, "repo_count": 1, "metadata": { "public": "false" } } ], "repository": [ { "project_id": 2, "project_name": "nginx", "project_public": false, "pull_count": 1, "repository_name": "nginx/nginx", "tags_count": 1 } ] }

三、获取镜像仓库的 tags

# curl -u "admin:Harbor12345" -X GET --header Accept: application/json http://192.168.56.21/api/repositories/项目名%2F镜像名称/tags

[root@dev-nanan ~]# curl -u "admin:Harbor12345" -X GET --header Accept: application/json http://192.168.56.21/api/repositories/nginx%2Fnginx/tags [ { "digest": "sha256:e4f0474a75c510f40b37b6b7dc2516241ffa8bde5a442bde3d372c9519c84d90", "name": "v1.13.12", "size": 44613870, "architecture": "amd64", "os": "linux", "docker_version": "17.06.2-ce", "author": "", "created": "2018-04-30T13:55:45.019400581Z", "signature": null } ]

四、软删除镜像仓库中指定版本镜像

【删除web界面上的指定办版本镜像】

#curl -u "admin:Harbor12345" -X DELETE -H Accept: text/plain  "http://192.168.56.21/api/repositories/项目名/镜像名称/tags/镜像版本号"

[root@node ~]# curl -u "admin:Harbor12345" -X DELETE -H Accept: text/plain  "http://192.168.56.21/api/repositories/nginx/nginx/tags/v1.13.12"

五、硬删除镜像仓库中指定版本镜像

Harbor私有仓库运行一段时间后,仓库中存有大量镜像,会占用太多的存储空间。直接通过Harbor界面删除相关镜像,并不会自动删除存储中的文件和镜像。需要停止Harbor服务,执行垃圾回收命令,进行存储空间清理和回收。 
#进入compose所在目录[config.yml]
[root@node ~]# cd /root/harbor

#停止Harbor相关的compose服务
[root@node harbor]# docker-compose stop
Stopping harbor-jobservice  ... done
Stopping nginx              ... done
Stopping harbor-ui          ... done
Stopping harbor-db          ... done
Stopping registry           ... done
Stopping harbor-adminserver ... done
Stopping harbor-log         ... done

#使用--dry-run参数运行容器,预览运行效果,但不删除任何数据
[root@node harbor]# docker run -it --name gc --rm --volumes-from registry vmware/registry:2.6.2-photon garbage-collect --dry-run /etc/registry/config.yml
nginx/nginx
0 blobs marked, 5 blobs eligible for deletion【0个blobs被标记,5个blobs可删除]
blob eligible for deletion: sha256:3c091c23e29d0ddfc902b0be63b1a08a853ef39973f92fab39ad1727eac012bf
blob eligible for deletion: sha256:4a99993b863683bef1c776732e14d2372f6ed52b48e94783f4a1b58af289db07
blob eligible for deletion: sha256:ae513a47849c895a155ddfb868d6ba247f60240ec8495482eca74c4a2c13a881
blob eligible for deletion: sha256:e4f0474a75c510f40b37b6b7dc2516241ffa8bde5a442bde3d372c9519c84d90
blob eligible for deletion: sha256:f2aa67a397c49232112953088506d02074a1fe577f65dc2052f158a3e5da52e8

#不使用--dry-run参数,将删除相关的文件和镜像
[root@node harbor]# docker run -it --name gc --rm --volumes-from registry vmware/registry:2.6.2-photon garbage-collect  /etc/registry/config.yml
nginx/nginx
0 blobs marked, 5 blobs eligible for deletion#【0个blobs被标记,5个blobs可删除]
blob eligible for deletion: sha256:3c091c23e29d0ddfc902b0be63b1a08a853ef39973f92fab39ad1727eac012bf
INFO[0000] Deleting blob: /docker/registry/v2/blobs/sha256/3c/3c091c23e29d0ddfc902b0be63b1a08a853ef39973f92fab39ad1727eac012bf  go.version=go1.7.3 instance.id=8a97e002-522a-4a6c-b390-122880d53183 service=registry
blob eligible for deletion: sha256:4a99993b863683bef1c776732e14d2372f6ed52b48e94783f4a1b58af289db07
INFO[0000] Deleting blob: /docker/registry/v2/blobs/sha256/4a/4a99993b863683bef1c776732e14d2372f6ed52b48e94783f4a1b58af289db07  go.version=go1.7.3 instance.id=8a97e002-522a-4a6c-b390-122880d53183 service=registry
blob eligible for deletion: sha256:ae513a47849c895a155ddfb868d6ba247f60240ec8495482eca74c4a2c13a881
INFO[0000] Deleting blob: /docker/registry/v2/blobs/sha256/ae/ae513a47849c895a155ddfb868d6ba247f60240ec8495482eca74c4a2c13a881  go.version=go1.7.3 instance.id=8a97e002-522a-4a6c-b390-122880d53183 service=registry
blob eligible for deletion: sha256:e4f0474a75c510f40b37b6b7dc2516241ffa8bde5a442bde3d372c9519c84d90
INFO[0000] Deleting blob: /docker/registry/v2/blobs/sha256/e4/e4f0474a75c510f40b37b6b7dc2516241ffa8bde5a442bde3d372c9519c84d90  go.version=go1.7.3 instance.id=8a97e002-522a-4a6c-b390-122880d53183 service=registry
blob eligible for deletion: sha256:f2aa67a397c49232112953088506d02074a1fe577f65dc2052f158a3e5da52e8
INFO[0000] Deleting blob: /docker/registry/v2/blobs/sha256/f2/f2aa67a397c49232112953088506d02074a1fe577f65dc2052f158a3e5da52e8  go.version=go1.7.3 instance.id=8a97e002-522a-4a6c-b390-122880d53183 service=registry

[root@node harbor]# docker-compose start
Starting log         ... done
Starting adminserver ... done
Starting registry    ... done
Starting ui          ... done
Starting mysql       ... done
Starting jobservice  ... done
Starting proxy       ... done

六、删除镜像仓库tag脚本

#!/bin/bash
URL="http://192.168.56.21"
USER="admin"
PASS="Harbor12345"
PRO="nginx"
HARBOR_PAHT="/root/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  stop
   docker run -it --name gc --rm --volumes-from registry vmware/registry:2.6.2-photon garbage-collect  /etc/registry/config.yml
   docker-compose 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

Harbor api 操作

上一篇:[转]算法之美


下一篇:iOS动画效果集合、 通过摄像头获取心率、仿淘宝滑动样式、瀑布流、分类切换布局等源码