Created
April 15, 2021 03:51
-
-
Save hakur/b95c242c4ee09cf2a436286af66a2f31 to your computer and use it in GitHub Desktop.
一些蚊子腿docker镜像url函数。some docker image url functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e; | |
# joinBy 将一个数组的内容通过特定字符连接起来 | |
# joinBy join an array element to a string by specific string | |
# 使用方法 joinBy "/" ${array[@]} | |
# usage joinBy "/" ${array[@]} | |
# code from https://stackoverflow.com/questions/1527049/how-can-i-join-elements-of-an-array-in-bash | |
# 代码出处 https://stackoverflow.com/questions/1527049/how-can-i-join-elements-of-an-array-in-bash | |
function joinBy() { | |
local IFS="$1"; shift; echo "$*"; | |
} | |
# readFileLineWithCallback 读取文件行并执行回调函数,将会把读取的每一行内容传给回调函数,最后一行必须是空行 | |
# readFileLineWithCallback read file lines,then push each lines to callback function,last line must be an empty line | |
# 使用方法: readFileLineWithCallback docker-image-list.txt echo | |
# usage: readFileLineWithCallback docker-image-list.txt echo | |
function readFileLineWithCallback() { | |
file=$1 | |
callbackFunction=$2 | |
if [ "$file" == "" ];then | |
echo "missing file name" | |
return 1 | |
fi | |
if [ "$callbackFunction" == "" ];then | |
echo "missing callback function name" | |
return 1 | |
fi | |
while IFS= read -r line; do | |
if [ $line == "" ];then | |
continue | |
fi | |
$callbackFunction $line | |
done < $file | |
} | |
# fixDockerImageURL 通过输入docker镜像来地址来修正URL,比如输入nginx:1.9.0,将会输出docker.io/library/nginx:1.9.0 | |
# fixDockerImageURL input docker image url,then output standard image url | |
# 使用方法: fixDockerImageURL nginx,将会输出 docker.io/library/nginx:latest | |
# usage: fixDockerImageURL nginx,will out put "docker.io/library/nginx:latest" | |
function fixDockerImageURL() { | |
local imageURL=$1 | |
local imageURLSplitedArr=(${imageURL//\// }) | |
local registryDomain="" | |
local imageOrganizationPath="" | |
local imageName="" | |
local imageTag="" | |
local hasDomain="no" | |
local imageURLSplitedArrLen=${#imageURLSplitedArr[@]} | |
# 检查是否包含域名 | |
if [ "$imageURLSplitedArrLen" != "1" ] && [[ ${imageURLSplitedArr[0]} == *"."* ]];then | |
registryDomain=${imageURLSplitedArr[0]} | |
hasDomain="yes" | |
else | |
registryDomain="docker.io" | |
fi | |
# 设置镜像值 | |
local image=${imageURLSplitedArr[-1]} | |
local tagArr=(${image//:/ }) | |
local tagArrLen=${#tagArr[@]} | |
if [ "$tagArrLen" == "1" ] || [ "${tagArr[-1]}" == "" ];then | |
imageTag="latest" | |
imageName=${tagArr[0]} | |
else | |
imageTag=${tagArr[-1]} | |
imageName=${tagArr[-2]} | |
fi | |
# 设置镜像组织路径 | |
if [ "$hasDomain" == "yes" ];then | |
imageURLSplitedArr[0]="" | |
fi | |
imageURLSplitedArr[-1]="" | |
imageOrganizationPath=$(joinBy "/" ${imageURLSplitedArr[@]}) | |
if [ "$imageOrganizationPath" == "" ];then | |
imageOrganizationPath="library" | |
fi | |
echo $registryDomain/$imageOrganizationPath/$imageName:$imageTag | |
} | |
# getDockerImageTag 通过输入一个image地址获取镜像tag名称 | |
# getDockerImageTag input docker image url ,then output image tag | |
# 使用方法: getDockerImageTag docker.io/library/nginx:1.9.0,将会输出 1.9.0 | |
# usage: getDockerImageTag docker.io/library/nginx:1.9.0,will ouput 1.9.0 | |
function getDockerImageTag(){ | |
local imageURL=$(fixDockerImageURL $1) | |
local imageNameWithTag=$(getDockerImageNameWithTag $imageURL) | |
local arr=(${imageNameWithTag//:/ }) | |
local tag=${arr[1]} | |
if [ "$tag" == "" ];then | |
tag="latest" | |
fi | |
echo $tag | |
} | |
# getDockerImageNameWithoutTag 通过输入一个image地址获取不带tag的镜像名称 | |
# getDockerImageNameWithoutTag input docker image url,then output image name without tag | |
# 使用方法: getDockerImageNameWithoutTag docker.io/library/nginx:1.9.0,将会输出 nginx | |
# usage: getDockerImageNameWithoutTag docker.io/library/nginx:1.9.0,will output nginx | |
function getDockerImageNameWithoutTag(){ | |
local imageURL=$(fixDockerImageURL $1) | |
local imageNameWithTag=$(getDockerImageNameWithTag $imageURL) | |
local arr=(${imageNameWithTag//:/ }) | |
echo ${arr[0]} | |
} | |
# getDockerImageNameWithTag 通过输入一个镜像地址来获取带tag的镜像名称 | |
# getDockerImageNameWithTag input docker image url,then output image name with tag | |
# 使用方法: getDockerImageNameWithTag docker.io/library/nginx:1.9.0,将会输出 nginx:1.9.0 | |
# usage: getDockerImageNameWithTag docker.io/library/nginx:1.9, will output nginx:1.9.0 | |
function getDockerImageNameWithTag() { | |
local imageURL=$(fixDockerImageURL $1) | |
local imageURLSplitedArr=(${imageURL//\// }) | |
echo ${imageURLSplitedArr[-1]} | |
} | |
# getDockerImageNameWithPath 通过输入一个镜像地址来获取镜像URI | |
# getDockerImageNameWithPath input docker image url, then output URI | |
# 使用方法: getDockerImageNameWithPath docker.io/library/nginx:1.9.0,将会输出 library/nginx:1.9.0 | |
# usage: getDockerImageNameWithPath docker.io/library/nginx:1.9, will output library/nginx:1.9.0 | |
function getDockerImageNameWithPath() { | |
local imageURL=$(fixDockerImageURL $1) | |
local imageURLSplitedArr=(${imageURL//\// }) | |
imageURLSplitedArr[0]="" | |
echo $(joinBy "/" ${imageURLSplitedArr[@]}) | |
} | |
# replcaeDockerImageRegistry 替换docker镜像的registry host。比如docker.io/library/nginx:1.9.0 替换成 registry.sensetime.com/library/nginx:1.9.0 | |
# replcaeDockerImageRegistry replace docker image registry host。比如docker.io/library/nginx:1.9.0 替换成 registry.sensetime.com/library/nginx:1.9.0 | |
# 使用方法: replcaeDockerImageRegistry docker.io/library/nginx:1.9.0 registry.sensetime.com | |
# usage: replcaeDockerImageRegistry docker.io/library/nginx:1.9.0 registry.sensetime.com | |
function replcaeDockerImageRegistry() { | |
local imageURL=$(fixDockerImageURL $1) | |
local imageRegistryHost=$2 | |
local imageURLSplitedArr=(${imageURL//\// }) | |
imageURLSplitedArr[0]=$imageRegistryHost | |
echo $(joinBy "/" ${imageURLSplitedArr[@]}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment