Jakub Josef | 6ee6f99 | 2017-01-27 16:16:04 +0100 | [diff] [blame] | 1 | import java.util.regex.Pattern |
| 2 | /** |
| 3 | * |
Jakub Josef | fc2f241 | 2017-02-27 15:14:07 +0100 | [diff] [blame] | 4 | * OS images build pipeline |
Jakub Josef | 6ee6f99 | 2017-01-27 16:16:04 +0100 | [diff] [blame] | 5 | * |
azvyagintsev | 0178567 | 2018-02-27 17:33:14 +0200 | [diff] [blame] | 6 | * Target build slave node: |
| 7 | * target-build-slave: [ qemu ] |
| 8 | * |
| 9 | * This pipeline require to have slave with such dep's: |
| 10 | * ubuntu-pkgs: [ qemu-kvm, libvirt-bin, cloud-image-utils ] |
| 11 | * |
Jakub Josef | 6ee6f99 | 2017-01-27 16:16:04 +0100 | [diff] [blame] | 12 | * Expected parameters: |
| 13 | * BUILD_OS |
| 14 | * BUILD_ONLY |
| 15 | * PACKER_DEBUG |
| 16 | * PACKER_URL |
| 17 | * PACKER_ZIP |
| 18 | * PACKER_ZIP_MD5 |
| 19 | * PACKER_ARGS |
| 20 | * UPLOAD_URL |
| 21 | * SKIP_UPLOAD |
| 22 | * CLEANUP_OLD |
| 23 | * CLEANUP_KEEP |
| 24 | * PIPELINE_LIBS_URL |
| 25 | * PIPELINE_LIBS_BRANCH |
| 26 | * PIPELINE_LIBS_CREDENTIALS_ID |
Jakub Josef | fc2f241 | 2017-02-27 15:14:07 +0100 | [diff] [blame] | 27 | * GLANCE_UPLOAD |
| 28 | * GLANCE_IMG_TYPES |
| 29 | * GLANCE_URL |
| 30 | * GLANCE_CREDENTIALS_ID |
| 31 | * GLANCE_PROJECT |
| 32 | * GLANCE_ARGS |
| 33 | * OPENSTACK_API_CLIENT |
Jiri Broulik | 4ea221c | 2018-04-10 13:48:06 +0200 | [diff] [blame] | 34 | * IMAGE_NAME - Name of the result image. |
| 35 | * EXTRA_VARIABLES - list of key:value variables required by template.json |
Jakub Josef | 6ee6f99 | 2017-01-27 16:16:04 +0100 | [diff] [blame] | 36 | */ |
| 37 | |
Jakub Josef | 6ee6f99 | 2017-01-27 16:16:04 +0100 | [diff] [blame] | 38 | // Load shared libs |
Jakub Josef | fc2f241 | 2017-02-27 15:14:07 +0100 | [diff] [blame] | 39 | common = new com.mirantis.mk.Common() |
Jiri Broulik | 2c75a20 | 2018-04-10 15:05:38 +0200 | [diff] [blame^] | 40 | def date = new Date() |
| 41 | def dateTime = date.format("ddMMyyyy-HHmmss") |
Jakub Josef | 6ee6f99 | 2017-01-27 16:16:04 +0100 | [diff] [blame] | 42 | |
Jiri Broulik | 4ea221c | 2018-04-10 13:48:06 +0200 | [diff] [blame] | 43 | ArrayList extra_vars = EXTRA_VARIABLES.readLines() |
| 44 | IMAGE_NAME = IMAGE_NAME + "-" + dateTime |
| 45 | |
Jakub Josef | 6ee6f99 | 2017-01-27 16:16:04 +0100 | [diff] [blame] | 46 | @NonCPS |
| 47 | def getCleanupImageList(remoteImagesString, imageType, osImage) { |
| 48 | def remoteImages = remoteImagesString.tokenize("\n") |
| 49 | def imageTypeForRegex = Pattern.quote(imageType) |
Jakub Josef | c6bcfd7 | 2017-02-14 18:14:28 +0100 | [diff] [blame] | 50 | def osImageForRegex = Pattern.quote(osImage.replaceAll(/\./,"-")) |
| 51 | def remoteImagesSameType = remoteImages.findAll { it -> |
| 52 | it =~ /${imageTypeForRegex}$/ |
Jakub Josef | 6ee6f99 | 2017-01-27 16:16:04 +0100 | [diff] [blame] | 53 | } |
Jakub Josef | 338a1cb | 2018-01-10 18:50:51 +0100 | [diff] [blame] | 54 | def imagesToClean = remoteImagesSameType.toSorted().findAll { it -> |
Jakub Josef | c6bcfd7 | 2017-02-14 18:14:28 +0100 | [diff] [blame] | 55 | it =~ /^${osImageForRegex}-/ |
Jakub Josef | 6ee6f99 | 2017-01-27 16:16:04 +0100 | [diff] [blame] | 56 | } |
Jakub Josef | 338a1cb | 2018-01-10 18:50:51 +0100 | [diff] [blame] | 57 | // dont cleanup non timestamp images |
Richard Felkl | 014083d | 2018-03-12 14:58:36 +0100 | [diff] [blame] | 58 | return imagesToClean.findAll { it -> |
Jakub Josef | 338a1cb | 2018-01-10 18:50:51 +0100 | [diff] [blame] | 59 | it =~ /${osImageForRegex}-x(64|32)-\d+${imageTypeForRegex}/ |
| 60 | } |
Filip Pytloun | 35640b6 | 2017-02-23 09:45:34 +0100 | [diff] [blame] | 61 | } |
Richard Felkl | c3053fc | 2018-03-06 11:15:50 +0100 | [diff] [blame] | 62 | |
| 63 | timeout(time: 12, unit: 'HOURS') { |
| 64 | node('qemu') { |
| 65 | // Define global variables |
| 66 | def workspace = common.getWorkspace() |
| 67 | def buildTypes = BUILD_ONLY.tokenize(" ") |
| 68 | def createdImages=[] |
| 69 | def uploadedImages=[] |
| 70 | def cleanedImages=[] |
| 71 | |
| 72 | checkout scm |
| 73 | try { |
| 74 | stage("prepare") { |
| 75 | if (!fileExists("${workspace}/tmp")) { |
| 76 | sh "mkdir -p ${workspace}/tmp" |
| 77 | } |
| 78 | if (!fileExists("${workspace}/images")) { |
| 79 | sh "mkdir ${workspace}/images" |
| 80 | } |
| 81 | } |
| 82 | if (!fileExists("bin")) { |
| 83 | common.infoMsg("Downloading packer") |
| 84 | sh "mkdir bin" |
| 85 | dir("bin") { |
| 86 | sh "wget -O ${PACKER_ZIP} ${PACKER_URL}" |
| 87 | sh "echo \"${PACKER_ZIP_MD5} ${PACKER_ZIP}\" >> md5sum" |
| 88 | sh "md5sum -c --status md5sum" |
| 89 | sh "unzip ${PACKER_ZIP}" |
| 90 | } |
| 91 | } |
| 92 | // clean images dir before building |
| 93 | sh(script: String.format("rm -rf %s/images/*", BUILD_OS), returnStatus: true) |
| 94 | // clean virtualenv is exists |
| 95 | sh(script: String.format("rm -rf %s/venv", workspace), returnStatus: true) |
| 96 | |
| 97 | stage("build") { |
| 98 | dir(BUILD_OS) { |
Jiri Broulik | 4ea221c | 2018-04-10 13:48:06 +0200 | [diff] [blame] | 99 | withEnv(extra_vars + [String.format("PATH=%s:%s/bin", env.PATH, workspace), |
Richard Felkl | c3053fc | 2018-03-06 11:15:50 +0100 | [diff] [blame] | 100 | "PACKER_LOG_PATH=${workspace}/packer.log", |
| 101 | "PACKER_LOG=1", |
Jiri Broulik | 4ea221c | 2018-04-10 13:48:06 +0200 | [diff] [blame] | 102 | "IMAGE_NAME=${IMAGE_NAME}", |
Richard Felkl | c3053fc | 2018-03-06 11:15:50 +0100 | [diff] [blame] | 103 | "TMPDIR=${workspace}/tmp" |
| 104 | ]) { |
| 105 | if (PACKER_DEBUG == 'true') { |
| 106 | PACKER_ARGS = "${PACKER_ARGS} -debug" |
| 107 | } |
| 108 | |
| 109 | if (fileExists("config-drive/user-data.yaml")) { |
| 110 | common.infoMsg("Creating cloud-config drive") |
| 111 | if (fileExists("config-drive/cloudata.iso")) { |
| 112 | sh "rm -v config-drive/cloudata.iso" |
| 113 | } |
| 114 | sh "cloud-localds config-drive/cloudata.iso config-drive/user-data.yaml" |
| 115 | } |
| 116 | sh "packer build -only=${BUILD_ONLY} ${PACKER_ARGS} -parallel=false template.json" |
| 117 | |
| 118 | def packerStatus = sh(script: "grep \"Some builds didn't complete successfully and had errors\" ${PACKER_LOG_PATH}", returnStatus: true) |
| 119 | // grep returns 0 if find something |
| 120 | if (packerStatus != 0) { |
| 121 | if (buildTypes.contains("qemu")) { |
| 122 | def imageQemu = sh(script: "find images/ | grep -- '-qemu-' | tail -1", returnStdout: true).trim() |
| 123 | if (imageQemu != null && imageQemu != "") { |
| 124 | def qemuConvertStatus = sh(script: "qemu-img convert -c -O qcow2 ${imageQemu} ${imageQemu}.qcow2", returnStatus:true) |
| 125 | if(qemuConvertStatus == 0){ |
| 126 | def imageDir = imageQemu.substring(0, imageQemu.lastIndexOf("/") + 1) |
| 127 | def imageQemuName = imageQemu.substring(imageQemu.lastIndexOf("/") + 1) |
| 128 | def moveResult = sh(script: "mv ${imageQemu}.qcow2 ${imageDir}..", returnStatus: true) |
| 129 | if(moveResult == 0){ |
| 130 | sh "rm -rf ${imageDir}" |
| 131 | sh "rm -f ${imageQemu}" |
| 132 | createdImages.add(imageQemuName+".qcow2") |
| 133 | } |
| 134 | }else{ |
| 135 | throw new Exception("Qemu image convert failed") |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | if (buildTypes.contains("docker")) { |
| 140 | def imageDocker = sh(script: "find images/ | grep -- '-docker-' | grep '.tar\$' | tail -1", returnStdout: true).trim() |
| 141 | if (imageDocker != null && imageDocker != "") { |
| 142 | def pbZip2Status = sh(script: "pbzip2 ${imageDocker}", returnStatus: true) |
| 143 | if(pbZip2Status == 0){ |
| 144 | sh "rm -f ${imageDocker}" |
| 145 | createdImages.add(imageDocker+".bz2") |
| 146 | }else{ |
| 147 | throw new Exception("pbzip2 image convert failed") |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | } else { |
| 153 | throw new Exception("Packer build failed") |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | stage("upload"){ |
| 159 | dir(BUILD_OS + "/images") { |
| 160 | def images = findFiles(glob: "*.*") |
| 161 | def imageBuilds = [:] |
| 162 | def openstack = new com.mirantis.mk.Openstack() |
| 163 | def openstackEnv = String.format("%s/venv", workspace); |
| 164 | def openstackVersion = OPENSTACK_API_CLIENT ? OPENSTACK_API_CLIENT : 'liberty' |
Richard Felkl | 26d43ad | 2018-03-06 11:28:21 +0100 | [diff] [blame] | 165 | def rcFile = openstack.createOpenstackEnv(workspace, GLANCE_URL, GLANCE_CREDENTIALS_ID, GLANCE_PROJECT) |
Richard Felkl | c3053fc | 2018-03-06 11:15:50 +0100 | [diff] [blame] | 166 | def glanceImgTypes = GLANCE_IMG_TYPES.tokenize(" ") |
| 167 | openstack.setupOpenstackVirtualenv(openstackEnv, openstackVersion) |
| 168 | openstack.runOpenstackCommand("pip install python-glanceclient==1.0.0", rcFile, openstackEnv) |
| 169 | for (int i = 0; i < images.size(); i++) { |
| 170 | def imageName = images[i].name |
| 171 | def imageNameList = imageName.tokenize(".") |
| 172 | def imageType = "." + imageNameList[imageNameList.size() - 1] |
| 173 | if(imageType.equals(".md5")){ |
| 174 | continue; |
| 175 | } |
| 176 | |
| 177 | imageBuilds["build${i}"]={ |
| 178 | if (SKIP_UPLOAD != 'true') { |
| 179 | sh "md5sum ${imageName} > ${imageName}.md5" |
| 180 | common.infoMsg("Uploading image " + imageName) |
| 181 | def uploadImageStatus = sh(script: "curl -f -T ${imageName} ${UPLOAD_URL}", returnStatus: true) |
| 182 | def uploadMd5Status = sh(script: "curl -f -T ${imageName}.md5 ${UPLOAD_URL}", returnStatus: true) |
| 183 | // upload latest |
| 184 | def latestImageName = imageName.substring(0, imageName.lastIndexOf("-")) + "-latest" + imageType |
| 185 | common.infoMsg("Uploading image ${imageName} as latest") |
| 186 | def uploadLatestStatus = sh(script: "curl -f -T ${imageName} ${UPLOAD_URL}${latestImageName}", returnStatus: true) |
| 187 | def uploadLatestMd5Status = sh(script: "curl -f -T ${imageName}.md5 ${UPLOAD_URL}${latestImageName}.md5", returnStatus: true) |
| 188 | if(uploadLatestStatus != 0 || uploadLatestMd5Status != 0){ |
| 189 | common.errorMsg("Latest image upload failed") |
| 190 | } |
| 191 | if (GLANCE_UPLOAD == 'true' && glanceImgTypes.contains(imageType.substring(1))) { |
| 192 | def glanceRunArgs = String.format("%s --disk-format %s --container-format bare", GLANCE_ARGS, imageType.substring(1)) |
| 193 | if (GLANCE_PUBLIC == 'true') { |
| 194 | glanceRunArgs += " --visibility public" |
| 195 | } |
| 196 | |
| 197 | def imageShortName = imageNameList.get(0) |
| 198 | openstack.runOpenstackCommand(String.format("glance image-create --name '%s' %s --file %s", imageShortName, glanceRunArgs, imageName), rcFile, openstackEnv) |
| 199 | } |
| 200 | if(uploadImageStatus==0 && uploadMd5Status == 0){ |
| 201 | uploadedImages.add(imageName) |
| 202 | sh(String.format("rm -r %s %s.md5",imageName, imageName)) |
| 203 | createdImages.remove(imageName) |
| 204 | }else{ |
| 205 | throw new Exception("Image upload failed") |
| 206 | } |
| 207 | } |
| 208 | if (CLEANUP_OLD == 'true') { |
| 209 | def remoteImages = sh(script: "curl -f -sss ${UPLOAD_URL} | grep -Eo '>.*\\.(qcow2|box|tar\\.bz2)</a>' | sed -e 's,>,,g' -e 's,</a,,g'", returnStdout: true) |
| 210 | if (remoteImages != "") { |
| 211 | def cleanupImages = getCleanupImageList(remoteImages, imageType, BUILD_OS) |
| 212 | def deleteCount = cleanupImages.size() - Integer.parseInt(CLEANUP_KEEP) |
| 213 | if (deleteCount > 0) { |
| 214 | for (int j = 0; j < deleteCount; j++) { |
| 215 | common.infoMsg(String.format("Deleting image %s from aptly", cleanupImages[j])) |
| 216 | sh "curl -f -X DELETE ${UPLOAD_URL}" + cleanupImages[j] |
| 217 | sh "curl -f -X DELETE ${UPLOAD_URL}" + cleanupImages[j] + ".md5" |
| 218 | cleanedImages.add(cleanupImages[j]) |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | parallel imageBuilds |
| 226 | common.infoMsg(String.format("Uploaded %s images with names %s", uploadedImages.size(), uploadedImages.toString())) |
| 227 | common.infoMsg(String.format("Cleaned %s images with names %s", cleanedImages.size(), cleanedImages.toString())) |
| 228 | } |
| 229 | } |
| 230 | } catch (Throwable e) { |
| 231 | // If there was an error or exception thrown, the build failed |
| 232 | currentBuild.result = "FAILURE" |
| 233 | throw e |
| 234 | } finally { |
| 235 | common.sendNotification(currentBuild.result, "", ["slack"]) |
| 236 | if (buildTypes.contains("docker")) { |
| 237 | withEnv(["PACKER_LOG_PATH=${workspace}/packer.log"]) { |
| 238 | sh "docker rmi --force \$(grep \"docker: Image ID:\" ${PACKER_LOG_PATH} | cut -d : -f 6 | head -1 | sed s,\\ ,,g) || true" |
| 239 | } |
| 240 | } |
| 241 | // clean created images if error occured |
| 242 | if(!createdImages.isEmpty()){ |
| 243 | dir(BUILD_OS + "/images"){ |
| 244 | for(int i=0;i<createdImages.size();i++){ |
| 245 | sh String.format("rm -f %s",createdImages.get(i)) |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | } |
azvyagintsev | 6d45385 | 2018-02-26 16:56:37 +0200 | [diff] [blame] | 251 | } |