OFFICERenewalWildcardJenkinsfile 7.9 KB

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