Dennis Dmitriev | 056ecbf | 2019-05-20 15:36:29 +0300 | [diff] [blame] | 1 | /** |
| 2 | * |
| 3 | * Deploy the product cluster using Jenkins master on CICD cluster |
| 4 | * |
| 5 | * Expected parameters: |
| 6 | |
| 7 | * IMAGE_NAME Name of the resulting image in the Glance or in artifacts |
| 8 | |
| 9 | * BUILD_CONFIG_DRIVE_PATH Relative path in tcp-qa to the directory with meta-data and user-data files |
| 10 | * BUILD_PACKER_CONFIG_PATH Relative path in tcp-qa to the file with packer config (packer.json) |
| 11 | * BASE_IMAGE_URL Base image to build a new image, in qcow2. For example, released ubuntu cloud image |
| 12 | * BASE_IMAGE_MD5 Base image MD5 checksum. Image will be re-downloaded if not match with the local image checksum |
| 13 | |
| 14 | * PACKER_URL URL to the zip archive with packer binary, see https://releases.hashicorp.com/packer/ |
| 15 | * PACKER_ZIP_MD5 MD5 of the zip archive with packer binary |
| 16 | |
| 17 | * OS_AUTH_URL OpenStack keystone catalog URL |
| 18 | * OS_PROJECT_NAME OpenStack project (tenant) name |
| 19 | * OS_USER_DOMAIN_NAME OpenStack user domain name |
| 20 | * OS_CREDENTIALS OpenStack username and password credentials ID in Jenkins |
| 21 | * UPLOAD_IMAGE_TO_GLANCE If True: upload image to glance; if False: store as an artifact |
| 22 | |
| 23 | * TCP_QA_REFS Reference to the tcp-qa change on review.gerrithub.io, like refs/changes/46/418546/41 |
| 24 | */ |
| 25 | |
| 26 | @Library('tcp-qa')_ |
| 27 | |
| 28 | def common = new com.mirantis.mk.Common() |
| 29 | def shared = new com.mirantis.system_qa.SharedPipeline() |
| 30 | |
| 31 | timeout(time: 6, unit: 'HOURS') { |
| 32 | node () { |
| 33 | try { |
| 34 | |
| 35 | stage("Clean the environment and clone tcp-qa") { |
| 36 | deleteDir() |
| 37 | shared.run_cmd("""\ |
| 38 | git clone https://github.com/Mirantis/tcp-qa.git ${WORKSPACE} |
| 39 | """) |
| 40 | shared.update_working_dir(false) |
| 41 | sh "mkdir ./tmp" |
| 42 | } |
| 43 | |
| 44 | def packer_zipname = "/tmp/packer.zip" |
| 45 | def configdrive_isoname = "./tmp/config-drive.iso" |
| 46 | |
| 47 | stage("Prepare Packer") { |
| 48 | // Check that the archive is already downloaded and has a correct checksum. Remove if not match |
| 49 | if (fileExists(packer_zipname)) { |
| 50 | sh(script: "bash -cex 'md5sum -c --status <(echo ${PACKER_ZIP_MD5} ${packer_zipname})' || rm -f ${packer_zipname}", returnStdout: true) |
| 51 | } |
| 52 | // If the file is missing or removed, then download it and check the checksum |
| 53 | if (!fileExists(packer_zipname)) { |
| 54 | sh(script: "wget --quiet -O ${packer_zipname} ${PACKER_URL}", returnStdout: true) |
| 55 | // Should fail the job if not match |
| 56 | sh(script: "bash -cex 'md5sum -c --status <(echo ${PACKER_ZIP_MD5} ${packer_zipname})'", returnStdout: true) |
| 57 | } |
| 58 | sh "unzip ${packer_zipname}" |
| 59 | } |
| 60 | |
| 61 | stage("Build the cloudinit ISO") { |
| 62 | // Check that genisoimage is installed, or try to install it |
| 63 | sh "which genisoimage || sudo apt-get -y install genisoimage" |
| 64 | // Generate config-drive ISO |
| 65 | sh "mkisofs -o ${configdrive_isoname} -V cidata -r -J --quiet ${BUILD_CONFIG_DRIVE_PATH}" |
| 66 | } |
| 67 | |
| 68 | stage("Build the image '${IMAGE_NAME}'") { |
| 69 | // Build the image |
| 70 | sh (script: """\ |
| 71 | set -ex; |
| 72 | export PACKER_LOG=1; |
| 73 | export PACKER_CACHE_DIR='/tmp/packer_cache_${IMAGE_NAME}/'; |
| 74 | mkdir -p \${PACKER_CACHE_DIR}; |
| 75 | ./packer build -machine-readable -parallel=false -only='qemu' ${BUILD_PACKER_CONFIG_PATH}; |
| 76 | """, returnStdout: true) |
| 77 | } |
| 78 | |
| 79 | |
| 80 | if (env.UPLOAD_IMAGE_TO_GLANCE) { |
| 81 | |
| 82 | stage("Upload generated config drive ISO into volume on cfg01 node") { |
| 83 | withCredentials([ |
| 84 | [$class : 'UsernamePasswordMultiBinding', |
| 85 | credentialsId : env.OS_CREDENTIALS, |
| 86 | passwordVariable: 'OS_PASSWORD', |
| 87 | usernameVariable: 'OS_USERNAME'] |
| 88 | ]) { |
| 89 | env.OS_IDENTITY_API_VERSION = 3 |
| 90 | |
| 91 | def imagePath = "tmp/${IMAGE_NAME}/${IMAGE_NAME}.qcow2" |
| 92 | shared.run_cmd("""\ |
| 93 | openstack --insecure image delete ${IMAGE_NAME} || true |
| 94 | sleep 3 |
| 95 | openstack --insecure image create ${IMAGE_NAME} --file ${imagePath} --disk-format qcow2 --container-format bare |
| 96 | """) |
| 97 | } |
| 98 | } |
| 99 | } else { |
| 100 | |
| 101 | stage("Archive artifacts") { |
| 102 | archiveArtifacts artifacts: "tmp/${IMAGE_NAME}/${IMAGE_NAME}.qcow2" |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | } catch (e) { |
| 107 | common.printMsg("Job is failed", "purple") |
| 108 | throw e |
| 109 | } finally { |
| 110 | // Remove the image after job is finished |
| 111 | sh "rm -f ./tmp/${IMAGE_NAME}.qcow2 || true" |
| 112 | } // try |
| 113 | } // node |
| 114 | } // timeout |