sh Openshift部署com oc

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh Openshift部署com oc相关的知识,希望对你有一定的参考价值。

### to fix the issue about oc login token shared between the job executions ###
HOME=$(pwd)/.${RD_JOB_EXECID}
cd $HOME
### fix end ###

### functions ###
# produz logging no console
## $1 mensagem
function info() {

  (echo -e "> > > > [`date +"%Y-%m-%d_%H-%M-%S"`] $1")

}

# produz logging de erros no console
## $1 mensagem de erro
function error() {

  (>&1 echo -e "> > > > [`date +"%Y-%m-%d_%H-%M-%S"`] *ERROR* $1")

}
### /functions/ ###

#echo "> > > > [`date +"%Y-%m-%d_%H-%M-%S"`] [OPENSHIFT_PROJECT].: ${RD_OPTION_OPENSHIFT_PROJECT}"
info "[OPENSHIFT_PROJECT].: ${RD_OPTION_OPENSHIFT_PROJECT}"
#echo "> > > > [`date +"%Y-%m-%d_%H-%M-%S"`] [APP_NAME]..........: ${RD_OPTION_APP_NAME}"
info "[APP_NAME]..........: ${RD_OPTION_APP_NAME}"
#echo "> > > > [`date +"%Y-%m-%d_%H-%M-%S"`] [PARAM_URL_REGISTRY]: ${RD_OPTION_PARAM_URL_REGISTRY}"
info "[PARAM_URL_REGISTRY]: ${RD_OPTION_PARAM_URL_REGISTRY}"

# Get the current registry url
CURR=`oc get dc ${RD_OPTION_APP_NAME} -n ${RD_OPTION_OPENSHIFT_PROJECT} --template="{{(index .spec.template.spec.containers 0).image}}"`

if [ "$?" == 0 ]; then
    #echo "> > > > [`date +"%Y-%m-%d_%H-%M-%S"`] Imagem antiga: ${CURR}"
    info "Imagem antiga: ${CURR}"

    # Get the latest deployed number
    DEPLOYED=$(oc deploy ${RD_OPTION_APP_NAME} -n ${RD_OPTION_OPENSHIFT_PROJECT} | grep deployed | awk -F ' ' '{print $3}')

    #echo "> > > > [`date +"%Y-%m-%d_%H-%M-%S"`] Current deployment ${DEPLOYED}"
    info "Current deployment ${DEPLOYED}"

    # Apply the new image
    oc patch dc ${RD_OPTION_APP_NAME} \
       -n ${RD_OPTION_OPENSHIFT_PROJECT} \
       --patch='{"spec":{"template":{"spec":{"containers":[{"name":"'${RD_OPTION_APP_NAME}'","image":"'${RD_OPTION_PARAM_URL_REGISTRY}'"}]}}}}'

    if [ "$?" == 0 ]; then
      #### Sync deployment ####
      #echo "> > > > [`date +"%Y-%m-%d_%H-%M-%S"`] Rolling deployment ${DEPLOY}"
      info "Rolling deployment ${DEPLOY}"

      # Run until deployed or failed
      while :; do
        # Get the number of started pending or running number
        if [ -z "$DEPLOY" ]; then
          DEPLOY=$(oc deploy ${RD_OPTION_APP_NAME} -n ${RD_OPTION_OPENSHIFT_PROJECT} | grep -v $DEPLOYED | awk -F ' ' '{print $3}')
        fi

        # Get the status of rolling deployment
        STATUS=$(oc deploy ${RD_OPTION_APP_NAME} -n ${RD_OPTION_OPENSHIFT_PROJECT} | grep -v $DEPLOYED | awk -F ' ' '{print $4}')
        #echo "> > > > [`date +"%Y-%m-%d_%H-%M-%S"`] ${RD_OPTION_APP_NAME} - ${DEPLOY} - ${STATUS}"
        info "${RD_OPTION_APP_NAME} - ${DEPLOY} - ${STATUS}"

        case $STATUS in
          running)
            sleep 5
            continue
          ;;
          pending)
            sleep 5
            continue
          ;;
          failed)
            #echo "> > > > [`date +"%Y-%m-%d_%H-%M-%S"`] [FAILURE] Deployment ${DEPLOY} finalizado com *erro*"
            error "Deployment ${DEPLOY} finalizado com *erro*"
            exit 3
          ;;
          deployed)
            #echo "> > > > [`date +"%Y-%m-%d_%H-%M-%S"`] [SUCCESS] Deployment ${DEPLOY} finalizado com sucesso."
            info "[SUCCESS] Deployment ${DEPLOY} finalizado com sucesso."
            break
          ;;
          *)
            #echo "> > > > [`date +"%Y-%m-%d_%H-%M-%S"`] ${RD_OPTION_APP_NAME} - ${DEPLOY} - $STATUS . . ."
            info "${RD_OPTION_APP_NAME} - ${DEPLOY} - $STATUS . . ."
            continue
          ;;
        esac
      done
      #### Sync deployment ####
    else
      #echo "> > > > [`date +"%Y-%m-%d_%H-%M-%S"`] [FAILURE] Patch *failed*, deployment not started"
      error "Patch *failed*, deployment not started"
      exit 2
    fi
else
  #echo "> > > > [`date +"%Y-%m-%d_%H-%M-%S"`] [FAILURE] Can not get the current config"
  error "Can not get the current config"
  exit 1
fi

NAMESPACE=$1
APP=$1

