Jenkinsfile 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. pipeline {
  2. agent {
  3. label "swarm"
  4. }
  5. environment {
  6. DOCKER_REGISTRY='dev-registry.infoclinica.ru:5000'
  7. DOCKER_IMAGE='ovpn'
  8. SERVICE_IMAGE='container_run'
  9. SERVICE_NAME='ovpn'
  10. SWARM_GIT_URL='ssh://git@git.sdsys.ru:8022/iru/stack-deploy.git'
  11. SWARM_GIT_NAME='stack-deploy'
  12. PKI_GIT_URL='ssh://git@git.sdsys.ru:8022/iru/openvpn-pki.git'
  13. PKI_GIT_NAME='openvpn-pki'
  14. GOST_GIT_DIR='openvpn'
  15. JENKINS_MAIL='jenkins@sdsys.ru'
  16. CLUSTER_NAME='dev-iru-swarm.infoclinica.lan'
  17. }
  18. parameters {
  19. string(
  20. name: "repo",
  21. defaultValue: "prod",
  22. description: "Repository to build and/or deploy from."
  23. )
  24. string(
  25. name: "mailto",
  26. defaultValue: "tomishinets.v@sdsys.ru",
  27. description: "Email which has to be notified."
  28. )
  29. }
  30. stages {
  31. stage("Pull PKI repo") {
  32. steps {
  33. withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
  34. sh '''GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  35. git clone ${PKI_GIT_URL}
  36. '''
  37. }
  38. sh '''cp ${WORKSPACE}/openvpn-pki/open/easy-rsa/keys/ca.crt \
  39. ${WORKSPACE}/openvpn-pki/open/easy-rsa/keys/server.crt \
  40. ${WORKSPACE}/openvpn-pki/open/easy-rsa/keys/server.key \
  41. ${WORKSPACE}/openvpn-pki/open/easy-rsa/keys/sds-zub.ru.crt \
  42. ${WORKSPACE}/openvpn-pki/open/easy-rsa/keys/sds-zub.ru.key \
  43. ${WORKSPACE}/openvpn-pki/open/easy-rsa/keys/ta.key \
  44. ${WORKSPACE}/openvpn-pki/open/easy-rsa/keys/stonevpn.crl \
  45. ${WORKSPACE}/openvpn-pki/open/easy-rsa/keys/dh2048.pem \
  46. ${WORKSPACE}/openvpn/keys
  47. ls -al ${WORKSPACE}/openvpn/keys/
  48. '''
  49. }
  50. }
  51. stage("Build") {
  52. steps {
  53. echo "Building ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${BUILD_NUMBER}."
  54. sh "docker build --no-cache -t ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${BUILD_NUMBER} ."
  55. }
  56. }
  57. stage("Staging") {
  58. steps {
  59. echo "Run ${DOCKER_IMAGE} in server mode."
  60. sh '''container_id_server=`docker run -d --rm -e "mode=server" \
  61. --privileged ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${BUILD_NUMBER}`
  62. container_ip_server=`docker inspect ${container_id_server} --format='{{.NetworkSettings.IPAddress}}'`
  63. container_id_client=`docker run -d --rm -e "mode=client" -e "server=${container_ip_server}" --privileged ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${BUILD_NUMBER}`
  64. sleep 15
  65. docker exec ${container_id_client} ping -c 3 -q 10.10.20.1
  66. if [ $? != 0 ]
  67. then
  68. echo "Can not connect to VPN server !!!"
  69. docker stop ${container_id_server} ${container_id_client}
  70. exit 1
  71. else
  72. echo "VPN server is started"
  73. docker stop ${container_id_server} ${container_id_client}
  74. fi
  75. '''
  76. }
  77. }
  78. stage("Publish") {
  79. steps {
  80. echo "Publishing ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${BUILD_NUMBER}."
  81. sh "docker push ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${BUILD_NUMBER}"
  82. }
  83. }
  84. stage("Prod-like") {
  85. steps {
  86. echo "Check Prod-like cluster status"
  87. sh '''ping -c 2 ${CLUSTER_NAME}
  88. if [ $? -eq 0 ]; then
  89. export DOCKER_CERT_PATH=/run/secrets/swarm
  90. export DOCKER_HOST=tcp://${CLUSTER_NAME}:2376 DOCKER_TLS_VERIFY=1
  91. docker node ls --format "{{.Hostname}} {{.TLSStatus}}" | while read host status
  92. do
  93. if [ $status != Ready ]; then echo "Cluster ${CLUSTER_NAME} state is inconsistent"; exit 1
  94. else echo "HOST: $host STATUS: $status"
  95. fi
  96. done
  97. else echo "Host not Found"; exit 1
  98. fi
  99. '''
  100. echo "Run containers in Prod-like"
  101. sh '''export DOCKER_CERT_PATH=/run/secrets/swarm
  102. export DOCKER_HOST=tcp://dev-iru-swarm1.infoclinica.lan:2376 DOCKER_TLS_VERIFY=1
  103. if [ !$(docker service ps -q ${DOCKER_IMAGE}) ];then
  104. docker service create --replicas 2 \
  105. --mount type=bind,source=/var/run/docker.sock,destination=/var/run/docker.sock \
  106. --name ${SERVICE_NAME} ${DOCKER_REGISTRY}/${SERVICE_IMAGE}:1 -p 1194:1194 \
  107. --privileged --security-opt seccomp=unconfined \
  108. --tmpfs /run --tmpfs /run/lock -v /sys/fs/cgroup:/sys/fs/cgroup:ro \
  109. -e "mode=server" ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${BUILD_NUMBER}
  110. else
  111. docker service update \
  112. --args "-p 1194:1194 --privileged --security-opt seccomp=unconfined \
  113. --tmpfs /run --tmpfs /run/lock -v /sys/fs/cgroup:/sys/fs/cgroup:ro \
  114. -e "mode=server" ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${BUILD_NUMBER}" \
  115. ${SERVICE_NAME}
  116. if [ $? != 0 ]; then docker service rollback ${SERVICE_NAME}; fi
  117. fi
  118. '''
  119. }
  120. }
  121. }
  122. post {
  123. always {
  124. deleteDir()
  125. }
  126. }
  127. }