blob: 40920680b69731e6d186502a56a78f03d19e434f [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.
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ál65f8f752017-03-30 17:51:46 +020017 * COMMIT_CHANGES Commit model to repo
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020018 *
19**/
20
21common = new com.mirantis.mk.Common()
22git = new com.mirantis.mk.Git()
23python = new com.mirantis.mk.Python()
24
25timestamps {
26 node() {
Tomáš Kukrál9f6260f2017-03-29 23:58:26 +020027 def templateEnv = "${env.WORKSPACE}/template"
28 def modelEnv = "${env.WORKSPACE}/model"
29
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020030 try {
Tomáš Kukrál31067bd2017-04-12 18:01:50 +020031 def templateContext = readYaml text: COOKIECUTTER_TEMPLATE_CONTEXT
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020032 def templateDir = "${templateEnv}/template/dir"
33 def templateOutputDir = "${env.WORKSPACE}/template"
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020034 def cutterEnv = "${env.WORKSPACE}/cutter"
35 def jinjaEnv = "${env.WORKSPACE}/jinja"
Tomáš Kukrál1a09ab02017-04-13 10:21:54 +020036 def clusterName = templateContext.default_context.cluster_name
37 def clusterDomain = templateContext.default_context.cluster_domain
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020038 def targetBranch = "feature/${clusterName}"
Tomáš Kukrál931f9972017-03-30 23:44:57 +020039 def outputDestination = "${modelEnv}/classes/cluster/${clusterName}"
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020040
Tomáš Kukrál08bfb952017-04-12 13:10:52 +020041 currentBuild.description = clusterName
Tomáš Kukrál2a8fc322017-04-12 18:06:20 +020042 print("Using context:\n" + COOKIECUTTER_TEMPLATE_CONTEXT)
43
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020044 stage ('Download Cookiecutter template') {
45 git.checkoutGitRepository(templateEnv, COOKIECUTTER_TEMPLATE_URL, COOKIECUTTER_TEMPLATE_BRANCH, COOKIECUTTER_TEMPLATE_CREDENTIALS)
46 }
47
48 stage ('Download full Reclass model') {
Tomáš Kukrál4aac3aa2017-04-04 14:04:02 +020049 if (RECLASS_MODEL_URL != '') {
50 git.checkoutGitRepository(modelEnv, RECLASS_MODEL_URL, RECLASS_MODEL_BRANCH, RECLASS_MODEL_CREDENTIALS)
51 }
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020052 }
53
54 stage('Generate base infrastructure') {
55 templateDir = "${templateEnv}/cluster_product/infra"
Tomáš Kukrál931f9972017-03-30 23:44:57 +020056 templateOutputDir = "${env.WORKSPACE}/template/output/infra"
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020057 sh "mkdir -p ${templateOutputDir}"
Tomáš Kukrálbdc51812017-03-31 09:55:07 +020058 sh "mkdir -p ${outputDestination}"
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020059 python.setupCookiecutterVirtualenv(cutterEnv)
Tomáš Kukrál8e417682017-04-13 09:55:50 +020060 python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv)
Tomáš Kukrál931f9972017-03-30 23:44:57 +020061 sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020062 }
63
64 stage('Generate product CI/CD') {
65 if (COOKIECUTTER_INSTALL_CICD.toBoolean()) {
66 templateDir = "${templateEnv}/cluster_product/cicd"
Tomáš Kukrál931f9972017-03-30 23:44:57 +020067 templateOutputDir = "${env.WORKSPACE}/template/output/cicd"
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020068 sh "mkdir -p ${templateOutputDir}"
69 python.setupCookiecutterVirtualenv(cutterEnv)
Tomáš Kukrál8e417682017-04-13 09:55:50 +020070 python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv)
Tomáš Kukrál931f9972017-03-30 23:44:57 +020071 sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020072 }
73 }
74
75 stage('Generate product OpenContrail') {
76 if (COOKIECUTTER_INSTALL_CONTRAIL.toBoolean()) {
Tomáš Kukrále8042572017-03-30 22:28:06 +020077 templateDir = "${templateEnv}/cluster_product/opencontrail"
Tomáš Kukrál931f9972017-03-30 23:44:57 +020078 templateOutputDir = "${env.WORKSPACE}/template/output/opencontrail"
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020079 sh "mkdir -p ${templateOutputDir}"
80 python.setupCookiecutterVirtualenv(cutterEnv)
Tomáš Kukrál8e417682017-04-13 09:55:50 +020081 python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv)
Tomáš Kukrál931f9972017-03-30 23:44:57 +020082 sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020083 }
84 }
85
86 stage('Generate product Kubernetes') {
87 if (COOKIECUTTER_INSTALL_KUBERNETES.toBoolean()) {
88 templateDir = "${templateEnv}/cluster_product/kubernetes"
Tomáš Kukrál931f9972017-03-30 23:44:57 +020089 templateOutputDir = "${env.WORKSPACE}/template/output/kubernetes"
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020090 sh "mkdir -p ${templateOutputDir}"
91 python.setupCookiecutterVirtualenv(cutterEnv)
Tomáš Kukrál8e417682017-04-13 09:55:50 +020092 python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv)
Tomáš Kukrál931f9972017-03-30 23:44:57 +020093 sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020094 }
95 }
96
97 stage('Generate product OpenStack') {
98 if (COOKIECUTTER_INSTALL_OPENSTACK.toBoolean()) {
99 templateDir = "${templateEnv}/cluster_product/openstack"
Tomáš Kukrál931f9972017-03-30 23:44:57 +0200100 templateOutputDir = "${env.WORKSPACE}/template/output/openstack"
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200101 sh "mkdir -p ${templateOutputDir}"
102 python.setupCookiecutterVirtualenv(cutterEnv)
Tomáš Kukrál8e417682017-04-13 09:55:50 +0200103 python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv)
Tomáš Kukrál931f9972017-03-30 23:44:57 +0200104 sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200105 }
106 }
107
108 stage('Generate product StackLight') {
109 if (COOKIECUTTER_INSTALL_STACKLIGHT.toBoolean()) {
110 templateDir = "${templateEnv}/cluster_product/stacklight"
Tomáš Kukrál931f9972017-03-30 23:44:57 +0200111 templateOutputDir = "${env.WORKSPACE}/template/output/stacklight"
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200112 sh "mkdir -p ${templateOutputDir}"
113 python.setupCookiecutterVirtualenv(cutterEnv)
Tomáš Kukrál8e417682017-04-13 09:55:50 +0200114 python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv)
Tomáš Kukrál931f9972017-03-30 23:44:57 +0200115 sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200116 }
117 }
118
119 stage('Generate new SaltMaster node') {
120 def nodeFile = "${modelEnv}/nodes/cfg01.${clusterDomain}.yml"
121 def nodeString = """classes:
122- cluster.${clusterName}.infra.config
123parameters:
124 _param:
125 linux_system_codename: xenial
126 reclass_data_revision: master
127 linux:
128 system:
129 name: cfg01
130 domain: ${clusterDomain}
131"""
Tomáš Kukrál4aac3aa2017-04-04 14:04:02 +0200132 sh "mkdir -p ${modelEnv}/nodes/"
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200133 writeFile(file: nodeFile, text: nodeString)
134 }
135
Tomáš Kukrál6c5957e2017-03-30 18:04:38 +0200136 stage ('Save changes to Reclass model') {
137 if (COMMIT_CHANGES.toBoolean()) {
Tomáš Kukrál4aac3aa2017-04-04 14:04:02 +0200138 git.changeGitBranch(modelEnv, targetBranch)
Tomáš Kukrál6c5957e2017-03-30 18:04:38 +0200139 git.commitGitChanges(modelEnv, "Added new cluster ${clusterName}")
Tomáš Kukrál65f8f752017-03-30 17:51:46 +0200140 git.pushGitChanges(modelEnv, targetBranch, 'origin', RECLASS_MODEL_CREDENTIALS)
141 }
Tomáš Kukrál931f9972017-03-30 23:44:57 +0200142 sh(returnStatus: true, script: "tar -zcvf ${clusterName}.tar.gz -C ${modelEnv} .")
Tomáš Kukrál4c98b582017-03-30 18:49:06 +0200143 archiveArtifacts artifacts: "${clusterName}.tar.gz"
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200144 }
145
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200146 } catch (Throwable e) {
147 // If there was an error or exception thrown, the build failed
148 currentBuild.result = "FAILURE"
149 throw e
150 } finally {
Tomáš Kukrál8b91db92017-03-29 08:30:49 +0200151 stage ('Clean workspace directories') {
Tomáš Kukrál2b983c32017-03-30 18:09:02 +0200152 sh(returnStatus: true, script: "rm -rfv ${templateEnv}")
153 sh(returnStatus: true, script: "rm -rfv ${modelEnv}")
Tomáš Kukrál8b91db92017-03-29 08:30:49 +0200154 }
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200155 // common.sendNotification(currentBuild.result,"",["slack"])
156 }
157 }
158}