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