lib脚本

作者: Joening | 来源:发表于2023-01-27 11:03 被阅读0次

    lib脚本

    
    function validate()
    {
        local log=$1
        shift
        local contain_str=$@
    
        for str in "$@"
        do
            grep -q "$str" $log
    
            [ $? -ne 0 ] && return 1
        done
        return 0
    }
    
    function run_step()
    {
        local func=$1
        shift
    
        echo "running step: \"$func\" ..."
    
        log=$(mktemp)
        echo "# run step \"$func\" output" > "$log"
        $func |tee -a "$log"
    
        validate "$log" "$@"
        r=$?
    
        if [ $r -ne 0 ]; then
            echo "run step \"$func\" error, abort! output log: $log"
            exit 1
        else
            echo "run step \"$func\" success"
            rm -f "$log"
        fi
    }
    
    function a()
    {
        echo "A is aaa, c is ccc"
    }
    
    # 需要参数 namespace 和 helm release name
    check_pod_running()
    {
      namespace=$1
        release_name=$2
    
        allready="true"
        has_pods="false"
    
      kubectl get ns ${namespace} >/dev/null
        if [[ "$?" != "0" ]]; then
                echo "not found ns ${namespace},or kubectl cmd error found"
                return 1
        fi
    
        for s in $(kubectl get pod -n ${namespace} | grep ${release_name} | awk '{print $3}');do
            has_pods="true"
        if [[ "x$s" == "xSTATUS" ]];then
          continue
        elif [[ "x$s" == "xCompleted" ]];then
          continue
        elif [[ "x$s" != "xRunning" ]];then
          echo "==> pod status is $s"
          allready="false"
          break
        else
          allcontainerready="true"
          for r in $(kubectl get pod -n ${namespace} | grep ${release_name} | awk '{print $2"/"$3}');do
            if [[ "x$r" =~ "xREADY" ]];then
              continue
                fi
            rc=$(echo $r | awk -F'/' '{print $1}')
            tc=$(echo $r | awk -F'/' '{print $2}')
            rs=$(echo $r | awk -F'/' '{print $3}')
            if [[ "x$rs" =~ "xCompleted" ]];then
                continue
            fi
            if [[ "x$rc" != "x$tc" ]];then
                echo "--> Pod has container not ready: $r($rc/$tc)"
                allcontainerready="false"
                break
            fi
              done
          if [ "$allcontainerready" == "true" ];then
            allready="true"
            break
          else
            echo "==> Wait all container ready."
            allready="flase"
          fi
          fi
        done
        if [ "$has_pods" == "false" ];then
            echo "No resource found in ns!${namespace}"
        return 1
        fi
        if [ "$allready" == "true" ];then
            return 0
        else
            return 1
        fi
    }
    

    相关文章

      网友评论

        本文标题:lib脚本

        本文链接:https://www.haomeiwen.com/subject/bkkehdtx.html