Added images glance upload possibility.

Change-Id: I656bc525c10e23d9a87b35b81340580930e8ac33
diff --git a/Jenkinsfile b/Jenkinsfile
index a26d60e..3803e61 100644
--- a/Jenkinsfile
+++ b/Jenkinsfile
@@ -1,7 +1,7 @@
 import java.util.regex.Pattern
 /**
  *
- * Images build pipeline
+ * OS images build pipeline
  *
  * Expected parameters:
  *    BUILD_OS
@@ -18,18 +18,26 @@
  *    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
  */
 
 // Load shared libs
-def common
-fileLoader.withGit(PIPELINE_LIBS_URL, PIPELINE_LIBS_BRANCH, PIPELINE_LIBS_CREDENTIALS_ID, '') {
-    common = fileLoader.load("common");
-}
+common = new com.mirantis.mk.Common()
 
 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") {
@@ -50,6 +58,11 @@
                 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),
@@ -73,11 +86,13 @@
                             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 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")
@@ -90,6 +105,7 @@
                                 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")
                                 }
@@ -104,9 +120,14 @@
         stage("upload"){
             dir(BUILD_OS + "/images") {
                 def images = findFiles(glob: "*.*")
-                def uploadedImages=[]
-                def cleanedImages=[]
                 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(".")
@@ -114,15 +135,27 @@
                     if(imageType.equals(".md5")){
                         continue;
                     }
+
                     imageBuilds["build${i}"]={
                         if (SKIP_UPLOAD != 'true') {
                             sh "md5sum ${imageName} > ${imageName}.md5"
                             println("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)
+
+                            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")
                             }
@@ -160,6 +193,14 @@
                 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))
+                }
+            }
+        }
     }
 }