if [ "$NAMESPACE" != "" ] && [ "$APP" != "" ] ; then
  #a. Get the latest deployed number
  DEPLOYED=$(oc deploy ${APP} -n ${NAMESPACE} | grep deployed | awk -F ' ' '{print $3}')

  #b. Trigger the deployment
  patch or set env

  #c. Get the number of started pending or running number
  DEPLOY=$(oc deploy ${APP} -n ${NAMESPACE} | grep -v $DEPLOYED | awk -F ' ' '{print $3}')

  echo "> > > > Rolling deployment #${DEPLOY}"

  #d. Get the status of started deploy
  #oc deploy ${APP} -n ${NAMESPACE} | grep -v $DEPLOYED | awd -F ' ' '{print $4}'

  #e. Run until deployed or failed
  while :; do
    STATUS=$(oc deploy ${APP} -n ${NAMESPACE} | grep -v $DEPLOYED | awd -F ' ' '{print $4}')
    echo "> > > > ${STATUS}"

    case $STATUS in
      running)
        sleep 5
        continue
      ;;
      pending)
        sleep 5
        continue
      ;;
      failed)
        echo "> > > > [FAILURE] Falha no Deployment"
        exit 1
      ;;
      deployed)
        echo "> > > > [SUCCESS] Deployment executado com sucesso."
        break
      ;;
      *)
        echo "> > > > $STATUS . . ."
        continue
      ;;
    esac
  done
else
  echo "Favor informar os parametros, exemplo: sync.sh [PROJECT_NAME] [APP_NAME]"
fi


### to fix the issue about oc login token shared between the job executions ###
HOME=$(pwd)/.${RD_JOB_EXECID}
cd $HOME
### fix end ###

### functions ###
# produz logging no console
## $1 mensagem
function info() {
  (echo -e "> > > > [`date +"%Y-%m-%d_%H-%M-%S"`] $1")
}

# produz logging de erros no console
## $1 mensagem de erro
function error() {
  (>&1 echo -e "> > > > [`date +"%Y-%m-%d_%H-%M-%S"`] *ERROR* $1")
}

# Acompanha em modo síncrono o deployment através do cli oc
## $1 nome do projeto openshift
## $2 nome do deployment Config
## $3 gatilho do deployment
function sync(){
  # Projeto Openshift
  NAMESPACE=$1

  # Deployment Config
  DC=$2

  # Gatilho do Deployment
  TRIGGER=$3

  info "[OPENSHIFT_PROJECT].: ${NAMESPACE}"
  info "[APP_NAME]..........: ${DC}"

  # Obter numero do deployment corrente, antes de disparar outro
  DEPLOYED=$(oc deploy ${DC} -n ${NAMESPACE} | grep deployed | awk -F ' ' '{print $3}')

  info "Current deployment ${DEPLOYED}"

  # Disparar gatilho do deployment
  eval $TRIGGER

  if [ "$?" == 0 ]; then
    #### Sync deployment ####
    info "Rolling deployment . . ."

    # Executar ate que seja status deployed ou failed
    while :; do
      # Obter numero do deployment disparado
      if [ -z "$DEPLOY" ]; then
        DEPLOY=$(oc deploy ${DC} -n ${NAMESPACE} | grep -v $DEPLOYED | awk -F ' ' '{print $3}')
      fi

      # Obter status do deployment disparado
      STATUS=$(oc deploy ${DC} -n ${NAMESPACE} | grep -v $DEPLOYED | awk -F ' ' '{print $4}')
      info "${DC} - ${DEPLOY} - ${STATUS}"

      case $STATUS in
        running)
          sleep 5
          continue
        ;;
        pending)
          sleep 5
          continue
        ;;
        failed)
          error "Deployment ${DEPLOY} finalizado com *erro*"
          exit 3
        ;;
        deployed)
          info "[SUCCESS] Deployment ${DEPLOY} finalizado com sucesso."
          break
        ;;
        *)
          info "${DC} - ${DEPLOY} - $STATUS . . ."
          continue
        ;;
      esac
    done
    #### Sync deployment ####
  else
    error "Deployment not triggered"
    exit 2
  fi
}
### /functions/ ###

info "[PARAM_URL_REGISTRY]: ${RD_OPTION_PARAM_URL_REGISTRY}"

# Get the current registry url
CURR=`oc get dc ${RD_OPTION_APP_NAME} -n ${RD_OPTION_OPENSHIFT_PROJECT} --template="{{(index .spec.template.spec.containers 0).image}}"`
if [ "$?" == 0 ]; then
  info "Imagem antiga: ${CURR}"

  sync ${RD_OPTION_OPENSHIFT_PROJECT} ${RD_OPTION_APP_NAME} \
       "oc patch dc ${RD_OPTION_APP_NAME} -n ${RD_OPTION_OPENSHIFT_PROJECT} --patch='{\"spec\":{\"template\":{\"spec\":{\"containers\":[{\"name\":\"'${RD_OPTION_APP_NAME}'\",\"image\":\"'${RD_OPTION_PARAM_URL_REGISTRY}'\"}]}}}}'"
else
  error "Can not get the current config"
  exit 1
fi

以上是关于sh Openshift部署com oc的主要内容,如果未能解决你的问题,请参考以下文章

Openshift CLI - 使用模板和 oc new-app 更新应用程序

带有 Cygwin 的 OpenShift oc 命令行

openshift上传java web项目

RedHat OpenShift QuickStart

OpenShift-OKD3.10部署

openshift安装部署