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