Fixed NonCPS method location and Ubuntu 16.04 image

Change-Id: Ib3c623d82316544e42b61d4637f0dbe24df5fc5b
diff --git a/Jenkinsfile b/Jenkinsfile
index 6a35dd6..1626c47 100644
--- a/Jenkinsfile
+++ b/Jenkinsfile
@@ -36,194 +36,6 @@
 // Load shared libs
 common = new com.mirantis.mk.Common()
 
-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([String.format("PATH=%s:%s/bin", env.PATH, workspace),
-                    "PACKER_LOG_PATH=${workspace}/packer.log",
-                    "PACKER_LOG=1",
-                    "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")
-                                }
-                            }
-                        }
-                        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("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(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))
-                }
-            }
-        }
-    }
-}
-
 @NonCPS
 def getCleanupImageList(remoteImagesString, imageType, osImage) {
     def remoteImages = remoteImagesString.tokenize("\n")
@@ -240,4 +52,192 @@
         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([String.format("PATH=%s:%s/bin", env.PATH, workspace),
+                        "PACKER_LOG_PATH=${workspace}/packer.log",
+                        "PACKER_LOG=1",
+                        "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")
+                                    }
+                                }
+                            }
+                            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("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(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))
+                    }
+                }
+            }
+        }
+    }
 }