blob: 99ee3ea2451d4cef207e752f1f13878fe97feef2 [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
Denis Egorenko032a2b72019-04-03 14:56:52 +040011import org.apache.commons.net.util.SubnetUtils
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020012
13common = new com.mirantis.mk.Common()
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +000014common2 = new com.mirantis.mcp.Common()
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020015git = new com.mirantis.mk.Git()
16python = new com.mirantis.mk.Python()
chnyda89191012017-05-29 15:38:35 +020017saltModelTesting = new com.mirantis.mk.SaltModelTesting()
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020018
Aleksey Zvyagintsev4a804212019-02-07 13:02:31 +000019slaveNode = env.getProperty('SLAVE_NODE') ?: 'virtual'
azvyagintsev6d678da2018-11-28 21:19:06 +020020gerritCredentials = env.getProperty('CREDENTIALS_ID') ?: 'gerrit'
21runTestModel = (env.getProperty('TEST_MODEL') ?: true).toBoolean()
azvyagintsev866b19a2018-11-20 18:21:43 +020022distribRevision = 'proposed'
23gitGuessedVersion = false
24
25
26def globalVariatorsUpdate() {
27 def templateContext = readYaml text: env.COOKIECUTTER_TEMPLATE_CONTEXT
28 def context = templateContext['default_context']
29 // TODO add more check's for critical var's
30 // Since we can't pin to any '_branch' variable from context, to identify 'default git revision' -
31 // because each of them, might be 'refs/' variable, we need to add some tricky trigger of using
32 // 'release/XXX' logic. This is totall guess - so,if even those one failed, to definitely must pass
33 // correct variable finally!
34 [context.get('cookiecutter_template_branch'), context.get('shared_reclass_branch'), context.get('mcp_common_scripts_branch')].any { branch ->
35 if (branch.toString().startsWith('release/')) {
36 gitGuessedVersion = branch
37 return true
38 }
39 }
azvyagintsev866b19a2018-11-20 18:21:43 +020040 // Use mcpVersion git tag if not specified branch for cookiecutter-templates
41 if (!context.get('cookiecutter_template_branch')) {
42 context['cookiecutter_template_branch'] = gitGuessedVersion ?: context['mcp_version']
43 }
44 // Don't have n/t/s for cookiecutter-templates repo, therefore use master
45 if (["nightly", "testing", "stable"].contains(context['cookiecutter_template_branch'])) {
46 context['cookiecutter_template_branch'] = 'master'
47 }
48 if (!context.get('shared_reclass_branch')) {
49 context['shared_reclass_branch'] = gitGuessedVersion ?: context['mcp_version']
50 }
51 // Don't have nightly/testing for reclass-system repo, therefore use master
52 if (["nightly", "testing", "stable"].contains(context['shared_reclass_branch'])) {
53 context['shared_reclass_branch'] = 'master'
54 }
55 if (!context.get('mcp_common_scripts_branch')) {
56 // Pin exactly to CC branch, since it might use 'release/XXX' format
57 context['mcp_common_scripts_branch'] = gitGuessedVersion ?: context['mcp_version']
58 }
59 // Don't have n/t/s for mcp-common-scripts repo, therefore use master
60 if (["nightly", "testing", "stable"].contains(context['mcp_common_scripts_branch'])) {
61 context['mcp_common_scripts_branch'] = 'master'
62 }
63 //
64 distribRevision = context['mcp_version']
65 if (['master'].contains(context['mcp_version'])) {
66 distribRevision = 'nightly'
67 }
68 if (distribRevision.contains('/')) {
69 distribRevision = distribRevision.split('/')[-1]
70 }
71 // Check if we are going to test bleeding-edge release, which doesn't have binary release yet
azvyagintsev8e081642019-02-03 19:15:22 +020072 // After 2018q4 releases, need to also check 'static' repo, for example ubuntu.
azvyagintsev4f34f7a2019-02-26 18:47:37 +020073 def binTest = common.checkRemoteBinary(['mcp_version': distribRevision])
azvyagintsev8e081642019-02-03 19:15:22 +020074 if (!binTest.linux_system_repo_url || !binTest.linux_system_repo_ubuntu_url) {
75 common.errorMsg("Binary release: ${distribRevision} not exist or not full. Fallback to 'proposed'! ")
azvyagintsev866b19a2018-11-20 18:21:43 +020076 distribRevision = 'proposed'
77 }
azvyagintsev8e081642019-02-03 19:15:22 +020078
azvyagintsev5400d1d2018-12-11 13:19:29 +020079 // (azvyagintsev) WA for PROD-25732
80 if (context.cookiecutter_template_url.contains('gerrit.mcp.mirantis.com/mk/cookiecutter-templates')) {
81 common.warningMsg('Apply WA for PROD-25732')
82 context.cookiecutter_template_url = 'ssh://gerrit.mcp.mirantis.com:29418/mk/cookiecutter-templates.git'
83 }
azvyagintsev866b19a2018-11-20 18:21:43 +020084 common.warningMsg("Fetching:\n" +
85 "DISTRIB_REVISION from ${distribRevision}")
Aleksey Zvyagintsev7ca28d22019-02-25 11:05:49 +000086 common.infoMsg("Using context:\n" + context)
87 print prettyPrint(toJson(context))
azvyagintsev866b19a2018-11-20 18:21:43 +020088 return context
89
90}
azvyagintsevf252b592018-08-13 18:39:14 +030091
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +000092timeout(time: 1, unit: 'HOURS') {
Aleksey Zvyagintsev7ca28d22019-02-25 11:05:49 +000093 node(slaveNode) {
azvyagintsev866b19a2018-11-20 18:21:43 +020094 def context = globalVariatorsUpdate()
azvyagintsev6d678da2018-11-28 21:19:06 +020095 def RequesterEmail = context.get('email_address', '')
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +000096 def templateEnv = "${env.WORKSPACE}/template"
azvyagintsev4f34f7a2019-02-26 18:47:37 +020097 // modelEnv - this is reclass root, aka /srv/salt/reclass
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +000098 def modelEnv = "${env.WORKSPACE}/model"
99 def testEnv = "${env.WORKSPACE}/test"
100 def pipelineEnv = "${env.WORKSPACE}/pipelines"
Tomáš Kukrál9f6260f2017-03-29 23:58:26 +0200101
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000102 try {
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000103 //
104 def cutterEnv = "${env.WORKSPACE}/cutter"
105 def systemEnv = "${modelEnv}/classes/system"
106 def testResult = false
107 def user
108 wrap([$class: 'BuildUser']) {
109 user = env.BUILD_USER_ID
110 }
azvyagintsev6d678da2018-11-28 21:19:06 +0200111 currentBuild.description = "${context['cluster_name']} ${RequesterEmail}"
azvyagintsev866b19a2018-11-20 18:21:43 +0200112
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000113 stage('Download Cookiecutter template') {
114 sh(script: 'find . -mindepth 1 -delete > /dev/null || true')
115 checkout([
116 $class : 'GitSCM',
117 branches : [[name: 'FETCH_HEAD'],],
118 extensions : [[$class: 'RelativeTargetDirectory', relativeTargetDir: templateEnv]],
119 userRemoteConfigs: [[url: context['cookiecutter_template_url'], refspec: context['cookiecutter_template_branch'], credentialsId: gerritCredentials],],
120 ])
121 }
122 stage('Create empty reclass model') {
123 dir(path: modelEnv) {
azvyagintsev4f34f7a2019-02-26 18:47:37 +0200124 sh 'rm -rfv .git; git init'
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000125 sshagent(credentials: [gerritCredentials]) {
126 sh "git submodule add ${context['shared_reclass_url']} 'classes/system'"
127 }
128 }
129 checkout([
130 $class : 'GitSCM',
131 branches : [[name: 'FETCH_HEAD'],],
132 extensions : [[$class: 'RelativeTargetDirectory', relativeTargetDir: systemEnv]],
133 userRemoteConfigs: [[url: context['shared_reclass_url'], refspec: context['shared_reclass_branch'], credentialsId: gerritCredentials],],
134 ])
azvyagintsev4f34f7a2019-02-26 18:47:37 +0200135 git.commitGitChanges(modelEnv, 'Added new shared reclass submodule', "${user}@localhost", "${user}")
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000136 }
137
138 stage('Generate model') {
Dmitry Pyzhov089fb4f2018-12-11 16:58:00 +0300139 // GNUPGHOME environment variable is required for all gpg commands
140 // and for python.generateModel execution
141 withEnv(["GNUPGHOME=${env.WORKSPACE}/gpghome"]) {
142 if (context['secrets_encryption_enabled'] == 'True') {
143 sh "mkdir gpghome; chmod 700 gpghome"
Dmitry Pyzhovb5c74c72018-12-17 22:08:50 +0300144 def secretKeyID = RequesterEmail ?: "salt@${context['cluster_domain']}".toString()
Dmitry Pyzhov089fb4f2018-12-11 16:58:00 +0300145 if (!context.get('secrets_encryption_private_key')) {
146 def batchData = """
147 Key-Type: 1
148 Key-Length: 4096
149 Expire-Date: 0
150 Name-Real: ${context['salt_master_hostname']}.${context['cluster_domain']}
151 Name-Email: ${secretKeyID}
152 """.stripIndent()
azvyagintsev7d6d46c2019-02-11 14:25:41 +0200153 writeFile file: 'gpg-batch.txt', text: batchData
Dmitry Pyzhov089fb4f2018-12-11 16:58:00 +0300154 sh "gpg --gen-key --batch < gpg-batch.txt"
155 sh "gpg --export-secret-key -a ${secretKeyID} > gpgkey.asc"
156 } else {
azvyagintsev7d6d46c2019-02-11 14:25:41 +0200157 writeFile file: 'gpgkey.asc', text: context['secrets_encryption_private_key']
Dmitry Pyzhov089fb4f2018-12-11 16:58:00 +0300158 sh "gpg --import gpgkey.asc"
159 secretKeyID = sh(returnStdout: true, script: 'gpg --list-secret-keys --with-colons | awk -F: -e "/^sec/{print \\$5; exit}"').trim()
160 }
161 context['secrets_encryption_key_id'] = secretKeyID
162 }
Stanislav Riazanovda45ea02018-12-21 16:12:50 +0400163 if (context.get('cfg_failsafe_ssh_public_key')) {
azvyagintsev7d6d46c2019-02-11 14:25:41 +0200164 writeFile file: 'failsafe-ssh-key.pub', text: context['cfg_failsafe_ssh_public_key']
Stanislav Riazanovda45ea02018-12-21 16:12:50 +0400165 }
azvyagintsev4f34f7a2019-02-26 18:47:37 +0200166 if (!fileExists(new File(templateEnv, 'tox.ini').toString())) {
azvyagintsev06dfe402019-03-22 17:58:53 +0200167 reqs = new File(templateEnv, 'requirements.txt').toString()
168 if (fileExists(reqs)) {
169 python.setupVirtualenv(cutterEnv, 'python2', [], reqs)
170 } else {
171 python.setupCookiecutterVirtualenv(cutterEnv)
172 }
azvyagintsev4f34f7a2019-02-26 18:47:37 +0200173 python.generateModel(common2.dumpYAML(['default_context': context]), 'default_context', context['salt_master_hostname'], cutterEnv, modelEnv, templateEnv, false)
174 } else {
175 // tox-based CC generated structure of reclass,from the root. Otherwise for bw compat, modelEnv
176 // still expect only lower lvl of project, aka model/classes/cluster/XXX/. So,lets dump result into
177 // temp dir, and then copy it over initial structure.
178 reclassTempRootDir = sh(script: "mktemp -d -p ${env.WORKSPACE}", returnStdout: true).trim()
179 python.generateModel(common2.dumpYAML(['default_context': context]), 'default_context', context['salt_master_hostname'], cutterEnv, reclassTempRootDir, templateEnv, false)
180 dir(modelEnv) {
181 common.warningMsg('Forming reclass-root structure...')
182 sh("cp -ra ${reclassTempRootDir}/reclass/* .")
183 }
184 }
Dmitry Pyzhov089fb4f2018-12-11 16:58:00 +0300185 git.commitGitChanges(modelEnv, "Create model ${context['cluster_name']}", "${user}@localhost", "${user}")
186 }
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000187 }
188
189 stage("Test") {
azvyagintsev6d678da2018-11-28 21:19:06 +0200190 if (runTestModel) {
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000191 sh("cp -r ${modelEnv} ${testEnv}")
azvyagintsev7d6d46c2019-02-11 14:25:41 +0200192 if (fileExists('gpgkey.asc')) {
193 common.infoMsg('gpgkey.asc found!Copy it into reclass folder for tests..')
194 sh("cp -v gpgkey.asc ${testEnv}/salt_master_pillar.asc")
195 }
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000196 def DockerCName = "${env.JOB_NAME.toLowerCase()}_${env.BUILD_TAG.toLowerCase()}"
197 common.infoMsg("Attempt to run test against distribRevision: ${distribRevision}")
198 try {
199 def config = [
Denis Egorenko926bc7d2019-02-19 14:27:34 +0400200 'dockerHostname' : "${context['salt_master_hostname']}",
201 'domain' : "${context['cluster_domain']}",
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000202 'reclassEnv' : testEnv,
203 'distribRevision' : distribRevision,
204 'dockerContainerName': DockerCName,
Denis Egorenkod87490e2019-02-26 20:00:41 +0400205 'testContext' : 'salt-model-node',
azvyagintsev4f34f7a2019-02-26 18:47:37 +0200206 'dockerExtraOpts' : ['--memory=3g']
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000207 ]
208 testResult = saltModelTesting.testNode(config)
209 common.infoMsg("Test finished: SUCCESS")
210 } catch (Exception ex) {
211 common.warningMsg("Test finished: FAILED")
212 testResult = false
213 }
214 } else {
215 common.warningMsg("Test stage has been skipped!")
216 }
217 }
218 stage("Generate config drives") {
219 // apt package genisoimage is required for this stage
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000220 // download create-config-drive
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000221 def commonScriptsRepoUrl = context['mcp_common_scripts_repo'] ?: 'ssh://gerrit.mcp.mirantis.com:29418/mcp/mcp-common-scripts'
222 checkout([
223 $class : 'GitSCM',
224 branches : [[name: 'FETCH_HEAD'],],
225 extensions : [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'mcp-common-scripts']],
azvyagintsev866b19a2018-11-20 18:21:43 +0200226 userRemoteConfigs: [[url: commonScriptsRepoUrl, refspec: context['mcp_common_scripts_branch'], credentialsId: gerritCredentials],],
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000227 ])
228
Denis Egorenkoaeaa0132019-02-25 16:55:08 +0400229 def outdateGeneration = false
230 if (fileExists('mcp-common-scripts/config-drive/create_config_drive.py')) {
231 sh 'cp mcp-common-scripts/config-drive/create_config_drive.py create-config-drive.py'
232 } else {
233 outdateGeneration = true
234 sh 'cp mcp-common-scripts/config-drive/create_config_drive.sh create-config-drive && chmod +x create-config-drive'
235 }
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000236 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'
237
238 sh "git clone --mirror https://github.com/Mirantis/mk-pipelines.git ${pipelineEnv}/mk-pipelines"
239 sh "git clone --mirror https://github.com/Mirantis/pipeline-library.git ${pipelineEnv}/pipeline-library"
Denis Egorenkoaeaa0132019-02-25 16:55:08 +0400240 args = [
azvyagintsev1385db92019-03-22 16:05:10 +0200241 "--user-data user_data", "--model ${modelEnv}",
Denis Egorenkoaeaa0132019-02-25 16:55:08 +0400242 "--mk-pipelines ${pipelineEnv}/mk-pipelines/", "--pipeline-library ${pipelineEnv}/pipeline-library/"
243 ]
Dmitry Pyzhov089fb4f2018-12-11 16:58:00 +0300244 if (context['secrets_encryption_enabled'] == 'True') {
Denis Egorenkoaeaa0132019-02-25 16:55:08 +0400245 args.add('--gpg-key gpgkey.asc')
Dmitry Pyzhov089fb4f2018-12-11 16:58:00 +0300246 }
Stanislav Riazanovda45ea02018-12-21 16:12:50 +0400247 if (context.get('cfg_failsafe_ssh_public_key')) {
Denis Egorenkoaaeda9b2019-02-28 20:55:59 +0400248 if (outdateGeneration) {
249 args.add('--ssh-key failsafe-ssh-key.pub')
250 } else {
Denis Egorenko85ddf0d2019-03-18 18:11:19 +0400251 if (context.get('cfg_failsafe_user')) {
252 args.add('--ssh-keys failsafe-ssh-key.pub')
253 args.add("--cloud-user-name ${context.get('cfg_failsafe_user')}")
254 }
Denis Egorenkoaaeda9b2019-02-28 20:55:59 +0400255 }
Stanislav Riazanovda45ea02018-12-21 16:12:50 +0400256 }
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000257 // load data from model
258 def smc = [:]
259 smc['SALT_MASTER_MINION_ID'] = "${context['salt_master_hostname']}.${context['cluster_domain']}"
260 smc['SALT_MASTER_DEPLOY_IP'] = context['salt_master_management_address']
Denis Egorenkoaeaa0132019-02-25 16:55:08 +0400261 if (outdateGeneration) {
262 smc['DEPLOY_NETWORK_GW'] = context['deploy_network_gateway']
263 smc['DEPLOY_NETWORK_NETMASK'] = context['deploy_network_netmask']
264 if (context.get('deploy_network_mtu')) {
265 smc['DEPLOY_NETWORK_MTU'] = context['deploy_network_mtu']
266 }
267 smc['DNS_SERVERS'] = context['dns_server01']
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000268 }
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000269 smc['MCP_VERSION'] = "${context['mcp_version']}"
270 if (context['local_repositories'] == 'True') {
Denis Egorenko6bfa7552019-02-05 19:09:25 +0400271 def localRepoIP = ''
Denis Egorenkof97aec22019-02-05 14:57:33 +0400272 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 +0400273 localRepoIP = context['local_repo_url']
Denis Egorenkof97aec22019-02-05 14:57:33 +0400274 smc['MCP_SALT_REPO_URL'] = "http://${localRepoIP}/ubuntu-xenial"
275 } else {
Denis Egorenko6bfa7552019-02-05 19:09:25 +0400276 localRepoIP = context['aptly_server_deploy_address']
Denis Egorenkof97aec22019-02-05 14:57:33 +0400277 smc['MCP_SALT_REPO_URL'] = "http://${localRepoIP}"
278 }
Denis Egorenko6bfa7552019-02-05 19:09:25 +0400279 smc['MCP_SALT_REPO_KEY'] = "http://${localRepoIP}/public.gpg"
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000280 smc['PIPELINES_FROM_ISO'] = 'false'
281 smc['PIPELINE_REPO_URL'] = "http://${localRepoIP}:8088"
282 smc['LOCAL_REPOS'] = 'true'
283 }
284 if (context['upstream_proxy_enabled'] == 'True') {
285 if (context['upstream_proxy_auth_enabled'] == 'True') {
286 smc['http_proxy'] = 'http://' + context['upstream_proxy_user'] + ':' + context['upstream_proxy_password'] + '@' + context['upstream_proxy_address'] + ':' + context['upstream_proxy_port']
287 smc['https_proxy'] = 'http://' + context['upstream_proxy_user'] + ':' + context['upstream_proxy_password'] + '@' + context['upstream_proxy_address'] + ':' + context['upstream_proxy_port']
288 } else {
289 smc['http_proxy'] = 'http://' + context['upstream_proxy_address'] + ':' + context['upstream_proxy_port']
290 smc['https_proxy'] = 'http://' + context['upstream_proxy_address'] + ':' + context['upstream_proxy_port']
291 }
292 }
293
294 for (i in common.entries(smc)) {
295 sh "sed -i 's,${i[0]}=.*,${i[0]}=${i[1]},' user_data"
296 }
297
Denis Egorenko032a2b72019-04-03 14:56:52 +0400298 // calculate netmask
Denis Egorenko1f131b72019-04-05 14:42:48 +0400299 def deployNetworkSubnet = ''
300 if (context.get('deploy_network_subnet')) {
301 def subnet = new SubnetUtils(context['deploy_network_subnet'])
302 deployNetworkSubnet = subnet.getInfo().getNetmask()
303 } else if (context.get('deploy_network_netmask')) { // case for 2018.4.0
304 deployNetworkSubnet = context['deploy_network_netmask']
305 } else {
306 error('Neither context parameter deploy_network_subnet or deploy_network_netmask should be set!')
307 }
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000308 // create cfg config-drive
Denis Egorenkoaeaa0132019-02-25 16:55:08 +0400309 if (outdateGeneration) {
azvyagintsev06dfe402019-03-22 17:58:53 +0200310 args += ["--hostname ${context['salt_master_hostname']}", "${context['salt_master_hostname']}.${context['cluster_domain']}-config.iso"]
Denis Egorenkoaeaa0132019-02-25 16:55:08 +0400311 sh "./create-config-drive ${args.join(' ')}"
312 } else {
313 args += [
314 "--name ${context['salt_master_hostname']}", "--hostname ${context['salt_master_hostname']}.${context['cluster_domain']}", "--clean-up",
Denis Egorenko032a2b72019-04-03 14:56:52 +0400315 "--ip ${context['salt_master_management_address']}", "--netmask ${deployNetworkSubnet}", "--gateway ${context['deploy_network_gateway']}",
Denis Egorenkoaaeda9b2019-02-28 20:55:59 +0400316 "--dns-nameservers ${context['dns_server01']},${context['dns_server02']}"
Denis Egorenkoaeaa0132019-02-25 16:55:08 +0400317 ]
azvyagintsevc4c047a2019-04-04 17:42:48 +0300318 sh "chmod 0755 create-config-drive.py ; ./create-config-drive.py ${args.join(' ')}"
Denis Egorenkoaeaa0132019-02-25 16:55:08 +0400319 }
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000320 sh("mkdir output-${context['cluster_name']} && mv ${context['salt_master_hostname']}.${context['cluster_domain']}-config.iso output-${context['cluster_name']}/")
321
322 // save cfg iso to artifacts
323 archiveArtifacts artifacts: "output-${context['cluster_name']}/${context['salt_master_hostname']}.${context['cluster_domain']}-config.iso"
324
325 if (context['local_repositories'] == 'True') {
326 def aptlyServerHostname = context.aptly_server_hostname
327 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"
328
329 def smc_apt = [:]
330 smc_apt['SALT_MASTER_DEPLOY_IP'] = context['salt_master_management_address']
Denis Egorenkoaeaa0132019-02-25 16:55:08 +0400331 if (outdateGeneration) {
332 smc_apt['APTLY_DEPLOY_IP'] = context['aptly_server_deploy_address']
333 smc_apt['APTLY_DEPLOY_NETMASK'] = context['deploy_network_netmask']
334 }
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000335 smc_apt['APTLY_MINION_ID'] = "${aptlyServerHostname}.${context['cluster_domain']}"
336
337 for (i in common.entries(smc_apt)) {
338 sh "sed -i \"s,export ${i[0]}=.*,export ${i[0]}=${i[1]},\" mirror_config"
339 }
340
341 // create apt config-drive
Denis Egorenkoaeaa0132019-02-25 16:55:08 +0400342 if (outdateGeneration) {
343 sh "./create-config-drive --user-data mirror_config --hostname ${aptlyServerHostname} ${aptlyServerHostname}.${context['cluster_domain']}-config.iso"
344 } else {
345 args = [
Denis Egorenko032a2b72019-04-03 14:56:52 +0400346 "--ip ${context['aptly_server_deploy_address']}", "--netmask ${deployNetworkSubnet}", "--gateway ${context['deploy_network_gateway']}",
Denis Egorenkoaaeda9b2019-02-28 20:55:59 +0400347 "--user-data mirror_config", "--hostname ${aptlyServerHostname}.${context['cluster_domain']}", "--name ${aptlyServerHostname}", "--clean-up",
348 "--dns-nameservers ${context['dns_server01']},${context['dns_server02']}"
Denis Egorenkoaeaa0132019-02-25 16:55:08 +0400349 ]
350 sh "python ./create-config-drive.py ${args.join(' ')}"
351 }
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000352 sh("mv ${aptlyServerHostname}.${context['cluster_domain']}-config.iso output-${context['cluster_name']}/")
353
354 // save apt iso to artifacts
355 archiveArtifacts artifacts: "output-${context['cluster_name']}/${aptlyServerHostname}.${context['cluster_domain']}-config.iso"
356 }
357 }
358
359 stage('Save changes reclass model') {
360 sh(returnStatus: true, script: "tar -czf output-${context['cluster_name']}/${context['cluster_name']}.tar.gz --exclude='*@tmp' -C ${modelEnv} .")
361 archiveArtifacts artifacts: "output-${context['cluster_name']}/${context['cluster_name']}.tar.gz"
362
azvyagintsev5400d1d2018-12-11 13:19:29 +0200363 if (RequesterEmail != '' && !RequesterEmail.contains('example')) {
azvyagintsev6d678da2018-11-28 21:19:06 +0200364 emailext(to: RequesterEmail,
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000365 attachmentsPattern: "output-${context['cluster_name']}/*",
366 body: "Mirantis Jenkins\n\nRequested reclass model ${context['cluster_name']} has been created and attached to this email.\nEnjoy!\n\nMirantis",
367 subject: "Your Salt model ${context['cluster_name']}")
368 }
369 dir("output-${context['cluster_name']}") {
370 deleteDir()
371 }
372 }
373
374 // Fail, but leave possibility to get failed artifacts
azvyagintsev6d678da2018-11-28 21:19:06 +0200375 if (!testResult && runTestModel) {
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000376 common.warningMsg('Test finished: FAILURE. Please check logs and\\or debug failed model manually!')
377 error('Test stage finished: FAILURE')
378 }
379
380 } catch (Throwable e) {
381 currentBuild.result = "FAILURE"
382 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
383 throw e
384 } finally {
385 stage('Clean workspace directories') {
Aleksey Zvyagintsev7ca28d22019-02-25 11:05:49 +0000386 sh(script: 'find . -mindepth 1 -delete > /dev/null || true')
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000387 }
388 // common.sendNotification(currentBuild.result,"",["slack"])
Denis Egorenko34c4a3b2019-03-12 12:48:15 +0400389 stage('Save artifacts to Artifactory') {
390 def artifactory = new com.mirantis.mcp.MCPArtifactory()
azvyagintsev06dfe402019-03-22 17:58:53 +0200391 def buildProps = ["context=${context['cluster_name']}"]
Denis Egorenko34c4a3b2019-03-12 12:48:15 +0400392 if (RequesterEmail != '' && !RequesterEmail.contains('example')) {
393 buildProps.add("emailTo=${RequesterEmail}")
394 }
395 def artifactoryLink = artifactory.uploadJobArtifactsToArtifactory([
azvyagintsev06dfe402019-03-22 17:58:53 +0200396 'artifactory' : 'mcp-ci',
Denis Egorenko34c4a3b2019-03-12 12:48:15 +0400397 'artifactoryRepo': "drivetrain-local/${JOB_NAME}/${context['cluster_name']}-${BUILD_NUMBER}",
azvyagintsev06dfe402019-03-22 17:58:53 +0200398 'buildProps' : buildProps,
Denis Egorenko34c4a3b2019-03-12 12:48:15 +0400399 ])
400 currentBuild.description += "<br/>${artifactoryLink}"
401 }
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400402 }
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200403 }
Mikhail Ivanov9f812922017-11-07 18:52:02 +0400404}