一键清理Nexus仓库

一键清理Nexus仓库

  • 现许多团队使用 Nexus 来管理 Docker 镜像,产品不断迭代,镜像仓库占用的磁盘空间也越来越大。
  • 由于 Nexus 的控制台并未提供批量操作镜像功能,清理镜像十分不便。
  • 本文分享一个清理 Nexus 中 Docker 镜像的小工具 — nexus-cli

可参考 nexus-cli 的相关资料,nexus-cli GitHub开发者博客原文Nexus 官方博客文章

下载脚本并授权

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: xxxxxx

http://127.0.0.1:8081 是 Nexus 服务的地址,为了安全,可以直接在 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,结合 sublime 的批量处理功能,十分方便.

清理磁盘空间

最后,创建两个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

小结

由于开发测试环境中应用迭代非常频繁,镜像消耗磁盘的速度也非常快。若磁盘空间足够,每隔一定时间手工清理即可;若需要频繁清理,使用脚本自动化处理也十分方便

下面附上脚本操作!


创建仓库信息文件

$ vim repo_list.txt
docker_hosted:1
docker_prod:2

内容格式 — 仓库名称:保留tag数

创建Shell脚本

#!/usr/bin/env bash

image_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(){
   
  # 自应答配置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;}
  }";

  # 列出仓库镜像转储文件
  $CLI image ls > $1_$image_file
  sed -i '$d' $1_$image_file
  cat $1_$image_file | while read line
  do
    echo "start clean repoName:$1 image:  $line"
    $CLI image delete -name $line -keep $2
  done
}

# 读取所有需要清理的仓库,调用配置
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数作为参数传给内部方法
       nexus_configure $Repository  $keepVersion
   done
   echo -e "\n----$(date '+%Y-%m-%d %H:%M:%S')\t Clean Succ!-----\n"
}

clean_all_repository

清理成功日志:
清理成功

文首列举的 nexus-cli 的资料中,有 nexus-cli 的更多操作命令,可自行参考。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章