一键清理Nexus仓库
生活随笔
收集整理的這篇文章主要介紹了
一键清理Nexus仓库
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一鍵清理Nexus倉庫
- 現(xiàn)許多團隊使用 Nexus 來管理 Docker 鏡像,產(chǎn)品不斷迭代,鏡像倉庫占用的磁盤空間也越來越大。
- 由于 Nexus 的控制臺并未提供批量操作鏡像功能,清理鏡像十分不便。
- 本文分享一個清理 Nexus 中 Docker 鏡像的小工具 — nexus-cli
可參考 nexus-cli 的相關(guān)資料,nexus-cli GitHub、開發(fā)者博客原文、Nexus 官方博客文章
下載腳本并授權(quán)
wget https://s3.eu-west-2.amazonaws.com/nexus-cli/1.0.0-beta/linux/nexus-cli chmod +x nexus-cli# 將工具放置/usr/local/bin下 mv nexus-cli /usr/local/bin/配置待清理的庫
$ nexus-cli configure Enter Nexus Host: http://127.0.0.1:8081 Enter Nexus Repository Name: demo Enter Nexus Username: admin Enter Nexus Password: xxxxxxhttp://127.0.0.1:8081 是 Nexus 服務(wù)的地址,為了安全,可以直接在 Nexus 所在的機器處理
列出所有鏡像
$ nexus-cli image ls xxl-job-demo xxl-job-testing zjyz-auth-demo ...這里僅列出三個作為例子。
清理鏡像
nexus-cli image delete -name $IMAGE_NAME -keep $X,-keep $X 表示保留幾個tag清理單個鏡像的tag,這里僅保留最新的兩個tag
$ nexus-cli image delete -name xxl-job-demo -keep 2 xxl-job-demo:2018.11.1-113322 image will be deleted ... xxl-job-demo:2018.11.1-113322 has been successful deleted xxl-job-demo:2018.11.1-145912 image will be deleted ... xxl-job-demo:2018.11.1-145912 has been successful deleted ...清理所有鏡像的tag,結(jié)合 sublime 的批量處理功能,十分方便.
清理磁盤空間
最后,創(chuàng)建兩個Nexus Task 來清理物理空間
-
先運行 Purge unused docker manifests and images
-
再運行 Compact blob store。
此時,可以查看磁盤目錄 sonatype-work/nexus3/blobs/ 的磁盤占用情況。
$ du -lh --max-depth=1 . | grep demo 2.1G ./demo小結(jié)
由于開發(fā)測試環(huán)境中應(yīng)用迭代非常頻繁,鏡像消耗磁盤的速度也非???。若磁盤空間足夠,每隔一定時間手工清理即可;若需要頻繁清理,使用腳本自動化處理也十分方便
下面附上腳本操作!
創(chuàng)建倉庫信息文件
$ vim repo_list.txt docker_hosted:1 docker_prod:2內(nèi)容格式 — 倉庫名稱:保留tag數(shù)
創(chuàng)建Shell腳本
#!/usr/bin/env bashimage_file=image.txt CLI=`which nexus-cli` NexusUserName='admin' NexusPwd='admin123'# 指定要清理的Nexus庫地址 NEXUS_URL='http://172.16.7.6:8081'# 獲取即將要清理的倉庫 NEXUS_Repository_NameList=`cat ./repo_list.txt`# 配置nexus倉庫 nexus_configure(){# 自應(yīng)答配置URL,Repository等信息expect -c "set timeout -1;spawn $CLI configure ;expect {*Host:* {send -- $NEXUS_URL\r;exp_continue;}*Name:* {send -- $1\r;exp_continue;}*Username:* {send -- $NexusUserName\r;exp_continue;}*Password:* {send -- $NexusPwd\r;exp_continue;}eof {exit 0;}}";# 列出倉庫鏡像轉(zhuǎn)儲文件$CLI image ls > $1_$image_filesed -i '$d' $1_$image_filecat $1_$image_file | while read linedoecho "start clean repoName:$1 image: $line"$CLI image delete -name $line -keep $2done }# 讀取所有需要清理的倉庫,調(diào)用配置 clean_all_repository(){echo -e "\n----$(date '+%Y-%m-%d %H:%M:%S')\t Start cleanning-----\n"for Repository_KeepVn in $NEXUS_Repository_NameList; do #遍歷要清理的docker倉庫Repository=`echo ${Repository_KeepVn} | awk -F: '{print $1}'`keepVersion=`echo ${Repository_KeepVn} | awk -F: '{print $2}'`echo -e "--- Repository:$Repository\tkeepVersionNum:$keepVersion ---"# 將倉庫名,保留tag數(shù)作為參數(shù)傳給內(nèi)部方法nexus_configure $Repository $keepVersiondoneecho -e "\n----$(date '+%Y-%m-%d %H:%M:%S')\t Clean Succ!-----\n" }clean_all_repository清理成功日志:
文首列舉的 nexus-cli 的資料中,有 nexus-cli 的更多操作命令,可自行參考。
總結(jié)
以上是生活随笔為你收集整理的一键清理Nexus仓库的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: apk提取
- 下一篇: 软考信息系统监理师,2016年3月18日