#! /bin/bash # 經過Harbor提供的API來批量刪除鏡像,人工刪除費時費力 # 通過測試發現,經過接口去刪除時提供的是的標籤,但實際上刪除的時候經過的是鏡像的IMAGE_ID,也就是說 # 若是我把同一個鏡像tag屢次上傳到harbor,經過藉口刪除時,只須要提供其中一個標籤,那麼和這個鏡像的IMAGE_ID相同的鏡像都會刪除 #### 項目個數 lines=`curl -s -u "admin:Harbor12345" -X GET -H "Content-Type: application/json" "https://harbor.k8stest.com/api/projects?" |grep "\"name\""|awk -F "\"" '{print $4}'|wc -l` ##### 展現當前有幾個項目 echo "當前Harbor有如下幾個項目:" for i in $(seq 1 $lines) do ###########具體是啥項目 a=`curl -s -u "admin:Harbor12345" -X GET -H "Content-Type: application/json" "https://harbor.k8stest.com/api/projects?" |grep "\"name\""|awk -F "\"" '{print $4}'|awk -v b=$i 'NR==b{print $1}'` echo $i、$a done #######選擇具體的項目 read -p "請輸入序號(1~$lines):,查看其下的鏡像倉庫:" number if [ $number -ge 1 -a $number -le $lines ] then #########選擇的是哪一個項目 c=`curl -s -u "admin:Harbor12345" -X GET -H "Content-Type: application/json" "https://harbor.k8stest.com/api/projects?" |grep "\"name\""|awk -F "\"" '{print $4}'|awk -v b=$number 'NR==b{print $1}'` #####多少個倉庫 d=`curl -s -u "admin:Harbor12345" -X GET -H "Content-Type: application/json" "https://harbor.k8stest.com/api/projects?" |grep "$c" -C 2 |grep "project_id" |awk '{print $2}' |awk -F "," '{print $1}'` #echo "\$d-----------$d" ######顯示倉庫個數 ## e=`curl -s -u "admin:Harbor12345" -X GET -H "Content-Type: application/json" "https://harbor.k8stest.com/api/repositories?project_id=$d" | grep "\"name\"" |awk -F "\"" '{print $4}' |awk -F "/" '{print $2}'|wc -l` e=`curl -s -u "admin:Harbor12345" -X GET -H "Content-Type: application/json" "https://harbor.k8stest.com/api/repositories?project_id=2" | grep "\"name\"" |awk -F "\"" '{print $4}' |sed 's/sc\///g'|wc -l` ####### 簡單展現 echo "項目$c下有如下鏡像倉庫:" for line in $(seq 1 $e) do #####具體的倉庫名 f=`curl -s -u "admin:Harbor12345" -X GET -H "Content-Type: application/json" "https://harbor.k8stest.com/api/repositories?project_id=$d" | grep "\"name\"" |awk -F "\"" '{print $4}' |sed 's/sc\///g'|awk -v g=$line 'NR==g{print $1}'` echo $line、$f done read -p "請輸入序號(1~$e):,查看其下的鏡像格式以及對應的數量:" num if [ $num -ge 1 -a $num -le $e ] then #### 鏡像倉庫名字 h=`curl -s -u "admin:Harbor12345" -X GET -H "Content-Type: application/json" "https://harbor.k8stest.com/api/repositories?project_id=$d" | grep "\"name\"" |awk -F "\"" '{print $4}' |sed 's/sc\///g'|awk -v g=$num 'NR==g{print $1}'` echo "您選擇的倉庫是$h" #### 標籤類型種類個數 # i=`curl -s -u "admin:Harbor12345" -X GET -H "Content-Type: application/json" "https://harbor.k8stest.com/api/repositories/$c%2F$h/tags/" |grep "\"name\"" |awk -F"\"" '{print $4}' | cut -c -6 |sort -n |uniq|wc -l` # echo $i #### 標籤類型以及個數 echo "##################################" echo "鏡像格式爲:若是是10月,則爲201810*" echo "##################################" #####每種鏡像格式以及其數量 curl -s -u "admin:Harbor12345" -X GET -H "Content-Type: application/json" "https://harbor.k8stest.com/api/repositories/$c%2F$h/tags/" |grep "\"name\"" |awk -F"\"" '{print $4}' | cut -c -6 |awk '{count[$1]++}END{for (i in count)print i,count[i]}' ######輸入鏡像格式,進行刪除 echo "若是想刪除某種形式的鏡像,請輸入類型:" read image_format ##########輸入類型的全部鏡像 images=`curl -s -u "admin:Harbor12345" -X GET -H "Content-Type: application/json" "https://harbor.k8stest.com/api/repositories/$c%2F$h/tags/" |grep "\"name\"" |awk -F"\"" '{print $4}'|grep $image_format|awk '{print $1}'` #########統計鏡像個數 count_image=`curl -s -u "admin:Harbor12345" -X GET -H "Content-Type: application/json" "https://harbor.k8stest.com/api/repositories/$c%2F$h/tags/" |grep "\"name\"" |awk -F"\"" '{print $4}'|grep $image_format|wc -l` for image_label in $images do #############執行刪除 # curl -u "admin:Harbor12345" -X DELETE -H "Content-Type: application/json" "https://harbor.k8stest.com/api/repositories/$c%2F$h/tags/$image_label" done if [ $? -eq 0 ] then echo "刪除成功" echo "本次共刪除$count_image個鏡像" fi fi fi