sh rancher-deploy.sh

Posted

tags:

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

timeout 360 rancher-compose -f docker-compose.yml -p $APPLICATION_NAME up -d --force-upgrade
error_code=$?

if [ "$error_code" -eq "0" ]; then
  echo "SUCCESS: Confirming Upgrade"
  rancher-compose -p $APPLICATION_NAME up -d --upgrade --confirm-upgrade
else
  echo "FAILURE: Rolling back"

  PROJECT_JSON=`curl -s --ssl -u "${RANCHER_ACCESS_KEY}:${RANCHER_SECRET_KEY}" \
    -X GET \
    -H 'Accept: application/json' \
    -H 'Content-Type: application/json' \
  "https://rancher.example.com/v1/projects/{$ENV_ID}/environments?name=${APPLICATION_NAME}&sort=id"`

  # Gets the id for the project from Rancher API
  PROJECT_ID=`echo "$PROJECT_JSON" | jq -r '.data[].id' `

  # Gets the links to the services (containers)
  SERVICES_LINK=`echo "$PROJECT_JSON" | jq -r '.data[].links.services' `

  # Grabs all the services for the project that are stuck in an upgrading state
  SERVICES_TO_CANCEL=`curl -s --ssl -u "${RANCHER_ACCESS_KEY}:${RANCHER_SECRET_KEY}" \
    -X GET \
    -H 'Accept: application/json' \
    -H 'Content-Type: application/json' \
  "${SERVICES_LINK}?state=upgrading" \
  | jq -r '.data[] | select(.state == "upgrading") | .actions.cancelupgrade'`

  # Loop through the services that need to be cancelled, and cancel them
  for i in $(echo "${SERVICES_TO_CANCEL}"); do
    SERVICE=`curl -s --ssl -u "${RANCHER_ACCESS_KEY}:${RANCHER_SECRET_KEY}" \
    -X POST \
    -H 'Accept: application/json' \
    -H 'Content-Type: application/json' \
    -d '{}' \
  "${i}" | jq -r '.id'`
    echo "Sent stop upgrade to $SERVICE"
  done

  # Checks the Rancher API to determine whether the services have been cancelled or not
  CONDITIONAL="curl -s --ssl -u \"${RANCHER_ACCESS_KEY}:${RANCHER_SECRET_KEY}\" \
    -X GET \
    -H 'Accept: application/json' \
    -H 'Content-Type: application/json' \
  \"${SERVICES_LINK}\" \
  | jq -r '.data[] | select(.state == \"canceling-upgrade\")'"

  # When all the services have been cancelled, we stop checking if they've been cancelled
  while [ -n "`eval $CONDITIONAL`" ]; do
    echo "Waiting for canceling-upgrade to finish..."
    sleep 5
  done

  # Get all the services that need to be rolled back
  SERVICES_TO_ROLLBACK=`curl -s --ssl -u "${RANCHER_ACCESS_KEY}:${RANCHER_SECRET_KEY}" \
    -X GET \
    -H 'Accept: application/json' \
    -H 'Content-Type: application/json' \
  "${SERVICES_LINK}" \
  | jq -r '.data[] | select(.state == "canceled-upgrade") | .actions.rollback'`

  # Roll them back
  for i in $(echo "${SERVICES_TO_ROLLBACK}"); do
    SERVICE=`curl -s --ssl -u "${RANCHER_ACCESS_KEY}:${RANCHER_SECRET_KEY}" \
    -X POST \
    -H 'Accept: application/json' \
    -H 'Content-Type: application/json' \
    -d '{}' \
  "${i}" | jq -r '.id'`
    echo "Rolled back $SERVICE"
  done
fi

以上是关于sh rancher-deploy.sh的主要内容,如果未能解决你的问题,请参考以下文章

如何使我的命令行在具有扩展名(.sh)和名称如“weird.sh.sh.sh”的文件上工作

sh sh_template.sh

sh sh.sh

Linux下面如何运行 SH文件

配置告警系统主脚本main.sh mon.sh load.sh 502.sh disk.sh

shell 脚本各种执行方式(source ./*.sh, . ./*.sh, ./*.sh)的区别