shell脚本批量实现项目镜像本地化:docker注册证书login到harbor、将镜像push到harbor仓库、批量判断镜像是否在harbor仓库中已存在、find+sed替换源代码镜像地址

1. 本地docker注册证书docker login连接到harbor仓库:

我们使用docker login/push/pull去与Harbor打交道,上传下载镜像等。 但是可能会出现x509: certificate signed by unknown authority之类的错误。

[root@test01 harbor.dev]# docker login harbor.dev
Authenticating with existing credentials…
Login did not succeed, error: Error response from daemon: Get https://harbor.dev/v2/: x509: certificate signed by unknown authority
Username (admin): admin
Password:
Error response from daemon: Get https://harbor.dev/v2/: x509: certificate signed by unknown authority

此时根据docker官方https://docs.docker.com/engine/security/certificates/,客户端要使用tls与Harbor通信,使用的还是自签证书,那么必须建立一个目录:/etc/docker/certs.d,在这个目录下建立签名的域名的目录, 那么整个目录为: /etc/docker/certs.d/xxxx(harbor域名), 然后把harbor的证书ca.crt等拷贝到这个目录即可。
在这里插入图片描述

2.使用shell脚本将大量镜像pull、tag、push到私有harbor仓库

#!/bin/bash

while IFS= read -r line; do
  image_name=$(echo "$line" | awk '{print $1}')

  new_name=$(echo "$line" | awk '{print $2}' | tr -d '\r')

  docker pull "$image_name"
  docker tag "$image_name" "$new_name"
  docker push "$new_name"

  echo "Pulled, tagged, and pushed $image_name as $new_name"
done < images.txt

如上所示,我们首先需要准备一个images.txt里面包括了所有下载镜像名及上传名,如下所示,每行包括image_name与new_name,其中用空格隔开

istio/proxyv2:1.14.1 harbor.dev/cube_studio/istio_proxyv2:1.14.1
istio/pilot:1.14.1 harbor.dev/cube_studio/pilot:1.14.1
mysql:5.7 harbor.dev/cube_studio/

当然,以上脚本的关键在于image_name与new_name的获取,若new_name可根据image_name修改得到,也可以更改脚本得到适合自己的new_name,可根据自己需要进行修改。

  #images.txt每行只有image_name,tr -d '\r'去掉换行符
  image_name=$(echo "$line" | awk '{print $1}' | tr -d '\r')
  
  #根据情况给出new_name的名称,可根据自己需要进行修改
  if [[ "$image_name" == *"ccr.ccs.tencentyun.com"* ]]; then
  #image_name中的ccr.ccs.tencentyun.com/hub替换为harbor.dev/hub
    new_name=$(echo "$image_name" | sed 's/ccr.ccs.tencentyun.com/harbor.dev\/hub//g')
  else
   #在image_name前加上harbor.dev/hub/
    new_name="harbor.dev/hub/$image_name"
  fi

此时images.txt每行只需要包括需要上传的镜像名即可,new_name可根据脚本得到,如下所示:

istio/proxyv2:1.14.1
istio/pilot:1.14.1
mysql:5.7

将Excel表格中的数据粘贴到记事本中,且每列间距都为一个空格

可在EXECL中将你需要的所有数据先合并成一列再导出。

  • 假定你原数据有AB两列,在C1单元格输入公式:=A1&" "&B1,将公式用填充柄向下复制,之后将C列粘贴到txt文档即每列间距都为一个空格

3. 判断仓库中是否已经存在镜像,若不存在则上传

采用上述脚本后有时会出现有些镜像已经上传过重复pull的情况,会验证影响效率,因此进行改进,结果如下所示:

#!/bin/bash

while read -r line; do
  image_name=$(echo "$line" | awk '{print $1}' | tr -d '\r')
  #根据情况给出new_name的名称,可根据自己需要进行修改
  if [[ "$image_name" == *"ccr.ccs.tencentyun.com"* ]]; then
    new_name=$(echo "$image_name" | sed 's/ccr.ccs.tencentyun.com/harbor.dev\/hub//g')
  else
    new_name="harbor.dev/hub/$image_name"
  fi
  
  #清除output.txt
  echo -n "" > output.txt
  
  result=$(timeout 8s docker pull "$new_name" 2>&1 | tee output.txt)
  #若输出Error response from daemon说明harbor仓库中不存在镜像,则直接上传
  if grep -q "Error response from daemon" output.txt; then
    echo "-------------------暂无该镜像-------------$new_name"
    #echo $image_name >> push_images.txt
    #echo $new_name >> push_newimages.txt
    
    docker pull "$image_name"
    docker tag "$image_name" "$new_name"
    docker push "$new_name"
    echo "Pulled, tagged, and pushed $image_name as $new_name"
    echo -n "" > output.txt
    result=$(timeout 15s docker pull "$new_name" 2>&1 | tee output.txt)
    if grep -q "Pulling from" output.txt; then
      echo "$new_name"
      echo "$image_name $new_name" >> exist_images.txt
    fi
  #若输出Pulling from说明harbor仓库中已存在该镜像,则写入exist_images.txt
  elif grep -q "Pulling from" output.txt; then
    echo "$new_name"
    echo "$image_name $new_name" >> exist_images.txt
  else
    echo "***************docker pull 时间过短***************$new_name"
    echo $new_name >> timeout_images.txt
  fi
done < push_images.txt

4. 镜像上传后,利用find+sed脚本修改项目源代码中的镜像地址

#!/bin/bash  
  
while IFS= read -r line; do  
  image_name=$(echo "$line" | awk '{print $1}')  
  new_name=$(echo "$line" | awk '{print $2}' | tr -d '\r')  
  
  # 定义多个项目文件夹路径,使用空格分隔  
  project_folders="D:\Gitlab\cubestudio-apps\docs D:\Gitlab\cubestudio-apps\install D:\Gitlab\cubestudio-apps\myapp"  
  
  # 遍历项目文件夹路径  
  for project_folder in $project_folders; do  
    find "$project_folder" -type f \( -name "*.yml" -o -name "*.yaml" -o -name "*.json" -o -name "*.sh" -o -name "*.md" -o -name "*.py" -o -name "*ockerfile*" \) -print0 | xargs -0 grep -l "$image_name" | while read -r file; do

    # 替换文件内容
    sed -i "s@$image_name@$new_name@g" "$file"
    echo "在文件 $file 中将 $image_name 替换为 $new_name "
    unix2dos "$file"
   done ;
  done;
done < images1.txt

文件应该包含两列数据,第一列是旧的镜像名称,第二列是新的镜像名称,并且每行数据之间使用空格分隔。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Pistachiout

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值