/**
 * Generate cookiecutter cluster by individual products
 *
 * Expected parameters:
 *   COOKIECUTTER_TEMPLATE_CONTEXT      Context parameters for the template generation.
 *   EMAIL_ADDRESS                      Email to send a created tar file
 *
 **/

common = new com.mirantis.mk.Common()
git = new com.mirantis.mk.Git()
python = new com.mirantis.mk.Python()
saltModelTesting = new com.mirantis.mk.SaltModelTesting()
ssh = new com.mirantis.mk.Ssh()

def reclassVersion = '1.5.4'
if (common.validInputParam('RECLASS_VERSION')) {
  reclassVersion = RECLASS_VERSION
}

timeout(time: 12, unit: 'HOURS') {
  node("python&&docker") {
    def templateEnv = "${env.WORKSPACE}/template"
    def modelEnv = "${env.WORKSPACE}/model"
    def testEnv = "${env.WORKSPACE}/test"
    def pipelineEnv = "${env.WORKSPACE}/pipelines"

    try {
      def templateContext = readYaml text: COOKIECUTTER_TEMPLATE_CONTEXT
      def mcpVersion = templateContext.default_context.mcp_version
      def sharedReclassUrl = templateContext.default_context.shared_reclass_url
      def clusterDomain = templateContext.default_context.cluster_domain
      def clusterName = templateContext.default_context.cluster_name
      def saltMaster = templateContext.default_context.salt_master_hostname
      def localRepositories = templateContext.default_context.local_repositories.toBoolean()
      def offlineDeployment = templateContext.default_context.offline_deployment.toBoolean()
      def cutterEnv = "${env.WORKSPACE}/cutter"
      def jinjaEnv = "${env.WORKSPACE}/jinja"
      def outputDestination = "${modelEnv}/classes/cluster/${clusterName}"
      def systemEnv = "${modelEnv}/classes/system"
      def targetBranch = "feature/${clusterName}"
      def templateBaseDir = "${env.WORKSPACE}/template"
      def templateDir = "${templateEnv}/template/dir"
      def templateOutputDir = templateBaseDir
      def user
      wrap([$class: 'BuildUser']) {
        user = env.BUILD_USER_ID
      }

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

      stage ('Download Cookiecutter template') {
        def cookiecutterTemplateUrl = templateContext.default_context.cookiecutter_template_url
        def cookiecutterTemplateBranch = templateContext.default_context.cookiecutter_template_branch
        git.checkoutGitRepository(templateEnv, cookiecutterTemplateUrl, 'master')
        // Use refspec if exists first of all
        if (cookiecutterTemplateBranch.toString().startsWith('refs/')) {
          dir(templateEnv) {
            ssh.agentSh("git fetch ${cookiecutterTemplateUrl} ${cookiecutterTemplateBranch} && git checkout FETCH_HEAD")
          }
        } else {
          // Use mcpVersion git tag if not specified branch for cookiecutter-templates
          if (cookiecutterTemplateBranch == '') {
            cookiecutterTemplateBranch = mcpVersion
            // Don't have nightly/testing/stable for cookiecutter-templates repo, therefore use master
            if(mcpVersion == "nightly" || mcpVersion == "testing" || mcpVersion == "stable"){
              cookiecutterTemplateBranch = 'master'
            }
          }
          git.changeGitBranch(templateEnv, cookiecutterTemplateBranch)
        }
      }

      stage ('Create empty reclass model') {
        dir(path: modelEnv) {
          sh "rm -rfv .git"
          sh "git init"
          ssh.agentSh("git submodule add ${sharedReclassUrl} 'classes/system'")
        }

        def sharedReclassBranch = templateContext.default_context.shared_reclass_branch
        // Use refspec if exists first of all
        if (sharedReclassBranch.toString().startsWith('refs/')) {
          dir(systemEnv) {
            ssh.agentSh("git fetch ${sharedReclassUrl} ${sharedReclassBranch} && git checkout FETCH_HEAD")
          }
        } else {
          // Use mcpVersion git tag if not specified branch for reclass-system
          if (sharedReclassBranch == '') {
            sharedReclassBranch = mcpVersion
            // Don't have nightly/testing/stable for reclass-system repo, therefore use master
            if(mcpVersion == "nightly" || mcpVersion == "testing" || mcpVersion == "stable"){
              sharedReclassBranch = 'master'
            }
          }
          git.changeGitBranch(systemEnv, sharedReclassBranch)
        }
        git.commitGitChanges(modelEnv, "Added new shared reclass submodule", "${user}@localhost", "${user}")
      }

      def productList = ["infra", "cicd", "opencontrail", "kubernetes", "openstack", "oss", "stacklight", "ceph"]
      for (product in productList) {

        // get templateOutputDir and productDir
        if (product.startsWith("stacklight")) {
          templateOutputDir = "${env.WORKSPACE}/output/stacklight"

          def stacklightVersion
          try {
            stacklightVersion = templateContext.default_context['stacklight_version']
          } catch (Throwable e) {
            common.warningMsg('Stacklight version loading failed')
          }

          if (stacklightVersion) {
            productDir = "stacklight" + stacklightVersion
          } else {
            productDir = "stacklight1"
          }

        } else {
          templateOutputDir = "${env.WORKSPACE}/output/${product}"
          productDir = product
        }

        if (product == "infra" || (templateContext.default_context["${product}_enabled"]
            && templateContext.default_context["${product}_enabled"].toBoolean())) {

          templateDir = "${templateEnv}/cluster_product/${productDir}"
          common.infoMsg("Generating product " + product + " from " + templateDir + " to " + templateOutputDir)

          sh "rm -rf ${templateOutputDir} || true"
          sh "mkdir -p ${templateOutputDir}"
          sh "mkdir -p ${outputDestination}"

          python.setupCookiecutterVirtualenv(cutterEnv)
          python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv, templateBaseDir)
          sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
        } else {
          common.warningMsg("Product " + product + " is disabled")
        }
      }

      if(localRepositories && !offlineDeployment){
        def aptlyModelUrl = templateContext.default_context.local_model_url
        dir(path: modelEnv) {
          ssh.agentSh "git submodule add \"${aptlyModelUrl}\" \"classes/cluster/${clusterName}/cicd/aptly\""
          if(!(mcpVersion in ["nightly", "testing", "stable"])){
            ssh.agentSh "cd \"classes/cluster/${clusterName}/cicd/aptly\";git fetch --tags;git checkout ${mcpVersion}"
          }
        }
      }

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

        git.commitGitChanges(modelEnv, "Create model ${clusterName}", "${user}@localhost", "${user}")
      }

      stage("Test") {
        if (TEST_MODEL.toBoolean() && sharedReclassUrl != '') {
          def testResult = false
          sh("cp -r ${modelEnv} ${testEnv}")
          def DockerCName = "${env.JOB_NAME.toLowerCase()}_${env.BUILD_TAG.toLowerCase()}"
          testResult = saltModelTesting.setupAndTestNode(
              "${saltMaster}.${clusterDomain}",
              "",
              "",
              testEnv,
              'pkg',
              'stable',
              reclassVersion,
              0,
              false,
              false,
              '',
              '',
              DockerCName)
          if (testResult) {
            common.infoMsg("Test finished: SUCCESS")
          } else {
            common.infoMsg('Test finished: FAILURE')
            throw new RuntimeException('Test stage finished: FAILURE')
          }
        } else {
          common.warningMsg("Test stage has been skipped!")
        }
      }
      stage("Generate config drives") {
        // apt package genisoimage is required for this stage

        // download create-config-drive
        // FIXME: that should be refactored, to use git clone - to be able download it from custom repo.
        def mcpCommonScriptsBranch = templateContext.default_context.mcp_common_scripts_branch
        if (mcpCommonScriptsBranch == '') {
          mcpCommonScriptsBranch = mcpVersion
          // Don't have nightly for mcp-common-scripts repo, therefore use master
          if(mcpVersion == "nightly"){
            mcpCommonScriptsBranch = 'master'
          }
        }
        def config_drive_script_url = "https://raw.githubusercontent.com/Mirantis/mcp-common-scripts/${mcpCommonScriptsBranch}/config-drive/create_config_drive.sh"
        def user_data_script_url = "https://raw.githubusercontent.com/Mirantis/mcp-common-scripts/${mcpCommonScriptsBranch}/config-drive/master_config.sh"

        sh "wget -O create-config-drive ${config_drive_script_url} && chmod +x create-config-drive"
        sh "wget -O user_data.sh ${user_data_script_url}"

        sh "git clone --mirror https://github.com/Mirantis/mk-pipelines.git ${pipelineEnv}/mk-pipelines"
        sh "git clone --mirror https://github.com/Mirantis/pipeline-library.git ${pipelineEnv}/pipeline-library"
        args = "--user-data user_data.sh --hostname ${saltMaster} --model ${modelEnv} --mk-pipelines ${pipelineEnv}/mk-pipelines/ --pipeline-library ${pipelineEnv}/pipeline-library/ ${saltMaster}.${clusterDomain}-config.iso"

        // load data from model
        def smc = [:]
        smc['SALT_MASTER_MINION_ID'] = "${saltMaster}.${clusterDomain}"
        smc['SALT_MASTER_DEPLOY_IP'] = templateContext['default_context']['salt_master_management_address']
        smc['DEPLOY_NETWORK_GW'] = templateContext['default_context']['deploy_network_gateway']
        smc['DEPLOY_NETWORK_NETMASK'] = templateContext['default_context']['deploy_network_netmask']
        smc['DNS_SERVERS'] = templateContext['default_context']['dns_server01']
        smc['MCP_VERSION'] = "${mcpVersion}"
        if (templateContext['default_context']['local_repositories'] == 'True'){
          def localRepoIP = templateContext['default_context']['local_repo_url']
          smc['MCP_SALT_REPO_KEY'] = "http://${localRepoIP}/public.gpg"
          smc['MCP_SALT_REPO_URL'] = "http://${localRepoIP}/ubuntu-xenial"
          smc['PIPELINES_FROM_ISO'] = 'false'
          smc['PIPELINE_REPO_URL'] = "http://${localRepoIP}:8088"
          smc['LOCAL_REPOS'] = 'true'
        }
        if (templateContext['default_context']['upstream_proxy_enabled'] == 'True'){
          if (templateContext['default_context']['upstream_proxy_auth_enabled'] == 'True'){
            smc['http_proxy'] = 'http://' + templateContext['default_context']['upstream_proxy_user'] + ':' + templateContext['default_context']['upstream_proxy_password'] + '@' + templateContext['default_context']['upstream_proxy_address'] + ':' + templateContext['default_context']['upstream_proxy_port']
            smc['https_proxy'] = 'http://' + templateContext['default_context']['upstream_proxy_user'] + ':' + templateContext['default_context']['upstream_proxy_password'] + '@' + templateContext['default_context']['upstream_proxy_address'] + ':' + templateContext['default_context']['upstream_proxy_port']
          } else {
            smc['http_proxy'] = 'http://' + templateContext['default_context']['upstream_proxy_address'] + ':' + templateContext['default_context']['upstream_proxy_port']
            smc['https_proxy'] = 'http://' + templateContext['default_context']['upstream_proxy_address'] + ':' + templateContext['default_context']['upstream_proxy_port']
          }
        }

        for (i in common.entries(smc)) {
          sh "sed -i 's,export ${i[0]}=.*,export ${i[0]}=${i[1]},' user_data.sh"
        }

        // create cfg config-drive
        sh "./create-config-drive ${args}"
        sh("mkdir output-${clusterName} && mv ${saltMaster}.${clusterDomain}-config.iso output-${clusterName}/")

        // save cfg iso to artifacts
        archiveArtifacts artifacts: "output-${clusterName}/${saltMaster}.${clusterDomain}-config.iso"

        if (templateContext['default_context']['local_repositories'] == 'True'){
          def aptlyServerHostname = templateContext.default_context.aptly_server_hostname
          def user_data_script_apt_url = "https://raw.githubusercontent.com/Mirantis/mcp-common-scripts/master/config-drive/mirror_config.sh"
          sh "wget -O mirror_config.sh ${user_data_script_apt_url}"

          def smc_apt = [:]
          smc_apt['SALT_MASTER_DEPLOY_IP'] = templateContext['default_context']['salt_master_management_address']
          smc_apt['APTLY_DEPLOY_IP'] = templateContext['default_context']['aptly_server_deploy_address']
          smc_apt['APTLY_DEPLOY_NETMASK'] = templateContext['default_context']['deploy_network_netmask']
          smc_apt['APTLY_MINION_ID'] = "${aptlyServerHostname}.${clusterDomain}"

          for (i in common.entries(smc_apt)) {
            sh "sed -i \"s,export ${i[0]}=.*,export ${i[0]}=${i[1]},\" mirror_config.sh"
          }

          // create apt config-drive
          sh "./create-config-drive --user-data mirror_config.sh --hostname ${aptlyServerHostname} ${aptlyServerHostname}.${clusterDomain}-config.iso"
          sh("mv ${aptlyServerHostname}.${clusterDomain}-config.iso output-${clusterName}/")

          // save apt iso to artifacts
          archiveArtifacts artifacts: "output-${clusterName}/${aptlyServerHostname}.${clusterDomain}-config.iso"
        }
      }

      stage ('Save changes reclass model') {
        sh(returnStatus: true, script: "tar -zcf output-${clusterName}/${clusterName}.tar.gz -C ${modelEnv} .")
        archiveArtifacts artifacts: "output-${clusterName}/${clusterName}.tar.gz"


        if (EMAIL_ADDRESS != null && EMAIL_ADDRESS != "") {
          emailext(to: EMAIL_ADDRESS,
              attachmentsPattern: "output-${clusterName}/*",
              body: "Mirantis Jenkins\n\nRequested reclass model ${clusterName} has been created and attached to this email.\nEnjoy!\n\nMirantis",
              subject: "Your Salt model ${clusterName}")
        }
        dir("output-${clusterName}"){
          deleteDir()
        }
      }

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