blob: c619ce2e66563e63495ef4e734a0cebcf5e94997 [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.
Sergey Galkin8b87f6e2018-10-24 18:40:13 +04006 * CREDENTIALS_ID Credentials id for git
azvyagintsev6d678da2018-11-28 21:19:06 +02007 * TEST_MODEL Run syntax tests for model
azvyagintsev3ed704f2018-07-09 15:49:27 +03008 **/
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +00009import static groovy.json.JsonOutput.toJson
10import static groovy.json.JsonOutput.prettyPrint
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020011
12common = new com.mirantis.mk.Common()
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +000013common2 = new com.mirantis.mcp.Common()
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020014git = new com.mirantis.mk.Git()
15python = new com.mirantis.mk.Python()
chnyda89191012017-05-29 15:38:35 +020016saltModelTesting = new com.mirantis.mk.SaltModelTesting()
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020017
Aleksey Zvyagintsev4a804212019-02-07 13:02:31 +000018slaveNode = env.getProperty('SLAVE_NODE') ?: 'virtual'
azvyagintsev6d678da2018-11-28 21:19:06 +020019gerritCredentials = env.getProperty('CREDENTIALS_ID') ?: 'gerrit'
20runTestModel = (env.getProperty('TEST_MODEL') ?: true).toBoolean()
azvyagintsev866b19a2018-11-20 18:21:43 +020021distribRevision = 'proposed'
22gitGuessedVersion = false
23
24
25def globalVariatorsUpdate() {
26 def templateContext = readYaml text: env.COOKIECUTTER_TEMPLATE_CONTEXT
27 def context = templateContext['default_context']
28 // TODO add more check's for critical var's
29 // Since we can't pin to any '_branch' variable from context, to identify 'default git revision' -
30 // because each of them, might be 'refs/' variable, we need to add some tricky trigger of using
31 // 'release/XXX' logic. This is totall guess - so,if even those one failed, to definitely must pass
32 // correct variable finally!
33 [context.get('cookiecutter_template_branch'), context.get('shared_reclass_branch'), context.get('mcp_common_scripts_branch')].any { branch ->
34 if (branch.toString().startsWith('release/')) {
35 gitGuessedVersion = branch
36 return true
37 }
38 }
Hanna Arhipovad94612d2019-02-01 14:44:54 +020039 if ("${context['salt_master_hostname']}.${context['cluster_domain']}".length() > 64) {
40 common.errorMsg("Cluster domain has too long name. Make ${context['cluster_domain']} shorter than 58 symbols.")
41 error('Invalid context provided')
42 }
azvyagintsev866b19a2018-11-20 18:21:43 +020043 // Use mcpVersion git tag if not specified branch for cookiecutter-templates
44 if (!context.get('cookiecutter_template_branch')) {
45 context['cookiecutter_template_branch'] = gitGuessedVersion ?: context['mcp_version']
46 }
47 // Don't have n/t/s for cookiecutter-templates repo, therefore use master
48 if (["nightly", "testing", "stable"].contains(context['cookiecutter_template_branch'])) {
49 context['cookiecutter_template_branch'] = 'master'
50 }
51 if (!context.get('shared_reclass_branch')) {
52 context['shared_reclass_branch'] = gitGuessedVersion ?: context['mcp_version']
53 }
54 // Don't have nightly/testing for reclass-system repo, therefore use master
55 if (["nightly", "testing", "stable"].contains(context['shared_reclass_branch'])) {
56 context['shared_reclass_branch'] = 'master'
57 }
58 if (!context.get('mcp_common_scripts_branch')) {
59 // Pin exactly to CC branch, since it might use 'release/XXX' format
60 context['mcp_common_scripts_branch'] = gitGuessedVersion ?: context['mcp_version']
61 }
62 // Don't have n/t/s for mcp-common-scripts repo, therefore use master
63 if (["nightly", "testing", "stable"].contains(context['mcp_common_scripts_branch'])) {
64 context['mcp_common_scripts_branch'] = 'master'
65 }
66 //
67 distribRevision = context['mcp_version']
68 if (['master'].contains(context['mcp_version'])) {
69 distribRevision = 'nightly'
70 }
71 if (distribRevision.contains('/')) {
72 distribRevision = distribRevision.split('/')[-1]
73 }
74 // Check if we are going to test bleeding-edge release, which doesn't have binary release yet
azvyagintsev8e081642019-02-03 19:15:22 +020075 // After 2018q4 releases, need to also check 'static' repo, for example ubuntu.
76 binTest = common.checkRemoteBinary(['mcp_version': distribRevision])
77 if (!binTest.linux_system_repo_url || !binTest.linux_system_repo_ubuntu_url) {
78 common.errorMsg("Binary release: ${distribRevision} not exist or not full. Fallback to 'proposed'! ")
azvyagintsev866b19a2018-11-20 18:21:43 +020079 distribRevision = 'proposed'
80 }
azvyagintsev8e081642019-02-03 19:15:22 +020081
azvyagintsev5400d1d2018-12-11 13:19:29 +020082 // (azvyagintsev) WA for PROD-25732
83 if (context.cookiecutter_template_url.contains('gerrit.mcp.mirantis.com/mk/cookiecutter-templates')) {
84 common.warningMsg('Apply WA for PROD-25732')
85 context.cookiecutter_template_url = 'ssh://gerrit.mcp.mirantis.com:29418/mk/cookiecutter-templates.git'
86 }
azvyagintsev866b19a2018-11-20 18:21:43 +020087 common.warningMsg("Fetching:\n" +
88 "DISTRIB_REVISION from ${distribRevision}")
89 common.infoMsg("Using context:\n" + context)
90 print prettyPrint(toJson(context))
91 return context
92
93}
azvyagintsevf252b592018-08-13 18:39:14 +030094
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +000095timeout(time: 1, unit: 'HOURS') {
azvyagintsev636493c2018-09-12 17:17:05 +030096 node(slaveNode) {
azvyagintsev866b19a2018-11-20 18:21:43 +020097 def context = globalVariatorsUpdate()
azvyagintsev6d678da2018-11-28 21:19:06 +020098 def RequesterEmail = context.get('email_address', '')
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +000099 def templateEnv = "${env.WORKSPACE}/template"
100 def modelEnv = "${env.WORKSPACE}/model"
101 def testEnv = "${env.WORKSPACE}/test"
102 def pipelineEnv = "${env.WORKSPACE}/pipelines"
Tomáš Kukrál9f6260f2017-03-29 23:58:26 +0200103
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000104 try {
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000105 //
106 def cutterEnv = "${env.WORKSPACE}/cutter"
107 def systemEnv = "${modelEnv}/classes/system"
108 def testResult = false
109 def user
110 wrap([$class: 'BuildUser']) {
111 user = env.BUILD_USER_ID
112 }
azvyagintsev6d678da2018-11-28 21:19:06 +0200113 currentBuild.description = "${context['cluster_name']} ${RequesterEmail}"
azvyagintsev866b19a2018-11-20 18:21:43 +0200114
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000115 stage('Download Cookiecutter template') {
116 sh(script: 'find . -mindepth 1 -delete > /dev/null || true')
117 checkout([
118 $class : 'GitSCM',
119 branches : [[name: 'FETCH_HEAD'],],
120 extensions : [[$class: 'RelativeTargetDirectory', relativeTargetDir: templateEnv]],
121 userRemoteConfigs: [[url: context['cookiecutter_template_url'], refspec: context['cookiecutter_template_branch'], credentialsId: gerritCredentials],],
122 ])
123 }
124 stage('Create empty reclass model') {
125 dir(path: modelEnv) {
126 sh "rm -rfv .git; git init"
127 sshagent(credentials: [gerritCredentials]) {
128 sh "git submodule add ${context['shared_reclass_url']} 'classes/system'"
129 }
130 }
131 checkout([
132 $class : 'GitSCM',
133 branches : [[name: 'FETCH_HEAD'],],
134 extensions : [[$class: 'RelativeTargetDirectory', relativeTargetDir: systemEnv]],
135 userRemoteConfigs: [[url: context['shared_reclass_url'], refspec: context['shared_reclass_branch'], credentialsId: gerritCredentials],],
136 ])
137 git.commitGitChanges(modelEnv, "Added new shared reclass submodule", "${user}@localhost", "${user}")
138 }
139
140 stage('Generate model') {
Dmitry Pyzhov089fb4f2018-12-11 16:58:00 +0300141 // GNUPGHOME environment variable is required for all gpg commands
142 // and for python.generateModel execution
143 withEnv(["GNUPGHOME=${env.WORKSPACE}/gpghome"]) {
144 if (context['secrets_encryption_enabled'] == 'True') {
145 sh "mkdir gpghome; chmod 700 gpghome"
Dmitry Pyzhovb5c74c72018-12-17 22:08:50 +0300146 def secretKeyID = RequesterEmail ?: "salt@${context['cluster_domain']}".toString()
Dmitry Pyzhov089fb4f2018-12-11 16:58:00 +0300147 if (!context.get('secrets_encryption_private_key')) {
148 def batchData = """
149 Key-Type: 1
150 Key-Length: 4096
151 Expire-Date: 0
152 Name-Real: ${context['salt_master_hostname']}.${context['cluster_domain']}
153 Name-Email: ${secretKeyID}
154 """.stripIndent()
155 writeFile file:'gpg-batch.txt', text:batchData
156 sh "gpg --gen-key --batch < gpg-batch.txt"
157 sh "gpg --export-secret-key -a ${secretKeyID} > gpgkey.asc"
158 } else {
159 writeFile file:'gpgkey.asc', text:context['secrets_encryption_private_key']
160 sh "gpg --import gpgkey.asc"
161 secretKeyID = sh(returnStdout: true, script: 'gpg --list-secret-keys --with-colons | awk -F: -e "/^sec/{print \\$5; exit}"').trim()
162 }
163 context['secrets_encryption_key_id'] = secretKeyID
164 }
Stanislav Riazanovda45ea02018-12-21 16:12:50 +0400165 if (context.get('cfg_failsafe_ssh_public_key')) {
166 writeFile file:'failsafe-ssh-key.pub', text:context['cfg_failsafe_ssh_public_key']
167 }
Dmitry Pyzhov089fb4f2018-12-11 16:58:00 +0300168 python.setupCookiecutterVirtualenv(cutterEnv)
169 // FIXME refactor generateModel
170 python.generateModel(common2.dumpYAML(['default_context': context]), 'default_context', context['salt_master_hostname'], cutterEnv, modelEnv, templateEnv, false)
171 git.commitGitChanges(modelEnv, "Create model ${context['cluster_name']}", "${user}@localhost", "${user}")
172 }
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000173 }
174
175 stage("Test") {
azvyagintsev6d678da2018-11-28 21:19:06 +0200176 if (runTestModel) {
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000177 sh("cp -r ${modelEnv} ${testEnv}")
178 def DockerCName = "${env.JOB_NAME.toLowerCase()}_${env.BUILD_TAG.toLowerCase()}"
179 common.infoMsg("Attempt to run test against distribRevision: ${distribRevision}")
180 try {
181 def config = [
182 'dockerHostname' : "${context['salt_master_hostname']}.${context['cluster_domain']}",
183 'reclassEnv' : testEnv,
184 'distribRevision' : distribRevision,
185 'dockerContainerName': DockerCName,
186 'testContext' : 'salt-model-node'
187 ]
188 testResult = saltModelTesting.testNode(config)
189 common.infoMsg("Test finished: SUCCESS")
190 } catch (Exception ex) {
191 common.warningMsg("Test finished: FAILED")
192 testResult = false
193 }
194 } else {
195 common.warningMsg("Test stage has been skipped!")
196 }
197 }
198 stage("Generate config drives") {
199 // apt package genisoimage is required for this stage
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000200 // download create-config-drive
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000201 def commonScriptsRepoUrl = context['mcp_common_scripts_repo'] ?: 'ssh://gerrit.mcp.mirantis.com:29418/mcp/mcp-common-scripts'
202 checkout([
203 $class : 'GitSCM',
204 branches : [[name: 'FETCH_HEAD'],],
205 extensions : [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'mcp-common-scripts']],
azvyagintsev866b19a2018-11-20 18:21:43 +0200206 userRemoteConfigs: [[url: commonScriptsRepoUrl, refspec: context['mcp_common_scripts_branch'], credentialsId: gerritCredentials],],
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000207 ])
208
209 sh 'cp mcp-common-scripts/config-drive/create_config_drive.sh create-config-drive && chmod +x create-config-drive'
210 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'
211
212 sh "git clone --mirror https://github.com/Mirantis/mk-pipelines.git ${pipelineEnv}/mk-pipelines"
213 sh "git clone --mirror https://github.com/Mirantis/pipeline-library.git ${pipelineEnv}/pipeline-library"
214 args = "--user-data user_data --hostname ${context['salt_master_hostname']} --model ${modelEnv} --mk-pipelines ${pipelineEnv}/mk-pipelines/ --pipeline-library ${pipelineEnv}/pipeline-library/ ${context['salt_master_hostname']}.${context['cluster_domain']}-config.iso"
Dmitry Pyzhov089fb4f2018-12-11 16:58:00 +0300215 if (context['secrets_encryption_enabled'] == 'True') {
216 args = "--gpg-key gpgkey.asc " + args
217 }
Stanislav Riazanovda45ea02018-12-21 16:12:50 +0400218 if (context.get('cfg_failsafe_ssh_public_key')) {
219 args = "--ssh-key failsafe-ssh-key.pub " + args
220 }
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000221
222 // load data from model
223 def smc = [:]
224 smc['SALT_MASTER_MINION_ID'] = "${context['salt_master_hostname']}.${context['cluster_domain']}"
225 smc['SALT_MASTER_DEPLOY_IP'] = context['salt_master_management_address']
226 smc['DEPLOY_NETWORK_GW'] = context['deploy_network_gateway']
227 smc['DEPLOY_NETWORK_NETMASK'] = context['deploy_network_netmask']
228 if (context.get('deploy_network_mtu')) {
229 smc['DEPLOY_NETWORK_MTU'] = context['deploy_network_mtu']
230 }
231 smc['DNS_SERVERS'] = context['dns_server01']
232 smc['MCP_VERSION'] = "${context['mcp_version']}"
233 if (context['local_repositories'] == 'True') {
Denis Egorenko6bfa7552019-02-05 19:09:25 +0400234 def localRepoIP = ''
Denis Egorenkof97aec22019-02-05 14:57:33 +0400235 if (context['mcp_version'] in ['2018.4.0', '2018.8.0', '2018.8.0-milestone1', '2018.11.0']) {
Denis Egorenko6bfa7552019-02-05 19:09:25 +0400236 localRepoIP = context['local_repo_url']
Denis Egorenkof97aec22019-02-05 14:57:33 +0400237 smc['MCP_SALT_REPO_URL'] = "http://${localRepoIP}/ubuntu-xenial"
238 } else {
Denis Egorenko6bfa7552019-02-05 19:09:25 +0400239 localRepoIP = context['aptly_server_deploy_address']
Denis Egorenkof97aec22019-02-05 14:57:33 +0400240 smc['MCP_SALT_REPO_URL'] = "http://${localRepoIP}"
241 }
Denis Egorenko6bfa7552019-02-05 19:09:25 +0400242 smc['MCP_SALT_REPO_KEY'] = "http://${localRepoIP}/public.gpg"
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000243 smc['PIPELINES_FROM_ISO'] = 'false'
244 smc['PIPELINE_REPO_URL'] = "http://${localRepoIP}:8088"
245 smc['LOCAL_REPOS'] = 'true'
246 }
247 if (context['upstream_proxy_enabled'] == 'True') {
248 if (context['upstream_proxy_auth_enabled'] == 'True') {
249 smc['http_proxy'] = 'http://' + context['upstream_proxy_user'] + ':' + context['upstream_proxy_password'] + '@' + context['upstream_proxy_address'] + ':' + context['upstream_proxy_port']
250 smc['https_proxy'] = 'http://' + context['upstream_proxy_user'] + ':' + context['upstream_proxy_password'] + '@' + context['upstream_proxy_address'] + ':' + context['upstream_proxy_port']
251 } else {
252 smc['http_proxy'] = 'http://' + context['upstream_proxy_address'] + ':' + context['upstream_proxy_port']
253 smc['https_proxy'] = 'http://' + context['upstream_proxy_address'] + ':' + context['upstream_proxy_port']
254 }
255 }
256
257 for (i in common.entries(smc)) {
258 sh "sed -i 's,${i[0]}=.*,${i[0]}=${i[1]},' user_data"
259 }
260
261 // create cfg config-drive
262 sh "./create-config-drive ${args}"
263 sh("mkdir output-${context['cluster_name']} && mv ${context['salt_master_hostname']}.${context['cluster_domain']}-config.iso output-${context['cluster_name']}/")
264
265 // save cfg iso to artifacts
266 archiveArtifacts artifacts: "output-${context['cluster_name']}/${context['salt_master_hostname']}.${context['cluster_domain']}-config.iso"
267
268 if (context['local_repositories'] == 'True') {
269 def aptlyServerHostname = context.aptly_server_hostname
270 sh "[ -f mcp-common-scripts/config-drive/mirror_config.yaml ] && cp mcp-common-scripts/config-drive/mirror_config.yaml mirror_config || cp mcp-common-scripts/config-drive/mirror_config.sh mirror_config"
271
272 def smc_apt = [:]
273 smc_apt['SALT_MASTER_DEPLOY_IP'] = context['salt_master_management_address']
274 smc_apt['APTLY_DEPLOY_IP'] = context['aptly_server_deploy_address']
275 smc_apt['APTLY_DEPLOY_NETMASK'] = context['deploy_network_netmask']
276 smc_apt['APTLY_MINION_ID'] = "${aptlyServerHostname}.${context['cluster_domain']}"
277
278 for (i in common.entries(smc_apt)) {
279 sh "sed -i \"s,export ${i[0]}=.*,export ${i[0]}=${i[1]},\" mirror_config"
280 }
281
282 // create apt config-drive
283 sh "./create-config-drive --user-data mirror_config --hostname ${aptlyServerHostname} ${aptlyServerHostname}.${context['cluster_domain']}-config.iso"
284 sh("mv ${aptlyServerHostname}.${context['cluster_domain']}-config.iso output-${context['cluster_name']}/")
285
286 // save apt iso to artifacts
287 archiveArtifacts artifacts: "output-${context['cluster_name']}/${aptlyServerHostname}.${context['cluster_domain']}-config.iso"
288 }
289 }
290
291 stage('Save changes reclass model') {
292 sh(returnStatus: true, script: "tar -czf output-${context['cluster_name']}/${context['cluster_name']}.tar.gz --exclude='*@tmp' -C ${modelEnv} .")
293 archiveArtifacts artifacts: "output-${context['cluster_name']}/${context['cluster_name']}.tar.gz"
294
azvyagintsev5400d1d2018-12-11 13:19:29 +0200295 if (RequesterEmail != '' && !RequesterEmail.contains('example')) {
azvyagintsev6d678da2018-11-28 21:19:06 +0200296 emailext(to: RequesterEmail,
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000297 attachmentsPattern: "output-${context['cluster_name']}/*",
298 body: "Mirantis Jenkins\n\nRequested reclass model ${context['cluster_name']} has been created and attached to this email.\nEnjoy!\n\nMirantis",
299 subject: "Your Salt model ${context['cluster_name']}")
300 }
301 dir("output-${context['cluster_name']}") {
302 deleteDir()
303 }
304 }
305
306 // Fail, but leave possibility to get failed artifacts
azvyagintsev6d678da2018-11-28 21:19:06 +0200307 if (!testResult && runTestModel) {
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000308 common.warningMsg('Test finished: FAILURE. Please check logs and\\or debug failed model manually!')
309 error('Test stage finished: FAILURE')
310 }
311
312 } catch (Throwable e) {
313 currentBuild.result = "FAILURE"
314 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
315 throw e
316 } finally {
317 stage('Clean workspace directories') {
318 sh(script: 'find . -mindepth 1 -delete > /dev/null || true')
319 }
320 // common.sendNotification(currentBuild.result,"",["slack"])
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400321 }
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200322 }
Mikhail Ivanov9f812922017-11-07 18:52:02 +0400323}