blob: 010b0ba93481e0105630d096f45d77d25dc45457 [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 **/
azvyagintsev8692f432019-04-24 19:00:14 +03009
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +000010import static groovy.json.JsonOutput.toJson
11import static groovy.json.JsonOutput.prettyPrint
Denis Egorenko032a2b72019-04-03 14:56:52 +040012import org.apache.commons.net.util.SubnetUtils
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020013
14common = new com.mirantis.mk.Common()
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +000015common2 = new com.mirantis.mcp.Common()
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020016git = new com.mirantis.mk.Git()
17python = new com.mirantis.mk.Python()
chnyda89191012017-05-29 15:38:35 +020018saltModelTesting = new com.mirantis.mk.SaltModelTesting()
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020019
Aleksey Zvyagintsev4a804212019-02-07 13:02:31 +000020slaveNode = env.getProperty('SLAVE_NODE') ?: 'virtual'
azvyagintsev6d678da2018-11-28 21:19:06 +020021gerritCredentials = env.getProperty('CREDENTIALS_ID') ?: 'gerrit'
22runTestModel = (env.getProperty('TEST_MODEL') ?: true).toBoolean()
azvyagintsev866b19a2018-11-20 18:21:43 +020023distribRevision = 'proposed'
24gitGuessedVersion = false
25
azvyagintsev8692f432019-04-24 19:00:14 +030026def GenerateModelToxDocker(Map params) {
27 def ccRoot = params['ccRoot']
28 def context = params['context']
29 def outDir = params['outDir']
30 def envOpts = params['envOpts']
31 if (fileExists(new File(ccRoot, 'tox.ini').toString())) {
32 def tempContextFile = new File(ccRoot, 'tempContext.yaml_' + UUID.randomUUID().toString()).toString()
33 writeFile file: tempContextFile, text: context
34 // Get Jenkins user UID and GID
35 def jenkinsUID = sh(script: 'id -u', returnStdout: true).trim()
36 def jenkinsGID = sh(script: 'id -g', returnStdout: true).trim()
37 /// by default, process in image operates via root user
38 /// Otherwise, gpg key for model and all files managed by jenkins user
39 /// To make it compatible, install rrequirementfrom user, but generate model via jenkins
40 def configRun = ['distribRevision': 'nightly',
41 'envOpts' : envOpts + ["CONFIG_FILE=$tempContextFile",
42 "OUTPUT_DIR=${outDir}",
43 "jenkinsUID=${jenkinsUID}",
44 "jenkinsGID=${jenkinsGID}"
45 ],
46 'runCommands' : [
47 '001_prepare_generate_auto_reqs': {
48 sh('''
49 groupadd -g ${jenkinsGID} jenkins
50 useradd -u ${jenkinsUID} -g ${jenkinsGID} -m jenkins
51 # Install required packages
52 apt-get install -y python3-dev python-tox
53 ''')
54 },
55 '002_run_generate_auto' : {
56 print('[Cookiecutter build] Result:\n' +
57 sh(returnStdout: true, script: 'cd ' + ccRoot + '; su jenkins -c "tox -ve generate_auto" '))
58 }
59 ]
60 ]
61
62 print(saltModelTesting.setupDockerAndTest(configRun))
63 }
64
65}
66
azvyagintsev866b19a2018-11-20 18:21:43 +020067
68def globalVariatorsUpdate() {
69 def templateContext = readYaml text: env.COOKIECUTTER_TEMPLATE_CONTEXT
70 def context = templateContext['default_context']
71 // TODO add more check's for critical var's
72 // Since we can't pin to any '_branch' variable from context, to identify 'default git revision' -
73 // because each of them, might be 'refs/' variable, we need to add some tricky trigger of using
74 // 'release/XXX' logic. This is totall guess - so,if even those one failed, to definitely must pass
75 // correct variable finally!
76 [context.get('cookiecutter_template_branch'), context.get('shared_reclass_branch'), context.get('mcp_common_scripts_branch')].any { branch ->
77 if (branch.toString().startsWith('release/')) {
78 gitGuessedVersion = branch
79 return true
80 }
81 }
azvyagintsev866b19a2018-11-20 18:21:43 +020082 // Use mcpVersion git tag if not specified branch for cookiecutter-templates
83 if (!context.get('cookiecutter_template_branch')) {
84 context['cookiecutter_template_branch'] = gitGuessedVersion ?: context['mcp_version']
85 }
86 // Don't have n/t/s for cookiecutter-templates repo, therefore use master
87 if (["nightly", "testing", "stable"].contains(context['cookiecutter_template_branch'])) {
88 context['cookiecutter_template_branch'] = 'master'
89 }
90 if (!context.get('shared_reclass_branch')) {
91 context['shared_reclass_branch'] = gitGuessedVersion ?: context['mcp_version']
92 }
93 // Don't have nightly/testing for reclass-system repo, therefore use master
94 if (["nightly", "testing", "stable"].contains(context['shared_reclass_branch'])) {
95 context['shared_reclass_branch'] = 'master'
96 }
97 if (!context.get('mcp_common_scripts_branch')) {
98 // Pin exactly to CC branch, since it might use 'release/XXX' format
99 context['mcp_common_scripts_branch'] = gitGuessedVersion ?: context['mcp_version']
100 }
101 // Don't have n/t/s for mcp-common-scripts repo, therefore use master
102 if (["nightly", "testing", "stable"].contains(context['mcp_common_scripts_branch'])) {
103 context['mcp_common_scripts_branch'] = 'master'
104 }
105 //
106 distribRevision = context['mcp_version']
107 if (['master'].contains(context['mcp_version'])) {
108 distribRevision = 'nightly'
109 }
110 if (distribRevision.contains('/')) {
111 distribRevision = distribRevision.split('/')[-1]
112 }
113 // Check if we are going to test bleeding-edge release, which doesn't have binary release yet
azvyagintsev8e081642019-02-03 19:15:22 +0200114 // After 2018q4 releases, need to also check 'static' repo, for example ubuntu.
azvyagintsev4f34f7a2019-02-26 18:47:37 +0200115 def binTest = common.checkRemoteBinary(['mcp_version': distribRevision])
azvyagintsev8e081642019-02-03 19:15:22 +0200116 if (!binTest.linux_system_repo_url || !binTest.linux_system_repo_ubuntu_url) {
117 common.errorMsg("Binary release: ${distribRevision} not exist or not full. Fallback to 'proposed'! ")
azvyagintsev866b19a2018-11-20 18:21:43 +0200118 distribRevision = 'proposed'
119 }
azvyagintsev8e081642019-02-03 19:15:22 +0200120
azvyagintsev5400d1d2018-12-11 13:19:29 +0200121 // (azvyagintsev) WA for PROD-25732
122 if (context.cookiecutter_template_url.contains('gerrit.mcp.mirantis.com/mk/cookiecutter-templates')) {
123 common.warningMsg('Apply WA for PROD-25732')
124 context.cookiecutter_template_url = 'ssh://gerrit.mcp.mirantis.com:29418/mk/cookiecutter-templates.git'
125 }
azvyagintsev866b19a2018-11-20 18:21:43 +0200126 common.warningMsg("Fetching:\n" +
127 "DISTRIB_REVISION from ${distribRevision}")
azvyagintsev8692f432019-04-24 19:00:14 +0300128 common.infoMsg("Using context:\n")
Aleksey Zvyagintsev7ca28d22019-02-25 11:05:49 +0000129 print prettyPrint(toJson(context))
azvyagintsev866b19a2018-11-20 18:21:43 +0200130 return context
131
132}
azvyagintsevf252b592018-08-13 18:39:14 +0300133
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000134timeout(time: 1, unit: 'HOURS') {
Aleksey Zvyagintsev7ca28d22019-02-25 11:05:49 +0000135 node(slaveNode) {
azvyagintsev866b19a2018-11-20 18:21:43 +0200136 def context = globalVariatorsUpdate()
azvyagintsev6d678da2018-11-28 21:19:06 +0200137 def RequesterEmail = context.get('email_address', '')
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000138 def templateEnv = "${env.WORKSPACE}/template"
azvyagintsev4f34f7a2019-02-26 18:47:37 +0200139 // modelEnv - this is reclass root, aka /srv/salt/reclass
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000140 def modelEnv = "${env.WORKSPACE}/model"
141 def testEnv = "${env.WORKSPACE}/test"
142 def pipelineEnv = "${env.WORKSPACE}/pipelines"
Tomáš Kukrál9f6260f2017-03-29 23:58:26 +0200143
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000144 try {
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000145 //
146 def cutterEnv = "${env.WORKSPACE}/cutter"
147 def systemEnv = "${modelEnv}/classes/system"
148 def testResult = false
149 def user
150 wrap([$class: 'BuildUser']) {
151 user = env.BUILD_USER_ID
152 }
azvyagintsev6d678da2018-11-28 21:19:06 +0200153 currentBuild.description = "${context['cluster_name']} ${RequesterEmail}"
azvyagintsev866b19a2018-11-20 18:21:43 +0200154
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000155 stage('Download Cookiecutter template') {
156 sh(script: 'find . -mindepth 1 -delete > /dev/null || true')
157 checkout([
158 $class : 'GitSCM',
159 branches : [[name: 'FETCH_HEAD'],],
160 extensions : [[$class: 'RelativeTargetDirectory', relativeTargetDir: templateEnv]],
161 userRemoteConfigs: [[url: context['cookiecutter_template_url'], refspec: context['cookiecutter_template_branch'], credentialsId: gerritCredentials],],
162 ])
163 }
164 stage('Create empty reclass model') {
165 dir(path: modelEnv) {
azvyagintsev4f34f7a2019-02-26 18:47:37 +0200166 sh 'rm -rfv .git; git init'
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000167 sshagent(credentials: [gerritCredentials]) {
168 sh "git submodule add ${context['shared_reclass_url']} 'classes/system'"
169 }
170 }
171 checkout([
172 $class : 'GitSCM',
173 branches : [[name: 'FETCH_HEAD'],],
174 extensions : [[$class: 'RelativeTargetDirectory', relativeTargetDir: systemEnv]],
175 userRemoteConfigs: [[url: context['shared_reclass_url'], refspec: context['shared_reclass_branch'], credentialsId: gerritCredentials],],
176 ])
azvyagintsev4f34f7a2019-02-26 18:47:37 +0200177 git.commitGitChanges(modelEnv, 'Added new shared reclass submodule', "${user}@localhost", "${user}")
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000178 }
179
180 stage('Generate model') {
azvyagintsev8692f432019-04-24 19:00:14 +0300181 def envOpts = ["GNUPGHOME=${env.WORKSPACE}/gpghome"]
182 withEnv(envOpts) {
Dmitry Pyzhov089fb4f2018-12-11 16:58:00 +0300183 if (context['secrets_encryption_enabled'] == 'True') {
azvyagintsev8692f432019-04-24 19:00:14 +0300184 sh 'mkdir gpghome; chmod 700 gpghome'
Dmitry Pyzhovb5c74c72018-12-17 22:08:50 +0300185 def secretKeyID = RequesterEmail ?: "salt@${context['cluster_domain']}".toString()
Dmitry Pyzhov089fb4f2018-12-11 16:58:00 +0300186 if (!context.get('secrets_encryption_private_key')) {
187 def batchData = """
188 Key-Type: 1
189 Key-Length: 4096
190 Expire-Date: 0
191 Name-Real: ${context['salt_master_hostname']}.${context['cluster_domain']}
192 Name-Email: ${secretKeyID}
193 """.stripIndent()
azvyagintsev7d6d46c2019-02-11 14:25:41 +0200194 writeFile file: 'gpg-batch.txt', text: batchData
Dmitry Pyzhov089fb4f2018-12-11 16:58:00 +0300195 sh "gpg --gen-key --batch < gpg-batch.txt"
196 sh "gpg --export-secret-key -a ${secretKeyID} > gpgkey.asc"
197 } else {
azvyagintsev7d6d46c2019-02-11 14:25:41 +0200198 writeFile file: 'gpgkey.asc', text: context['secrets_encryption_private_key']
Dmitry Pyzhov089fb4f2018-12-11 16:58:00 +0300199 sh "gpg --import gpgkey.asc"
200 secretKeyID = sh(returnStdout: true, script: 'gpg --list-secret-keys --with-colons | awk -F: -e "/^sec/{print \\$5; exit}"').trim()
201 }
202 context['secrets_encryption_key_id'] = secretKeyID
203 }
Stanislav Riazanovda45ea02018-12-21 16:12:50 +0400204 if (context.get('cfg_failsafe_ssh_public_key')) {
azvyagintsev7d6d46c2019-02-11 14:25:41 +0200205 writeFile file: 'failsafe-ssh-key.pub', text: context['cfg_failsafe_ssh_public_key']
Stanislav Riazanovda45ea02018-12-21 16:12:50 +0400206 }
azvyagintsev4f34f7a2019-02-26 18:47:37 +0200207 if (!fileExists(new File(templateEnv, 'tox.ini').toString())) {
azvyagintsev06dfe402019-03-22 17:58:53 +0200208 reqs = new File(templateEnv, 'requirements.txt').toString()
209 if (fileExists(reqs)) {
210 python.setupVirtualenv(cutterEnv, 'python2', [], reqs)
211 } else {
212 python.setupCookiecutterVirtualenv(cutterEnv)
213 }
azvyagintsev4f34f7a2019-02-26 18:47:37 +0200214 python.generateModel(common2.dumpYAML(['default_context': context]), 'default_context', context['salt_master_hostname'], cutterEnv, modelEnv, templateEnv, false)
215 } else {
216 // tox-based CC generated structure of reclass,from the root. Otherwise for bw compat, modelEnv
217 // still expect only lower lvl of project, aka model/classes/cluster/XXX/. So,lets dump result into
218 // temp dir, and then copy it over initial structure.
219 reclassTempRootDir = sh(script: "mktemp -d -p ${env.WORKSPACE}", returnStdout: true).trim()
azvyagintsev8692f432019-04-24 19:00:14 +0300220 GenerateModelToxDocker(['context': common2.dumpYAML(['default_context': context]),
221 'ccRoot' : templateEnv,
222 'outDir' : reclassTempRootDir,
223 'envOpts': envOpts])
azvyagintsev4f34f7a2019-02-26 18:47:37 +0200224 dir(modelEnv) {
225 common.warningMsg('Forming reclass-root structure...')
226 sh("cp -ra ${reclassTempRootDir}/reclass/* .")
227 }
228 }
Dmitry Pyzhov089fb4f2018-12-11 16:58:00 +0300229 git.commitGitChanges(modelEnv, "Create model ${context['cluster_name']}", "${user}@localhost", "${user}")
230 }
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000231 }
232
azvyagintsev8692f432019-04-24 19:00:14 +0300233 stage('Test') {
azvyagintsev6d678da2018-11-28 21:19:06 +0200234 if (runTestModel) {
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000235 sh("cp -r ${modelEnv} ${testEnv}")
azvyagintsev7d6d46c2019-02-11 14:25:41 +0200236 if (fileExists('gpgkey.asc')) {
237 common.infoMsg('gpgkey.asc found!Copy it into reclass folder for tests..')
238 sh("cp -v gpgkey.asc ${testEnv}/salt_master_pillar.asc")
239 }
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000240 def DockerCName = "${env.JOB_NAME.toLowerCase()}_${env.BUILD_TAG.toLowerCase()}"
241 common.infoMsg("Attempt to run test against distribRevision: ${distribRevision}")
242 try {
243 def config = [
Denis Egorenko926bc7d2019-02-19 14:27:34 +0400244 'dockerHostname' : "${context['salt_master_hostname']}",
245 'domain' : "${context['cluster_domain']}",
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000246 'reclassEnv' : testEnv,
247 'distribRevision' : distribRevision,
248 'dockerContainerName': DockerCName,
Denis Egorenkod87490e2019-02-26 20:00:41 +0400249 'testContext' : 'salt-model-node',
azvyagintsev4f34f7a2019-02-26 18:47:37 +0200250 'dockerExtraOpts' : ['--memory=3g']
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000251 ]
252 testResult = saltModelTesting.testNode(config)
253 common.infoMsg("Test finished: SUCCESS")
254 } catch (Exception ex) {
255 common.warningMsg("Test finished: FAILED")
256 testResult = false
257 }
258 } else {
259 common.warningMsg("Test stage has been skipped!")
260 }
261 }
262 stage("Generate config drives") {
263 // apt package genisoimage is required for this stage
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000264 // download create-config-drive
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000265 def commonScriptsRepoUrl = context['mcp_common_scripts_repo'] ?: 'ssh://gerrit.mcp.mirantis.com:29418/mcp/mcp-common-scripts'
266 checkout([
267 $class : 'GitSCM',
268 branches : [[name: 'FETCH_HEAD'],],
269 extensions : [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'mcp-common-scripts']],
azvyagintsev866b19a2018-11-20 18:21:43 +0200270 userRemoteConfigs: [[url: commonScriptsRepoUrl, refspec: context['mcp_common_scripts_branch'], credentialsId: gerritCredentials],],
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000271 ])
272
Denis Egorenkoaeaa0132019-02-25 16:55:08 +0400273 def outdateGeneration = false
274 if (fileExists('mcp-common-scripts/config-drive/create_config_drive.py')) {
275 sh 'cp mcp-common-scripts/config-drive/create_config_drive.py create-config-drive.py'
276 } else {
277 outdateGeneration = true
278 sh 'cp mcp-common-scripts/config-drive/create_config_drive.sh create-config-drive && chmod +x create-config-drive'
279 }
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000280 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'
281
azvyagintsev8692f432019-04-24 19:00:14 +0300282 sh "git clone --depth 10 --mirror https://github.com/Mirantis/mk-pipelines.git ${pipelineEnv}/mk-pipelines"
283 sh "git clone --depth 10 --mirror https://github.com/Mirantis/pipeline-library.git ${pipelineEnv}/pipeline-library"
Denis Egorenkoaeaa0132019-02-25 16:55:08 +0400284 args = [
azvyagintsev1385db92019-03-22 16:05:10 +0200285 "--user-data user_data", "--model ${modelEnv}",
Denis Egorenkoaeaa0132019-02-25 16:55:08 +0400286 "--mk-pipelines ${pipelineEnv}/mk-pipelines/", "--pipeline-library ${pipelineEnv}/pipeline-library/"
287 ]
Dmitry Pyzhov089fb4f2018-12-11 16:58:00 +0300288 if (context['secrets_encryption_enabled'] == 'True') {
Denis Egorenkoaeaa0132019-02-25 16:55:08 +0400289 args.add('--gpg-key gpgkey.asc')
Dmitry Pyzhov089fb4f2018-12-11 16:58:00 +0300290 }
Stanislav Riazanovda45ea02018-12-21 16:12:50 +0400291 if (context.get('cfg_failsafe_ssh_public_key')) {
Denis Egorenkoaaeda9b2019-02-28 20:55:59 +0400292 if (outdateGeneration) {
293 args.add('--ssh-key failsafe-ssh-key.pub')
294 } else {
Denis Egorenko85ddf0d2019-03-18 18:11:19 +0400295 if (context.get('cfg_failsafe_user')) {
296 args.add('--ssh-keys failsafe-ssh-key.pub')
297 args.add("--cloud-user-name ${context.get('cfg_failsafe_user')}")
298 }
Denis Egorenkoaaeda9b2019-02-28 20:55:59 +0400299 }
Stanislav Riazanovda45ea02018-12-21 16:12:50 +0400300 }
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000301 // load data from model
302 def smc = [:]
303 smc['SALT_MASTER_MINION_ID'] = "${context['salt_master_hostname']}.${context['cluster_domain']}"
304 smc['SALT_MASTER_DEPLOY_IP'] = context['salt_master_management_address']
Denis Egorenkoaeaa0132019-02-25 16:55:08 +0400305 if (outdateGeneration) {
306 smc['DEPLOY_NETWORK_GW'] = context['deploy_network_gateway']
307 smc['DEPLOY_NETWORK_NETMASK'] = context['deploy_network_netmask']
308 if (context.get('deploy_network_mtu')) {
309 smc['DEPLOY_NETWORK_MTU'] = context['deploy_network_mtu']
310 }
311 smc['DNS_SERVERS'] = context['dns_server01']
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000312 }
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000313 smc['MCP_VERSION'] = "${context['mcp_version']}"
314 if (context['local_repositories'] == 'True') {
Denis Egorenko6bfa7552019-02-05 19:09:25 +0400315 def localRepoIP = ''
Denis Egorenkof97aec22019-02-05 14:57:33 +0400316 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 +0400317 localRepoIP = context['local_repo_url']
Denis Egorenkof97aec22019-02-05 14:57:33 +0400318 smc['MCP_SALT_REPO_URL'] = "http://${localRepoIP}/ubuntu-xenial"
319 } else {
Denis Egorenko6bfa7552019-02-05 19:09:25 +0400320 localRepoIP = context['aptly_server_deploy_address']
Denis Egorenkof97aec22019-02-05 14:57:33 +0400321 smc['MCP_SALT_REPO_URL'] = "http://${localRepoIP}"
322 }
Denis Egorenko6bfa7552019-02-05 19:09:25 +0400323 smc['MCP_SALT_REPO_KEY'] = "http://${localRepoIP}/public.gpg"
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000324 smc['PIPELINES_FROM_ISO'] = 'false'
325 smc['PIPELINE_REPO_URL'] = "http://${localRepoIP}:8088"
326 smc['LOCAL_REPOS'] = 'true'
327 }
328 if (context['upstream_proxy_enabled'] == 'True') {
329 if (context['upstream_proxy_auth_enabled'] == 'True') {
330 smc['http_proxy'] = 'http://' + context['upstream_proxy_user'] + ':' + context['upstream_proxy_password'] + '@' + context['upstream_proxy_address'] + ':' + context['upstream_proxy_port']
331 smc['https_proxy'] = 'http://' + context['upstream_proxy_user'] + ':' + context['upstream_proxy_password'] + '@' + context['upstream_proxy_address'] + ':' + context['upstream_proxy_port']
332 } else {
333 smc['http_proxy'] = 'http://' + context['upstream_proxy_address'] + ':' + context['upstream_proxy_port']
334 smc['https_proxy'] = 'http://' + context['upstream_proxy_address'] + ':' + context['upstream_proxy_port']
335 }
336 }
337
338 for (i in common.entries(smc)) {
339 sh "sed -i 's,${i[0]}=.*,${i[0]}=${i[1]},' user_data"
340 }
341
Denis Egorenko032a2b72019-04-03 14:56:52 +0400342 // calculate netmask
Denis Egorenko1f131b72019-04-05 14:42:48 +0400343 def deployNetworkSubnet = ''
344 if (context.get('deploy_network_subnet')) {
345 def subnet = new SubnetUtils(context['deploy_network_subnet'])
346 deployNetworkSubnet = subnet.getInfo().getNetmask()
347 } else if (context.get('deploy_network_netmask')) { // case for 2018.4.0
348 deployNetworkSubnet = context['deploy_network_netmask']
349 } else {
350 error('Neither context parameter deploy_network_subnet or deploy_network_netmask should be set!')
351 }
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000352 // create cfg config-drive
Denis Egorenkoaeaa0132019-02-25 16:55:08 +0400353 if (outdateGeneration) {
azvyagintsev06dfe402019-03-22 17:58:53 +0200354 args += ["--hostname ${context['salt_master_hostname']}", "${context['salt_master_hostname']}.${context['cluster_domain']}-config.iso"]
Denis Egorenkoaeaa0132019-02-25 16:55:08 +0400355 sh "./create-config-drive ${args.join(' ')}"
356 } else {
357 args += [
358 "--name ${context['salt_master_hostname']}", "--hostname ${context['salt_master_hostname']}.${context['cluster_domain']}", "--clean-up",
Denis Egorenko032a2b72019-04-03 14:56:52 +0400359 "--ip ${context['salt_master_management_address']}", "--netmask ${deployNetworkSubnet}", "--gateway ${context['deploy_network_gateway']}",
Denis Egorenkoaaeda9b2019-02-28 20:55:59 +0400360 "--dns-nameservers ${context['dns_server01']},${context['dns_server02']}"
Denis Egorenkoaeaa0132019-02-25 16:55:08 +0400361 ]
azvyagintsevc4c047a2019-04-04 17:42:48 +0300362 sh "chmod 0755 create-config-drive.py ; ./create-config-drive.py ${args.join(' ')}"
Denis Egorenkoaeaa0132019-02-25 16:55:08 +0400363 }
Aleksey Zvyagintsevaa235f22019-04-24 12:46:42 +0000364 sh("mkdir output-${context['cluster_name']} && mv ${context['salt_master_hostname']}.${context['cluster_domain']}-config.iso output-${context['cluster_name']}/")
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000365
366 // save cfg iso to artifacts
Aleksey Zvyagintsevaa235f22019-04-24 12:46:42 +0000367 archiveArtifacts artifacts: "output-${context['cluster_name']}/${context['salt_master_hostname']}.${context['cluster_domain']}-config.iso"
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000368
369 if (context['local_repositories'] == 'True') {
370 def aptlyServerHostname = context.aptly_server_hostname
371 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"
372
373 def smc_apt = [:]
374 smc_apt['SALT_MASTER_DEPLOY_IP'] = context['salt_master_management_address']
Denis Egorenkoaeaa0132019-02-25 16:55:08 +0400375 if (outdateGeneration) {
376 smc_apt['APTLY_DEPLOY_IP'] = context['aptly_server_deploy_address']
377 smc_apt['APTLY_DEPLOY_NETMASK'] = context['deploy_network_netmask']
378 }
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000379 smc_apt['APTLY_MINION_ID'] = "${aptlyServerHostname}.${context['cluster_domain']}"
380
381 for (i in common.entries(smc_apt)) {
382 sh "sed -i \"s,export ${i[0]}=.*,export ${i[0]}=${i[1]},\" mirror_config"
383 }
384
385 // create apt config-drive
Denis Egorenkoaeaa0132019-02-25 16:55:08 +0400386 if (outdateGeneration) {
387 sh "./create-config-drive --user-data mirror_config --hostname ${aptlyServerHostname} ${aptlyServerHostname}.${context['cluster_domain']}-config.iso"
388 } else {
389 args = [
Denis Egorenko032a2b72019-04-03 14:56:52 +0400390 "--ip ${context['aptly_server_deploy_address']}", "--netmask ${deployNetworkSubnet}", "--gateway ${context['deploy_network_gateway']}",
Denis Egorenkoaaeda9b2019-02-28 20:55:59 +0400391 "--user-data mirror_config", "--hostname ${aptlyServerHostname}.${context['cluster_domain']}", "--name ${aptlyServerHostname}", "--clean-up",
392 "--dns-nameservers ${context['dns_server01']},${context['dns_server02']}"
Denis Egorenkoaeaa0132019-02-25 16:55:08 +0400393 ]
394 sh "python ./create-config-drive.py ${args.join(' ')}"
395 }
Aleksey Zvyagintsevaa235f22019-04-24 12:46:42 +0000396 sh("mv ${aptlyServerHostname}.${context['cluster_domain']}-config.iso output-${context['cluster_name']}/")
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000397
398 // save apt iso to artifacts
Aleksey Zvyagintsevaa235f22019-04-24 12:46:42 +0000399 archiveArtifacts artifacts: "output-${context['cluster_name']}/${aptlyServerHostname}.${context['cluster_domain']}-config.iso"
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000400 }
401 }
402
403 stage('Save changes reclass model') {
Denis Egorenkod9865252019-04-24 15:41:57 +0400404 sh(returnStatus: true, script: "tar -czf ${context['cluster_name']}.tar.gz --exclude='*@tmp' -C ${modelEnv} .")
405 archiveArtifacts artifacts: "${context['cluster_name']}.tar.gz"
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000406
azvyagintsev5400d1d2018-12-11 13:19:29 +0200407 if (RequesterEmail != '' && !RequesterEmail.contains('example')) {
azvyagintsev6d678da2018-11-28 21:19:06 +0200408 emailext(to: RequesterEmail,
Aleksey Zvyagintsevaa235f22019-04-24 12:46:42 +0000409 attachmentsPattern: "output-${context['cluster_name']}/*",
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000410 body: "Mirantis Jenkins\n\nRequested reclass model ${context['cluster_name']} has been created and attached to this email.\nEnjoy!\n\nMirantis",
411 subject: "Your Salt model ${context['cluster_name']}")
412 }
Aleksey Zvyagintsevaa235f22019-04-24 12:46:42 +0000413 dir("output-${context['cluster_name']}") {
414 deleteDir()
415 }
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000416 }
417
418 // Fail, but leave possibility to get failed artifacts
azvyagintsev6d678da2018-11-28 21:19:06 +0200419 if (!testResult && runTestModel) {
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000420 common.warningMsg('Test finished: FAILURE. Please check logs and\\or debug failed model manually!')
421 error('Test stage finished: FAILURE')
422 }
423
424 } catch (Throwable e) {
425 currentBuild.result = "FAILURE"
426 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
427 throw e
428 } finally {
429 stage('Clean workspace directories') {
Aleksey Zvyagintsev7ca28d22019-02-25 11:05:49 +0000430 sh(script: 'find . -mindepth 1 -delete > /dev/null || true')
Aleksey Zvyagintsevb16902d2018-10-29 12:33:48 +0000431 }
432 // common.sendNotification(currentBuild.result,"",["slack"])
Denis Egorenko34c4a3b2019-03-12 12:48:15 +0400433 stage('Save artifacts to Artifactory') {
434 def artifactory = new com.mirantis.mcp.MCPArtifactory()
azvyagintsev06dfe402019-03-22 17:58:53 +0200435 def buildProps = ["context=${context['cluster_name']}"]
Denis Egorenko34c4a3b2019-03-12 12:48:15 +0400436 if (RequesterEmail != '' && !RequesterEmail.contains('example')) {
437 buildProps.add("emailTo=${RequesterEmail}")
438 }
439 def artifactoryLink = artifactory.uploadJobArtifactsToArtifactory([
azvyagintsev06dfe402019-03-22 17:58:53 +0200440 'artifactory' : 'mcp-ci',
Denis Egorenko34c4a3b2019-03-12 12:48:15 +0400441 'artifactoryRepo': "drivetrain-local/${JOB_NAME}/${context['cluster_name']}-${BUILD_NUMBER}",
azvyagintsev06dfe402019-03-22 17:58:53 +0200442 'buildProps' : buildProps,
Denis Egorenko34c4a3b2019-03-12 12:48:15 +0400443 ])
444 currentBuild.description += "<br/>${artifactoryLink}"
445 }
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400446 }
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200447 }
Mikhail Ivanov9f812922017-11-07 18:52:02 +0400448}