Jenkinsfile_keygen 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. pipeline {
  2. agent {
  3. label "swarm"
  4. }
  5. environment {
  6. DOCKER_REGISTRY='dev-registry.infoclinica.ru:5000'
  7. DOCKER_IMAGE='ovpn-rsa'
  8. SERVICE_NAME="ovpn-rsa_server"
  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. DOCKER_CERT_PATH='/run/secrets/swarm'
  16. CLUSTER_NAME='dev-iru-swarm.infoclinica.lan'
  17. COMMAND=''
  18. }
  19. parameters {
  20. string(
  21. name: "branch",
  22. defaultValue: "97009",
  23. description: "Which branch to use"
  24. )
  25. choice (
  26. choices: 'keygen\nrevoke',
  27. description: 'Whats is action?',
  28. name: 'TASK_ACTION')
  29. choice (
  30. choices: 'client\nadmin',
  31. description: 'Whats is mode?',
  32. name: 'MODE')
  33. string(
  34. name: "client_mail",
  35. defaultValue: "tomishinets.v@sdsys.ru",
  36. description: "Email which has to be recieved certs and key"
  37. )
  38. string(
  39. name: "key_name",
  40. defaultValue: "test",
  41. description: "The names for generation keys and certs."
  42. )
  43. string(
  44. name: "mailto",
  45. defaultValue: "tomishinets.v@sdsys.ru",
  46. description: "Email which has to be notified."
  47. )
  48. }
  49. stages {
  50. stage("Pull repo") {
  51. steps {
  52. withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
  53. sh '''GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  54. git clone ${PKI_GIT_URL}
  55. cd ${WORKSPACE}/${PKI_GIT_NAME} && git checkout ${branch}
  56. '''
  57. }
  58. }
  59. }
  60. stage("Generate Keys and Certs or Revoke") {
  61. steps {
  62. script {
  63. switch (TASK_ACTION) {
  64. case 'keygen':
  65. def cert = "${WORKSPACE}/${PKI_GIT_NAME}/open/easy-rsa/client_keys/sds-${key_name}.zip"
  66. if (fileExists(cert)) {
  67. currentBuild.result = 'ABORTED'
  68. error ("Cert already exist!!!")
  69. return
  70. }
  71. COMMAND ="keygen.sh"
  72. break
  73. case 'revoke':
  74. def cert = "${WORKSPACE}/${PKI_GIT_NAME}/open/easy-rsa/client_keys/sds-${key_name}.zip"
  75. if (!fileExists(cert)) {
  76. currentBuild.result = 'ABORTED'
  77. error ("Cert doesn't exist!!!")
  78. return
  79. }
  80. COMMAND ="revoke.sh"
  81. break
  82. }
  83. echo "Running ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:latest."
  84. withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
  85. sh """set +x
  86. docker pull $DOCKER_REGISTRY/iru/$DOCKER_IMAGE:latest
  87. docker run -i --rm -e TZ=Europe/Moscow -e mode=keygen -e "SSHKEY=`cat ${GIT_SSH_KEY}`" \
  88. -e git_url=${PKI_GIT_URL} -e git_dir=${PKI_GIT_NAME} \
  89. $DOCKER_REGISTRY/iru/$DOCKER_IMAGE:latest /tmp/$COMMAND $key_name $branch
  90. """
  91. }
  92. }
  93. }
  94. }
  95. stage("Pull new version of REPOs") {
  96. steps {
  97. script {
  98. echo "Delete old repo version"
  99. sh 'rm -rf ${WORKSPACE}/${PKI_GIT_NAME} && rm -rf ${WORKSPACE}/${OVPN_GIT_DIR}'
  100. withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
  101. sh '''GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  102. git clone ${OVPN_GIT_URL}
  103. cd ${WORKSPACE}/${OVPN_GIT_DIR} && git checkout ${branch} && cd ${WORKSPACE}
  104. GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  105. git clone ${PKI_GIT_URL}
  106. cd ${WORKSPACE}/${PKI_GIT_NAME} && git checkout ${branch}
  107. '''
  108. }
  109. }
  110. }
  111. }
  112. stage("Generate configs") {
  113. when {
  114. expression { params.TASK_ACTION == 'keygen' }
  115. }
  116. steps {
  117. script {
  118. switch (MODE) {
  119. case 'client':
  120. file = "${WORKSPACE}/${OVPN_GIT_DIR}/ip_client.txt"
  121. break
  122. case 'admin':
  123. file = "${WORKSPACE}/${OVPN_GIT_DIR}/ip_admin.txt"
  124. break
  125. }
  126. string ip = readFile(file)
  127. split = ip.tokenize(".")
  128. if (split[3].toInteger() >= 254) {
  129. currentBuild.result == 'FAILURE'
  130. error ("The last oktet => 254!!!")
  131. return
  132. } else {
  133. split[3] = (split[3].toInteger() + 1) + ""
  134. def newIp = split.join(".")
  135. string txt = split[3].toString()
  136. writeFile file: file, text: newIp
  137. def conf = "${WORKSPACE}/${OVPN_GIT_DIR}/${OVPN_GIT_DIR}/ccd/${key_name}"
  138. writeFile file: conf, text: "ifconfig-push " + newIp + " 255.255.0.0"
  139. }
  140. withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
  141. sh '''cd ${OVPN_GIT_DIR}
  142. echo "Add new config for ${key_name}" > ../commit.txt
  143. git add -A
  144. git config --global user.email "${JENKINS_MAIL}"
  145. git config --global user.name "Jenkins"
  146. git commit -F ../commit.txt
  147. GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  148. git push origin ${branch}
  149. '''
  150. }
  151. }
  152. }
  153. }
  154. stage("Send key, certs and config with email") {
  155. when {
  156. expression { params.TASK_ACTION == 'keygen' }
  157. }
  158. steps {
  159. script {
  160. fileZip = "${WORKSPACE}/${PKI_GIT_NAME}/open/easy-rsa/client_keys/sds-${key_name}.zip"
  161. if ( !fileExists("${fileZip}")) {
  162. currentBuild.result == 'FAILURE'
  163. return
  164. } else {
  165. withEnv(["zip=${fileZip}"]) {
  166. withCredentials([usernamePassword(credentialsId: 'jenkins', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
  167. sh '''echo "Ваши ключ, сертификаты и конфигурационный файл для подключения к infoclinica.ru" | email -s "Your Certs and Key" \
  168. -f ${JENKINS_MAIL} \
  169. -r ${SMTP_SERVER} \
  170. -m login \
  171. -u ${USERNAME} \
  172. -i ${PASSWORD} \
  173. -a ${zip} \
  174. ${client_mail}
  175. '''
  176. }
  177. }
  178. }
  179. }
  180. }
  181. }
  182. stage("Update ccd-files and crl.pem") {
  183. steps {
  184. script {
  185. def NODE = sh (script: "DOCKER_HOST=tcp://${CLUSTER_NAME}:2376 DOCKER_TLS_VERIFY=1 docker service ps \${SERVICE_NAME} --format '{{.Node}}' --filter desired-state=Running" , returnStdout: true).trim()
  186. sh "if [ -z ${NODE} ]; then echo '${SERVICE_NAME} does not running'; exit 1; fi"
  187. def container_id = sh (script: "DOCKER_HOST=tcp://${NODE}:2376 DOCKER_TLS_VERIFY=1 docker ps -q -f label=ru.sdsys.subcontainer=\${SERVICE_NAME}" , returnStdout: true).trim()
  188. sh "DOCKER_HOST=tcp://${NODE}:2376 DOCKER_TLS_VERIFY=1 docker cp ${WORKSPACE}/${OVPN_GIT_DIR}/${OVPN_GIT_DIR}/ccd/ ${container_id}:/etc/${OVPN_GIT_DIR}/persist"
  189. sh "DOCKER_HOST=tcp://${NODE}:2376 DOCKER_TLS_VERIFY=1 docker cp ${WORKSPACE}/${PKI_GIT_NAME}/open/easy-rsa/keys/stonevpn.crl ${container_id}:/etc/${OVPN_GIT_DIR}/persist/stonevpn.crl"
  190. }
  191. }
  192. }
  193. }
  194. post {
  195. always {
  196. echo "CleaningUp work directory"
  197. deleteDir()
  198. }
  199. failure {
  200. mail charset: 'UTF-8',
  201. subject: "Jenkins build ERROR",
  202. mimeType: 'text/html',
  203. to: "${mailto}",
  204. 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}"
  205. }
  206. aborted {
  207. mail charset: 'UTF-8',
  208. subject: "Jenkins build ERROR",
  209. mimeType: 'text/html',
  210. to: "${client_mail}",
  211. 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}"
  212. }
  213. }
  214. }