blob: 1b45d1f33426af5238b4d5c86216fe856287c377 [file] [log] [blame]
import java.util.regex.Pattern
/**
*
* OS images build pipeline
*
* Target build slave node:
* target-build-slave: [ qemu ]
*
* This pipeline require to have slave with such dep's:
* ubuntu-pkgs: [ qemu-kvm, libvirt-bin, cloud-image-utils ]
*
* Expected parameters:
* BUILD_OS
* BUILD_ONLY
* PACKER_DEBUG
* PACKER_URL
* PACKER_ZIP
* PACKER_ZIP_MD5
* PACKER_ARGS
* UPLOAD_URL
* SKIP_UPLOAD
* CLEANUP_OLD
* CLEANUP_KEEP
* PIPELINE_LIBS_URL
* PIPELINE_LIBS_BRANCH
* PIPELINE_LIBS_CREDENTIALS_ID
* GLANCE_UPLOAD
* GLANCE_IMG_TYPES
* GLANCE_URL
* GLANCE_CREDENTIALS_ID
* GLANCE_PROJECT
* GLANCE_ARGS
* OPENSTACK_API_CLIENT
* IMAGE_NAME - Name of the result image.
* EXTRA_VARIABLES - list of key:value variables required by template.json
*/
// Load shared libs
common = new com.mirantis.mk.Common()
def date = new Date()
def dateTime = date.format("ddMMyyyy-HHmmss")
ArrayList extra_vars = EXTRA_VARIABLES.readLines()
IMAGE_NAME = IMAGE_NAME + "-" + dateTime
@NonCPS
def getCleanupImageList(remoteImagesString, imageType, osImage) {
def remoteImages = remoteImagesString.tokenize("\n")
def imageTypeForRegex = Pattern.quote(imageType)
def osImageForRegex = Pattern.quote(osImage.replaceAll(/\./,"-"))
def remoteImagesSameType = remoteImages.findAll { it ->
it =~ /${imageTypeForRegex}$/
}
def imagesToClean = remoteImagesSameType.toSorted().findAll { it ->
it =~ /^${osImageForRegex}-/
}
// dont cleanup non timestamp images
return imagesToClean.findAll { it ->
it =~ /${osImageForRegex}-x(64|32)-\d+${imageTypeForRegex}/
}
}
timeout(time: 12, unit: 'HOURS') {
node('qemu') {
// Define global variables
def workspace = common.getWorkspace()
def buildTypes = BUILD_ONLY.tokenize(" ")
def createdImages=[]
def uploadedImages=[]
def cleanedImages=[]
checkout scm
try {
stage("prepare") {
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 bin"
dir("bin") {
sh "wget -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: String.format("rm -rf %s/images/*", BUILD_OS), returnStatus: true)
// clean virtualenv is exists
sh(script: String.format("rm -rf %s/venv", workspace), returnStatus: true)
stage("build") {
dir(BUILD_OS) {
withEnv(extra_vars + [String.format("PATH=%s:%s/bin", env.PATH, workspace),
"PACKER_LOG_PATH=${workspace}/packer.log",
"PACKER_LOG=1",
"IMAGE_NAME=${IMAGE_NAME}",
"TMPDIR=${workspace}/tmp"
]) {
if (PACKER_DEBUG == 'true') {
PACKER_ARGS = "${PACKER_ARGS} -debug"
}
if (fileExists("config-drive/user-data.yaml")) {
common.infoMsg("Creating cloud-config drive")
if (fileExists("config-drive/cloudata.iso")) {
sh "rm -v config-drive/cloudata.iso"
}
sh "cloud-localds config-drive/cloudata.iso config-drive/user-data.yaml"
}
sh "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("qemu")) {
def imageQemu = sh(script: "find images/ | grep -- '-qemu-' | tail -1", returnStdout: true).trim()
if (imageQemu != null && imageQemu != "") {
def qemuConvertStatus = sh(script: "qemu-img convert -c -O qcow2 ${imageQemu} ${imageQemu}.qcow2", returnStatus:true)
if(qemuConvertStatus == 0){
def imageDir = imageQemu.substring(0, imageQemu.lastIndexOf("/") + 1)
def imageQemuName = imageQemu.substring(imageQemu.lastIndexOf("/") + 1)
def moveResult = sh(script: "mv ${imageQemu}.qcow2 ${imageDir}..", returnStatus: true)
if(moveResult == 0){
sh "rm -rf ${imageDir}"
sh "rm -f ${imageQemu}"
createdImages.add(imageQemuName+".qcow2")
}
}else{
throw new Exception("Qemu image convert failed")
}
} else {
throw new Exception("No Qemu images with '-qemu-' in its path")
}
}
if (buildTypes.contains("docker")) {
def imageDocker = sh(script: "find images/ | grep -- '-docker-' | grep '.tar\$' | tail -1", returnStdout: true).trim()
if (imageDocker != null && imageDocker != "") {
def pbZip2Status = sh(script: "pbzip2 ${imageDocker}", returnStatus: true)
if(pbZip2Status == 0){
sh "rm -f ${imageDocker}"
createdImages.add(imageDocker+".bz2")
}else{
throw new Exception("pbzip2 image convert failed")
}
} else {
throw new Exception("No Docker images with '-docker-' in its path")
}
}
} else {
throw new Exception("Packer build failed")
}
}
}
}
stage("upload"){
dir(BUILD_OS + "/images") {
def images = findFiles(glob: "*.*")
def imageBuilds = [:]
def openstack = new com.mirantis.mk.Openstack()
def openstackEnv = String.format("%s/venv", workspace);
def openstackVersion = OPENSTACK_API_CLIENT ? OPENSTACK_API_CLIENT : 'liberty'
def rcFile = openstack.createOpenstackEnv(workspace, GLANCE_URL, GLANCE_CREDENTIALS_ID, GLANCE_PROJECT)
def glanceImgTypes = GLANCE_IMG_TYPES.tokenize(" ")
openstack.setupOpenstackVirtualenv(openstackEnv, openstackVersion)
openstack.runOpenstackCommand("pip install python-glanceclient==1.0.0", rcFile, openstackEnv)
for (int i = 0; i < images.size(); i++) {
def imageName = images[i].name
def imageNameList = imageName.tokenize(".")
def imageType = "." + imageNameList[imageNameList.size() - 1]
if(imageType.equals(".md5")){
continue;
}
imageBuilds["build${i}"]={
if (SKIP_UPLOAD != 'true') {
sh "md5sum ${imageName} > ${imageName}.md5"
common.infoMsg("Uploading image " + imageName)
def uploadImageStatus = sh(script: "curl -f -T ${imageName} ${UPLOAD_URL}", returnStatus: true)
def uploadMd5Status = sh(script: "curl -f -T ${imageName}.md5 ${UPLOAD_URL}", returnStatus: true)
// upload latest
def latestImageName = imageName.substring(0, imageName.lastIndexOf("-")) + "-latest" + imageType
common.infoMsg("Uploading image ${imageName} as latest")
def uploadLatestStatus = sh(script: "curl -f -T ${imageName} ${UPLOAD_URL}${latestImageName}", returnStatus: true)
def uploadLatestMd5Status = sh(script: "curl -f -T ${imageName}.md5 ${UPLOAD_URL}${latestImageName}.md5", returnStatus: true)
if(uploadLatestStatus != 0 || uploadLatestMd5Status != 0){
common.errorMsg("Latest image upload failed")
}
if (GLANCE_UPLOAD == 'true' && glanceImgTypes.contains(imageType.substring(1))) {
def glanceRunArgs = String.format("%s --disk-format %s --container-format bare", GLANCE_ARGS, imageType.substring(1))
if (GLANCE_PUBLIC == 'true') {
glanceRunArgs += " --visibility public"
}
def imageShortName = imageNameList.get(0)
openstack.runOpenstackCommand(String.format("glance image-create --name '%s' %s --file %s", imageShortName, glanceRunArgs, imageName), rcFile, openstackEnv)
}
if(uploadImageStatus==0 && uploadMd5Status == 0){
uploadedImages.add(imageName)
sh(String.format("rm -r %s %s.md5",imageName, imageName))
createdImages.remove(imageName)
}else{
throw new Exception("Image upload failed")
}
}
if (CLEANUP_OLD == 'true') {
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)
if (remoteImages != "") {
def cleanupImages = getCleanupImageList(remoteImages, imageType, BUILD_OS)
def deleteCount = cleanupImages.size() - Integer.parseInt(CLEANUP_KEEP)
if (deleteCount > 0) {
for (int j = 0; j < deleteCount; j++) {
common.infoMsg(String.format("Deleting image %s from aptly", cleanupImages[j]))
sh "curl -f -X DELETE ${UPLOAD_URL}" + cleanupImages[j]
sh "curl -f -X DELETE ${UPLOAD_URL}" + cleanupImages[j] + ".md5"
cleanedImages.add(cleanupImages[j])
}
}
}
}
}
}
parallel imageBuilds
common.infoMsg(String.format("Uploaded %s images with names %s", uploadedImages.size(), uploadedImages.toString()))
common.infoMsg(String.format("Cleaned %s images with names %s", cleanedImages.size(), cleanedImages.toString()))
}
}
} catch (Throwable e) {
// If there was an error or exception thrown, the build failed
currentBuild.result = "FAILURE"
throw e
} finally {
common.sendNotification(currentBuild.result, "", ["slack"])
if (buildTypes.contains("docker")) {
withEnv(["PACKER_LOG_PATH=${workspace}/packer.log"]) {
sh "docker rmi --force \$(grep \"docker: Image ID:\" ${PACKER_LOG_PATH} | cut -d : -f 6 | head -1 | sed s,\\ ,,g) || true"
}
}
// clean created images if error occured
if(!createdImages.isEmpty()){
dir(BUILD_OS + "/images"){
for(int i=0;i<createdImages.size();i++){
sh String.format("rm -f %s",createdImages.get(i))
}
}
}
}
}
}