Hanna Arhipova | d62fef4 | 2020-01-03 22:56:15 +0200 | [diff] [blame] | 1 | /** |
| 2 | * |
| 3 | * Deploy the product cluster using Jenkins master on CICD cluster |
| 4 | * |
| 5 | * Expected parameters: |
| 6 | * STACKS List of deployments from 'all physical' view in the Jenkins |
| 7 | They should be separated by newline |
| 8 | * PARENT_NODE_NAME |
| 9 | * OS_AUTH_URL OpenStack keystone catalog URL |
| 10 | * OS_PROJECT_NAME OpenStack project (tenant) name |
| 11 | * OS_CREDENTIALS OpenStack username and password credentials ID in Jenkins |
| 12 | |
| 13 | * TCP_QA_REFS Reference to the tcp-qa change on Gerrit, like refs/changes/46/418546/41 |
| 14 | */ |
| 15 | @Library('tcp-qa')_ |
| 16 | |
| 17 | common = new com.mirantis.mk.Common() |
| 18 | shared = new com.mirantis.system_qa.SharedPipeline() |
| 19 | import jenkins.model.* |
| 20 | |
| 21 | def stacks = env.STACKS.readLines() |
| 22 | |
| 23 | def get_last_build_time(job_name) { |
| 24 | def job = Hudson.instance.getJob(job_name) |
| 25 | def last_build = Jenkins.instance.getItemByFullName(job.fullName).getLastBuild() |
| 26 | if (last_build == null) { |
| 27 | return null |
| 28 | } |
| 29 | def upStreamBuild = Jenkins.getInstance().getItemByFullName(job.fullName).getBuildByNumber(last_build.getNumber()) |
| 30 | return upStreamBuild.getTime() |
| 31 | } |
| 32 | |
| 33 | @NonCPS |
| 34 | def oldest_job(map) { |
| 35 | println map.sort({a, b -> a.value <=> b.value})*.value |
| 36 | return map.sort({a, b -> a.value <=> b.value})*.key[0] |
| 37 | } |
| 38 | |
| 39 | // ============================================================================ |
| 40 | timeout(time: 15, unit: 'HOURS') { |
| 41 | node (env.PARENT_NODE_NAME) { |
| 42 | stage("Remove extra stacks") { |
| 43 | withCredentials([ |
| 44 | [$class : 'UsernamePasswordMultiBinding', |
| 45 | credentialsId : env.OS_CREDENTIALS, |
| 46 | passwordVariable: 'OS_PASSWORD', |
| 47 | usernameVariable: 'OS_USERNAME'] |
| 48 | ]) { |
| 49 | env.OS_IDENTITY_API_VERSION = 3 |
| 50 | for (stack_name in stacks) { |
| 51 | shared.run_cmd(""" |
| 52 | # export OS_IDENTITY_API_VERSION=3 |
| 53 | # export OS_AUTH_URL=${OS_AUTH_URL} |
| 54 | # export OS_USERNAME=${OS_USERNAME} |
| 55 | # export OS_PASSWORD=${OS_PASSWORD} |
| 56 | # export OS_PROJECT_NAME=${OS_PROJECT_NAME} |
| 57 | openstack --insecure stack delete -y ${stack_name} || true |
| 58 | timeout 20m /bin/bash -c "while openstack --insecure stack show ${stack_name} -f value -c stack_status; do sleep 10; done"; |
| 59 | """) |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
Hanna Arhipova | d62fef4 | 2020-01-03 22:56:15 +0200 | [diff] [blame] | 64 | stage("Start deployment") { |
| 65 | def jobs_map = [:] |
| 66 | for (stack_name in stacks) { |
| 67 | println stack_name |
| 68 | println get_last_build_time(stack_name) |
| 69 | jobs_map.put ("${stack_name}", get_last_build_time(stack_name)) |
| 70 | } |
| 71 | stack_to_deploy = oldest_job(jobs_map) |
| 72 | |
| 73 | println "Build ${stack_to_deploy}" |
| 74 | currentBuild.description = "${stack_to_deploy}" |
| 75 | // Trigger job |
| 76 | def deploy = build job: "${stack_to_deploy}", |
| 77 | parameters: [ |
| 78 | string(name: 'PARENT_NODE_NAME', value: env.PARENT_NODE_NAME), |
Hanna Arhipova | 3be5062 | 2021-01-11 13:11:45 +0200 | [diff] [blame] | 79 | string(name: 'OS_CREDENTIALS', value: env.OS_CREDENTIALS), |
| 80 | string(name: 'TCP_QA_REFS', value: env.TCP_QA_REFS) |
Hanna Arhipova | d62fef4 | 2020-01-03 22:56:15 +0200 | [diff] [blame] | 81 | ] |
| 82 | } |
| 83 | } |
| 84 | } |