SC2010
Don't use ls | grep. Use a glob or a for loop with a condition to allow non-alphanumeric filenames.
Problematic code:
ls /directory | grep mystringor
rm $(ls | grep -v '\.c$')Correct code:
ls /directory/*mystring*or
# BASH
shopt -s extglob
rm -- !(*.c)
# POSIX
for f in ./*
do
case $f in
*.c) true;;
*) rm "$f";;
esac
doneRationale:
Parsing ls is generally a bad idea because the output is fragile and human readable. To better handle non-alphanumeric filenames, use a glob. If you need more advanced matching than a glob can provide, use a for loop.
Exceptions:
-
lshas sorting options that are tricky to get right with other commands. If a specific order of files is needed, ls <sort options> | grep might be the best alternative. - network shares like AFS behave much faster using ls
Related resources:
#!/bin/bash #author: chuxiaowei@360.cn #date: 2020.08.04
: ${HELM_MINIMUM_ACCEPTABLE_VERSION:='3.2.4'} : ${ACTION_INFO_PATH:='./resources/info'} : ${PACKAGE_VALUES_PATH:='./resources/values'} : ${DOCKER_IMAGES_PATH:='./resources/dockerImages'} : ${HELM_LOCAL_INSTALL_SHELL:='./resources/helm/install_helm.sh'}
版本比较辅助函数
VER_SPLIT_SED='s/./ /g;s/(:digit:)([^[:digit:] ])/\1 \2/g;s/([^[:digit:] ])(:digit:)/\1 \2/g'
Compare with one element of version components
_ver_cmp_1() { "$1" = "$2" && return 0 if -z "${1//[[:digit:/}" ]] && -z "${2//[[:digit:/}" ]]; then # Both $1 and $2 are numbers # Using arithmetic comparison (( $1 > $2 )) && return 1 (( $1 < $2 )) && return 2 else # Either or both are not numbers, containing non-digit characters # Using string comparison "$1" > "$2" && return 1 "$1" < "$2" && return 2 fi
This should not be happening
exit 1 }
ver_cmp() { local A B i result A=($(sed "$VER_SPLIT_SED" <<< "$1")) B=($(sed "$VER_SPLIT_SED" <<< "$2")) i=0 while (( i < ${#A[@]} )) && (( i < ${#B[@]})); do _ver_cmp_1 "${A[i]}" "${B[i]}" result=$? $result =~ [12] && return $result let i++ done
Which has more, then it is the newer version
_ver_cmp_1 "${#A[i]}" "${#B[i]}" return $? }
解析json
parse_json(){
# {"target_version":"1.1.0","current_version":"1.0.0"}
local keystr="$2":""
echo cat "$1" | sed "s/{\"//g" | sed "s/\"}//g" | sed "s/\",\"/+++/g" | tr "+++" "\n" | grep "$keystr" | sed "s/$keystr//g"
}
log
log() { local msg='' for m in $@; do msg="$msg $m" done echo -e "[---info-->>>] $msg" }
logerr() { local msg='' for m in $@; do msg="$msg $m" done echo -e "[--error-->>>] $msg" }
get_absolute_path() { local path="$(dirname $0)/$(echo $1 | sed "s/.///g")" echo $path }
helm 安装
helm_install() {
read -p "helm离线安装使用资源包自带的version 3.2.4 版本,在线安装将联网拉取线上最新版本。是否离线安装?(y/n [y]): " offline
echo $offline
if [[ "$offline" == "y" || "$offline" == "Y" || "$offline" == echo -e "\n" ]];then
log "执行helm安装脚本..."
local shellPath=$(get_absolute_path $HELM_LOCAL_INSTALL_SHELL)
chmod 700 $shellPath
eval $shellPath
else
local helmInstallShellUrl='https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3'
log "准备拉取helm安装脚本: $helmInstallShellUrl"
curl -fsSL -o get_helm.sh $helmInstallShellUrl
log "为helm安装脚本赋权..."
chmod 700 get_helm.sh
log "执行helm安装脚本..."
./get_helm.sh
fi
local version=$(get_helm_version)
if [ "$version" != "" ];then
log "helm install success. current Version : $version"
fi
}
获取helm 版本
get_helm_version() { local helmVersionInfo=$(helm version | grep version.BuildInfo) local info=$(echo $helmVersionInfo) if [ "$info" == "" ];then echo "" fi # helmVersion=$(echo $info | grep -o '"."' | sed 's/"//g') # helmVersion=${helmVersion%%,} local helmVersion=$(helm version --template="{{ .Version }}") helmVersion=$(echo $helmVersion | sed 's/v//g') echo "$helmVersion" }
环境检查,检查helm是否安装,版本检查
check_helm_env() { # 1.检查kubectl local kubeVersion=$(kubectl version) if [ "$(echo $kubeVersion | grep 'command not found')" != "" ];then logerr "未检测到 kubectl命令,请安装 kubectl 命令行工具后重试。" exit 3 fi local kubeContextInfo=$(kubectl config current-context) if [ "$(echo $kubeContextInfo | grep 'current-context is not set')" != "" ];then logerr "未检测到 kubectl config 配置,请在配置kubectl config后重试." exit 3 fi
# 2.检查helm
local version=$(get_helm_version)
if [ $version == "" ];then
log "can not find helm command... try install."
local infoPath=$(get_absolute_path $ACTION_INFO_PATH)
local actionType=$(parse_json $infoPath "action_type")
if [[ $actionType == "install" ]];then
helm_install
version=$(get_helm_version)
else
exit 4
fi
fi
log "current helm version: $version"
set +e
ver_cmp "$version" "$HELM_MINIMUM_ACCEPTABLE_VERSION"
cmp_res=$?
set -e
if [ $cmp_res == 2 ] # <
then
log "当前版本太低,请升级helm版本,helm version 不低于 v$HELM_MINIMUM_ACCEPTABLE_VERSION ."
read -p "是否立即升级?(y/n [y]): " flag
if [[ "$flag" == "y" || "$flag" == "Y" || "$flag" == `echo -e "\n"` ]];then
helm_install
else
log "程序已退出,请升级 helm 版本后重试。"
exit 4
fi
fi
}
package_action_confirmation() {
local infoPath=$(get_absolute_path $ACTION_INFO_PATH)
local releaseName=$(parse_json $infoPath "release_name")
local actionType=$(parse_json $infoPath "action_type")
local targetVersion=$(parse_json $infoPath "target_version")
local changeLog=$(parse_json $infoPath "change_log")
log "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
log "\t Release Name: $releaseName"
log "\t Target Version: $targetVersion"
log "\t Action Type: $actionType"
log "\t Change Log: $changeLog"
log "\t Another K8S Nodes: $K8S_IPs"
log "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
local flag=""
read -p "请确认上述关于安装包任务的相关信息,是否立即执行?(y/n [y]): " flag
if [[ "$flag" == "y" || "$flag" == "Y" || "$flag" == echo -e "\n" ]];then
return
else
log "已取消【立即执行】操作,程序已退出。"
exit 0
fi
}
make_docker_image_dir(){
mkdir -p /root/local_images/images/
# 确保存镜像的文件夹存在
if [ $# -lt 1 ];then
continue
fi
# 同步到其他节点
for i in $@;
do
ssh -p 22 -nq $i 'mkdir -p /root/local_images/images/'
done
}
拉镜像
pull_docker_images() {
read -p "是否需要为安装拉取镜像, 并同步到其他节点?(y/n [n]): " flag
if [[ "$flag" == "y" || "$flag" == "Y" ]];then
log "即将拉取镜像..."
else
log "跳过【镜像拉取】"
return
fi
local tmp_dockerImage='dockerImage.tmp'
if [ -f $tmp_dockerImage ];then
rm -rf $tmp_dockerImage
fi
local dockerImagesPath=$(get_absolute_path $DOCKER_IMAGES_PATH)
local content=$(sort -n $dockerImagesPath | uniq >> $tmp_dockerImage)
imagesCount=0
log "+++++++++++++++++++++++ 将要拉取同步下列镜像 +++++++++++++++++++++++++"
for LINE in `cat "./$tmp_dockerImage"`
do
log "\t$LINE"
let imagesCount+=1
done
log "++++++++++++++++++++++++ 共 $imagesCount 个镜像++++++++++++++++++++++++++++"
make_docker_image_dir $@
imagesCount=0
# 并发执,控制最大并发数为10
start_time=`date +%s`
tmp_fifofile="/tmp/$$.fifo" # 已进程号命令创建临时文件
mkfifo $tmp_fifofile
exec 6<>$tmp_fifofile # 将FD6指向FIFO类型
rm $tmp_fifofile
thread_num=10 #最大进程数
for ((i=0;i<${thread_num};i++));do
echo >&6
done
for LINE in `cat "./$tmp_dockerImage"`
do
read -u6
{
local imageAddr=`echo $LINE | sed "s/'//g"`
local imageName=`basename $imageAddr`
docker pull $imageAddr
docker save $imageAddr -o /root/local_images/images/$imageName.tar
log "当前节点 : $imageName 拉取成功"
# 检查入参是否有传入其他节点
if [ $# -lt 1 ];then
continue
fi
# 同步到其他节点
for i in $@;
do
if ssh -p 22 -nq $i test -e "/root/local_images/images/$imageName.tar" ;then
log "$i : $imageName 已存在"
else
log "准备同步 $i : $imageName"
rsync -avz --progress /root/local_images/images/$imageName.tar $i:/root/local_images/images/$imageName.tar
fi
log "$i : docker will load $imageName"
ssh -p 22 -nq $i "docker load -i /root/local_images/images/$imageName.tar"
log "$i : $imageName 同步、加载完成"
done
let imagesCount+=1
echo >&6
} &
done
wait
stop_time=`date +%s`
log "拉取同步镜像共耗时:`expr $stop_time - $start_time`"
exec 6<&- #关闭文件描述符的读
exec 6>&- #关闭文件描述符的写
rm "./$tmp_dockerImage"
}
package_install() { local infoPath=$(get_absolute_path $ACTION_INFO_PATH) local releaseName=$(parse_json $infoPath "release_name") local packagePath=$(parse_json $infoPath "package_path") local actionType=$(parse_json $infoPath "action_type")
local actionCMD=''
case "$actionType" in
'install') actionCMD='upgrade --install' ;;
'upgrade') actionCMD='upgrade --install' ;;
'rollback') actionCMD='upgrade' ;;
esac
packagePath=$(get_absolute_path $packagePath)
local valuesDir=$(get_absolute_path $PACKAGE_VALUES_PATH)
local valuesCmd=''
for j in `ls $valuesDir`;
do
valuesCmd="$valuesCmd -f $valuesDir/$j"
done
local HELM_CMD="helm $actionCMD $releaseName $packagePath $valuesCmd"
log "执行命令: $HELM_CMD"
eval $HELM_CMD
}
jobs_install_cmd() { local infoPath=$(get_absolute_path $ACTION_INFO_PATH) local packagePath=$(parse_json $infoPath "job_package_path") local actionType=$(parse_json $infoPath "action_type")
if [[ "$actionType" == "rollback" ]];then
log "当前执行的是回滚操作,job暂不执行"
echo ""
else if [[ "$packagePath" == "" ]];then
log "未发现有需要执行的jobs"
echo "
else
local pwdPath=`pwd`
local releaseName=`basename $pwdPath`
releaseName="$releaseName-jobs"
packagePath=$(get_absolute_path $packagePath)
local valuesDir=$(get_absolute_path $PACKAGE_VALUES_PATH)
local valuesCmd=''
for j in `ls $valuesDir`;
do
valuesCmd="$valuesCmd -f $valuesDir/$j"
done
local HELM_CMD="helm install $releaseName $packagePath $valuesCmd"
echo $HELM_CMD
fi
}
jobs_install() {
local HELM_CMD=$(jobs_install_cmd)
if "$HELM_CMD" == "";then log "离线包版本job检查完毕" else read -p "检测到离线包中有 jobs 可以执行,是否需要执行离线包中的 jobs? (m:手动模式)(y/m/n [n]): " flag if "$flag" == "y";then log "开始执行版本任务..." HELM_CMD="$HELM_CMD --set tags.all=true" if "$flag" == "m";then HELM_CMD="$HELM_CMD --set migration.manual=true" fi log "执行命令: $HELM_CMD" eval $HELM_CMD else log "跳过【jobs 执行】" fi fi }
fail_trap() { result=$? if [ "$result" != "0" ]; then case "$result" in "3") echo "请确保安装 kubectl 命令行工具,以及正确配置集群环境." ;; "4") echo "请确保安装 helm 命令行工具,以及版本不低于 $HELM_MINIMUM_ACCEPTABLE_VERSION ." ;; esac if -n "$INPUT_ARGUMENTS"; then logerr "Failed to install package with the arguments provided: $INPUT_ARGUMENTS" help else logerr "Failed to install package." fi echo -e "\tFor support, read the file Readme.md" fi exit $result }
help () { echo "Accepted cli arguments are:" echo -e "\t[--help | -h ] ->> prints this help" echo -e "\t[--install | -i <k8s集群地址> ] . When it not defined, the dcoker images cannot be synchronized to other nodes." echo -e "\te.g. --install k8s2 k8s3 k8s4 k8s5 or -i k8s2 k8s3 k8s4 k8s5" }
main() {
# 1.检查执行环境
log "检查相关执行环境..."
check_helm_env
# 2.离线任务信息确认
log "离线任务信息确认..."
package_action_confirmation
# 3.拉取docker镜像
log "为服务安装拉取相关镜像..."
pull_docker_images $@
log "拉取相关镜像操作已完成"
# 4.执行版本任务
log "检查版本job..."
jobs_install
# 5.执行版本操作
log "开始安装离线包..."
package_install
log "安装包执行完毕."
}
trap "fail_trap" EXIT set -e
解析参数
export INPUT_ARGUMENTS="${@}" set -u while $# -gt 0; do case $1 in '--install'|-i) shift if $# -ne 0; then export K8S_IPs=$@ fi ;; '--help'|-h) help exit 0 ;; *) exit 1 ;; esac break done set +u
main $K8S_IPs
Pages 405
- Home
- Azure Pipelines
- CentOS6
- Checks
- CircleCI
- Contrib
- DevGuide
- Directive
- GitLab CI
- Ignore
- Integration
- JUnit
- More Installation Guides
- Parser error
- Phabricator
- Recursiveness
- SC1000
- SC1001
- SC1003
- SC1004
- SC1007
- SC1008
- SC1009
- SC1010
- SC1011
- SC1012
- SC1014
- SC1015
- SC1016
- SC1017
- SC1018
- SC1019
- SC1020
- SC1026
- SC1027
- SC1028
- SC1029
- SC1033
- SC1034
- SC1035
- SC1036
- SC1037
- SC1038
- SC1039
- SC1040
- SC1041
- SC1042
- SC1044
- SC1045
- SC1046
- SC1047
- SC1048
- SC1049
- SC1050
- SC1051
- SC1052
- SC1053
- SC1054
- SC1056
- SC1058
- SC1061
- SC1062
- SC1064
- SC1065
- SC1066
- SC1067
- SC1068
- SC1069
- SC1070
- SC1071
- SC1072
- SC1073
- SC1075
- SC1077
- SC1078
- SC1079
- SC1081
- SC1082
- SC1083
- SC1084
- SC1086
- SC1087
- SC1088
- SC1089
- SC1090
- SC1091
- SC1094
- SC1095
- SC1097
- SC1098
- SC1099
- SC1100
- SC1101
- SC1102
- SC1104
- SC1105
- SC1107
- SC1108
- SC1109
- SC1110
- SC1111
- SC1112
- SC1113
- SC1114
- SC1115
- SC1116
- SC1117
- SC1118
- SC1119
- SC1120
- SC1121
- SC1122
- SC1123
- SC1124
- SC1125
- SC1126
- SC1127
- SC1128
- SC1129
- SC1130
- SC1131
- SC1132
- SC1133
- SC1135
- SC1136
- SC2000
- SC2001
- SC2002
- SC2003
- SC2004
- SC2005
- SC2006
- SC2007
- SC2008
- SC2009
- SC2010
- SC2011
- SC2012
- SC2013
- SC2014
- SC2015
- SC2016
- SC2017
- SC2018
- SC2019
- SC2020
- SC2021
- SC2022
- SC2024
- SC2025
- SC2026
- SC2027
- SC2028
- SC2029
- SC2030
- SC2031
- SC2032
- SC2033
- SC2034
- SC2035
- SC2036
- SC2037
- SC2038
- SC2039
- SC2040
- SC2041
- SC2043
- SC2044
- SC2045
- SC2046
- SC2048
- SC2049
- SC2050
- SC2051
- SC2053
- SC2054
- SC2055
- SC2056
- SC2057
- SC2058
- SC2059
- SC2060
- SC2061
- SC2062
- SC2063
- SC2064
- SC2065
- SC2066
- SC2067
- SC2068
- SC2069
- SC2070
- SC2071
- SC2072
- SC2073
- SC2074
- SC2076
- SC2077
- SC2078
- SC2079
- SC2080
- SC2081
- SC2082
- SC2084
- SC2086
- SC2087
- SC2088
- SC2089
- SC2090
- SC2091
- SC2092
- SC2093
- SC2094
- SC2095
- SC2096
- SC2097
- SC2098
- SC2099
- SC2100
- SC2101
- SC2102
- SC2103
- SC2104
- SC2105
- SC2106
- SC2107
- SC2108
- SC2109
- SC2110
- SC2112
- SC2114
- SC2115
- SC2116
- SC2117
- SC2119
- SC2120
- SC2121
- SC2122
- SC2123
- SC2124
- SC2125
- SC2126
- SC2128
- SC2129
- SC2130
- SC2139
- SC2140
- SC2141
- SC2142
- SC2143
- SC2144
- SC2145
- SC2146
- SC2147
- SC2148
- SC2149
- SC2150
- SC2151
- SC2152
- SC2153
- SC2154
- SC2155
- SC2156
- SC2157
- SC2158
- SC2159
- SC2160
- SC2161
- SC2162
- SC2163
- SC2164
- SC2165
- SC2166
- SC2167
- SC2168
- SC2169
- SC2170
- SC2171
- SC2172
- SC2173
- SC2174
- SC2175
- SC2176
- SC2177
- SC2178
- SC2179
- SC2180
- SC2181
- SC2182
- SC2183
- SC2184
- SC2185
- SC2186
- SC2187
- SC2188
- SC2189
- SC2190
- SC2191
- SC2192
- SC2193
- SC2194
- SC2195
- SC2196
- SC2197
- SC2198
- SC2199
- SC2200
- SC2201
- SC2202
- SC2203
- SC2204
- SC2205
- SC2206
- SC2207
- SC2208
- SC2209
- SC2210
- SC2211
- SC2212
- SC2213
- SC2214
- SC2215
- SC2216
- SC2217
- SC2218
- SC2219
- SC2220
- SC2221
- SC2222
- SC2223
- SC2224
- SC2225
- SC2226
- SC2227
- SC2229
- SC2230
- SC2231
- SC2232
- SC2233
- SC2234
- SC2235
- SC2236
- SC2237
- SC2238
- SC2239
- SC2240
- SC2241
- SC2242
- SC2243
- SC2244
- SC2245
- SC2246
- SC2247
- SC2248
- SC2249
- SC2250
- SC2251
- SC2252
- SC2253
- SC2254
- SC2255
- SC2256
- SC2257
- SC2259
- SC2260
- SC2261
- SC2262
- SC2263
- SC2264
- SC3001
- SC3002
- SC3013
- SC3014
- SC3017
- SC3018
- SC3020
- SC3021
- SC3022
- SC3023
- SC3024
- SC3025
- SC3026
- SC3028
- SC3031
- SC3034
- SC3035
- SC3037
- SC3038
- SC3039
- SC3043
- SC3045
- SC3046
- SC3047
- SC3048
- SC3049
- SC3050
- SC3053
- SC3054
- SC3055
- SC3056
- SC3057
- SC3060
- severity
- Template
- TravisCI