/**
 * Generate cookiecutter cluster by individual products
 *
 * Expected parameters:
 *   COOKIECUTTER_TEMPLATE_CREDENTIALS  Credentials to the Cookiecutter template repo.
 *   COOKIECUTTER_TEMPLATE_URL          Cookiecutter template repo address.
 *   COOKIECUTTER_TEMPLATE_BRANCH       Branch for the template.
 *   COOKIECUTTER_TEMPLATE_CONTEXT      Context parameters for the template generation.
 *   COOKIECUTTER_INSTALL_CICD          Whether to install CI/CD stack.
 *   COOKIECUTTER_INSTALL_CONTRAIL      Whether to install OpenContrail SDN.
 *   COOKIECUTTER_INSTALL_KUBERNETES    Whether to install Kubernetes.
 *   COOKIECUTTER_INSTALL_OPENSTACK     Whether to install OpenStack cloud.
 *   COOKIECUTTER_INSTALL_STACKLIGHT    Whether to install StackLight monitoring.
 *   RECLASS_MODEL_URL                  Reclass model repo address
 *   RECLASS_MODEL_CREDENTIALS          Credentials to the Reclass model repo.
 *   RECLASS_MODEL_BRANCH               Branch for the template to push to model.
 *   COMMIT_CHANGES                     Commit model to repo
 *
**/

common = new com.mirantis.mk.Common()
git = new com.mirantis.mk.Git()
python = new com.mirantis.mk.Python()

timestamps {
    node() {
        def templateEnv = "${env.WORKSPACE}/template"
        def modelEnv = "${env.WORKSPACE}/model"

        try {
            def templateContext = readYaml text: COOKIECUTTER_TEMPLATE_CONTEXT
            def templateDir = "${templateEnv}/template/dir"
            def templateOutputDir = "${env.WORKSPACE}/template"
            def cutterEnv = "${env.WORKSPACE}/cutter"
            def jinjaEnv = "${env.WORKSPACE}/jinja"
            def clusterName = templateContext.default_context.cluster_name
            def clusterDomain = templateContext.default_context.cluster_domain
            def targetBranch = "feature/${clusterName}"
            def outputDestination = "${modelEnv}/classes/cluster/${clusterName}"

            currentBuild.description = clusterName
            print("Using context:\n" + COOKIECUTTER_TEMPLATE_CONTEXT)

            stage ('Download Cookiecutter template') {
                git.checkoutGitRepository(templateEnv, COOKIECUTTER_TEMPLATE_URL, COOKIECUTTER_TEMPLATE_BRANCH, COOKIECUTTER_TEMPLATE_CREDENTIALS)
            }

            stage ('Download full Reclass model') {
                if (RECLASS_MODEL_URL != '') {
                    git.checkoutGitRepository(modelEnv, RECLASS_MODEL_URL, RECLASS_MODEL_BRANCH, RECLASS_MODEL_CREDENTIALS)
                }
            }

            stage('Generate base infrastructure') {
                templateDir = "${templateEnv}/cluster_product/infra"
                templateOutputDir = "${env.WORKSPACE}/template/output/infra"
                sh "mkdir -p ${templateOutputDir}"
                sh "mkdir -p ${outputDestination}"
                python.setupCookiecutterVirtualenv(cutterEnv)
                python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv)
                sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
            }

            stage('Generate product CI/CD') {
                if (COOKIECUTTER_INSTALL_CICD.toBoolean()) {
                    templateDir = "${templateEnv}/cluster_product/cicd"
                    templateOutputDir = "${env.WORKSPACE}/template/output/cicd"
                    sh "mkdir -p ${templateOutputDir}"
                    python.setupCookiecutterVirtualenv(cutterEnv)
                    python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv)
                    sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
                }
            }

            stage('Generate product OpenContrail') {
                if (COOKIECUTTER_INSTALL_CONTRAIL.toBoolean()) {
                    templateDir = "${templateEnv}/cluster_product/opencontrail"
                    templateOutputDir = "${env.WORKSPACE}/template/output/opencontrail"
                    sh "mkdir -p ${templateOutputDir}"
                    python.setupCookiecutterVirtualenv(cutterEnv)
                    python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv)
                    sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
                }
            }

            stage('Generate product Kubernetes') {
                if (COOKIECUTTER_INSTALL_KUBERNETES.toBoolean()) {
                    templateDir = "${templateEnv}/cluster_product/kubernetes"
                    templateOutputDir = "${env.WORKSPACE}/template/output/kubernetes"
                    sh "mkdir -p ${templateOutputDir}"
                    python.setupCookiecutterVirtualenv(cutterEnv)
                    python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv)
                    sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
                }
            }

            stage('Generate product OpenStack') {
                if (COOKIECUTTER_INSTALL_OPENSTACK.toBoolean()) {
                    templateDir = "${templateEnv}/cluster_product/openstack"
                    templateOutputDir = "${env.WORKSPACE}/template/output/openstack"
                    sh "mkdir -p ${templateOutputDir}"
                    python.setupCookiecutterVirtualenv(cutterEnv)
                    python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv)
                    sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
                }
            }

            stage('Generate product StackLight') {
                if (COOKIECUTTER_INSTALL_STACKLIGHT.toBoolean()) {
                    templateDir = "${templateEnv}/cluster_product/stacklight"
                    templateOutputDir = "${env.WORKSPACE}/template/output/stacklight"
                    sh "mkdir -p ${templateOutputDir}"
                    python.setupCookiecutterVirtualenv(cutterEnv)
                    python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv)
                    sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
                }
            }

            stage('Generate new SaltMaster node') {
                def nodeFile = "${modelEnv}/nodes/cfg01.${clusterDomain}.yml"
                def nodeString = """classes:
- cluster.${clusterName}.infra.config
parameters:
    _param:
        linux_system_codename: xenial
        reclass_data_revision: master
    linux:
        system:
            name: cfg01
            domain: ${clusterDomain}
"""
                sh "mkdir -p ${modelEnv}/nodes/"
                writeFile(file: nodeFile, text: nodeString)
            }

            stage ('Save changes to Reclass model') {
                if (COMMIT_CHANGES.toBoolean()) {
                    git.changeGitBranch(modelEnv, targetBranch)
                    git.commitGitChanges(modelEnv, "Added new cluster ${clusterName}")
                    git.pushGitChanges(modelEnv, targetBranch, 'origin', RECLASS_MODEL_CREDENTIALS)
                }
                sh(returnStatus: true, script: "tar -zcvf ${clusterName}.tar.gz -C ${modelEnv} .")
                archiveArtifacts artifacts: "${clusterName}.tar.gz"
            }

        } catch (Throwable e) {
             // If there was an error or exception thrown, the build failed
             currentBuild.result = "FAILURE"
             throw e
        } finally {
            stage ('Clean workspace directories') {
                sh(returnStatus: true, script: "rm -rfv ${templateEnv}")
                sh(returnStatus: true, script: "rm -rfv ${modelEnv}")
            }
             // common.sendNotification(currentBuild.result,"",["slack"])
        }
    }
}
