Fixed Jenkins pipeline

Change-Id: Ida88a43f560f8b6a74f1746bf880604a14ac59c9
diff --git a/Jenkinsfile b/Jenkinsfile
index 3ca638a..1c5e4af 100755
--- a/Jenkinsfile
+++ b/Jenkinsfile
@@ -20,7 +20,6 @@
  *    PIPELINE_LIBS_CREDENTIALS_ID
  */
 
-
 // Load shared libs
 def common
 fileLoader.withGit(PIPELINE_LIBS_URL, PIPELINE_LIBS_BRANCH, PIPELINE_LIBS_CREDENTIALS_ID, '') {
@@ -70,8 +69,12 @@
                             if (imageQemu != null && imageQemu != "") {
                                 def qemuConvertStatus = sh(script: "qemu-img convert -c -O qcow2 ${imageQemu} ${imageQemu}.qcow2", returnStatus:true)
                                 if(qemuConvertStatus == 0){
-                                    sh "mv ${imageQemu}/${imageQemu}.qcow2 .."
-                                    sh "rm -f ${imageQemu}"
+                                    def imageDir = imageQemu.substring(0, imageQemu.lastIndexOf("/") + 1);
+                                    def moveResult = sh(script: "mv ${imageQemu}.qcow2  ${imageDir}..", returnStatus: true)
+                                    if(moveResult == 0){
+                                        sh "rm -rf ${imageDir}"
+                                        sh "rm -f ${imageQemu}"
+                                    }
                                 }else{
                                     throw new Exception("Qemu image convert failed")
                                 }
@@ -115,7 +118,7 @@
                             def uploadMd5Status = sh(script: "curl -f -T ${imageName}.md5 ${UPLOAD_URL}", returnStatus: true)
                             if(uploadImageStatus==0 && uploadMd5Status == 0){
                                 uploadedImages.add(imageName)
-                                sh(String.format("rm -r *%s*", BUILD_OS.replaceAll(/\./,"-")))
+                                sh(String.format("rm -r %s %s.md5",imageName, imageName))
                             }else{
                                 throw new Exception("Image upload failed")
                             }
@@ -128,13 +131,8 @@
                                 if (deleteCount > 0) {
                                     for (int j = 0; j < deleteCount; j++) {
                                         println(String.format("Deleting image %s from aptly", cleanupImages[j]))
-                                        sh "curl -f -X DELETE ${UPLOAD_URL}/" + cleanupImages[j]
-                                        def imageTypeForRegex = Pattern.quote(imageType)
-                                        def imageMd5 = cleanupImages[j].replaceAll(/${imageTypeForRegex}$/, ".md5")
-                                        sh "curl -f -X DELETE ${UPLOAD_URL}/" + imageMd5
-
-                                        sh "curl -f -X DELETE ${UPLOAD_URL}/${cleanupImages[j]} || true"
-                                        sh "curl -f -X DELETE ${UPLOAD_URL}/${imageMd5} || true"
+                                        sh "curl -f -X DELETE ${UPLOAD_URL}" + cleanupImages[j]
+                                        sh "curl -f -X DELETE ${UPLOAD_URL}" + cleanupImages[j] + ".md5"
                                         cleanedImages.add(cleanupImages[j])
                                     }
                                 }
@@ -165,11 +163,11 @@
 def getCleanupImageList(remoteImagesString, imageType, osImage) {
     def remoteImages = remoteImagesString.tokenize("\n")
     def imageTypeForRegex = Pattern.quote(imageType)
-    def osImageForRegex = Pattern.quote(osImage)
-    def remoteImagesSameType = remoteImages.findAll {
-        it = ~/${imageTypeForRegex}$/
+    def osImageForRegex = Pattern.quote(osImage.replaceAll(/\./,"-"))
+    def remoteImagesSameType = remoteImages.findAll { it ->
+        it =~ /${imageTypeForRegex}$/
     }
-    return remoteImagesSameType.toSorted().findAll {
-        it = ~/^${osImageForRegex}-/
+    return remoteImagesSameType.toSorted().findAll { it ->
+        it =~ /^${osImageForRegex}-/
     }
 }
\ No newline at end of file