docker-deploy-instance 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #!/bin/bash
  2. #
  3. # NOT YET UPGRADED
  4. # docker-deploy - Deploy docker image to an instance
  5. #
  6. # usage: docker-deploy
  7. #
  8. echo NOT YET UPGRADED
  9. exit 255
  10. IMAGE=${NAME}
  11. UPART=$(echo ${PART} | tr '[:lower:]' '[:upper:]')
  12. eval DEPLOY_VALIDATE=\${DEPLOY_${UPART}_VALIDATE}
  13. eval DEPLOY_GROUP=\${DEPLOY_${UPART}_GROUP}
  14. eval DEPLOY_COMMAND=\${DEPLOY_${UPART}_COMMAND}
  15. eval DEPLOY_TAG=\${DEPLOY_${UPART}_TAG}
  16. export AWS_ACCESS_KEY_ID=${DEPLOY_KEYS_AWS_ACCESS}
  17. export AWS_SECRET_ACCESS_KEY=${DEPLOY_KEYS_AWS_SECRET}
  18. export AWS_DEFAULT_REGION=${DEPLOY_AWS_REGION}
  19. . $(dirname ${BASH_SOURCE[0]})/common
  20. echo -e "\nDeploy container ${IMAGE}:${VERSION}\n"
  21. getTargets() {
  22. local arn targets tcount tag value
  23. tag="${DEPLOY_TAG/=*}"
  24. value="${DEPLOY_TAG/*=}"
  25. aws ec2 describe-instances --filter "Name=tag:${tag},Values=${value}" \
  26. --output text --query 'Reservations[].Instances[].InstanceId'
  27. if [ $? != 0 ] ; then
  28. echo "Cannot describe instances"
  29. exit 255
  30. fi
  31. }
  32. getHost() {
  33. local target
  34. target=$1
  35. aws ec2 describe-instances --instance-ids ${target} --output text --query Reservations[0].Instances[0].PrivateIpAddress
  36. }
  37. pullImage() {
  38. local i
  39. i=0
  40. while [ $i -lt 12 ]
  41. do
  42. echo Pull image ${IMAGE}:${VERSION}
  43. docker pull ${IMAGE}:${VERSION} | \
  44. egrep -v 'Already exists|Pulling|Waiting|Verifying|Download complete|Pull complete|Digest:'
  45. if [ $? = 0 ] ; then
  46. return 0
  47. fi
  48. echo "Cannot pull image, retry in 5 seconds. (${i})"
  49. sleep 5
  50. i=$((i+1))
  51. done
  52. return 1
  53. }
  54. validate() {
  55. local host i
  56. host=$1
  57. echo "Validate application at http://${host}${DEPLOY_VALIDATE}"
  58. i=0
  59. while [ $i -lt 5 ]
  60. do
  61. code=$(curl -s -o /dev/null --retry 10 --retry-delay 1 --retry-max-time 15 \
  62. -I -w "%{http_code}" http://${host}${DEPLOY_VALIDATE})
  63. if [ "${code}" = 200 ] ; then
  64. echo "PASSED: Health check successful"
  65. return 0
  66. fi
  67. echo "Continue to wait for application, retry in 5 seconds. (${i})"
  68. sleep 5
  69. i=$((i+1))
  70. done
  71. echo "FAILED: Cannot validate application, status ${code}"
  72. return 1
  73. }
  74. dockerLogin
  75. count=0
  76. passed=0
  77. for target in $(getTargets)
  78. do
  79. fail=
  80. count=$((count+1))
  81. host=$(getHost ${target})
  82. export DOCKER_HOST=tcp://${host}:2375
  83. if ! pullImage ; then
  84. echo "Cannot pull ${IMAGE}:${VERSION} on ${target}"
  85. continue
  86. fi
  87. echo
  88. echo "----------------------------------------------------------------------------------"
  89. echo "Deploy to instance ${target} at ${host}"
  90. echo "----------------------------------------------------------------------------------"
  91. current=$(docker ps --filter ancestor=${IMAGE}:${VERSION} --format '{{.ID}}')
  92. if [ $? != 0 ] ; then
  93. echo "Cannot talk to docker on ${target}"
  94. fail=1
  95. continue
  96. fi
  97. if [ "${current}" != "" ] ; then
  98. echo "Target ${target} already running version ${IMAGE}:${VERSION}"
  99. if [ "${FORCE}" = "" ] ; then
  100. passed=$((passed+1))
  101. continue
  102. fi
  103. fi
  104. #
  105. # Gracefully stop containers
  106. #
  107. containers=$(docker ps --filter "name=${NAME}" --format '{{.ID}}')
  108. if [ "${containers}" != "" ] ; then
  109. echo "Gracefully stop traffic on ${NAME}"
  110. echo "docker kill -s SIGQUIT ${NAME}"
  111. docker kill -s SIGQUIT ${NAME}
  112. echo "Stopping container ${NAME} ${containers}"
  113. docker stop ${NAME}
  114. if [ $? != 0 ] ; then
  115. echo "Cannot stop container ${container} on ${target}, continuing ..."
  116. # May not be running, continue
  117. fi
  118. fi
  119. #
  120. # Remove existing containers
  121. #
  122. echo "Remove container ${NAME} ${containers}"
  123. docker rm ${NAME} >/dev/null 2>&1
  124. #
  125. # Start new container
  126. #
  127. COMMAND=`echo ${DEPLOY_COMMAND} | sed "s/-d/-d -e HOST=${host}/"`
  128. echo "Start ${COMMAND}"
  129. ${COMMAND}
  130. if [ $? != 0 ] ; then
  131. echo "Cannot start container ${IMAGE}:${VERSION} on ${target}"
  132. echo "WARNING: target ${target} is not registered with load balancer, skip further deployments."
  133. fail=1
  134. fi
  135. #
  136. # Validate
  137. #
  138. if [ "${fail}" = "" ] ; then
  139. echo -n "Started: "
  140. docker ps --filter "ancestor=${IMAGE}:${VERSION}" --format '{{.ID}}, {{.Image}}, {{.Status}}'
  141. if validate ${host} ; then
  142. passed=$((passed+1))
  143. fi
  144. fi
  145. dockerLogout
  146. done
  147. if [ "${passed}" != "${count}" ] ; then
  148. echo "FAILED, upgraded ${passed} instances of ${count} with ${IMAGE}:${VERSION}"
  149. exit 1
  150. fi
  151. echo -e "\nPASSED, all ${count} instances running ${IMAGE}:${VERSION}"
  152. echo -e "\nRunning docker garbage collection"
  153. docker system prune -f >/dev/null
  154. DOCKER_HOST= docker system prune -f >/dev/null
  155. )
  156. exit 0