[root@localhost ~]# cat /opt/pass/deploywork/harbor_nginx/clean-arm.sh
#!/bin/bash
if [ $# -eq 0 ]; then
echo "read harbor admin passwd and url, for example, sh clean-arm.sh 10.10.10.10 admin pass"
exit 1
fi
readonly HARBOR_URL=$1
readonly HARBOR_USER=$2
readonly HARBOR_PASSWD=$3
set -o nounset
set -o pipefail
function get_projects(){
projects_list=$(curl -s -k -u ${HARBOR_USER}:${HARBOR_PASSWD} "http://${HARBOR_URL}/api/v2.0/projects?page=1&page_size=100"|grep -Po '(?<="name")\W*\K[^ :]*' | awk -F '"' '{{print $1}}')
}
#列出所有项目中镜像并输出到文件
function get_repos(){
mkdir -p $PWD/reposList
for project in $projects_list;do
repos_list=$(curl -s -k -u ${HARBOR_USER}:${HARBOR_PASSWD} "http://${HARBOR_URL}/api/v2.0/projects/${project}/repositories?page=1&page_size=100"| grep -Po '(?<="name")\W*\K[^ :]*'| awk -F '"' '{{print $1}}' | awk -F '/' '{{print $2 }}')
echo "${repos_list}" > $PWD/reposList/${project}.txt
done
}
#删除镜像
function delete_images(){
for project in $projects_list
do
for repo in `cat $PWD/reposList/${project}.txt`
do
curl -X 'DELETE' -u ${HARBOR_USER}:${HARBOR_PASSWD} "http://${HARBOR_URL}/api/v2.0/projects/${project}/repositories/${repo}"
done
done
}
#删除项目
function delete_projects(){
for project in $projects_list;do
curl -X 'DELETE' -u ${HARBOR_USER}:${HARBOR_PASSWD} "http://${HARBOR_URL}/api/v2.0/projects/${project}"
done
}
function entrance(){
get_projects
get_repos
delete_images
delete_projects
}
entrance
执行
clean-arm.sh ip harboradmin harborpasswd
sh clean-arm.sh 10.10.10.10 admin passwd