OFFICERenewalWildcardJenkinsfile 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. TARGET_HOSTS_PROXMOX = [ 'kvm1' ]
  10. pipeline {
  11. agent {
  12. label "swarm"
  13. }
  14. environment {
  15. REGISTRY_OFFICE='registry.sdsys.ru'
  16. CLUSTER_OFFICE='swarm.sdsys.ru'
  17. DOCKER_CERT_PATH='/run/secrets/swarm'
  18. IMAGE_NAME='registry.infoclinica.ru:5000/acme-dns:1.5'
  19. JENKINS_MAIL='jenkins.dev@sdsys.ru'
  20. SMTP_SERVER='mail.sdsys.ru'
  21. RECIPIENT_MAIL_BOX='admin@sdsys.ru'
  22. PKI_GIT_NAME='pki'
  23. DOMAIN='sdsys.ru'
  24. PKI_GIT_URL='ssh://git@git.sdsys.ru:8022/sdsys/pki.git'
  25. SWARM_GIT_URL='ssh://git@git.sdsys.ru:8022/sdsys/swarm.git'
  26. SWARM_GIT_NAME='swarm'
  27. }
  28. parameters {
  29. string(
  30. name: "mailto",
  31. defaultValue: "admin@sdsys.ru",
  32. description: "Email which has to be notified."
  33. )
  34. }
  35. stages {
  36. stage("Calculate Variables") {
  37. steps {
  38. script {
  39. ENDDATE = sh (script: "echo|openssl s_client -servername ${DOMAIN} -connect ${DOMAIN}:443 2>/dev/null|openssl x509 -noout -enddate", returnStdout: true).trim()
  40. CONFIG_DIR = PKI_GIT_NAME + '/' + DOMAIN + '/wildcard/letsencrypt'
  41. BACKUP_FILE = PKI_GIT_NAME + '/' + DOMAIN + '/wildcard/acme-dns/' + DOMAIN + '.dump.gz'
  42. COMMAND = PKI_GIT_NAME + '/' + DOMAIN + '/wildcard/acme-dns/' + 'renewal.sh'
  43. withCredentials([usernamePassword(credentialsId: 'jenkins', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
  44. JENKINS_USER = USERNAME
  45. JENKINS_PASS = PASSWORD
  46. }
  47. }
  48. }
  49. }
  50. // stage("Run Renewal") {
  51. // steps {
  52. // withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
  53. // sh """set +x
  54. // docker run -t --rm -e TZ=Europe/Moscow \
  55. // -e DOMAIN=${DOMAIN} -e CONFIG_DIR=${CONFIG_DIR} -e BACKUP_FILE=${BACKUP_FILE} \
  56. // -e JENKINS_MAIL=${JENKINS_MAIL} -e JENKINS_MAIL_USER=${JENKINS_USER} -e JENKINS_MAIL_PASS=${JENKINS_PASS} \
  57. // -e git_url=${PKI_GIT_URL} -e SMTP_SERVER=${SMTP_SERVER} -e RECIPIENT_MAIL_BOX=${RECIPIENT_MAIL_BOX} \
  58. // -e "SSHKEY=`cat ${GIT_SSH_KEY}`" -p 5353:53/udp -p 5353:53/tcp ${IMAGE_NAME} \
  59. // /${COMMAND}
  60. // """
  61. // }
  62. // }
  63. // }
  64. stage("Update docker secret in SWARM cluster") {
  65. steps {
  66. script {
  67. gitOps.clone(PKI_GIT_URL)
  68. gitOps.clone(SWARM_GIT_URL)
  69. NEW_ENDDATE = sh (script: "openssl x509 -enddate -noout -in ${CONFIG_DIR}/live/${DOMAIN}/cert.pem", returnStdout: true).trim()
  70. ENDDATE = sh (script: "echo|openssl s_client -servername ${REGISTRY_OFFICE} -connect ${REGISTRY_OFFICE}:443 2>/dev/null|openssl x509 -noout -enddate", returnStdout: true).trim()
  71. if (ENDDATE != NEW_ENDDATE) {
  72. echo "Update docker secret in ${CLUSTER_OFFICE}"
  73. NODE_IP = sh (script: "DOCKER_HOST=tcp://${CLUSTER_OFFICE}:2376 DOCKER_TLS_VERIFY=1 docker node inspect self -f '{{.Status.Addr}}'" , returnStdout: true).trim()
  74. dockerWCrenewal.update_secret(NODE_IP, SWARM_GIT_NAME, DOMAIN, CONFIG_DIR)
  75. }
  76. }
  77. }
  78. }
  79. stage("Update certificate and key to Proxmox") {
  80. steps {
  81. script {
  82. NEW_ENDDATE = sh (script: "openssl x509 -enddate -noout -in ${CONFIG_DIR}/live/${DOMAIN}/cert.pem", returnStdout: true).trim()
  83. TARGET_HOSTS_PROXMOX.each { item ->
  84. ENDDATE = sh (script: "echo|openssl s_client -servername ${item}.${DOMAIN} -connect ${item}.${DOMAIN}:8006 2>/dev/null|openssl x509 -noout -enddate", returnStdout: true).trim()
  85. if (ENDDATE != NEW_ENDDATE) {
  86. echo "Update certificate and key for ${item}"
  87. def PLAYBOOK = PKI_GIT_NAME + '/' + DOMAIN + '/wildcard/acme-dns/' + 'proxmox.yml'
  88. def TARGET_HOST = item + '.' + DOMAIN
  89. def TARGET_DIR = WORKSPACE + '/' + CONFIG_DIR
  90. dockerWCrenewal.update_sertificate(PLAYBOOK, TARGET_DIR, TARGET_HOST, DOMAIN)
  91. }
  92. }
  93. }
  94. }
  95. }
  96. stage("Update certificate and key APACHE-HOSTS") {
  97. steps {
  98. script {
  99. NEW_ENDDATE = sh (script: "openssl x509 -enddate -noout -in ${CONFIG_DIR}/live/${DOMAIN}/cert.pem", returnStdout: true).trim()
  100. TARGET_HOSTS_APACHE.each { item ->
  101. ENDDATE = sh (script: "echo|openssl s_client -servername ${item}.${DOMAIN} -connect ${item}.${DOMAIN}:443 2>/dev/null|openssl x509 -noout -enddate", returnStdout: true).trim()
  102. if (ENDDATE != NEW_ENDDATE) {
  103. echo "Update certificate and key for ${item}"
  104. def PLAYBOOK = PKI_GIT_NAME + '/' + DOMAIN + '/wildcard/acme-dns/' + 'apache.yml'
  105. def TARGET_HOST = item + '.' + DOMAIN
  106. def TARGET_DIR = WORKSPACE + '/' + CONFIG_DIR
  107. dockerWCrenewal.update_sertificate(PLAYBOOK, TARGET_DIR, TARGET_HOST, DOMAIN)
  108. }
  109. }
  110. }
  111. }
  112. }
  113. stage("Update certificate and key to ZIMBRA") {
  114. steps {
  115. script {
  116. ENDDATE = sh (script: "echo|openssl s_client -servername ${SMTP_SERVER} -connect ${SMTP_SERVER}:443 2>/dev/null|openssl x509 -noout -enddate", returnStdout: true).trim()
  117. if (ENDDATE != NEW_ENDDATE) {
  118. echo "Update certificate and key for ${SMTP_SERVER}"
  119. sh "cat ${PKI_GIT_NAME}/${DOMAIN}/wildcard/acme-dns/letsencrypt.ca.pem >> ${CONFIG_DIR}/live/${DOMAIN}/fullchain.pem"
  120. PLAYBOOK = PKI_GIT_NAME + '/' + DOMAIN + '/wildcard/acme-dns/' + 'mail.yml'
  121. TARGET_DIR = WORKSPACE + '/' + CONFIG_DIR
  122. dockerWCrenewal.update_sertificate(PLAYBOOK, TARGET_DIR, SMTP_SERVER, DOMAIN)
  123. }
  124. }
  125. }
  126. }
  127. }
  128. post {
  129. always {
  130. echo "CleaningUp work directory"
  131. deleteDir()
  132. }
  133. success {
  134. mail charset: 'UTF-8',
  135. subject: "Jenkins build SUCCESS",
  136. mimeType: 'text/html',
  137. to: "${mailto}",
  138. 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}"
  139. }
  140. failure {
  141. mail charset: 'UTF-8',
  142. subject: "Jenkins build ERROR",
  143. mimeType: 'text/html',
  144. to: "${mailto}",
  145. 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}"
  146. }
  147. }
  148. }