RenewalJenkinsfile 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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_PASS = PASSWORD
  44. }
  45. }
  46. }
  47. }
  48. stage("Run Renewal") {
  49. steps {
  50. withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
  51. sh """set +x
  52. DOCKER_HOST=tcp://${CLUSTER_NAME_OPEN}:2376 DOCKER_TLS_VERIFY=1 docker run -t --rm -e TZ=Europe/Moscow \
  53. -e DOMAIN=${DOMAIN} -e CONFIG_DIR=${CONFIG_DIR} -e BACKUP_FILE=${BACKUP_FILE} \
  54. -e JENKINS_MAIL_PASS=${JENKINS_PASS} -e JENKINS_MAIL_USER=${JENKINS_MAIL} \
  55. -e git_url=${PKI_GIT_URL} -e SMTP_SERVER=${SMTP_SERVER} -e RECIPIENT_MAIL_BOX=${RECIPIENT_MAIL_BOX} \
  56. -e "SSHKEY=`cat ${GIT_SSH_KEY}`" -p 53:53/udp -p 53:53/tcp ${IMAGE_NAME} \
  57. /${COMMAND}
  58. """
  59. }
  60. }
  61. }
  62. stage("Update secret in CLUSTERS") {
  63. steps {
  64. script {
  65. git_clone(PKI_GIT_URL)
  66. git_clone(STACK-DEPLOY_GIT_URL)
  67. withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
  68. sh """GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  69. git clone ${PKI_GIT_URL}
  70. GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  71. git clone ${STACK-DEPLOY_GIT_URL}
  72. """
  73. }
  74. def NEW_ENDDATE = sh (script: "openssl x509 -enddate -noout -in ${CONFIG_DIR}/live/${DOMAIN}/cert.pem", returnStdout: true).trim()
  75. if (ENDDATE != NEW_ENDDATE) {
  76. echo "Update docker secret in ${CLUSTER_NAME_PROD}"
  77. 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()
  78. update_secret(NODE_IP, STACK-DEPLOY_GIT_NAME, DOMAIN, CONFIG_DIR)
  79. echo "Update docker secret in ${CLUSTER_NAME_DEV}"
  80. 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()
  81. update_secret(NODE_IP, STACK-DEPLOY_GIT_NAME, DOMAIN, CONFIG_DIR)
  82. }
  83. }
  84. }
  85. }
  86. }
  87. post {
  88. always {
  89. echo "CleaningUp work directory"
  90. deleteDir()
  91. }
  92. success {
  93. mail charset: 'UTF-8',
  94. subject: "Jenkins build SUCCESS",
  95. mimeType: 'text/html',
  96. to: "${mailto}",
  97. 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}"
  98. }
  99. failure {
  100. mail charset: 'UTF-8',
  101. subject: "Jenkins build ERROR",
  102. mimeType: 'text/html',
  103. to: "${mailto}",
  104. 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}"
  105. }
  106. }
  107. }
  108. def git_clone(String REPO) {
  109. withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
  110. sh """GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  111. git clone ${REPO}
  112. """
  113. }
  114. }
  115. def update_secret(String NODE_IP, String STACK-DEPLOY_GIT_NAME, String DOMAIN, String CONFIG_DIR) {
  116. sh """export DOCKER_HOST=tcp://${NODE_IP}:2376 DOCKER_TLS_VERIFY=1
  117. docker service rm infrastructure_registry
  118. docker secret rm infoclinica_full
  119. docker secret create infoclinica_full ${CONFIG_DIR}/archive/${DOMAIN}/${DOMAIN}.full-bundle
  120. cd ${STACK-DEPLOY_GIT_NAME}
  121. ./infrastructure.sh
  122. """
  123. }