RenewalJenkinsfile 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. JENKINS_PASS = ''
  2. ENDDATE = ''
  3. NEW_ENDDATE = ''
  4. BACKUP_FILE = ''
  5. CONFIG_DIR = ''
  6. COMMAND = ''
  7. pipeline {
  8. agent {
  9. label "swarm"
  10. }
  11. environment {
  12. CLUSTER_NAME_OPEN='iru-swarm1-open.infoclinica.lan'
  13. CLUSTER_NAME_PROD='iru-swarm.infoclinica.lan'
  14. CLUSTER_NAME_DEV='dev-iru-swarm.infoclinica.lan'
  15. DOCKER_CERT_PATH='/run/secrets/swarm'
  16. IMAGE_NAME='registry.infoclinica.ru:5000/acme-dns:1.3'
  17. JENKINS_MAIL='jenkins.dev@sdsys.ru'
  18. SMTP_SERVER='mail.sdsys.ru'
  19. RECIPIENT_MAIL_BOX='admin@sdsys.ru'
  20. PKI_GIT_SUBDIR='iru'
  21. PKI_GIT_NAME='pki'
  22. DOMAIN='infoclinica.ru'
  23. PKI_GIT_URL='ssh://git@git.sdsys.ru:8022/sdsys/pki.git'
  24. STACK-DEPLOY_GIT_URL='ssh://git@git.sdsys.ru:8022/iru/stack-deploy.git'
  25. STACK-DEPLOY_GIT_NAME='stack-deploy'
  26. }
  27. parameters {
  28. string(
  29. name: "mailto",
  30. defaultValue: "admin@sdsys.ru",
  31. description: "Email which has to be notified."
  32. )
  33. }
  34. stages {
  35. stage("Calculate Variables) {
  36. steps {
  37. script {
  38. ENDDATE = sh (script: "$(echo | openssl s_client -servername ${DOMAIN} -connect ${DOMAIN}:443 2>/dev/null | openssl x509 -noout -enddate)", returnStdout: true).trim()
  39. CONFIG_DIR = PKI_GIT_NAME + '/' + PKI_GIT_SUBDIR + '/wildcard/letsencrypt'
  40. BACKUP_FILE = PKI_GIT_NAME + '/' + PKI_GIT_SUBDIR + '/wildcard/acme-dns/' + DOMAIN + '.dump.gz'
  41. COMMAND = PKI_GIT_NAME + '/' + PKI_GIT_SUBDIR + '/wildcard/acme-dns/' + 'renewal.sh'
  42. withCredentials([usernamePassword(credentialsId: 'jenkins', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
  43. JENKINS_USER = USERNAME
  44. JENKINS_PASS = PASSWORD
  45. }
  46. }
  47. }
  48. }
  49. stage("Run Renewal") {
  50. steps {
  51. withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
  52. sh """set +x
  53. DOCKER_HOST=tcp://${CLUSTER_NAME_OPEN}:2376 DOCKER_TLS_VERIFY=1 docker run -t --rm -e TZ=Europe/Moscow \
  54. -e DOMAIN=${DOMAIN} -e CONFIG_DIR=${CONFIG_DIR} -e BACKUP_FILE=${BACKUP_FILE} \
  55. -e JENKINS_MAIL=${JENKINS_MAIL} -e JENKINS_MAIL_USER=${JENKINS_USER} -e JENKINS_MAIL_PASS=${JENKINS_PASS} \
  56. -e git_url=${PKI_GIT_URL} -e SMTP_SERVER=${SMTP_SERVER} -e RECIPIENT_MAIL_BOX=${RECIPIENT_MAIL_BOX} \
  57. -e "SSHKEY=`cat ${GIT_SSH_KEY}`" -p 53:53/udp -p 53:53/tcp ${IMAGE_NAME} \
  58. /${COMMAND}
  59. """
  60. }
  61. }
  62. }
  63. stage("Update secret in CLUSTERS") {
  64. steps {
  65. script {
  66. git_clone(PKI_GIT_URL)
  67. git_clone(STACK-DEPLOY_GIT_URL)
  68. withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
  69. sh """GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  70. git clone ${PKI_GIT_URL}
  71. GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  72. git clone ${STACK-DEPLOY_GIT_URL}
  73. """
  74. }
  75. def NEW_ENDDATE = sh (script: "openssl x509 -enddate -noout -in ${CONFIG_DIR}/live/${DOMAIN}/cert.pem", returnStdout: true).trim()
  76. if (ENDDATE != NEW_ENDDATE) {
  77. echo "Update docker secret in ${CLUSTER_NAME_PROD}"
  78. def NODE_IP = sh (script: "DOCKER_HOST=tcp://${CLUSTER_NAME_PROD}:2376 DOCKER_TLS_VERIFY=1 docker node inspect self -f '{{.Status.Addr}}'" , returnStdout: true).trim()
  79. update_secret(NODE_IP, STACK-DEPLOY_GIT_NAME, DOMAIN, CONFIG_DIR)
  80. echo "Update docker secret in ${CLUSTER_NAME_DEV}"
  81. NODE_IP = sh (script: "DOCKER_HOST=tcp://${CLUSTER_NAME_DEV}:2376 DOCKER_TLS_VERIFY=1 docker node inspect self -f '{{.Status.Addr}}'" , returnStdout: true).trim()
  82. update_secret(NODE_IP, STACK-DEPLOY_GIT_NAME, DOMAIN, CONFIG_DIR)
  83. }
  84. }
  85. }
  86. }
  87. }
  88. post {
  89. always {
  90. echo "CleaningUp work directory"
  91. deleteDir()
  92. }
  93. success {
  94. mail charset: 'UTF-8',
  95. subject: "Jenkins build SUCCESS",
  96. mimeType: 'text/html',
  97. to: "${mailto}",
  98. body: "<b>ATTENTION!!!</b> <b><br> Jenkins job successed.\n\n <b><br>Project Name:</b> ${env.JOB_NAME} <b><br>Renewal certs and keys</b> <b><br>\nBuild Number:</b> ${env.BUILD_NUMBER} <b><br>\nURL Build:</b> ${RUN_DISPLAY_URL}"
  99. }
  100. failure {
  101. mail charset: 'UTF-8',
  102. subject: "Jenkins build ERROR",
  103. mimeType: 'text/html',
  104. to: "${mailto}",
  105. body: "<b>ATTENTION!!!</b> <b><br> Jenkins job failed.\n\n <b><br>Project Name:</b> ${env.JOB_NAME} <b><br>\nBuild Number:</b> ${env.BUILD_NUMBER} <b><br>\nURL Build:</b> ${RUN_DISPLAY_URL}"
  106. }
  107. }
  108. }
  109. def git_clone(String REPO) {
  110. withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
  111. sh """GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  112. git clone ${REPO}
  113. """
  114. }
  115. }
  116. def update_secret(String NODE_IP, String STACK-DEPLOY_GIT_NAME, String DOMAIN, String CONFIG_DIR) {
  117. sh """export DOCKER_HOST=tcp://${NODE_IP}:2376 DOCKER_TLS_VERIFY=1
  118. docker service rm infrastructure_registry
  119. docker secret rm infoclinica_full
  120. docker secret create infoclinica_full ${CONFIG_DIR}/archive/${DOMAIN}/${DOMAIN}.full-bundle
  121. cd ${STACK-DEPLOY_GIT_NAME}
  122. ./infrastructure.sh
  123. """
  124. }