blob: a9ca48f8a496709d7343ae25693c56f2cfb96347 [file] [log] [blame]
Tomáš Kukrál7ded3642017-03-27 15:52:51 +02001/**
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ál91e49252017-05-09 14:40:26 +02009 * EMAIL_ADDRESS Email to send a created tar file
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020010 * 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ál65f8f752017-03-30 17:51:46 +020013 * COMMIT_CHANGES Commit model to repo
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020014 *
15**/
16
17common = new com.mirantis.mk.Common()
18git = new com.mirantis.mk.Git()
19python = new com.mirantis.mk.Python()
20
21timestamps {
22 node() {
Tomáš Kukrál9f6260f2017-03-29 23:58:26 +020023 def templateEnv = "${env.WORKSPACE}/template"
24 def modelEnv = "${env.WORKSPACE}/model"
25
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020026 try {
Tomáš Kukrálf2a74ab2017-05-09 17:53:32 +020027 def templateContext = readYaml text: COOKIECUTTER_TEMPLATE_CONTEXT
Tomáš Kukrál91e49252017-05-09 14:40:26 +020028 def clusterDomain = templateContext.default_context.cluster_domain
29 def clusterName = templateContext.default_context.cluster_name
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020030 def cutterEnv = "${env.WORKSPACE}/cutter"
31 def jinjaEnv = "${env.WORKSPACE}/jinja"
Tomáš Kukrál931f9972017-03-30 23:44:57 +020032 def outputDestination = "${modelEnv}/classes/cluster/${clusterName}"
Tomáš Kukrál91e49252017-05-09 14:40:26 +020033 def targetBranch = "feature/${clusterName}"
34 def templateBaseDir = "${env.WORKSPACE}/template"
Tomáš Kukrál91e49252017-05-09 14:40:26 +020035 def templateDir = "${templateEnv}/template/dir"
36 def templateOutputDir = templateBaseDir
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020037
Tomáš Kukrál08bfb952017-04-12 13:10:52 +020038 currentBuild.description = clusterName
Tomáš Kukrál2a8fc322017-04-12 18:06:20 +020039 print("Using context:\n" + COOKIECUTTER_TEMPLATE_CONTEXT)
40
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020041 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ál4aac3aa2017-04-04 14:04:02 +020046 if (RECLASS_MODEL_URL != '') {
47 git.checkoutGitRepository(modelEnv, RECLASS_MODEL_URL, RECLASS_MODEL_BRANCH, RECLASS_MODEL_CREDENTIALS)
48 }
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020049 }
50
51 stage('Generate base infrastructure') {
52 templateDir = "${templateEnv}/cluster_product/infra"
Tomáš Kukrál931f9972017-03-30 23:44:57 +020053 templateOutputDir = "${env.WORKSPACE}/template/output/infra"
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020054 sh "mkdir -p ${templateOutputDir}"
Tomáš Kukrálbdc51812017-03-31 09:55:07 +020055 sh "mkdir -p ${outputDestination}"
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020056 python.setupCookiecutterVirtualenv(cutterEnv)
Jakub Josefda18fc42017-04-26 14:14:25 +020057 python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv, templateBaseDir)
Tomáš Kukrál931f9972017-03-30 23:44:57 +020058 sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020059 }
60
61 stage('Generate product CI/CD') {
Tomáš Kukrál91e49252017-05-09 14:40:26 +020062 if (templateContext.default_context.cicd_enabled.toBoolean()) {
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020063 templateDir = "${templateEnv}/cluster_product/cicd"
Tomáš Kukrál931f9972017-03-30 23:44:57 +020064 templateOutputDir = "${env.WORKSPACE}/template/output/cicd"
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020065 sh "mkdir -p ${templateOutputDir}"
66 python.setupCookiecutterVirtualenv(cutterEnv)
Jakub Josefda18fc42017-04-26 14:14:25 +020067 python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv, templateBaseDir)
Tomáš Kukrál931f9972017-03-30 23:44:57 +020068 sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020069 }
70 }
71
72 stage('Generate product OpenContrail') {
Tomáš Kukrál91e49252017-05-09 14:40:26 +020073 if (templateContext.default_context.opencontrail_enabled.toBoolean()) {
Tomáš Kukrále8042572017-03-30 22:28:06 +020074 templateDir = "${templateEnv}/cluster_product/opencontrail"
Tomáš Kukrál931f9972017-03-30 23:44:57 +020075 templateOutputDir = "${env.WORKSPACE}/template/output/opencontrail"
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020076 sh "mkdir -p ${templateOutputDir}"
77 python.setupCookiecutterVirtualenv(cutterEnv)
Jakub Josefda18fc42017-04-26 14:14:25 +020078 python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv, templateBaseDir)
Tomáš Kukrál931f9972017-03-30 23:44:57 +020079 sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020080 }
81 }
82
83 stage('Generate product Kubernetes') {
Tomáš Kukrál91e49252017-05-09 14:40:26 +020084 if (templateContext.default_context.kubernetes_enabled.toBoolean()) {
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020085 templateDir = "${templateEnv}/cluster_product/kubernetes"
Tomáš Kukrál931f9972017-03-30 23:44:57 +020086 templateOutputDir = "${env.WORKSPACE}/template/output/kubernetes"
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020087 sh "mkdir -p ${templateOutputDir}"
88 python.setupCookiecutterVirtualenv(cutterEnv)
Jakub Josefda18fc42017-04-26 14:14:25 +020089 python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv, templateBaseDir)
Tomáš Kukrál931f9972017-03-30 23:44:57 +020090 sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020091 }
92 }
93
94 stage('Generate product OpenStack') {
Tomáš Kukrál91e49252017-05-09 14:40:26 +020095 if (templateContext.default_context.openstack_enabled.toBoolean()) {
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020096 templateDir = "${templateEnv}/cluster_product/openstack"
Tomáš Kukrál931f9972017-03-30 23:44:57 +020097 templateOutputDir = "${env.WORKSPACE}/template/output/openstack"
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020098 sh "mkdir -p ${templateOutputDir}"
99 python.setupCookiecutterVirtualenv(cutterEnv)
Jakub Josefda18fc42017-04-26 14:14:25 +0200100 python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv, templateBaseDir)
Tomáš Kukrál931f9972017-03-30 23:44:57 +0200101 sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200102 }
103 }
104
105 stage('Generate product StackLight') {
Tomáš Kukrál91e49252017-05-09 14:40:26 +0200106 if (templateContext.default_context.stacklight_enabled.toBoolean()) {
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200107 templateDir = "${templateEnv}/cluster_product/stacklight"
Tomáš Kukrál931f9972017-03-30 23:44:57 +0200108 templateOutputDir = "${env.WORKSPACE}/template/output/stacklight"
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200109 sh "mkdir -p ${templateOutputDir}"
110 python.setupCookiecutterVirtualenv(cutterEnv)
Jakub Josefda18fc42017-04-26 14:14:25 +0200111 python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv, templateBaseDir)
Tomáš Kukrál931f9972017-03-30 23:44:57 +0200112 sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200113 }
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
120parameters:
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ál4aac3aa2017-04-04 14:04:02 +0200129 sh "mkdir -p ${modelEnv}/nodes/"
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200130 writeFile(file: nodeFile, text: nodeString)
131 }
132
Tomáš Kukrál6c5957e2017-03-30 18:04:38 +0200133 stage ('Save changes to Reclass model') {
Tomáš Kukrál91e49252017-05-09 14:40:26 +0200134 if (env.getEnvironment().containsKey('COMMIT_CHANGES') && COMMIT_CHANGES.toBoolean() && RECLASS_MODEL_URL != null && RECLASS_MODEL_URL != "") {
Tomáš Kukrál4aac3aa2017-04-04 14:04:02 +0200135 git.changeGitBranch(modelEnv, targetBranch)
Tomáš Kukrál6c5957e2017-03-30 18:04:38 +0200136 git.commitGitChanges(modelEnv, "Added new cluster ${clusterName}")
Tomáš Kukrál65f8f752017-03-30 17:51:46 +0200137 git.pushGitChanges(modelEnv, targetBranch, 'origin', RECLASS_MODEL_CREDENTIALS)
138 }
Tomáš Kukrál91e49252017-05-09 14:40:26 +0200139
Tomáš Kukrál931f9972017-03-30 23:44:57 +0200140 sh(returnStatus: true, script: "tar -zcvf ${clusterName}.tar.gz -C ${modelEnv} .")
Tomáš Kukrál4c98b582017-03-30 18:49:06 +0200141 archiveArtifacts artifacts: "${clusterName}.tar.gz"
Tomáš Kukrál91e49252017-05-09 14:40:26 +0200142 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ál7ded3642017-03-27 15:52:51 +0200148 }
149
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200150 } 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ál8b91db92017-03-29 08:30:49 +0200155 stage ('Clean workspace directories') {
Tomáš Kukrál2b983c32017-03-30 18:09:02 +0200156 sh(returnStatus: true, script: "rm -rfv ${templateEnv}")
157 sh(returnStatus: true, script: "rm -rfv ${modelEnv}")
Tomáš Kukrál8b91db92017-03-29 08:30:49 +0200158 }
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200159 // common.sendNotification(currentBuild.result,"",["slack"])
160 }
161 }
162}