Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.14 KB | None | 0 0
  1. // Sandbox approvals that you will need (at least):
  2. // staticMethod org.codehaus.groovy.runtime.DefaultGroovyMethods plus java.lang.Object[]
  3. // staticMethod org.codehaus.groovy.runtime.DefaultGroovyMethods getAt java.lang.Iterable int
  4. // java.lang.Object[]
  5. // staticMethod org.codehaus.groovy.runtime.DefaultGroovyMethods plus java.util.List java.lang.Object
  6.  
  7. // check for required parameters. assign them to the env for
  8. // convenience and make sure that an exception is raised if any
  9. // are missing as a side-effect
  10.  
  11. env.APP = APP
  12. env.REPO = REPO
  13. env.ADMIN_EMAIL = ADMIN_EMAIL
  14.  
  15. def hosts = HOSTS.split(" ")
  16.  
  17. // optional (not all apps use celery/beat)
  18. def celery_hosts = [:]
  19. def beat_hosts = [:]
  20. try {
  21. celery_hosts = CELERY_HOSTS.split(" ")
  22. } catch (hostsErr) {
  23. celery_hosts = []
  24. }
  25. try {
  26. beat_hosts = BEAT_HOSTS.split(" ")
  27. } catch (beatserr) {
  28. beat_hosts = []
  29. }
  30.  
  31. def all_hosts = hosts + celery_hosts + beat_hosts as Set
  32.  
  33. def sentry = true
  34.  
  35. try {
  36. env.SENTRY_URL = SENTRY_URL
  37. } catch (sentryErr) {
  38. println "sentry not configured"
  39. sentry = false
  40. }
  41.  
  42. def err = null
  43. currentBuild.result = "SUCCESS"
  44.  
  45. try {
  46. node {
  47. stage('Checkout') {
  48. checkout scm
  49. TAG = sh (script: "git log -n 1 --pretty=format:'%H'", returnStdout: true)
  50. env.TAG = TAG
  51. }
  52. stage("Build") {
  53. retry_backoff(5) { sh "docker pull ${REPO}/${APP}:latest" }
  54. sh "make build"
  55. }
  56. stage("Docker Push") {
  57. retry_backoff(5) { sh "docker push ${REPO}/${APP}:${TAG}" }
  58. }
  59. }
  60.  
  61. node {
  62. def branches = [:]
  63. for (int i = 0; i < all_hosts.size(); i++) {
  64. branches["pull-${i}"] = create_pull_exec(i, all_hosts[i])
  65. }
  66. parallel branches
  67.  
  68. def host = all_hosts[0]
  69.  
  70. stage("Migrate") {
  71. sh "ssh ${host} /usr/local/bin/docker-runner ${APP} migrate"
  72. }
  73. stage("Collectstatic") {
  74. sh "ssh ${host} /usr/local/bin/docker-runner ${APP} collectstatic"
  75. }
  76. stage("Compress") {
  77. sh "ssh ${host} /usr/local/bin/docker-runner ${APP} compress"
  78. }
  79. }
  80.  
  81. node {
  82. def branches = [:]
  83. for (int i = 0; i < hosts.size(); i++) {
  84. branches["web-restart-${i}"] = create_restart_web_exec(i, hosts[i])
  85. }
  86. parallel branches
  87. }
  88.  
  89. node {
  90. def branches = [:]
  91. for (int i = 0; i < celery_hosts.size(); i++) {
  92. branches["celery-restart-${i}"] = create_restart_celery_exec(i, celery_hosts[i])
  93. }
  94. parallel branches
  95. }
  96.  
  97. node {
  98. def branches = [:]
  99. for (int i = 0; i < beat_hosts.size(); i++) {
  100. branches["beat-restart-${i}"] = create_restart_beat_exec(i, beat_hosts[i])
  101. }
  102. parallel branches
  103. }
  104.  
  105. if (sentry) {
  106. node {
  107. stage("Sentry") {
  108. sh '''COMMIT=$(git log -n 1 --pretty=format:'%H')
  109. curl ${SENTRY_URL} \
  110. -X POST \
  111. -H "Content-Type: application/json" \
  112. -d "{\\\"version\\\": \\\"${COMMIT}\\\"}"
  113. '''
  114. }
  115. }
  116. }
  117.  
  118. } catch (caughtError) {
  119. err = caughtError
  120. currentBuild.result = "FAILURE"
  121. } finally {
  122. (currentBuild.result != "ABORTED") && node {
  123. step([$class: 'Mailer',
  124. notifyEveryUnstableBuild: true,
  125. recipients: ADMIN_EMAIL,
  126. sendToIndividuals: true])
  127. }
  128.  
  129. /* Must re-throw exception to propagate error */
  130. if (err) {
  131. throw err
  132. }
  133. }
  134.  
  135. // -------------------- helper functions ----------------------
  136.  
  137. def create_pull_exec(int i, String host) {
  138. cmd = {
  139. node {
  140. stage("Docker Pull - "+i) {
  141. sh """
  142. ssh ${host} docker pull \${REPOSITORY}\$REPO/${APP}:\$TAG
  143. ssh ${host} cp /var/www/${APP}/TAG /var/www/${APP}/REVERT || true
  144. ssh ${host} "echo export TAG=\$TAG > /var/www/${APP}/TAG"
  145. """
  146. }
  147. }
  148. }
  149. return cmd
  150. }
  151.  
  152. def create_restart_web_exec(int i, String host) {
  153. cmd = {
  154. node {
  155. stage("Restart Gunicorn - "+i) {
  156. sh """
  157. ssh ${host} sudo systemctl stop ${APP}.service || true
  158. ssh ${host} sudo systemctl start ${APP}.service
  159. """
  160. }
  161. }
  162. }
  163. return cmd
  164. }
  165.  
  166. def create_restart_celery_exec(int i, String host) {
  167. cmd = {
  168. node {
  169. stage("Restart Worker - "+i) {
  170. sh """
  171. ssh ${host} sudo systemctl stop ${APP}-worker.service || true
  172. ssh ${host} sudo systemctl start ${APP}-worker.service
  173. """
  174. }
  175. }
  176. }
  177. return cmd
  178. }
  179.  
  180. def create_restart_beat_exec(int i, String host) {
  181. cmd = {
  182. node {
  183. stage("Restart Beat - "+i) {
  184. sh """
  185. ssh ${host} sudo systemctl stop ${APP}-beat.service|| true
  186. ssh ${host} sudo systemctl start ${APP}-beat.service
  187. """
  188. }
  189. }
  190. }
  191. return cmd
  192. }
  193.  
  194. // retry with exponential backoff
  195. def retry_backoff(int max_attempts, Closure c) {
  196. int n = 0
  197. while (n < max_attempts) {
  198. try {
  199. c()
  200. return
  201. } catch (err) {
  202. if ((n + 1) >= max_attempts) {
  203. // we're done. re-raise the exception
  204. throw err
  205. }
  206. sleep(2**n)
  207. n++
  208. }
  209. }
  210. return
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement