blob: 6769e0e57c4a9b85a95aa219afb4ef427defe68b [file] [log] [blame]
Jakub Josef6ee6f992017-01-27 16:16:04 +01001import java.util.regex.Pattern
2/**
3 *
Jakub Joseffc2f2412017-02-27 15:14:07 +01004 * OS images build pipeline
Jakub Josef6ee6f992017-01-27 16:16:04 +01005 *
azvyagintsev01785672018-02-27 17:33:14 +02006 * 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 Josef6ee6f992017-01-27 16:16:04 +010012 * 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 Joseffc2f2412017-02-27 15:14:07 +010027 * GLANCE_UPLOAD
28 * GLANCE_IMG_TYPES
29 * GLANCE_URL
30 * GLANCE_CREDENTIALS_ID
31 * GLANCE_PROJECT
32 * GLANCE_ARGS
33 * OPENSTACK_API_CLIENT
Jiri Broulik4ea221c2018-04-10 13:48:06 +020034 * IMAGE_NAME - Name of the result image.
35 * EXTRA_VARIABLES - list of key:value variables required by template.json
Jakub Josef6ee6f992017-01-27 16:16:04 +010036 */
37
Jakub Josef6ee6f992017-01-27 16:16:04 +010038// Load shared libs
Jakub Joseffc2f2412017-02-27 15:14:07 +010039common = new com.mirantis.mk.Common()
Jiri Broulik2c75a202018-04-10 15:05:38 +020040def date = new Date()
41def dateTime = date.format("ddMMyyyy-HHmmss")
Jakub Josef6ee6f992017-01-27 16:16:04 +010042
Jiri Broulik4ea221c2018-04-10 13:48:06 +020043ArrayList extra_vars = EXTRA_VARIABLES.readLines()
44IMAGE_NAME = IMAGE_NAME + "-" + dateTime
45
Jakub Josef6ee6f992017-01-27 16:16:04 +010046@NonCPS
47def getCleanupImageList(remoteImagesString, imageType, osImage) {
48 def remoteImages = remoteImagesString.tokenize("\n")
49 def imageTypeForRegex = Pattern.quote(imageType)
Jakub Josefc6bcfd72017-02-14 18:14:28 +010050 def osImageForRegex = Pattern.quote(osImage.replaceAll(/\./,"-"))
51 def remoteImagesSameType = remoteImages.findAll { it ->
52 it =~ /${imageTypeForRegex}$/
Jakub Josef6ee6f992017-01-27 16:16:04 +010053 }
Jakub Josef338a1cb2018-01-10 18:50:51 +010054 def imagesToClean = remoteImagesSameType.toSorted().findAll { it ->
Jakub Josefc6bcfd72017-02-14 18:14:28 +010055 it =~ /^${osImageForRegex}-/
Jakub Josef6ee6f992017-01-27 16:16:04 +010056 }
Jakub Josef338a1cb2018-01-10 18:50:51 +010057 // dont cleanup non timestamp images
Richard Felkl014083d2018-03-12 14:58:36 +010058 return imagesToClean.findAll { it ->
Jakub Josef338a1cb2018-01-10 18:50:51 +010059 it =~ /${osImageForRegex}-x(64|32)-\d+${imageTypeForRegex}/
60 }
Filip Pytloun35640b62017-02-23 09:45:34 +010061}
Richard Felklc3053fc2018-03-06 11:15:50 +010062
63timeout(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 Broulik4ea221c2018-04-10 13:48:06 +020099 withEnv(extra_vars + [String.format("PATH=%s:%s/bin", env.PATH, workspace),
Richard Felklc3053fc2018-03-06 11:15:50 +0100100 "PACKER_LOG_PATH=${workspace}/packer.log",
101 "PACKER_LOG=1",
Jiri Broulik4ea221c2018-04-10 13:48:06 +0200102 "IMAGE_NAME=${IMAGE_NAME}",
Richard Felklc3053fc2018-03-06 11:15:50 +0100103 "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 Felkl26d43ad2018-03-06 11:28:21 +0100165 def rcFile = openstack.createOpenstackEnv(workspace, GLANCE_URL, GLANCE_CREDENTIALS_ID, GLANCE_PROJECT)
Richard Felklc3053fc2018-03-06 11:15:50 +0100166 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 }
azvyagintsev6d453852018-02-26 16:56:37 +0200251}