Jenkinsfile_genkey 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. pipeline {
  2. agent {
  3. label "swarm"
  4. }
  5. environment {
  6. DOCKER_REGISTRY='dev-registry.infoclinica.ru:5000'
  7. DOCKER_IMAGE='ovpn'
  8. SERVICE_NAME="keygen"
  9. PKI_GIT_URL='ssh://git@git.sdsys.ru:8022/iru/openvpn-pki.git'
  10. PKI_GIT_NAME='openvpn-pki'
  11. OVPN_GIT_URL='ssh://git@git.sdsys.ru:8022/iru/openvpn.git'
  12. OVPN_GIT_DIR='openvpn'
  13. JENKINS_MAIL='jenkins.dev@sdsys.ru'
  14. SMTP_SERVER='mail.sdsys.ru'
  15. }
  16. parameters {
  17. string(
  18. name: "client_mail",
  19. defaultValue: "tomishinets.v@sdsys.ru",
  20. description: "Email which has to be recieved certs and key"
  21. )
  22. string(
  23. name: "key_name",
  24. defaultValue: "test",
  25. description: "The names for generation keys and certs."
  26. )
  27. string(
  28. name: "mode",
  29. defaultValue: "client",
  30. description: "For who generate cert, key and conf-file (clients or admins)"
  31. )
  32. string(
  33. name: "mailto",
  34. defaultValue: "tomishinets.v@sdsys.ru",
  35. description: "Email which has to be notified."
  36. )
  37. }
  38. stages {
  39. stage("Check if already exist CNAME") {
  40. steps {
  41. withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
  42. sh '''GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  43. git clone ${PKI_GIT_URL}'''
  44. }
  45. script {
  46. def zip = "${WORKSPACE}/${PKI_GIT_NAME}/open/easy-rsa/client_keys/sds-${key_name}.zip"
  47. if (fileExists(zip)) {
  48. currentBuild.result = 'ABORTED'
  49. return
  50. }
  51. }
  52. }
  53. }
  54. stage("Generate Keys and Certs") {
  55. steps {
  56. script {
  57. if (currentBuild.result == 'ABORTED') {
  58. return
  59. }
  60. echo "Running ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:latest."
  61. withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
  62. sh '''set +x
  63. docker run -i --rm -e TZ=Europe/Moscow -e "mode=keygen" -e "SSHKEY=$(cat ${GIT_SSH_KEY})" \
  64. ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:latest /tmp/keygen.sh ${key_name}
  65. '''
  66. }
  67. }
  68. }
  69. }
  70. stage("Generate configs") {
  71. steps {
  72. script {
  73. if (currentBuild.result == 'ABORTED') {
  74. return
  75. }
  76. echo "Delete old repo version"
  77. sh 'rm -rf ${WORKSPACE}/${PKI_GIT_NAME} && rm -rf ${WORKSPACE}/${OVPN_GIT_DIR}'
  78. echo "Generate ccd config"
  79. withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
  80. sh '''GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  81. git clone ${OVPN_GIT_URL}'''
  82. }
  83. if (mode == 'client') {
  84. def file = "${WORKSPACE}/${OVPN_GIT_DIR}/ip_client.txt"
  85. }
  86. if (mode == 'admin') {
  87. def file = "${WORKSPACE}/${OVPN_GIT_DIR}/ip_admin.txt"
  88. }
  89. string ip = readFile(file)
  90. split = ip.tokenize(".")
  91. if (split[3].toInteger() >= 254) {
  92. currentBuild.result == 'FAILURE'
  93. return
  94. } else {
  95. split[3] = (split[3].toInteger() + 1) + ""
  96. def newIp = split.join(".")
  97. string txt = split[3].toString()
  98. writeFile file: file, text: newIp
  99. def conf = "${WORKSPACE}/${OVPN_GIT_DIR}/${OVPN_GIT_DIR}/ccd/${key_name}"
  100. if (mode == 'client') {
  101. writeFile file: conf, text: "ifconfig-push " + newIp + " 255.255.255.0"
  102. }
  103. if (mode == 'admin') {
  104. writeFile file: conf, text: "ifconfig-push " + newIp + " 255.255.0.0"
  105. }
  106. }
  107. withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
  108. sh '''cd ${OVPN_GIT_DIR}
  109. echo "Add new config for ${key_name}" > ../commit.txt
  110. git add -A
  111. git config --global user.email "${JENKINS_MAIL}"
  112. git config --global user.name "Jenkins"
  113. git commit -F ../commit.txt
  114. GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  115. git push origin master
  116. ls'''
  117. }
  118. }
  119. }
  120. }
  121. stage("Send key, certs and config with email") {
  122. steps {
  123. script {
  124. if ( currentBuild.result == 'ABORTED' || currentBuild.result == 'FAILURE' ) {
  125. return
  126. }
  127. echo "Pull new OPENVPN-PKI repo version"
  128. withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
  129. sh '''GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  130. git clone ${PKI_GIT_URL}'''
  131. }
  132. echo "Send certs,key and config-file to client"
  133. def fileZip = "${WORKSPACE}/${PKI_GIT_NAME}/open/easy-rsa/client_keys/sds-${key_name}.zip"
  134. if ( !fileExists("${fileZip}")) {
  135. currentBuild.result == 'FAILURE'
  136. return
  137. } else {
  138. withCredentials([usernamePassword(credentialsId: 'jenkins', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
  139. sh '''echo "Ваши ключ, сертификаты и конфигурационный файл для подключения к infoclinica.ru" | email -s "Your Certs and Key" \
  140. -f ${JENKINS_MAIL} \
  141. -r ${SMTP_SERVER} \
  142. -m login \
  143. -u ${USERNAME} \
  144. -i ${PASSWORD} \
  145. -a ${WORKSPACE}/sds-${key_name}.zip \
  146. ${client_mail}
  147. '''
  148. }
  149. }
  150. }
  151. }
  152. }
  153. }
  154. post {
  155. always {
  156. echo "CleaningUp work directory"
  157. deleteDir()
  158. }
  159. failure {
  160. mail charset: 'UTF-8',
  161. subject: "Jenkins build ERROR",
  162. mimeType: 'text/html',
  163. to: "${mailto}",
  164. 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}"
  165. }
  166. aborted {
  167. mail charset: 'UTF-8',
  168. subject: "Jenkins build ERROR",
  169. mimeType: 'text/html',
  170. to: "${client_mail}",
  171. body: "<b>ATTENTION!!!</b> <b><br> Jenkins job aborted.\n\n <b><br> The CNAME ${key_name} is already exists!\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}"
  172. }
  173. }
  174. }