/**
 *
 * Deploy the product cluster using Jenkins master on CICD cluster
 *
 * Expected parameters:
 *   STACKS                        List of deployments from 'all physical' view in the Jenkins
                                   They should be separated by newline
 *   PARENT_NODE_NAME
 *   OS_AUTH_URL                   OpenStack keystone catalog URL
 *   OS_PROJECT_NAME               OpenStack project (tenant) name
 *   OS_CREDENTIALS                OpenStack username and password credentials ID in Jenkins

 *   TCP_QA_REFS                   Reference to the tcp-qa change on Gerrit, like refs/changes/46/418546/41
 */
@Library('tcp-qa')_

common = new com.mirantis.mk.Common()
shared = new com.mirantis.system_qa.SharedPipeline()
import jenkins.model.*

def stacks = env.STACKS.readLines()

def get_last_build_time(job_name) {
  def job = Hudson.instance.getJob(job_name)
  def last_build = Jenkins.instance.getItemByFullName(job.fullName).getLastBuild()
  if (last_build == null) {
  	return null
  }
  def upStreamBuild = Jenkins.getInstance().getItemByFullName(job.fullName).getBuildByNumber(last_build.getNumber())
  return upStreamBuild.getTime()
}

@NonCPS
def oldest_job(map) {
  return map.sort({a, b -> a.value <=> b.value})*.key[0]
}

// ============================================================================
timeout(time: 15, unit: 'HOURS') {
  node (env.PARENT_NODE_NAME) {
    stage("Remove extra stacks") {
        withCredentials([
           [$class          : 'UsernamePasswordMultiBinding',
           credentialsId   : env.OS_CREDENTIALS,
           passwordVariable: 'OS_PASSWORD',
           usernameVariable: 'OS_USERNAME']
        ]) {
            env.OS_IDENTITY_API_VERSION = 3
            for (stack_name in stacks) {
              shared.run_cmd("""
                export OS_IDENTITY_API_VERSION=3
                export OS_AUTH_URL=${OS_AUTH_URL}
                export OS_USERNAME=${OS_USERNAME}
                export OS_PASSWORD=${OS_PASSWORD}
                export OS_PROJECT_NAME=${OS_PROJECT_NAME}
                export OS_USER_DOMAIN_NAME=ldap_mirantis
                openstack --insecure stack delete -y ${stack_name} || true
                timeout 20m /bin/bash -c "while openstack --insecure stack show ${stack_name} -f value -c stack_status; do sleep 10; done";
              """)
            }
        }
    }

    stage("Start deployment") {
      def jobs_map = [:]
      for (stack_name in stacks) {
        jobs_map.put ("${stack_name}", get_last_build_time(stack_name))
      }
      stack_to_deploy = oldest_job(jobs_map)

      println "Build ${stack_to_deploy}"
      currentBuild.description = "${stack_to_deploy}"
      // Trigger job
      def deploy = build job: "${stack_to_deploy}",
      parameters: [
            string(name: 'PARENT_NODE_NAME', value: env.PARENT_NODE_NAME),
            string(name: 'OS_CREDENTIALS', value: env.OS_CREDENTIALS),
            string(name: 'TCP_QA_REFS', value: env.TCP_QA_REFS)
        ]
    }
  }
}