blob: 39590e5ae8823b414a3171d988f95138c82430fc [file] [log] [blame]
Tomáš Kukrál7ded3642017-03-27 15:52:51 +02001/**
2 * Generate cookiecutter cluster by individual products
3 *
4 * Expected parameters:
Tomáš Kukrál7ded3642017-03-27 15:52:51 +02005 * COOKIECUTTER_TEMPLATE_CONTEXT Context parameters for the template generation.
Tomáš Kukrál91e49252017-05-09 14:40:26 +02006 * EMAIL_ADDRESS Email to send a created tar file
Tomáš Kukrál7ded3642017-03-27 15:52:51 +02007 *
8**/
9
Leontii Istomin9adf9182018-02-23 13:16:20 +010010// Deprecation to avoid unexpected behaviour because it should be passed via initial context.
11// Need to delete this "if" statement at 1 April 2018.
12if(env.COOKIECUTTER_TEMPLATE_CREDENTIALS ||
13 env.COOKIECUTTER_TEMPLATE_URL ||
14 env.COOKIECUTTER_TEMPLATE_BRANCH ||
15 env.COOKIECUTTER_TEMPLATE_PATH ||
16 env.SHARED_RECLASS_URL){
17 println '''
18 DEPRECATION: Please note that the following variables are deprocated:
19 - COOKIECUTTER_TEMPLATE_CREDENTIALS
20 - COOKIECUTTER_TEMPLATE_URL
21 - COOKIECUTTER_TEMPLATE_BRANCH
22 - COOKIECUTTER_TEMPLATE_PATH
23 - SHARED_RECLASS_URL
24 You need to pass the values using the following variables from initial cookiecutter context:
25 - cookiecutter_template_url
26 - cookiecutter_template_branch
27 - shared_reclass_url
28 The following variables are not needed anymore:
29 - COOKIECUTTER_TEMPLATE_CREDENTIALS - cookiecutter-templates repos are accessible for anounimous
30 (https://gerrit.mcp.mirantis.net)
31 - COOKIECUTTER_TEMPLATE_PATH - hardcoded to "${env.WORKSPACE}/template"
32 '''
33 currentBuild.result = "FAILURE"
34 return
35}
36
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020037common = new com.mirantis.mk.Common()
38git = new com.mirantis.mk.Git()
39python = new com.mirantis.mk.Python()
chnyda89191012017-05-29 15:38:35 +020040saltModelTesting = new com.mirantis.mk.SaltModelTesting()
Tomáš Kukrálcebc1a02017-07-25 16:10:37 +020041ssh = new com.mirantis.mk.Ssh()
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020042
Jakub Josefa63f9862018-01-11 17:58:38 +010043timeout(time: 12, unit: 'HOURS') {
44 node("python&&docker") {
45 def templateEnv = "${env.WORKSPACE}/template"
46 def modelEnv = "${env.WORKSPACE}/model"
47 def testEnv = "${env.WORKSPACE}/test"
48 def pipelineEnv = "${env.WORKSPACE}/pipelines"
Tomáš Kukrál9f6260f2017-03-29 23:58:26 +020049
Jakub Josefa63f9862018-01-11 17:58:38 +010050 try {
51 def templateContext = readYaml text: COOKIECUTTER_TEMPLATE_CONTEXT
Leontii Istomin9adf9182018-02-23 13:16:20 +010052 def mcpVersion = templateContext.default_context.mcp_version
53 def sharedReclassUrl = templateContext.default_context.shared_reclass_url
Jakub Josefa63f9862018-01-11 17:58:38 +010054 def clusterDomain = templateContext.default_context.cluster_domain
55 def clusterName = templateContext.default_context.cluster_name
56 def saltMaster = templateContext.default_context.salt_master_hostname
57 def cutterEnv = "${env.WORKSPACE}/cutter"
58 def jinjaEnv = "${env.WORKSPACE}/jinja"
59 def outputDestination = "${modelEnv}/classes/cluster/${clusterName}"
Leontii Istomin9adf9182018-02-23 13:16:20 +010060 def systemEnv = "${modelEnv}/classes/system"
Jakub Josefa63f9862018-01-11 17:58:38 +010061 def targetBranch = "feature/${clusterName}"
62 def templateBaseDir = "${env.WORKSPACE}/template"
63 def templateDir = "${templateEnv}/template/dir"
64 def templateOutputDir = templateBaseDir
65 def user
66 wrap([$class: 'BuildUser']) {
67 user = env.BUILD_USER_ID
Tomáš Kukrála3f4ba72017-08-03 15:40:22 +020068 }
69
Jakub Josefa63f9862018-01-11 17:58:38 +010070 currentBuild.description = clusterName
71 print("Using context:\n" + COOKIECUTTER_TEMPLATE_CONTEXT)
Tomáš Kukrál0c6835d2017-07-18 16:00:27 +020072
Jakub Josefa63f9862018-01-11 17:58:38 +010073 stage ('Download Cookiecutter template') {
Leontii Istomin9adf9182018-02-23 13:16:20 +010074 def cookiecutterTemplateUrl = templateContext.default_context.cookiecutter_template_url
75 def cookiecutterTemplateBranch = templateContext.default_context.cookiecutter_template_branch
76 git.checkoutGitRepository(templateEnv, cookiecutterTemplateUrl, 'master')
77 // Use refspec if exists first of all
78 if (cookiecutterTemplateBranch.toString().startsWith('refs/')) {
Jakub Josefa63f9862018-01-11 17:58:38 +010079 dir(templateEnv) {
Leontii Istomin9adf9182018-02-23 13:16:20 +010080 ssh.agentSh("git fetch ${cookiecutterTemplateUrl} ${cookiecutterTemplateBranch} && git checkout FETCH_HEAD")
Jakub Josefa63f9862018-01-11 17:58:38 +010081 }
82 } else {
Leontii Istomin9adf9182018-02-23 13:16:20 +010083 // Use mcpVersion git tag if not specified branch for cookiecutter-templates
84 if (cookiecutterTemplateBranch == '') {
85 cookiecutterTemplateBranch = mcpVersion
86 // Don't have nightly/testing/stable for cookiecutter-templates repo, therefore use master
87 if(mcpVersion == "nightly" || mcpVersion == "testing" || mcpVersion == "stable"){
88 cookiecutterTemplateBranch = 'master'
89 }
90 }
91 git.changeGitBranch(templateEnv, cookiecutterTemplateBranch)
Jakub Josefa63f9862018-01-11 17:58:38 +010092 }
Jakub Josefa63f9862018-01-11 17:58:38 +010093 }
94
95 stage ('Create empty reclass model') {
96 dir(path: modelEnv) {
97 sh "rm -rfv .git"
98 sh "git init"
Leontii Istomin4bf8bc72018-03-02 10:36:39 +010099 ssh.agentSh("git submodule add ${sharedReclassUrl} 'classes/system'")
Jakub Josefa63f9862018-01-11 17:58:38 +0100100 }
Leontii Istomin9adf9182018-02-23 13:16:20 +0100101 def sharedReclassBranch = templateContext.default_context.shared_reclass_branch
102 // Use refspec if exists first of all
103 if (sharedReclassBranch.toString().startsWith('refs/')) {
104 dir(systemEnv) {
105 ssh.agentSh("git fetch ${sharedReclassUrl} ${sharedReclassBranch} && git checkout FETCH_HEAD")
106 }
107 } else {
108 // Use mcpVersion git tag if not specified branch for reclass-system
109 if (sharedReclassBranch == '') {
110 sharedReclassBranch = mcpVersion
111 // Don't have nightly/testing/stable for reclass-system repo, therefore use master
112 if(mcpVersion == "nightly" || mcpVersion == "testing" || mcpVersion == "stable"){
113 sharedReclassBranch = 'master'
114 }
115 }
116 git.changeGitBranch(systemEnv, sharedReclassBranch)
117 }
118 git.commitGitChanges(modelEnv, "Added new shared reclass submodule", "${user}@localhost", "${user}")
Jakub Josefa63f9862018-01-11 17:58:38 +0100119 }
120
121 def productList = ["infra", "cicd", "opencontrail", "kubernetes", "openstack", "oss", "stacklight", "ceph"]
122 for (product in productList) {
123
124 // get templateOutputDir and productDir
125 if (product.startsWith("stacklight")) {
126 templateOutputDir = "${env.WORKSPACE}/output/stacklight"
127
128 def stacklightVersion
129 try {
130 stacklightVersion = templateContext.default_context['stacklight_version']
131 } catch (Throwable e) {
132 common.warningMsg('Stacklight version loading failed')
Richard Felkl9543faa2018-01-11 09:47:35 +0100133 }
134
Jakub Josefa63f9862018-01-11 17:58:38 +0100135 if (stacklightVersion) {
136 productDir = "stacklight" + stacklightVersion
137 } else {
138 productDir = "stacklight1"
139 }
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200140
Tomáš Kukrála3f4ba72017-08-03 15:40:22 +0200141 } else {
Jakub Josefa63f9862018-01-11 17:58:38 +0100142 templateOutputDir = "${env.WORKSPACE}/output/${product}"
143 productDir = product
Tomáš Kukrál9a6821e2017-07-24 11:07:01 +0200144 }
Tomáš Kukrála3f4ba72017-08-03 15:40:22 +0200145
Jakub Josefa63f9862018-01-11 17:58:38 +0100146 if (product == "infra" || (templateContext.default_context["${product}_enabled"]
147 && templateContext.default_context["${product}_enabled"].toBoolean())) {
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200148
Jakub Josefa63f9862018-01-11 17:58:38 +0100149 templateDir = "${templateEnv}/cluster_product/${productDir}"
150 common.infoMsg("Generating product " + product + " from " + templateDir + " to " + templateOutputDir)
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400151
Jakub Josefa63f9862018-01-11 17:58:38 +0100152 sh "rm -rf ${templateOutputDir} || true"
153 sh "mkdir -p ${templateOutputDir}"
154 sh "mkdir -p ${outputDestination}"
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400155
Jakub Josefa63f9862018-01-11 17:58:38 +0100156 python.setupCookiecutterVirtualenv(cutterEnv)
157 python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv, templateBaseDir)
158 sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
Jiri Broulikd1375f72018-01-09 10:14:59 +0100159 } else {
Jakub Josefa63f9862018-01-11 17:58:38 +0100160 common.warningMsg("Product " + product + " is disabled")
Jiri Broulikd1375f72018-01-09 10:14:59 +0100161 }
162 }
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400163
Jakub Josefa63f9862018-01-11 17:58:38 +0100164 stage('Generate new SaltMaster node') {
165 def nodeFile = "${modelEnv}/nodes/${saltMaster}.${clusterDomain}.yml"
166 def nodeString = """classes:
Leontii Istomin56b6b112018-01-15 12:14:24 +0300167- cluster.${clusterName}.infra.config
168parameters:
169 _param:
170 linux_system_codename: xenial
171 reclass_data_revision: master
172 linux:
173 system:
174 name: ${saltMaster}
175 domain: ${clusterDomain}
Jakub Josefa63f9862018-01-11 17:58:38 +0100176 """
177 sh "mkdir -p ${modelEnv}/nodes/"
178 writeFile(file: nodeFile, text: nodeString)
179
180 git.commitGitChanges(modelEnv, "Create model ${clusterName}", "${user}@localhost", "${user}")
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400181 }
182
Jakub Josefa63f9862018-01-11 17:58:38 +0100183 stage("Test") {
Leontii Istomin9adf9182018-02-23 13:16:20 +0100184 if (sharedReclassUrl != "" && TEST_MODEL && TEST_MODEL.toBoolean()) {
Jakub Josefa63f9862018-01-11 17:58:38 +0100185 sh("cp -r ${modelEnv} ${testEnv}")
186 saltModelTesting.setupAndTestNode("${saltMaster}.${clusterDomain}", "", testEnv)
187 }
188 }
Richard Felkl9fd47942017-10-20 16:23:29 +0200189
Jakub Josefa63f9862018-01-11 17:58:38 +0100190 stage("Generate config drives") {
191 // apt package genisoimage is required for this stage
Richard Felkl9fd47942017-10-20 16:23:29 +0200192
Jakub Josefa63f9862018-01-11 17:58:38 +0100193 // download create-config-drive
194 // FIXME: that should be refactored, to use git clone - to be able download it from custom repo.
195 def config_drive_script_url = "https://raw.githubusercontent.com/Mirantis/mcp-common-scripts/master/config-drive/create_config_drive.sh"
196 def user_data_script_url = "https://raw.githubusercontent.com/Mirantis/mcp-common-scripts/master/config-drive/master_config.sh"
Richard Felkl9fd47942017-10-20 16:23:29 +0200197
Jakub Josefa63f9862018-01-11 17:58:38 +0100198 sh "wget -O create-config-drive ${config_drive_script_url} && chmod +x create-config-drive"
199 sh "wget -O user_data.sh ${user_data_script_url}"
Richard Felkl9fd47942017-10-20 16:23:29 +0200200
Jakub Josefa63f9862018-01-11 17:58:38 +0100201 sh "git clone --mirror https://github.com/Mirantis/mk-pipelines.git ${pipelineEnv}/mk-pipelines"
202 sh "git clone --mirror https://github.com/Mirantis/pipeline-library.git ${pipelineEnv}/pipeline-library"
203 args = "--user-data user_data.sh --hostname ${saltMaster} --model ${modelEnv} --mk-pipelines ${pipelineEnv}/mk-pipelines/ --pipeline-library ${pipelineEnv}/pipeline-library/ ${saltMaster}.${clusterDomain}-config.iso"
204
205 // load data from model
206 def smc = [:]
207 smc['SALT_MASTER_MINION_ID'] = "${saltMaster}.${clusterDomain}"
208 smc['SALT_MASTER_DEPLOY_IP'] = templateContext['default_context']['salt_master_management_address']
209 smc['DEPLOY_NETWORK_GW'] = templateContext['default_context']['deploy_network_gateway']
210 smc['DEPLOY_NETWORK_NETMASK'] = templateContext['default_context']['deploy_network_netmask']
211 smc['DNS_SERVERS'] = templateContext['default_context']['dns_server01']
212 if (templateContext['default_context']['local_repositories'] == 'True'){
213 smc['PIPELINES_FROM_ISO'] = 'false'
214 smc['PIPELINE_REPO_URL'] = 'http://' + templateContext['default_context']['aptly_server_deploy_address'] + ':8088'
215 }
216 if (templateContext['default_context']['upstream_proxy_enabled'] == 'True'){
217 if (templateContext['default_context']['upstream_proxy_auth_enabled'] == 'True'){
218 smc['http_proxy'] = 'http://' + templateContext['default_context']['upstream_proxy_user'] + ':' + templateContext['default_context']['upstream_proxy_password'] + '@' + templateContext['default_context']['upstream_proxy_address'] + ':' + templateContext['default_context']['upstream_proxy_port']
219 smc['https_proxy'] = 'http://' + templateContext['default_context']['upstream_proxy_user'] + ':' + templateContext['default_context']['upstream_proxy_password'] + '@' + templateContext['default_context']['upstream_proxy_address'] + ':' + templateContext['default_context']['upstream_proxy_port']
220 } else {
221 smc['http_proxy'] = 'http://' + templateContext['default_context']['upstream_proxy_address'] + ':' + templateContext['default_context']['upstream_proxy_port']
222 smc['https_proxy'] = 'http://' + templateContext['default_context']['upstream_proxy_address'] + ':' + templateContext['default_context']['upstream_proxy_port']
223 }
Richard Felkl9fd47942017-10-20 16:23:29 +0200224 }
225
Jakub Josefa63f9862018-01-11 17:58:38 +0100226 for (i in common.entries(smc)) {
227 sh "sed -i \"s,export ${i[0]}=.*,export ${i[0]}=${i[1]},\" user_data.sh"
228 }
Richard Felkl9fd47942017-10-20 16:23:29 +0200229
Jakub Josefa63f9862018-01-11 17:58:38 +0100230 // create cfg config-drive
231 sh "./create-config-drive ${args}"
232 sh("mkdir output-${clusterName} && mv ${saltMaster}.${clusterDomain}-config.iso output-${clusterName}/")
233
234 // save cfg iso to artifacts
235 archiveArtifacts artifacts: "output-${clusterName}/${saltMaster}.${clusterDomain}-config.iso"
236
237 if (templateContext['default_context']['local_repositories'] == 'True'){
238 def aptlyServerHostname = templateContext.default_context.aptly_server_hostname
239 def user_data_script_apt_url = "https://raw.githubusercontent.com/Mirantis/mcp-common-scripts/master/config-drive/mirror_config.sh"
240 sh "wget -O mirror_config.sh ${user_data_script_apt_url}"
241
242 def smc_apt = [:]
243 smc_apt['SALT_MASTER_DEPLOY_IP'] = templateContext['default_context']['salt_master_management_address']
244 smc_apt['APTLY_DEPLOY_IP'] = templateContext['default_context']['aptly_server_deploy_address']
245 smc_apt['APTLY_DEPLOY_NETMASK'] = templateContext['default_context']['deploy_network_netmask']
246 smc_apt['APTLY_MINION_ID'] = "${aptlyServerHostname}.${clusterDomain}"
247
248 for (i in common.entries(smc_apt)) {
249 sh "sed -i \"s,export ${i[0]}=.*,export ${i[0]}=${i[1]},\" mirror_config.sh"
250 }
251
252 // create apt config-drive
253 sh "./create-config-drive --user-data mirror_config.sh --hostname ${aptlyServerHostname} ${aptlyServerHostname}.${clusterDomain}-config.iso"
254 sh("mv ${aptlyServerHostname}.${clusterDomain}-config.iso output-${clusterName}/")
255
256 // save apt iso to artifacts
257 archiveArtifacts artifacts: "output-${clusterName}/${aptlyServerHostname}.${clusterDomain}-config.iso"
258 }
Richard Felkl9fd47942017-10-20 16:23:29 +0200259 }
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400260
Jakub Josefa63f9862018-01-11 17:58:38 +0100261 stage ('Save changes reclass model') {
262 sh(returnStatus: true, script: "tar -zcf output-${clusterName}/${clusterName}.tar.gz -C ${modelEnv} .")
263 archiveArtifacts artifacts: "output-${clusterName}/${clusterName}.tar.gz"
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400264
265
Jakub Josefa63f9862018-01-11 17:58:38 +0100266 if (EMAIL_ADDRESS != null && EMAIL_ADDRESS != "") {
267 emailext(to: EMAIL_ADDRESS,
268 attachmentsPattern: "output-${clusterName}/*",
269 body: "Mirantis Jenkins\n\nRequested reclass model ${clusterName} has been created and attached to this email.\nEnjoy!\n\nMirantis",
270 subject: "Your Salt model ${clusterName}")
271 }
272 dir("output-${clusterName}"){
273 deleteDir()
274 }
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400275 }
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400276
Jakub Josefa63f9862018-01-11 17:58:38 +0100277 } catch (Throwable e) {
278 // If there was an error or exception thrown, the build failed
279 currentBuild.result = "FAILURE"
280 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
281 throw e
282 } finally {
283 stage ('Clean workspace directories') {
284 sh(returnStatus: true, script: "rm -rf ${templateEnv}")
285 sh(returnStatus: true, script: "rm -rf ${modelEnv}")
286 sh(returnStatus: true, script: "rm -rf ${pipelineEnv}")
287 }
288 // common.sendNotification(currentBuild.result,"",["slack"])
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400289 }
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200290 }
Mikhail Ivanov9f812922017-11-07 18:52:02 +0400291}