OFFICERenewalWildcardJenkinsfile 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. JENKINS_PASS = ''
  2. ENDDATE = ''
  3. NEW_ENDDATE = ''
  4. BACKUP_FILE = ''
  5. CONFIG_DIR = ''
  6. COMMAND = ''
  7. TARGET_HOSTS_APACHE = [ 'sugar', 'owncloud' ]
  8. TARGET_HOSTS_PROXMOX = [ 'kvm-test', 'kvm1', 'kvm2', 'kvm3', 'kvm4', 'kvm5', 'kvm6', 'kvm7' ]
  9. pipeline {
  10. agent {
  11. label "swarm"
  12. }
  13. environment {
  14. REGISTRY_OFFICE='registry.sdsys.ru'
  15. CLUSTER_OFFICE='swarm.sdsys.ru'
  16. DOCKER_CERT_PATH='/run/secrets/swarm'
  17. IMAGE_NAME='registry.infoclinica.ru:5000/acme-dns:1.5'
  18. JENKINS_MAIL='jenkins.dev@sdsys.ru'
  19. SMTP_SERVER='mail.sdsys.ru'
  20. RECIPIENT_MAIL_BOX='admin@sdsys.ru'
  21. PKI_GIT_NAME='pki'
  22. DOMAIN='sdsys.ru'
  23. PKI_GIT_URL='ssh://git@git.sdsys.ru:8022/sdsys/pki.git'
  24. SWARM_GIT_URL='ssh://git@git.sdsys.ru:8022/sdsys/swarm.git'
  25. SWARM_GIT_NAME='swarm'
  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 + '/' + DOMAIN + '/wildcard/letsencrypt'
  40. BACKUP_FILE = PKI_GIT_NAME + '/' + DOMAIN + '/wildcard/acme-dns/' + DOMAIN + '.dump.gz'
  41. COMMAND = PKI_GIT_NAME + '/' + DOMAIN + '/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 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 5353:53/udp -p 5353:53/tcp ${IMAGE_NAME} \
  58. /${COMMAND}
  59. """
  60. }
  61. }
  62. }
  63. stage("Update docker secret in SWARM cluster") {
  64. steps {
  65. script {
  66. gitOps.clone(PKI_GIT_URL)
  67. gitOps.clone(SWARM_GIT_URL)
  68. NEW_ENDDATE = sh (script: "openssl x509 -enddate -noout -in ${CONFIG_DIR}/live/${DOMAIN}/cert.pem", returnStdout: true).trim()
  69. ENDDATE = sh (script: "echo|openssl s_client -servername ${REGISTRY_OFFICE} -connect ${REGISTRY_OFFICE}:443 2>/dev/null|openssl x509 -noout -enddate", returnStdout: true).trim()
  70. if (ENDDATE != NEW_ENDDATE) {
  71. echo "Update docker secret in ${CLUSTER_OFFICE}"
  72. NODE_IP = sh (script: "DOCKER_HOST=tcp://${CLUSTER_OFFICE}:2376 DOCKER_TLS_VERIFY=1 docker node inspect self -f '{{.Status.Addr}}'" , returnStdout: true).trim()
  73. dockerWCrenewal.update_secret(NODE_IP, SWARM_GIT_NAME, DOMAIN, CONFIG_DIR)
  74. }
  75. }
  76. }
  77. }
  78. stage("Update certificate and key to Proxmox") {
  79. steps {
  80. script {
  81. NEW_ENDDATE = sh (script: "openssl x509 -enddate -noout -in ${CONFIG_DIR}/live/${DOMAIN}/cert.pem", returnStdout: true).trim()
  82. TARGET_HOSTS_PROXMOX.each { item ->
  83. ENDDATE = sh (script: "echo|openssl s_client -servername ${item}.${DOMAIN} -connect ${item}.${DOMAIN}:8006 2>/dev/null|openssl x509 -noout -enddate", returnStdout: true).trim()
  84. if (ENDDATE != NEW_ENDDATE) {
  85. echo "Update certificate and key for ${item}"
  86. def PLAYBOOK = PKI_GIT_NAME + '/' + DOMAIN + '/wildcard/acme-dns/' + 'proxmox.yml'
  87. def TARGET_HOST = item + '.' + DOMAIN
  88. def TARGET_DIR = WORKSPACE + '/' + CONFIG_DIR
  89. dockerWCrenewal.update_sertificate(PLAYBOOK, TARGET_DIR, TARGET_HOST, DOMAIN)
  90. }
  91. }
  92. }
  93. }
  94. }
  95. stage("Update certificate and key APACHE-HOSTS") {
  96. steps {
  97. script {
  98. NEW_ENDDATE = sh (script: "openssl x509 -enddate -noout -in ${CONFIG_DIR}/live/${DOMAIN}/cert.pem", returnStdout: true).trim()
  99. TARGET_HOSTS_APACHE.each { item ->
  100. ENDDATE = sh (script: "echo|openssl s_client -servername ${item}.${DOMAIN} -connect ${item}.${DOMAIN}:443 2>/dev/null|openssl x509 -noout -enddate", returnStdout: true).trim()
  101. if (ENDDATE != NEW_ENDDATE) {
  102. echo "Update certificate and key for ${item}"
  103. def PLAYBOOK = PKI_GIT_NAME + '/' + DOMAIN + '/wildcard/acme-dns/' + 'apache.yml'
  104. def TARGET_HOST = item + '.' + DOMAIN
  105. def TARGET_DIR = WORKSPACE + '/' + CONFIG_DIR
  106. dockerWCrenewal.update_sertificate(PLAYBOOK, TARGET_DIR, TARGET_HOST, DOMAIN)
  107. }
  108. }
  109. }
  110. }
  111. }
  112. stage("Update certificate and key to ZIMBRA") {
  113. steps {
  114. script {
  115. ENDDATE = sh (script: "echo|openssl s_client -servername ${SMTP_SERVER} -connect ${SMTP_SERVER}:443 2>/dev/null|openssl x509 -noout -enddate", returnStdout: true).trim()
  116. if (ENDDATE != NEW_ENDDATE) {
  117. echo "Update certificate and key for ${SMTP_SERVER}"
  118. sh "cat ${PKI_GIT_NAME}/${DOMAIN}/wildcard/acme-dns/letsencrypt.ca.pem >> ${CONFIG_DIR}/live/${DOMAIN}/fullchain.pem"
  119. PLAYBOOK = PKI_GIT_NAME + '/' + DOMAIN + '/wildcard/acme-dns/' + 'mail.yml'
  120. TARGET_DIR = WORKSPACE + '/' + CONFIG_DIR
  121. dockerWCrenewal.update_sertificate(PLAYBOOK, TARGET_DIR, SMTP_SERVER, DOMAIN)
  122. }
  123. }
  124. }
  125. }
  126. }
  127. post {
  128. always {
  129. echo "CleaningUp work directory"
  130. deleteDir()
  131. }
  132. success {
  133. mail charset: 'UTF-8',
  134. subject: "Jenkins build SUCCESS",
  135. mimeType: 'text/html',
  136. to: "${mailto}",
  137. 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}"
  138. }
  139. failure {
  140. mail charset: 'UTF-8',
  141. subject: "Jenkins build ERROR",
  142. mimeType: 'text/html',
  143. to: "${mailto}",
  144. 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}"
  145. }
  146. }
  147. }