blob: 6e65f5eb5360a62cde3998ea546541ca3f503ec0 [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 *
azvyagintsev3ed704f2018-07-09 15:49:27 +03008 **/
Tomáš Kukrál7ded3642017-03-27 15:52:51 +02009
10common = new com.mirantis.mk.Common()
11git = new com.mirantis.mk.Git()
12python = new com.mirantis.mk.Python()
chnyda89191012017-05-29 15:38:35 +020013saltModelTesting = new com.mirantis.mk.SaltModelTesting()
Tomáš Kukrálcebc1a02017-07-25 16:10:37 +020014ssh = new com.mirantis.mk.Ssh()
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020015
azvyagintsevb8ddb492018-09-12 14:59:45 +030016reclassVersion = env.RECLASS_VERSION ?: 'v1.5.4'
17slaveNode = env.SLAVE_NODE ?: 'python&&docker'
azvyagintsevf252b592018-08-13 18:39:14 +030018
19timeout(time: 2, unit: 'HOURS') {
azvyagintsev636493c2018-09-12 17:17:05 +030020 node(slaveNode) {
21 def templateEnv = "${env.WORKSPACE}/template"
22 def modelEnv = "${env.WORKSPACE}/model"
23 def testEnv = "${env.WORKSPACE}/test"
24 def pipelineEnv = "${env.WORKSPACE}/pipelines"
Tomáš Kukrál9f6260f2017-03-29 23:58:26 +020025
azvyagintsev636493c2018-09-12 17:17:05 +030026 try {
27 def templateContext = readYaml text: COOKIECUTTER_TEMPLATE_CONTEXT
28 def mcpVersion = templateContext.default_context.mcp_version
29 def sharedReclassUrl = templateContext.default_context.shared_reclass_url
30 def clusterDomain = templateContext.default_context.cluster_domain
31 def clusterName = templateContext.default_context.cluster_name
32 def saltMaster = templateContext.default_context.salt_master_hostname
33 def localRepositories = templateContext.default_context.local_repositories.toBoolean()
34 def offlineDeployment = templateContext.default_context.offline_deployment.toBoolean()
35 def cutterEnv = "${env.WORKSPACE}/cutter"
36 def jinjaEnv = "${env.WORKSPACE}/jinja"
37 def outputDestination = "${modelEnv}/classes/cluster/${clusterName}"
38 def systemEnv = "${modelEnv}/classes/system"
39 def targetBranch = "feature/${clusterName}"
40 def templateBaseDir = "${env.WORKSPACE}/template"
41 def templateDir = "${templateEnv}/template/dir"
42 def templateOutputDir = templateBaseDir
43 def user
44 def testResult = false
45 wrap([$class: 'BuildUser']) {
46 user = env.BUILD_USER_ID
Tomáš Kukrála3f4ba72017-08-03 15:40:22 +020047 }
48
azvyagintsev636493c2018-09-12 17:17:05 +030049 currentBuild.description = clusterName
50 print("Using context:\n" + COOKIECUTTER_TEMPLATE_CONTEXT)
Richard Felkle77b0912018-01-31 17:12:23 +010051
azvyagintsev636493c2018-09-12 17:17:05 +030052 stage('Download Cookiecutter template') {
53 sh(script: 'find . -mindepth 1 -delete > /dev/null || true')
54 def cookiecutterTemplateUrl = templateContext.default_context.cookiecutter_template_url
55 def cookiecutterTemplateBranch = templateContext.default_context.cookiecutter_template_branch
56 git.checkoutGitRepository(templateEnv, cookiecutterTemplateUrl, 'master')
57 // Use refspec if exists first of all
58 if (cookiecutterTemplateBranch.toString().startsWith('refs/')) {
59 dir(templateEnv) {
60 ssh.agentSh("git fetch ${cookiecutterTemplateUrl} ${cookiecutterTemplateBranch} && git checkout FETCH_HEAD")
61 }
62 } else {
63 // Use mcpVersion git tag if not specified branch for cookiecutter-templates
64 if (cookiecutterTemplateBranch == '') {
65 cookiecutterTemplateBranch = mcpVersion
66 // Don't have nightly/testing/stable for cookiecutter-templates repo, therefore use master
67 if (["nightly", "testing", "stable"].contains(mcpVersion)) {
68 cookiecutterTemplateBranch = 'master'
69 }
70 }
71 git.changeGitBranch(templateEnv, cookiecutterTemplateBranch)
72 }
73 }
Jakub Josefa63f9862018-01-11 17:58:38 +010074
azvyagintsev636493c2018-09-12 17:17:05 +030075 stage('Create empty reclass model') {
76 dir(path: modelEnv) {
77 sh "rm -rfv .git"
78 sh "git init"
79 ssh.agentSh("git submodule add ${sharedReclassUrl} 'classes/system'")
80 }
Jakub Josefa63f9862018-01-11 17:58:38 +010081
azvyagintsev636493c2018-09-12 17:17:05 +030082 def sharedReclassBranch = templateContext.default_context.shared_reclass_branch
83 // Use refspec if exists first of all
84 if (sharedReclassBranch.toString().startsWith('refs/')) {
85 dir(systemEnv) {
86 ssh.agentSh("git fetch ${sharedReclassUrl} ${sharedReclassBranch} && git checkout FETCH_HEAD")
87 }
88 } else {
89 // Use mcpVersion git tag if not specified branch for reclass-system
90 if (sharedReclassBranch == '') {
91 sharedReclassBranch = mcpVersion
92 // Don't have nightly/testing for reclass-system repo, therefore use master
93 if (["nightly", "testing", "stable"].contains(mcpVersion)) {
94 common.warningMsg("Fetching reclass-system from master!")
95 sharedReclassBranch = 'master'
96 }
97 }
98 git.changeGitBranch(systemEnv, sharedReclassBranch)
99 }
100 git.commitGitChanges(modelEnv, "Added new shared reclass submodule", "${user}@localhost", "${user}")
101 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100102
azvyagintsev636493c2018-09-12 17:17:05 +0300103 def productList = ["infra", "cicd", "opencontrail", "kubernetes", "openstack", "oss", "stacklight", "ceph"]
104 for (product in productList) {
Richard Felkl9543faa2018-01-11 09:47:35 +0100105
azvyagintsev636493c2018-09-12 17:17:05 +0300106 // get templateOutputDir and productDir
107 if (product.startsWith("stacklight")) {
108 templateOutputDir = "${env.WORKSPACE}/output/stacklight"
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200109
azvyagintsev636493c2018-09-12 17:17:05 +0300110 def stacklightVersion
111 try {
112 stacklightVersion = templateContext.default_context['stacklight_version']
113 } catch (Throwable e) {
114 common.warningMsg('Stacklight version loading failed')
115 }
Tomáš Kukrála3f4ba72017-08-03 15:40:22 +0200116
azvyagintsev636493c2018-09-12 17:17:05 +0300117 if (stacklightVersion) {
118 productDir = "stacklight" + stacklightVersion
119 } else {
120 productDir = "stacklight1"
121 }
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200122
azvyagintsev636493c2018-09-12 17:17:05 +0300123 } else {
124 templateOutputDir = "${env.WORKSPACE}/output/${product}"
125 productDir = product
126 }
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400127
azvyagintsev636493c2018-09-12 17:17:05 +0300128 if (product == "infra" || (templateContext.default_context["${product}_enabled"]
129 && templateContext.default_context["${product}_enabled"].toBoolean())) {
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400130
azvyagintsev636493c2018-09-12 17:17:05 +0300131 templateDir = "${templateEnv}/cluster_product/${productDir}"
132 common.infoMsg("Generating product " + product + " from " + templateDir + " to " + templateOutputDir)
133
134 sh "rm -rf ${templateOutputDir} || true"
135 sh "mkdir -p ${templateOutputDir}"
136 sh "mkdir -p ${outputDestination}"
137
138 python.setupCookiecutterVirtualenv(cutterEnv)
139 python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv, templateBaseDir)
140 sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
141 } else {
142 common.warningMsg("Product " + product + " is disabled")
143 }
144 }
145
146 if (localRepositories && !offlineDeployment) {
147 def aptlyModelUrl = templateContext.default_context.local_model_url
148 dir(path: modelEnv) {
149 ssh.agentSh "git submodule add \"${aptlyModelUrl}\" \"classes/cluster/${clusterName}/cicd/aptly\""
150 if (!(mcpVersion in ["nightly", "testing", "stable"])) {
151 ssh.agentSh "cd \"classes/cluster/${clusterName}/cicd/aptly\";git fetch --tags;git checkout ${mcpVersion}"
152 }
153 }
154 }
155
156 stage('Generate new SaltMaster node') {
157 def nodeFile = "${modelEnv}/nodes/${saltMaster}.${clusterDomain}.yml"
158 def nodeString = """classes:
Leontii Istomin56b6b112018-01-15 12:14:24 +0300159- cluster.${clusterName}.infra.config
160parameters:
161 _param:
162 linux_system_codename: xenial
163 reclass_data_revision: master
164 linux:
165 system:
166 name: ${saltMaster}
167 domain: ${clusterDomain}
Jakub Josefa63f9862018-01-11 17:58:38 +0100168 """
azvyagintsev636493c2018-09-12 17:17:05 +0300169 sh "mkdir -p ${modelEnv}/nodes/"
170 writeFile(file: nodeFile, text: nodeString)
Jakub Josefa63f9862018-01-11 17:58:38 +0100171
azvyagintsev636493c2018-09-12 17:17:05 +0300172 git.commitGitChanges(modelEnv, "Create model ${clusterName}", "${user}@localhost", "${user}")
173 }
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400174
azvyagintsev636493c2018-09-12 17:17:05 +0300175 stage("Test") {
176 if (TEST_MODEL.toBoolean() && sharedReclassUrl != '') {
177 sh("cp -r ${modelEnv} ${testEnv}")
178 def DockerCName = "${env.JOB_NAME.toLowerCase()}_${env.BUILD_TAG.toLowerCase()}"
179 common.infoMsg("Attempt to run test against formula-version: ${mcpVersion}")
Denis Egorenko032b8ca2018-09-13 17:00:23 +0400180 try {
181 def config = [
182 'dockerHostname': "${saltMaster}.${clusterDomain}",
183 'reclassEnv': testEnv,
184 'formulasRevision': mcpVersion,
185 'reclassVersion': reclassVersion,
186 'dockerContainerName': DockerCName,
187 'testContext': 'salt-model-node'
188 ]
189 testResult = saltModelTesting.testNode(config)
azvyagintsev636493c2018-09-12 17:17:05 +0300190 common.infoMsg("Test finished: SUCCESS")
Denis Egorenko032b8ca2018-09-13 17:00:23 +0400191 } catch (Exception ex) {
192 common.warningMsg("Test finished: FAILED")
193 testResult = false
azvyagintsev636493c2018-09-12 17:17:05 +0300194 }
195 } else {
196 common.warningMsg("Test stage has been skipped!")
197 }
198 }
199 stage("Generate config drives") {
200 // apt package genisoimage is required for this stage
201
202 // download create-config-drive
203 // FIXME: that should be refactored, to use git clone - to be able download it from custom repo.
Ivan Berezovskiy95ec9272018-09-18 16:41:18 +0400204 def mcpCommonScriptsBranch = templateContext['default_context']['mcp_common_scripts_branch']
azvyagintsev636493c2018-09-12 17:17:05 +0300205 if (mcpCommonScriptsBranch == '') {
206 mcpCommonScriptsBranch = mcpVersion
207 // Don't have n/t/s for mcp-common-scripts repo, therefore use master
208 if (["nightly", "testing", "stable"].contains(mcpVersion)) {
209 common.warningMsg("Fetching mcp-common-scripts from master!")
210 mcpCommonScriptsBranch = 'master'
211 }
212 }
Ivan Berezovskiy95ec9272018-09-18 16:41:18 +0400213
214 def commonScriptsRepoUrl = 'https://gerrit.mcp.mirantis.net/mcp/mcp-common-scripts'
215 checkout([
216 $class: 'GitSCM',
217 branches: [[name: 'FETCH_HEAD'],],
218 extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'mcp-common-scripts']],
219 userRemoteConfigs: [[url: commonScriptsRepoUrl, refspec: mcpCommonScriptsBranch],],
220 ])
221
222 sh "cp mcp-common-scripts/config-drive/create_config_drive.sh create-config-drive && chmod +x create-config-drive"
Ivan Berezovskiy3379c7a2018-09-14 19:02:46 +0400223 sh "[ -f mcp-common-scripts/config-drive/master_config.sh ] && cp mcp-common-scripts/config-drive/master_config.sh user_data || cp mcp-common-scripts/config-drive/master_config.yaml user_data"
azvyagintsev636493c2018-09-12 17:17:05 +0300224
225 sh "git clone --mirror https://github.com/Mirantis/mk-pipelines.git ${pipelineEnv}/mk-pipelines"
226 sh "git clone --mirror https://github.com/Mirantis/pipeline-library.git ${pipelineEnv}/pipeline-library"
Ivan Berezovskiy3379c7a2018-09-14 19:02:46 +0400227 args = "--user-data user_data --hostname ${saltMaster} --model ${modelEnv} --mk-pipelines ${pipelineEnv}/mk-pipelines/ --pipeline-library ${pipelineEnv}/pipeline-library/ ${saltMaster}.${clusterDomain}-config.iso"
azvyagintsev636493c2018-09-12 17:17:05 +0300228
229 // load data from model
230 def smc = [:]
231 smc['SALT_MASTER_MINION_ID'] = "${saltMaster}.${clusterDomain}"
232 smc['SALT_MASTER_DEPLOY_IP'] = templateContext['default_context']['salt_master_management_address']
233 smc['DEPLOY_NETWORK_GW'] = templateContext['default_context']['deploy_network_gateway']
234 smc['DEPLOY_NETWORK_NETMASK'] = templateContext['default_context']['deploy_network_netmask']
235 if (templateContext['default_context'].get('deploy_network_mtu')) {
236 smc['DEPLOY_NETWORK_MTU'] = templateContext['default_context']['deploy_network_mtu']
237 }
238 smc['DNS_SERVERS'] = templateContext['default_context']['dns_server01']
239 smc['MCP_VERSION'] = "${mcpVersion}"
240 if (templateContext['default_context']['local_repositories'] == 'True') {
241 def localRepoIP = templateContext['default_context']['local_repo_url']
242 smc['MCP_SALT_REPO_KEY'] = "http://${localRepoIP}/public.gpg"
243 smc['MCP_SALT_REPO_URL'] = "http://${localRepoIP}/ubuntu-xenial"
244 smc['PIPELINES_FROM_ISO'] = 'false'
245 smc['PIPELINE_REPO_URL'] = "http://${localRepoIP}:8088"
246 smc['LOCAL_REPOS'] = 'true'
247 }
248 if (templateContext['default_context']['upstream_proxy_enabled'] == 'True') {
249 if (templateContext['default_context']['upstream_proxy_auth_enabled'] == 'True') {
250 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']
251 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']
252 } else {
253 smc['http_proxy'] = 'http://' + templateContext['default_context']['upstream_proxy_address'] + ':' + templateContext['default_context']['upstream_proxy_port']
254 smc['https_proxy'] = 'http://' + templateContext['default_context']['upstream_proxy_address'] + ':' + templateContext['default_context']['upstream_proxy_port']
255 }
256 }
257
258 for (i in common.entries(smc)) {
Ivan Berezovskiy3379c7a2018-09-14 19:02:46 +0400259 sh "sed -i 's,${i[0]}=.*,${i[0]}=${i[1]},' user_data"
azvyagintsev636493c2018-09-12 17:17:05 +0300260 }
261
262 // create cfg config-drive
263 sh "./create-config-drive ${args}"
264 sh("mkdir output-${clusterName} && mv ${saltMaster}.${clusterDomain}-config.iso output-${clusterName}/")
265
266 // save cfg iso to artifacts
267 archiveArtifacts artifacts: "output-${clusterName}/${saltMaster}.${clusterDomain}-config.iso"
268
269 if (templateContext['default_context']['local_repositories'] == 'True') {
270 def aptlyServerHostname = templateContext.default_context.aptly_server_hostname
Ivan Berezovskiy3379c7a2018-09-14 19:02:46 +0400271 sh "cp mcp-common-scripts/config-drive/mirror_config.sh mirror_config.sh"
azvyagintsev636493c2018-09-12 17:17:05 +0300272
273 def smc_apt = [:]
274 smc_apt['SALT_MASTER_DEPLOY_IP'] = templateContext['default_context']['salt_master_management_address']
275 smc_apt['APTLY_DEPLOY_IP'] = templateContext['default_context']['aptly_server_deploy_address']
276 smc_apt['APTLY_DEPLOY_NETMASK'] = templateContext['default_context']['deploy_network_netmask']
277 smc_apt['APTLY_MINION_ID'] = "${aptlyServerHostname}.${clusterDomain}"
278
279 for (i in common.entries(smc_apt)) {
280 sh "sed -i \"s,export ${i[0]}=.*,export ${i[0]}=${i[1]},\" mirror_config.sh"
281 }
282
283 // create apt config-drive
284 sh "./create-config-drive --user-data mirror_config.sh --hostname ${aptlyServerHostname} ${aptlyServerHostname}.${clusterDomain}-config.iso"
285 sh("mv ${aptlyServerHostname}.${clusterDomain}-config.iso output-${clusterName}/")
286
287 // save apt iso to artifacts
288 archiveArtifacts artifacts: "output-${clusterName}/${aptlyServerHostname}.${clusterDomain}-config.iso"
289 }
290 }
291
292 stage('Save changes reclass model') {
293 sh(returnStatus: true, script: "tar -czf output-${clusterName}/${clusterName}.tar.gz --exclude='*@tmp' -C ${modelEnv} .")
294 archiveArtifacts artifacts: "output-${clusterName}/${clusterName}.tar.gz"
295
azvyagintsev636493c2018-09-12 17:17:05 +0300296 if (EMAIL_ADDRESS != null && EMAIL_ADDRESS != "") {
297 emailext(to: EMAIL_ADDRESS,
298 attachmentsPattern: "output-${clusterName}/*",
299 body: "Mirantis Jenkins\n\nRequested reclass model ${clusterName} has been created and attached to this email.\nEnjoy!\n\nMirantis",
300 subject: "Your Salt model ${clusterName}")
301 }
302 dir("output-${clusterName}") {
303 deleteDir()
304 }
305 }
306
307 // Fail, but leave possibility to get failed artifacts
308 if (!testResult && TEST_MODEL.toBoolean()) {
309 common.warningMsg('Test finished: FAILURE. Please check logs and\\or debug failed model manually!')
310 error('Test stage finished: FAILURE')
311 }
312
313 } catch (Throwable e) {
314 currentBuild.result = "FAILURE"
315 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
316 throw e
317 } finally {
318 stage('Clean workspace directories') {
319 sh(script: 'find . -mindepth 1 -delete > /dev/null || true')
320 }
321 // common.sendNotification(currentBuild.result,"",["slack"])
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400322 }
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200323 }
Mikhail Ivanov9f812922017-11-07 18:52:02 +0400324}