| /** |
| * |
| * Build mirror image pipeline |
| * |
| * Expected parameters: |
| * IMAGE_NAME - Name of the result image. |
| * OS_CREDENTIALS_ID - ID of credentials for OpenStack API stored in Jenkins. |
| * OS_PROJECT - Project in OpenStack under the VM will be spawned. |
| * OS_URL - Keystone auth endpoint of the OpenStack. |
| * OS_VERSION - OpenStack version |
| * UPLOAD_URL - URL of an WebDAV used to upload the image after creating. |
| * VM_AVAILABILITY_ZONE - Availability zone in OpenStack in the VM will be spawned. |
| * VM_FLAVOR - Flavor to be used for VM in OpenStack. |
| * VM_FLOATING_IP_POOL - Floating IP pool to be used to assign floating IP to the VM. |
| * VM_IMAGE - Name of the image to be used for VM in OpenStack. |
| * VM_IP - Static IP that is assigned to the VM which belongs to the network used. |
| * VM_NETWORK_ID - ID of the network that VM connects to. |
| * EXTRA_VARIABLES - list of key:value variables required by template.json |
| * |
| */ |
| |
| // Load shared libs |
| def common = new com.mirantis.mk.Common() |
| def openstack = new com.mirantis.mk.Openstack() |
| def git = new com.mirantis.mk.Git() |
| def date = new Date() |
| def dateTime = date.format("ddMMyyyy-HHmmss") |
| def openstackServer = "" |
| def rcFile = "" |
| def openstackEnv = "" |
| def uploadImageStatus = "" |
| def uploadMd5Status = "" |
| ArrayList extra_vars = job_env.get('EXTRA_VARIABLES', '').readLines() |
| def IMAGE_NAME = job_env.get('IMAGE_NAME', "packer-image") + "-" + dateTime |
| |
| timeout(time: 8, unit: 'HOURS') { |
| node("python&&disk-xl") { |
| try { |
| def workspace = common.getWorkspace() |
| openstackEnv = "${workspace}/venv" |
| |
| stage("Prepare env") { |
| checkout scm |
| if (!fileExists("${workspace}/tmp")) { |
| sh "mkdir -p ${workspace}/tmp" |
| } |
| if (!fileExists("${workspace}/images")) { |
| sh "mkdir ${workspace}/images" |
| } |
| if (!fileExists("bin")) { |
| common.infoMsg("Downloading packer") |
| sh "mkdir -p bin" |
| dir("bin") { |
| sh "wget --quiet -O ${PACKER_ZIP} ${PACKER_URL}" |
| sh "echo \"${PACKER_ZIP_MD5} ${PACKER_ZIP}\" >> md5sum" |
| sh "md5sum -c --status md5sum" |
| sh "unzip ${PACKER_ZIP}" |
| } |
| } |
| // clean images dir before building |
| sh(script: "rm -rf ${BUILD_OS}/images/*", returnStatus: true) |
| // clean virtualenv is exists |
| sh(script: "rm -rf ${workspace}/venv", returnStatus: true) |
| |
| openstack.setupOpenstackVirtualenv(openstackEnv, OS_VERSION) |
| git.checkoutGitRepository(PACKER_TEMPLATES_REPO_NAME, PACKER_TEMPLATES_REPO_URL, PACKER_TEMPLATES_BRANCH) |
| } |
| |
| stage("Build Instance") { |
| rcFile = openstack.createOpenstackEnv(OS_URL, OS_CREDENTIALS_ID, OS_PROJECT, "default", "", "default", "2", "") |
| dir("${workspace}/${PACKER_TEMPLATES_REPO_NAME}/${BUILD_OS}/") { |
| withEnv(extra_vars + ["PATH=${env.PATH}:${workspace}/bin", |
| "PACKER_LOG_PATH=${workspace}/packer.log", |
| "PACKER_LOG=1", |
| "TMPDIR=${workspace}/tmp"]) { |
| if (PACKER_DEBUG.toBoolean()) { |
| PACKER_ARGS = "${PACKER_ARGS} -debug" |
| } |
| |
| sh "source extra.vars ; packer build -only=${BUILD_ONLY} ${PACKER_ARGS} -parallel=false template.json" |
| |
| def packerStatus = sh(script: "grep \"Some builds didn't complete successfully and had errors\" ${PACKER_LOG_PATH}", returnStatus: true) |
| // grep returns 0 if find something |
| if (packerStatus != 0) { |
| if (buildTypes.contains("openstack")) { |
| common.infoMsg("Openstack instance complete") |
| } |
| } else { |
| throw new Exception("Packer build failed") |
| } |
| } |
| } |
| } |
| |
| stage("Publish image") { |
| common.infoMsg("Saving image ${IMAGE_NAME}") |
| common.retry(3, 5) { |
| openstack.runOpenstackCommand("openstack image save --file ${IMAGE_NAME}.qcow2 ${IMAGE_NAME}", rcFile, openstackEnv) |
| } |
| sh "md5sum ${IMAGE_NAME}.qcow2 > ${IMAGE_NAME}.qcow2.md5" |
| |
| common.infoMsg("Uploading image ${IMAGE_NAME}") |
| common.retry(3, 5) { |
| uploadImageStatus = sh(script: "curl -f -T ${IMAGE_NAME}.qcow2 ${UPLOAD_URL}", returnStatus: true) |
| if (uploadImageStatus != 0) { |
| throw new Exception("Image upload failed") |
| } |
| } |
| |
| common.retry(3, 5) { |
| uploadMd5Status = sh(script: "curl -f -T ${IMAGE_NAME}.qcow2.md5 ${UPLOAD_URL}", returnStatus: true) |
| if (uploadMd5Status != 0) { |
| throw new Exception("MD5 sum upload failed") |
| } |
| } |
| currentBuild.description = "<a href='http://ci.mcp.mirantis.net:8085/images/${IMAGE_NAME}.qcow2'>${IMAGE_NAME}.qcow2</a>" |
| } |
| |
| } catch (Throwable e) { |
| // If there was an error or exception thrown, the build failed |
| currentBuild.result = "FAILURE" |
| throw e |
| } finally { |
| if (CLEANUP_AFTER) { |
| stage("Cleanup") { |
| if (openstackServer != "") { |
| openstack.runOpenstackCommand("openstack server delete ${IMAGE_NAME}", rcFile, openstackEnv) |
| openstack.runOpenstackCommand("openstack image delete ${IMAGE_NAME}", rcFile, openstackEnv) |
| } |
| dir(workspace) { |
| sh "rm -rf ./*" |
| } |
| } |
| |
| } else { |
| common.infoMsg("Env has not been cleanup!") |
| common.infoMsg("Packer private key:") |
| dir("${workspace}/${PACKER_TEMPLATES_REPO_NAME}/${BUILD_OS}/") { |
| sh "cat os_${BUILD_OS}.pem" |
| } |
| |
| } |
| } |
| |
| } |
| } |