Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 1 | /** |
| 2 | * Generate cookiecutter cluster by individual products |
| 3 | * |
| 4 | * Expected parameters: |
| 5 | * COOKIECUTTER_TEMPLATE_CREDENTIALS Credentials to the Cookiecutter template repo. |
| 6 | * COOKIECUTTER_TEMPLATE_URL Cookiecutter template repo address. |
| 7 | * COOKIECUTTER_TEMPLATE_BRANCH Branch for the template. |
| 8 | * COOKIECUTTER_TEMPLATE_CONTEXT Context parameters for the template generation. |
| 9 | * COOKIECUTTER_INSTALL_CICD Whether to install CI/CD stack. |
| 10 | * COOKIECUTTER_INSTALL_CONTRAIL Whether to install OpenContrail SDN. |
| 11 | * COOKIECUTTER_INSTALL_KUBERNETES Whether to install Kubernetes. |
| 12 | * COOKIECUTTER_INSTALL_OPENSTACK Whether to install OpenStack cloud. |
| 13 | * COOKIECUTTER_INSTALL_STACKLIGHT Whether to install StackLight monitoring. |
| 14 | * RECLASS_MODEL_URL Reclass model repo address |
| 15 | * RECLASS_MODEL_CREDENTIALS Credentials to the Reclass model repo. |
| 16 | * RECLASS_MODEL_BRANCH Branch for the template to push to model. |
Tomáš Kukrál | 65f8f75 | 2017-03-30 17:51:46 +0200 | [diff] [blame] | 17 | * COMMIT_CHANGES Commit model to repo |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 18 | * |
| 19 | **/ |
| 20 | |
| 21 | common = new com.mirantis.mk.Common() |
| 22 | git = new com.mirantis.mk.Git() |
| 23 | python = new com.mirantis.mk.Python() |
| 24 | |
| 25 | timestamps { |
| 26 | node() { |
Tomáš Kukrál | 9f6260f | 2017-03-29 23:58:26 +0200 | [diff] [blame] | 27 | def templateEnv = "${env.WORKSPACE}/template" |
| 28 | def modelEnv = "${env.WORKSPACE}/model" |
| 29 | |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 30 | try { |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 31 | def templateContext = python.loadJson(COOKIECUTTER_TEMPLATE_CONTEXT) |
| 32 | def templateDir = "${templateEnv}/template/dir" |
| 33 | def templateOutputDir = "${env.WORKSPACE}/template" |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 34 | def cutterEnv = "${env.WORKSPACE}/cutter" |
| 35 | def jinjaEnv = "${env.WORKSPACE}/jinja" |
| 36 | def clusterName = templateContext.cluster_name |
| 37 | def clusterDomain = templateContext.cluster_domain |
| 38 | def targetBranch = "feature/${clusterName}" |
Tomáš Kukrál | 931f997 | 2017-03-30 23:44:57 +0200 | [diff] [blame] | 39 | def outputDestination = "${modelEnv}/classes/cluster/${clusterName}" |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 40 | |
Tomáš Kukrál | 08bfb95 | 2017-04-12 13:10:52 +0200 | [diff] [blame^] | 41 | currentBuild.description = clusterName |
| 42 | |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 43 | stage ('Download Cookiecutter template') { |
| 44 | git.checkoutGitRepository(templateEnv, COOKIECUTTER_TEMPLATE_URL, COOKIECUTTER_TEMPLATE_BRANCH, COOKIECUTTER_TEMPLATE_CREDENTIALS) |
| 45 | } |
| 46 | |
| 47 | stage ('Download full Reclass model') { |
Tomáš Kukrál | 4aac3aa | 2017-04-04 14:04:02 +0200 | [diff] [blame] | 48 | if (RECLASS_MODEL_URL != '') { |
| 49 | git.checkoutGitRepository(modelEnv, RECLASS_MODEL_URL, RECLASS_MODEL_BRANCH, RECLASS_MODEL_CREDENTIALS) |
| 50 | } |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | stage('Generate base infrastructure') { |
| 54 | templateDir = "${templateEnv}/cluster_product/infra" |
Tomáš Kukrál | 931f997 | 2017-03-30 23:44:57 +0200 | [diff] [blame] | 55 | templateOutputDir = "${env.WORKSPACE}/template/output/infra" |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 56 | sh "mkdir -p ${templateOutputDir}" |
Tomáš Kukrál | bdc5181 | 2017-03-31 09:55:07 +0200 | [diff] [blame] | 57 | sh "mkdir -p ${outputDestination}" |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 58 | python.setupCookiecutterVirtualenv(cutterEnv) |
| 59 | python.buildCookiecutterTemplate(templateDir, templateContext, templateOutputDir, cutterEnv) |
Tomáš Kukrál | 931f997 | 2017-03-30 23:44:57 +0200 | [diff] [blame] | 60 | sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}" |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | stage('Generate product CI/CD') { |
| 64 | if (COOKIECUTTER_INSTALL_CICD.toBoolean()) { |
| 65 | templateDir = "${templateEnv}/cluster_product/cicd" |
Tomáš Kukrál | 931f997 | 2017-03-30 23:44:57 +0200 | [diff] [blame] | 66 | templateOutputDir = "${env.WORKSPACE}/template/output/cicd" |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 67 | sh "mkdir -p ${templateOutputDir}" |
| 68 | python.setupCookiecutterVirtualenv(cutterEnv) |
| 69 | python.buildCookiecutterTemplate(templateDir, templateContext, templateOutputDir, cutterEnv) |
Tomáš Kukrál | 931f997 | 2017-03-30 23:44:57 +0200 | [diff] [blame] | 70 | sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}" |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
| 74 | stage('Generate product OpenContrail') { |
| 75 | if (COOKIECUTTER_INSTALL_CONTRAIL.toBoolean()) { |
Tomáš Kukrál | e804257 | 2017-03-30 22:28:06 +0200 | [diff] [blame] | 76 | templateDir = "${templateEnv}/cluster_product/opencontrail" |
Tomáš Kukrál | 931f997 | 2017-03-30 23:44:57 +0200 | [diff] [blame] | 77 | templateOutputDir = "${env.WORKSPACE}/template/output/opencontrail" |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 78 | sh "mkdir -p ${templateOutputDir}" |
| 79 | python.setupCookiecutterVirtualenv(cutterEnv) |
| 80 | python.buildCookiecutterTemplate(templateDir, templateContext, templateOutputDir, cutterEnv) |
Tomáš Kukrál | 931f997 | 2017-03-30 23:44:57 +0200 | [diff] [blame] | 81 | sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}" |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
| 85 | stage('Generate product Kubernetes') { |
| 86 | if (COOKIECUTTER_INSTALL_KUBERNETES.toBoolean()) { |
| 87 | templateDir = "${templateEnv}/cluster_product/kubernetes" |
Tomáš Kukrál | 931f997 | 2017-03-30 23:44:57 +0200 | [diff] [blame] | 88 | templateOutputDir = "${env.WORKSPACE}/template/output/kubernetes" |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 89 | sh "mkdir -p ${templateOutputDir}" |
| 90 | python.setupCookiecutterVirtualenv(cutterEnv) |
| 91 | python.buildCookiecutterTemplate(templateDir, templateContext, templateOutputDir, cutterEnv) |
Tomáš Kukrál | 931f997 | 2017-03-30 23:44:57 +0200 | [diff] [blame] | 92 | sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}" |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
| 96 | stage('Generate product OpenStack') { |
| 97 | if (COOKIECUTTER_INSTALL_OPENSTACK.toBoolean()) { |
| 98 | templateDir = "${templateEnv}/cluster_product/openstack" |
Tomáš Kukrál | 931f997 | 2017-03-30 23:44:57 +0200 | [diff] [blame] | 99 | templateOutputDir = "${env.WORKSPACE}/template/output/openstack" |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 100 | sh "mkdir -p ${templateOutputDir}" |
| 101 | python.setupCookiecutterVirtualenv(cutterEnv) |
| 102 | python.buildCookiecutterTemplate(templateDir, templateContext, templateOutputDir, cutterEnv) |
Tomáš Kukrál | 931f997 | 2017-03-30 23:44:57 +0200 | [diff] [blame] | 103 | sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}" |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
| 107 | stage('Generate product StackLight') { |
| 108 | if (COOKIECUTTER_INSTALL_STACKLIGHT.toBoolean()) { |
| 109 | templateDir = "${templateEnv}/cluster_product/stacklight" |
Tomáš Kukrál | 931f997 | 2017-03-30 23:44:57 +0200 | [diff] [blame] | 110 | templateOutputDir = "${env.WORKSPACE}/template/output/stacklight" |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 111 | sh "mkdir -p ${templateOutputDir}" |
| 112 | python.setupCookiecutterVirtualenv(cutterEnv) |
| 113 | python.buildCookiecutterTemplate(templateDir, templateContext, templateOutputDir, cutterEnv) |
Tomáš Kukrál | 931f997 | 2017-03-30 23:44:57 +0200 | [diff] [blame] | 114 | sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}" |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
| 118 | stage('Generate new SaltMaster node') { |
| 119 | def nodeFile = "${modelEnv}/nodes/cfg01.${clusterDomain}.yml" |
| 120 | def nodeString = """classes: |
| 121 | - cluster.${clusterName}.infra.config |
| 122 | parameters: |
| 123 | _param: |
| 124 | linux_system_codename: xenial |
| 125 | reclass_data_revision: master |
| 126 | linux: |
| 127 | system: |
| 128 | name: cfg01 |
| 129 | domain: ${clusterDomain} |
| 130 | """ |
Tomáš Kukrál | 4aac3aa | 2017-04-04 14:04:02 +0200 | [diff] [blame] | 131 | sh "mkdir -p ${modelEnv}/nodes/" |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 132 | writeFile(file: nodeFile, text: nodeString) |
| 133 | } |
| 134 | |
Tomáš Kukrál | 6c5957e | 2017-03-30 18:04:38 +0200 | [diff] [blame] | 135 | stage ('Save changes to Reclass model') { |
| 136 | if (COMMIT_CHANGES.toBoolean()) { |
Tomáš Kukrál | 4aac3aa | 2017-04-04 14:04:02 +0200 | [diff] [blame] | 137 | git.changeGitBranch(modelEnv, targetBranch) |
Tomáš Kukrál | 6c5957e | 2017-03-30 18:04:38 +0200 | [diff] [blame] | 138 | git.commitGitChanges(modelEnv, "Added new cluster ${clusterName}") |
Tomáš Kukrál | 65f8f75 | 2017-03-30 17:51:46 +0200 | [diff] [blame] | 139 | git.pushGitChanges(modelEnv, targetBranch, 'origin', RECLASS_MODEL_CREDENTIALS) |
| 140 | } |
Tomáš Kukrál | 931f997 | 2017-03-30 23:44:57 +0200 | [diff] [blame] | 141 | sh(returnStatus: true, script: "tar -zcvf ${clusterName}.tar.gz -C ${modelEnv} .") |
Tomáš Kukrál | 4c98b58 | 2017-03-30 18:49:06 +0200 | [diff] [blame] | 142 | archiveArtifacts artifacts: "${clusterName}.tar.gz" |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 143 | } |
| 144 | |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 145 | } catch (Throwable e) { |
| 146 | // If there was an error or exception thrown, the build failed |
| 147 | currentBuild.result = "FAILURE" |
| 148 | throw e |
| 149 | } finally { |
Tomáš Kukrál | 8b91db9 | 2017-03-29 08:30:49 +0200 | [diff] [blame] | 150 | stage ('Clean workspace directories') { |
Tomáš Kukrál | 2b983c3 | 2017-03-30 18:09:02 +0200 | [diff] [blame] | 151 | sh(returnStatus: true, script: "rm -rfv ${templateEnv}") |
| 152 | sh(returnStatus: true, script: "rm -rfv ${modelEnv}") |
Tomáš Kukrál | 8b91db9 | 2017-03-29 08:30:49 +0200 | [diff] [blame] | 153 | } |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 154 | // common.sendNotification(currentBuild.result,"",["slack"]) |
| 155 | } |
| 156 | } |
| 157 | } |