Add 'packer-image-create' job

- add packer-image-create.groovy to create images with packer
- add 'tcp_tests/templates/_packer/' with configs for 'foundation'
  node

Change-Id: I23379bef389adad791df9343930095a57af3fb55
diff --git a/jobs/pipelines/packer-image-create.groovy b/jobs/pipelines/packer-image-create.groovy
new file mode 100644
index 0000000..94133a7
--- /dev/null
+++ b/jobs/pipelines/packer-image-create.groovy
@@ -0,0 +1,114 @@
+/**
+ *
+ * Deploy the product cluster using Jenkins master on CICD cluster
+ *
+ * Expected parameters:
+
+ *   IMAGE_NAME                    Name of the resulting image in the Glance or in artifacts
+
+ *   BUILD_CONFIG_DRIVE_PATH       Relative path in tcp-qa to the directory with meta-data and user-data files
+ *   BUILD_PACKER_CONFIG_PATH      Relative path in tcp-qa to the file with packer config (packer.json)
+ *   BASE_IMAGE_URL                Base image to build a new image, in qcow2. For example, released ubuntu cloud image
+ *   BASE_IMAGE_MD5                Base image MD5 checksum. Image will be re-downloaded if not match with the local image checksum
+
+ *   PACKER_URL                    URL to the zip archive with packer binary, see https://releases.hashicorp.com/packer/
+ *   PACKER_ZIP_MD5                MD5 of the zip archive with packer binary
+
+ *   OS_AUTH_URL                   OpenStack keystone catalog URL
+ *   OS_PROJECT_NAME               OpenStack project (tenant) name
+ *   OS_USER_DOMAIN_NAME           OpenStack user domain name
+ *   OS_CREDENTIALS                OpenStack username and password credentials ID in Jenkins
+ *   UPLOAD_IMAGE_TO_GLANCE        If True: upload image to glance; if False: store as an artifact
+
+ *   TCP_QA_REFS                   Reference to the tcp-qa change on review.gerrithub.io, like refs/changes/46/418546/41
+ */
+
+@Library('tcp-qa')_
+
+def common = new com.mirantis.mk.Common()
+def shared = new com.mirantis.system_qa.SharedPipeline()
+
+timeout(time: 6, unit: 'HOURS') {
+    node () {
+        try {
+
+            stage("Clean the environment and clone tcp-qa") {
+                deleteDir()
+                shared.run_cmd("""\
+                    git clone https://github.com/Mirantis/tcp-qa.git ${WORKSPACE}
+                """)
+                shared.update_working_dir(false)
+                sh "mkdir ./tmp"
+            }
+
+            def packer_zipname = "/tmp/packer.zip"
+            def configdrive_isoname = "./tmp/config-drive.iso"
+
+            stage("Prepare Packer") {
+                // Check that the archive is already downloaded and has a correct checksum. Remove if not match
+                if (fileExists(packer_zipname)) {
+                    sh(script: "bash -cex 'md5sum -c --status <(echo ${PACKER_ZIP_MD5} ${packer_zipname})' || rm -f ${packer_zipname}", returnStdout: true)
+                }
+                // If the file is missing or removed, then download it and check the checksum
+                if (!fileExists(packer_zipname)) {
+                    sh(script: "wget --quiet -O ${packer_zipname} ${PACKER_URL}", returnStdout: true)
+                    // Should fail the job if not match
+                    sh(script: "bash -cex 'md5sum -c --status <(echo ${PACKER_ZIP_MD5} ${packer_zipname})'", returnStdout: true)
+                }
+                sh "unzip ${packer_zipname}"
+            }
+
+            stage("Build the cloudinit ISO") {
+                // Check that genisoimage is installed, or try to install it
+                sh "which genisoimage || sudo apt-get -y install genisoimage"
+                // Generate config-drive ISO
+                sh "mkisofs -o ${configdrive_isoname} -V cidata -r -J --quiet ${BUILD_CONFIG_DRIVE_PATH}"
+            }
+
+            stage("Build the image '${IMAGE_NAME}'") {
+                // Build the image
+                sh (script: """\
+                    set -ex;
+                    export PACKER_LOG=1;
+                    export PACKER_CACHE_DIR='/tmp/packer_cache_${IMAGE_NAME}/';
+                    mkdir -p \${PACKER_CACHE_DIR};
+                    ./packer build -machine-readable -parallel=false -only='qemu' ${BUILD_PACKER_CONFIG_PATH};
+                """, returnStdout: true)
+            }
+
+
+            if (env.UPLOAD_IMAGE_TO_GLANCE) {
+
+                stage("Upload generated config drive ISO into volume on cfg01 node") {
+                    withCredentials([
+                       [$class          : 'UsernamePasswordMultiBinding',
+                       credentialsId   : env.OS_CREDENTIALS,
+                       passwordVariable: 'OS_PASSWORD',
+                       usernameVariable: 'OS_USERNAME']
+                    ]) {
+                        env.OS_IDENTITY_API_VERSION = 3
+
+                        def imagePath = "tmp/${IMAGE_NAME}/${IMAGE_NAME}.qcow2"
+                        shared.run_cmd("""\
+                            openstack --insecure image delete ${IMAGE_NAME} || true
+                            sleep 3
+                            openstack --insecure image create ${IMAGE_NAME} --file ${imagePath} --disk-format qcow2 --container-format bare
+                        """)
+                    }
+                }
+            } else {
+
+                stage("Archive artifacts") {
+                    archiveArtifacts artifacts: "tmp/${IMAGE_NAME}/${IMAGE_NAME}.qcow2"
+                }
+            }
+
+        } catch (e) {
+            common.printMsg("Job is failed", "purple")
+            throw e
+        } finally {
+            // Remove the image after job is finished
+            sh "rm -f ./tmp/${IMAGE_NAME}.qcow2 || true"
+        } // try
+    } // node
+} // timeout
\ No newline at end of file