OFFICERenewalWildcardJenkinsfile 7.8 KB

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