blob: 01c91da6fdea6e568d60a2e84ea072886717a519 [file] [log] [blame]
Jakub Josef4780ae62017-04-25 16:17:56 +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 * EMAIl_ADDRESS Email to send a created tar file
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.
13 *
14**/
15
16common = new com.mirantis.mk.Common()
17git = new com.mirantis.mk.Git()
18python = new com.mirantis.mk.Python()
19
20timestamps {
21 node() {
22 def templateEnv = "${env.WORKSPACE}/template"
23 def modelEnv = "${env.WORKSPACE}/model"
24
25 try {
26 def templateContext = readYaml text: COOKIECUTTER_TEMPLATE_CONTEXT
Jakub Josefda18fc42017-04-26 14:14:25 +020027 def templateBaseDir = "${env.WORKSPACE}/template"
Jakub Josef4780ae62017-04-25 16:17:56 +020028 def templateDir = "${templateEnv}/template/dir"
Jakub Josefda18fc42017-04-26 14:14:25 +020029 def templateOutputDir = templateBaseDir
Jakub Josef4780ae62017-04-25 16:17:56 +020030 def cutterEnv = "${env.WORKSPACE}/cutter"
31 def jinjaEnv = "${env.WORKSPACE}/jinja"
32 def clusterName = templateContext.default_context.cluster_name
33 def clusterDomain = templateContext.default_context.cluster_domain
34 def targetBranch = "feature/${clusterName}"
35 def outputDestination = "${modelEnv}/classes/cluster/${clusterName}"
36
37 currentBuild.description = clusterName
38 print("Using context:\n" + COOKIECUTTER_TEMPLATE_CONTEXT)
39
40 stage ('Download Cookiecutter template') {
41 git.checkoutGitRepository(templateEnv, COOKIECUTTER_TEMPLATE_URL, COOKIECUTTER_TEMPLATE_BRANCH, COOKIECUTTER_TEMPLATE_CREDENTIALS)
42 }
43
44 stage ('Download full Reclass model') {
45 if (RECLASS_MODEL_URL != '') {
46 git.checkoutGitRepository(modelEnv, RECLASS_MODEL_URL, RECLASS_MODEL_BRANCH, RECLASS_MODEL_CREDENTIALS)
47 }
48 }
49
50 stage('Generate base infrastructure') {
51 templateDir = "${templateEnv}/cluster_product/infra"
52 templateOutputDir = "${env.WORKSPACE}/template/output/infra"
53 sh "mkdir -p ${templateOutputDir}"
54 sh "mkdir -p ${outputDestination}"
55 python.setupCookiecutterVirtualenv(cutterEnv)
Jakub Josefda18fc42017-04-26 14:14:25 +020056 python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv, templateBaseDir)
Jakub Josef4780ae62017-04-25 16:17:56 +020057 sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
58 }
59
60 stage('Generate product CI/CD') {
61 if (templateContext.default_context.cicd_enabled.toBoolean()) {
62 templateDir = "${templateEnv}/cluster_product/cicd"
63 templateOutputDir = "${env.WORKSPACE}/template/output/cicd"
64 sh "mkdir -p ${templateOutputDir}"
65 python.setupCookiecutterVirtualenv(cutterEnv)
Jakub Josefda18fc42017-04-26 14:14:25 +020066 python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv, templateBaseDir)
Jakub Josef4780ae62017-04-25 16:17:56 +020067 sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
68 }
69 }
70
71 stage('Generate product OpenContrail') {
Jakub Joseff6b556a2017-04-26 18:23:18 +020072 if (templateContext.default_context.opencontrail_enabled.toBoolean()) {
Jakub Josef4780ae62017-04-25 16:17:56 +020073 templateDir = "${templateEnv}/cluster_product/opencontrail"
74 templateOutputDir = "${env.WORKSPACE}/template/output/opencontrail"
75 sh "mkdir -p ${templateOutputDir}"
76 python.setupCookiecutterVirtualenv(cutterEnv)
Jakub Josefda18fc42017-04-26 14:14:25 +020077 python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv, templateBaseDir)
Jakub Josef4780ae62017-04-25 16:17:56 +020078 sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
79 }
80 }
81
82 stage('Generate product Kubernetes') {
83 if (templateContext.default_context.kubernetes_enabled.toBoolean()) {
84 templateDir = "${templateEnv}/cluster_product/kubernetes"
85 templateOutputDir = "${env.WORKSPACE}/template/output/kubernetes"
86 sh "mkdir -p ${templateOutputDir}"
87 python.setupCookiecutterVirtualenv(cutterEnv)
Jakub Josefda18fc42017-04-26 14:14:25 +020088 python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv, templateBaseDir)
Jakub Josef4780ae62017-04-25 16:17:56 +020089 sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
90 }
91 }
92
93 stage('Generate product OpenStack') {
94 if (templateContext.default_context.openstack_enabled.toBoolean()) {
95 templateDir = "${templateEnv}/cluster_product/openstack"
96 templateOutputDir = "${env.WORKSPACE}/template/output/openstack"
97 sh "mkdir -p ${templateOutputDir}"
98 python.setupCookiecutterVirtualenv(cutterEnv)
Jakub Josefda18fc42017-04-26 14:14:25 +020099 python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv, templateBaseDir)
Jakub Josef4780ae62017-04-25 16:17:56 +0200100 sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
101 }
102 }
103
104 stage('Generate product StackLight') {
105 if (templateContext.default_context.stacklight_enabled.toBoolean()) {
106 templateDir = "${templateEnv}/cluster_product/stacklight"
107 templateOutputDir = "${env.WORKSPACE}/template/output/stacklight"
108 sh "mkdir -p ${templateOutputDir}"
109 python.setupCookiecutterVirtualenv(cutterEnv)
Jakub Josefda18fc42017-04-26 14:14:25 +0200110 python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv, templateBaseDir)
Jakub Josef4780ae62017-04-25 16:17:56 +0200111 sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
112 }
113 }
114
115 stage('Generate new SaltMaster node') {
116 def nodeFile = "${modelEnv}/nodes/cfg01.${clusterDomain}.yml"
117 def nodeString = """classes:
118- cluster.${clusterName}.infra.config
119parameters:
120 _param:
121 linux_system_codename: xenial
122 reclass_data_revision: master
123 linux:
124 system:
125 name: cfg01
126 domain: ${clusterDomain}
127"""
128 sh "mkdir -p ${modelEnv}/nodes/"
129 writeFile(file: nodeFile, text: nodeString)
130 }
131
132 stage ('Save changes to Reclass model') {
133 if (RECLASS_MODEL_URL != null && RECLASS_MODEL_URL != "") {
134 git.changeGitBranch(modelEnv, targetBranch)
135 git.commitGitChanges(modelEnv, "Added new cluster ${clusterName}")
136 git.pushGitChanges(modelEnv, targetBranch, 'origin', RECLASS_MODEL_CREDENTIALS)
137 }
138 sh(returnStatus: true, script: "tar -zcvf ${clusterName}.tar.gz -C ${modelEnv} .")
139 archiveArtifacts artifacts: "${clusterName}.tar.gz"
Jakub Josef7d854222017-04-26 18:27:01 +0200140 if(EMAIl_ADDRESS != null && EMAIL_ADDRESS){
Jakub Josef565804a2017-04-27 14:43:46 +0200141 emailext(to: EMAIL_ADDRESS, attachmentsPattern: "^${clusterName}\\.tar\\.gz\$", body: "Mirantis Jenkins\n\nRequested reclass model ${clusterName} created and has been attached to this email.\nEnjoy!\n\nMirantis Inc.", subject: "Your Salt model ${clusterName}")
Jakub Josef4780ae62017-04-25 16:17:56 +0200142 }
143 }
144
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 {
150 stage ('Clean workspace directories') {
151 sh(returnStatus: true, script: "rm -rfv ${templateEnv}")
152 sh(returnStatus: true, script: "rm -rfv ${modelEnv}")
153 }
154 // common.sendNotification(currentBuild.result,"",["slack"])
155 }
156 }
157}