blob: 9c34f588b5e182f0654175b8f170b3ad5af52ced [file] [log] [blame]
azvyagintsev9df82e52018-09-06 19:17:18 +03001/*
2Able to be triggered from Gerrit if :
3Variators:
4Modes:
51) manual run via job-build , possible to pass refspec
Denis Egorenkod8748942018-09-07 12:26:20 +04006 TODO: currently impossible to use custom COOKIECUTTER_TEMPLATE_URL| RECLASS_SYSTEM_URL Gerrit-one always used.
azvyagintsev9df82e52018-09-06 19:17:18 +03007 - for CC
8 - Reclass
Denis Egorenkod8748942018-09-07 12:26:20 +04009
102) gerrit trigger
azvyagintsev9df82e52018-09-06 19:17:18 +030011 Automatically switches if GERRIT_PROJECT variable detected
12 Always test GERRIT_REFSPEC VS GERRIT_BRANCH-master version of opposite project
13 */
14
Alexander Evseev02fe5eb2018-11-15 13:58:36 +010015import groovy.json.JsonOutput
16
chnydae80bb922017-05-29 17:48:40 +020017common = new com.mirantis.mk.Common()
azvyagintsev1385db92019-03-22 16:05:10 +020018mcpCommon = new com.mirantis.mcp.Common()
chnydabc63c9a2017-05-30 15:37:54 +020019gerrit = new com.mirantis.mk.Gerrit()
chnydae80bb922017-05-29 17:48:40 +020020python = new com.mirantis.mk.Python()
chnydae80bb922017-05-29 17:48:40 +020021
Denis Egorenkod67a6d22018-10-18 16:00:46 +040022extraVarsYAML = env.EXTRA_VARIABLES_YAML.trim() ?: ''
Denis Egorenko535d5b62018-09-28 11:28:10 +040023if (extraVarsYAML) {
24 common.mergeEnv(env, extraVarsYAML)
Alexander Evseev02fe5eb2018-11-15 13:58:36 +010025 extraVars = readYaml text: extraVarsYAML
26} else {
27 extraVars = [:]
Denis Egorenko535d5b62018-09-28 11:28:10 +040028}
29
Aleksey Zvyagintsev4a804212019-02-07 13:02:31 +000030slaveNode = env.SLAVE_NODE ?: 'virtual'
azvyagintseve7b8e792018-09-21 17:27:01 +030031checkIncludeOrder = env.CHECK_INCLUDE_ORDER ?: false
azvyagintsev5c0313d2018-08-13 17:13:35 +030032
azvyagintsev9df82e52018-09-06 19:17:18 +030033// Global var's
azvyagintsev9df82e52018-09-06 19:17:18 +030034gerritConData = [credentialsId : env.CREDENTIALS_ID,
35 gerritName : env.GERRIT_NAME ?: 'mcp-jenkins',
Roman Vyalovf0c596e2018-10-01 17:56:09 +030036 gerritHost : env.GERRIT_HOST ?: 'gerrit.mcp.mirantis.com',
azvyagintsevb266ad22018-09-11 12:11:11 +030037 gerritScheme : env.GERRIT_SCHEME ?: 'ssh',
38 gerritPort : env.GERRIT_PORT ?: '29418',
azvyagintsev9df82e52018-09-06 19:17:18 +030039 gerritRefSpec : null,
40 gerritProject : null,
41 withWipeOut : true,
42 GERRIT_CHANGE_NUMBER: null]
43//
Roman Vyalovf0c596e2018-10-01 17:56:09 +030044//ccTemplatesRepo = env.COOKIECUTTER_TEMPLATE_URL ?: 'ssh://mcp-jenkins@gerrit.mcp.mirantis.com:29418/mk/cookiecutter-templates'
azvyagintsev3a80e342018-09-19 19:22:48 +030045gerritDataCCHEAD = [:]
azvyagintsev9df82e52018-09-06 19:17:18 +030046gerritDataCC = [:]
47gerritDataCC << gerritConData
48gerritDataCC['gerritBranch'] = env.COOKIECUTTER_TEMPLATE_BRANCH ?: 'master'
azvyagintsev458278c2018-09-25 18:38:30 +030049gerritDataCC['gerritRefSpec'] = env.COOKIECUTTER_TEMPLATE_REF ?: null
azvyagintsev9df82e52018-09-06 19:17:18 +030050gerritDataCC['gerritProject'] = 'mk/cookiecutter-templates'
51//
Roman Vyalovf0c596e2018-10-01 17:56:09 +030052//reclassSystemRepo = env.RECLASS_SYSTEM_URL ?: 'ssh://mcp-jenkins@gerrit.mcp.mirantis.com:29418/salt-models/reclass-system'
azvyagintsev3a80e342018-09-19 19:22:48 +030053gerritDataRSHEAD = [:]
azvyagintsev9df82e52018-09-06 19:17:18 +030054gerritDataRS = [:]
55gerritDataRS << gerritConData
Denis Egorenkoe7040ac2018-10-24 20:11:04 +040056gerritDataRS['gerritBranch'] = env.RECLASS_SYSTEM_BRANCH ?: 'master'
azvyagintsev458278c2018-09-25 18:38:30 +030057gerritDataRS['gerritRefSpec'] = env.RECLASS_SYSTEM_GIT_REF ?: null
azvyagintsev9df82e52018-09-06 19:17:18 +030058gerritDataRS['gerritProject'] = 'salt-models/reclass-system'
59
azvyagintsevec055b42018-10-11 12:59:05 +030060// version of debRepos, aka formulas|reclass|ubuntu
azvyagintsev10e24012018-09-10 21:36:32 +030061testDistribRevision = env.DISTRIB_REVISION ?: 'nightly'
Denis Egorenko086aff12018-10-18 17:54:59 +040062
Denis Egorenkod8748942018-09-07 12:26:20 +040063// Name of sub-test chunk job
64chunkJobName = "test-mk-cookiecutter-templates-chunk"
65testModelBuildsData = [:]
Vasyl Saienko772e1232018-07-23 14:42:24 +030066
Denis Egorenkod8748942018-09-07 12:26:20 +040067def getAndUnpackNodesInfoArtifact(jobName, copyTo, build) {
68 return {
69 dir(copyTo) {
70 copyArtifacts(projectName: jobName, selector: specific(build), filter: "nodesinfo.tar.gz")
azvyagintsev3a80e342018-09-19 19:22:48 +030071 sh "tar -xf nodesinfo.tar.gz"
Denis Egorenkod8748942018-09-07 12:26:20 +040072 sh "rm -v nodesinfo.tar.gz"
73 }
74 }
75}
azvyagintsev87985532018-07-10 20:49:38 +030076
Denis Egorenko086aff12018-10-18 17:54:59 +040077def testModel(modelFile, reclassArtifactName, artifactCopyPath, useExtraRepos = false) {
azvyagintsev5c0313d2018-08-13 17:13:35 +030078 // modelFile - `modelfiname` from model/modelfiname/modelfiname.yaml
79 //* Grub all models and send it to check in paralell - by one in thread.
Denis Egorenkod8748942018-09-07 12:26:20 +040080 def _uuid = "${env.JOB_NAME.toLowerCase()}_${env.BUILD_TAG.toLowerCase()}_${modelFile.toLowerCase()}_" + UUID.randomUUID().toString().take(8)
Alexander Evseev02fe5eb2018-11-15 13:58:36 +010081 def _values = [
azvyagintsev4220bff2019-01-09 21:32:11 +020082 MODELS_TARGZ : "${env.BUILD_URL}/artifact/${reclassArtifactName}",
83 DockerCName : _uuid,
84 testReclassEnv : "model/${modelFile}/",
85 modelFile : "contexts/${modelFile}.yml",
Alexander Evseev02fe5eb2018-11-15 13:58:36 +010086 DISTRIB_REVISION: testDistribRevision,
azvyagintsev4220bff2019-01-09 21:32:11 +020087 useExtraRepos : useExtraRepos,
Alexander Evseev02fe5eb2018-11-15 13:58:36 +010088 ]
89 def _values_string = JsonOutput.toJson(_values << extraVars)
Denis Egorenkod8748942018-09-07 12:26:20 +040090 def chunkJob = build job: chunkJobName, parameters: [
azvyagintsev08b34e32018-09-12 12:29:42 +030091 [$class: 'TextParameterValue', name: 'EXTRA_VARIABLES_YAML',
azvyagintsev30bc82e2018-08-22 12:26:06 +030092 value : _values_string.stripIndent()],
azvyagintsev5c0313d2018-08-13 17:13:35 +030093 ]
Denis Egorenkod8748942018-09-07 12:26:20 +040094 // Put sub-job info into global map.
95 testModelBuildsData.put(_uuid, ['jobname' : chunkJob.fullProjectName,
96 'copyToDir': "${artifactCopyPath}/${modelFile}",
97 'buildId' : "${chunkJob.number}"])
chnydabc63c9a2017-05-30 15:37:54 +020098}
99
azvyagintsev0e503c72019-04-08 18:13:58 +0300100def StepTestModel(_basename, _reclassArtifactName, _artifactCopyPath, _useExtraRepos = false) {
azvyagintsev5c0313d2018-08-13 17:13:35 +0300101 // We need to wrap what we return in a Groovy closure, or else it's invoked
102 // when this method is called, not when we pass it to parallel.
103 // To do this, you need to wrap the code below in { }, and either return
104 // that explicitly, or use { -> } syntax.
105 // return node object
106 return {
107 node(slaveNode) {
azvyagintsev0e503c72019-04-08 18:13:58 +0300108 testModel(_basename, _reclassArtifactName, _artifactCopyPath, _useExtraRepos)
azvyagintsev5c0313d2018-08-13 17:13:35 +0300109 }
chnydae80bb922017-05-29 17:48:40 +0200110 }
azvyagintsev87985532018-07-10 20:49:38 +0300111}
112
azvyagintsev9df82e52018-09-06 19:17:18 +0300113def StepPrepareGit(templateEnvFolder, gerrit_data) {
azvyagintsev5c0313d2018-08-13 17:13:35 +0300114 // return git clone object
115 return {
azvyagintsev9df82e52018-09-06 19:17:18 +0300116 common.infoMsg("StepPrepareGit: ${gerrit_data}")
azvyagintsev5c0313d2018-08-13 17:13:35 +0300117 // fetch needed sources
118 dir(templateEnvFolder) {
azvyagintsev4f34f7a2019-02-26 18:47:37 +0200119 if (!gerrit_data['gerritRefSpec']) {
azvyagintsev9df82e52018-09-06 19:17:18 +0300120 // Get clean HEAD
121 gerrit_data['useGerritTriggerBuildChooser'] = false
Denis Egorenko80d45b22019-01-31 22:28:38 +0400122 }
123 def checkouted = gerrit.gerritPatchsetCheckout(gerrit_data)
124 if (!checkouted) {
125 error("Failed to get repo:${gerrit_data}")
azvyagintsev87985532018-07-10 20:49:38 +0300126 }
azvyagintsev5c0313d2018-08-13 17:13:35 +0300127 }
azvyagintsev87985532018-07-10 20:49:38 +0300128 }
azvyagintsev5c0313d2018-08-13 17:13:35 +0300129}
130
131def StepGenerateModels(_contextFileList, _virtualenv, _templateEnvDir) {
132 return {
azvyagintsev0e503c72019-04-08 18:13:58 +0300133 if (fileExists(new File(_templateEnvDir, 'tox.ini').toString())) {
134 // Merge contexts for nice base.yml based diff
135 dir(_templateEnvDir) {
136 sh('tox -ve merge_contexts')
137 }
138 }
azvyagintsev5c0313d2018-08-13 17:13:35 +0300139 for (contextFile in _contextFileList) {
Denis Egorenkof3469092018-10-17 17:05:50 +0400140 def basename = common.GetBaseName(contextFile, '.yml')
azvyagintsev1385db92019-03-22 16:05:10 +0200141 def contextYaml = readYaml text: readFile(file: "${_templateEnvDir}/contexts/${contextFile}")
142 // secrets_encryption overcomplicated for expected 'fast syntax tests'
143 // So, lets disable it. It would be tested only in generate-cookiecutter-products.groovy pipeline
144 if (contextYaml['default_context'].get('secrets_encryption_enabled')) {
145 common.warningMsg('Disabling secrets_encryption_enabled for tests!')
146 contextYaml['default_context']['secrets_encryption_enabled'] = 'False'
147 }
azvyagintsev0e503c72019-04-08 18:13:58 +0300148
Denis Egorenko5a7339c2019-09-26 12:14:59 +0400149 // disabling strong_usernames for tests to reduce diff between head and patched model
150 common.warningMsg('Disabling strong_usernames for tests!')
151 contextYaml['default_context']['strong_usernames'] = 'False'
152
azvyagintsev0e503c72019-04-08 18:13:58 +0300153 def context = mcpCommon.dumpYAML(contextYaml)
azvyagintsev4f34f7a2019-02-26 18:47:37 +0200154 if (!fileExists(new File(_templateEnvDir, 'tox.ini').toString())) {
155 common.warningMsg('Forming NEW reclass-root structure...')
156 python.generateModel(context, basename, 'cfg01', _virtualenv, "${_templateEnvDir}/model", _templateEnvDir)
157 } else {
158 // tox-based CC generated structure of reclass,from the root. Otherwise for bw compat, modelEnv
159 // still expect only lower lvl of project, aka model/classes/cluster/XXX/. So,lets dump result into
160 // temp dir, and then copy it over initial structure.
161 def reclassTempRootDir = sh(script: "mktemp -d -p ${env.WORKSPACE}", returnStdout: true).trim()
162 python.generateModel(context, basename, 'cfg01', _virtualenv, reclassTempRootDir, _templateEnvDir)
163 dir("${_templateEnvDir}/model/${basename}/") {
164 if (fileExists(new File(reclassTempRootDir, 'reclass').toString())) {
165 common.warningMsg('Forming NEW reclass-root structure...')
166 sh("cp -ra ${reclassTempRootDir}/reclass/* .")
167 } else {
168 // those hack needed only for period release/2019.2.0 => current patch.
169 common.warningMsg('Forming OLD reclass-root structure...')
170 sh("mkdir -p classes/cluster/ ; cd classes/cluster/; cp -ra ${reclassTempRootDir}/* .")
171 }
172 }
173 }
azvyagintsev5c0313d2018-08-13 17:13:35 +0300174 }
175 }
176}
177
azvyagintsev9df82e52018-09-06 19:17:18 +0300178def globalVariatorsUpdate() {
179 // Simple function, to check and define branch-around variables
180 // In general, simply make transition updates for non-master branch
181 // based on magic logic
Denis Egorenkofe9a3362018-10-17 15:33:25 +0400182 def newline = '<br/>'
183 def messages = []
azvyagintsevb266ad22018-09-11 12:11:11 +0300184 if (env.GERRIT_PROJECT) {
Denis Egorenkofe9a3362018-10-17 15:33:25 +0400185 messages.add("<font color='red'>GerritTrigger detected! We are in auto-mode:</font>")
186 messages.add("Test env variables has been changed:")
azvyagintseve0eb6cd2018-09-12 13:00:40 +0300187 // TODO are we going to have such branches?
azvyagintsev9df82e52018-09-06 19:17:18 +0300188 if (!['nightly', 'testing', 'stable', 'proposed', 'master'].contains(env.GERRIT_BRANCH)) {
189 gerritDataCC['gerritBranch'] = env.GERRIT_BRANCH
190 gerritDataRS['gerritBranch'] = env.GERRIT_BRANCH
azvyagintsev458278c2018-09-25 18:38:30 +0300191 testDistribRevision = env.GERRIT_BRANCH
azvyagintsev9df82e52018-09-06 19:17:18 +0300192 }
Denis Egorenko60e45c12018-11-12 12:00:11 +0400193 messages.add("COOKIECUTTER_TEMPLATE_BRANCH => ${gerritDataCC['gerritBranch']}")
194 messages.add("RECLASS_SYSTEM_BRANCH => ${gerritDataRS['gerritBranch']}")
azvyagintsev9df82e52018-09-06 19:17:18 +0300195 // Identify, who triggered. To whom we should pass refspec
196 if (env.GERRIT_PROJECT == 'salt-models/reclass-system') {
197 gerritDataRS['gerritRefSpec'] = env.GERRIT_REFSPEC
198 gerritDataRS['GERRIT_CHANGE_NUMBER'] = env.GERRIT_CHANGE_NUMBER
Denis Egorenkofe9a3362018-10-17 15:33:25 +0400199 messages.add("RECLASS_SYSTEM_GIT_REF => ${gerritDataRS['gerritRefSpec']}")
azvyagintsev9df82e52018-09-06 19:17:18 +0300200 } else if (env.GERRIT_PROJECT == 'mk/cookiecutter-templates') {
201 gerritDataCC['gerritRefSpec'] = env.GERRIT_REFSPEC
202 gerritDataCC['GERRIT_CHANGE_NUMBER'] = env.GERRIT_CHANGE_NUMBER
Denis Egorenkofe9a3362018-10-17 15:33:25 +0400203 messages.add("COOKIECUTTER_TEMPLATE_REF => ${gerritDataCC['gerritRefSpec']}")
azvyagintsev9df82e52018-09-06 19:17:18 +0300204 } else {
205 error("Unsuported gerrit-project triggered:${env.GERRIT_PROJECT}")
206 }
azvyagintsev9df82e52018-09-06 19:17:18 +0300207 } else {
Denis Egorenkofe9a3362018-10-17 15:33:25 +0400208 messages.add("<font color='red'>Non-gerrit trigger run detected!</font>")
azvyagintsev9df82e52018-09-06 19:17:18 +0300209 }
azvyagintsev3a80e342018-09-19 19:22:48 +0300210 gerritDataCCHEAD << gerritDataCC
211 gerritDataCCHEAD['gerritRefSpec'] = null
212 gerritDataCCHEAD['GERRIT_CHANGE_NUMBER'] = null
213 gerritDataRSHEAD << gerritDataRS
214 gerritDataRSHEAD['gerritRefSpec'] = null
215 gerritDataRSHEAD['GERRIT_CHANGE_NUMBER'] = null
azvyagintsev7d982812019-04-15 23:21:35 +0300216 // check for test XXX vs RELEASE branch, to get correct formulas
217 if (gerritDataCC['gerritBranch'].contains('release/')) {
218 testDistribRevision = gerritDataCC['gerritBranch']
219 } else if (gerritDataRS['gerritBranch'].contains('release')) {
220 testDistribRevision = gerritDataRS['gerritBranch']
221 }
azvyagintsev458278c2018-09-25 18:38:30 +0300222 // 'binary' branch logic w\o 'release/' prefix
223 if (testDistribRevision.contains('/')) {
224 testDistribRevision = testDistribRevision.split('/')[-1]
225 }
226 // Check if we are going to test bleeding-edge release, which doesn't have binary release yet
azvyagintsev5c200e72018-12-28 15:58:09 +0200227 // After 2018q4 releases, need to also check 'static' repo, for example ubuntu.
azvyagintsev4220bff2019-01-09 21:32:11 +0200228 binTest = common.checkRemoteBinary(['mcp_version': testDistribRevision])
azvyagintsev5c200e72018-12-28 15:58:09 +0200229 if (!binTest.linux_system_repo_url || !binTest.linux_system_repo_ubuntu_url) {
230 common.errorMsg("Binary release: ${testDistribRevision} not exist or not full. Fallback to 'proposed'! ")
azvyagintsev458278c2018-09-25 18:38:30 +0300231 testDistribRevision = 'proposed'
azvyagintsev458278c2018-09-25 18:38:30 +0300232 }
azvyagintsev7d982812019-04-15 23:21:35 +0300233 messages.add("DISTRIB_REVISION => ${testDistribRevision}")
Denis Egorenkofe9a3362018-10-17 15:33:25 +0400234 def message = messages.join(newline) + newline
azvyagintsev458278c2018-09-25 18:38:30 +0300235 currentBuild.description = currentBuild.description ? message + currentBuild.description : message
Denis Egorenkod8748942018-09-07 12:26:20 +0400236}
azvyagintsev9df82e52018-09-06 19:17:18 +0300237
Denis Egorenkoa6117fc2018-09-11 13:40:00 +0400238def replaceGeneratedValues(path) {
239 def files = sh(script: "find ${path} -name 'secrets.yml'", returnStdout: true)
240 def stepsForParallel = [:]
241 stepsForParallel.failFast = true
242 files.tokenize().each {
243 stepsForParallel.put("Removing generated passwords/secrets from ${it}",
244 {
245 def secrets = readYaml file: it
azvyagintsev08b34e32018-09-12 12:29:42 +0300246 for (String key in secrets['parameters']['_param'].keySet()) {
Denis Egorenkoa6117fc2018-09-11 13:40:00 +0400247 secrets['parameters']['_param'][key] = 'generated'
248 }
249 // writeYaml can't write to already existing file
250 writeYaml file: "${it}.tmp", data: secrets
251 sh "mv ${it}.tmp ${it}"
252 })
253 }
254 parallel stepsForParallel
255}
256
Denis Egorenkod8748942018-09-07 12:26:20 +0400257def linkReclassModels(contextList, envPath, archiveName) {
258 // to be able share reclass for all subenvs
259 // Also, makes artifact test more solid - use one reclass for all of sub-models.
260 // Archive Structure will be:
261 // tar.gz
262 // ├── contexts
263 // │   └── ceph.yml
Denis Egorenko7aa5ac52018-10-17 15:14:56 +0400264 // ├── classes-system <<< reclass system
Denis Egorenkod8748942018-09-07 12:26:20 +0400265 // ├── model
266 // │   └── ceph <<< from `context basename`
267 // │   ├── classes
268 // │   │   ├── cluster
Denis Egorenko7aa5ac52018-10-17 15:14:56 +0400269 // │   │   └── system -> ../../../classes-system
Denis Egorenkod8748942018-09-07 12:26:20 +0400270 // │   └── nodes
271 // │   └── cfg01.ceph-cluster-domain.local.yml
Denis Egorenko7aa5ac52018-10-17 15:14:56 +0400272 def archiveBaseName = common.GetBaseName(archiveName, '.tar.gz')
273 def classesSystemDir = 'classes-system'
274 // copy reclass system under envPath with -R and trailing / to support symlinks direct copy
275 sh("cp -R ${archiveBaseName}/ ${envPath}/${classesSystemDir}")
Denis Egorenkod8748942018-09-07 12:26:20 +0400276 dir(envPath) {
azvyagintsev0e503c72019-04-08 18:13:58 +0300277 for (String _context : contextList) {
278 def basename = common.GetBaseName(_context, '.yml')
Denis Egorenko7aa5ac52018-10-17 15:14:56 +0400279 dir("${envPath}/model/${basename}/classes") {
280 sh(script: "ln -sfv ../../../${classesSystemDir} system ")
Denis Egorenkod8748942018-09-07 12:26:20 +0400281 }
282 }
Denis Egorenkoa6117fc2018-09-11 13:40:00 +0400283 // replace all generated passwords/secrets/keys with hardcode value for infra/secrets.yaml
284 replaceGeneratedValues("${envPath}/model")
azvyagintsev3a80e342018-09-19 19:22:48 +0300285 // Save all models and all contexts. Warning! `h` flag must be used!
Denis Egorenko7aa5ac52018-10-17 15:14:56 +0400286 sh(script: "set -ex; tar -czhf ${env.WORKSPACE}/${archiveName} --exclude='*@tmp' contexts model ${classesSystemDir}", returnStatus: true)
Denis Egorenkod8748942018-09-07 12:26:20 +0400287 }
azvyagintsev3a80e342018-09-19 19:22:48 +0300288 archiveArtifacts artifacts: archiveName
azvyagintsev9df82e52018-09-06 19:17:18 +0300289}
290
azvyagintsev5c0313d2018-08-13 17:13:35 +0300291timeout(time: 1, unit: 'HOURS') {
292 node(slaveNode) {
azvyagintsev9df82e52018-09-06 19:17:18 +0300293 globalVariatorsUpdate()
azvyagintsev30bc82e2018-08-22 12:26:06 +0300294 def templateEnvHead = "${env.WORKSPACE}/EnvHead/"
295 def templateEnvPatched = "${env.WORKSPACE}/EnvPatched/"
azvyagintsev5c0313d2018-08-13 17:13:35 +0300296 def contextFileListHead = []
297 def contextFileListPatched = []
298 def vEnv = "${env.WORKSPACE}/venv"
Denis Egorenkod8748942018-09-07 12:26:20 +0400299 def headReclassArtifactName = "head_reclass.tar.gz"
300 def patchedReclassArtifactName = "patched_reclass.tar.gz"
301 def reclassNodeInfoDir = "${env.WORKSPACE}/reclassNodeInfo_compare/"
302 def reclassInfoHeadPath = "${reclassNodeInfoDir}/old"
303 def reclassInfoPatchedPath = "${reclassNodeInfoDir}/new"
azvyagintsev5c0313d2018-08-13 17:13:35 +0300304 try {
305 sh(script: 'find . -mindepth 1 -delete > /dev/null || true')
306 stage('Download and prepare CC env') {
307 // Prepare 2 env - for patchset, and for HEAD
azvyagintsev9df82e52018-09-06 19:17:18 +0300308 def paralellEnvs = [:]
azvyagintsev5c0313d2018-08-13 17:13:35 +0300309 paralellEnvs.failFast = true
azvyagintsev9df82e52018-09-06 19:17:18 +0300310 paralellEnvs['downloadEnvHead'] = StepPrepareGit(templateEnvHead, gerritDataCCHEAD)
azvyagintsev3a80e342018-09-19 19:22:48 +0300311 if (gerritDataCC.get('gerritRefSpec', null)) {
312 paralellEnvs['downloadEnvPatched'] = StepPrepareGit(templateEnvPatched, gerritDataCC)
313 parallel paralellEnvs
314 } else {
315 paralellEnvs['downloadEnvPatched'] = { common.warningMsg('No need to process: downloadEnvPatched') }
316 parallel paralellEnvs
317 sh("rsync -a --exclude '*@tmp' ${templateEnvHead} ${templateEnvPatched}")
318 }
Denis Egorenko3de070c2018-11-20 13:57:35 +0400319 if (env.CUSTOM_COOKIECUTTER_CONTEXT) {
320 // readYaml to check custom context structure
321 def customContext = readYaml text: env.CUSTOM_COOKIECUTTER_CONTEXT
322 writeYaml file: "${templateEnvHead}/contexts/custom_context.yml", data: customContext
323 writeYaml file: "${templateEnvPatched}/contexts/custom_context.yml", data: customContext
324 common.infoMsg("Using custom context provided from job parameter 'CUSTOM_COOKIECUTTER_CONTEXT'")
325 }
azvyagintsev5c0313d2018-08-13 17:13:35 +0300326 }
azvyagintsev8ab34fd2019-01-10 15:32:26 +0200327 stage('Check workflow_definition') {
328 // Prepare venv for old env's, aka non-tox based
329 if (!fileExists(new File(templateEnvPatched, 'tox.ini').toString()) || !fileExists(new File(templateEnvHead, 'tox.ini').toString())) {
330 python.setupVirtualenv(vEnv, 'python2', [], "${templateEnvPatched}/requirements.txt")
331 }
azvyagintsev5c0313d2018-08-13 17:13:35 +0300332 // Check only for patchset
azvyagintsev1037fb42019-01-10 13:52:38 +0200333 if (fileExists(new File(templateEnvPatched, 'tox.ini').toString())) {
334 dir(templateEnvPatched) {
335 output = sh(returnStdout: true, script: "tox -ve test")
336 common.infoMsg("[Cookiecutter test] Result: ${output}")
azvyagintsev4220bff2019-01-09 21:32:11 +0200337 }
azvyagintsev1037fb42019-01-10 13:52:38 +0200338
azvyagintsev3a80e342018-09-19 19:22:48 +0300339 } else {
azvyagintsev1037fb42019-01-10 13:52:38 +0200340 common.warningMsg('Old Cookiecutter env detected!')
azvyagintsev1037fb42019-01-10 13:52:38 +0200341 common.infoMsg(python.runVirtualenvCommand(vEnv, "python ${templateEnvPatched}/workflow_definition_test.py"))
azvyagintsev3a80e342018-09-19 19:22:48 +0300342 }
azvyagintsev5c0313d2018-08-13 17:13:35 +0300343 }
344
azvyagintsev8ab34fd2019-01-10 15:32:26 +0200345 stage('generate models') {
azvyagintsev5c0313d2018-08-13 17:13:35 +0300346 dir("${templateEnvHead}/contexts") {
347 for (String x : findFiles(glob: "*.yml")) {
348 contextFileListHead.add(x)
349 }
350 }
351 dir("${templateEnvPatched}/contexts") {
352 for (String x : findFiles(glob: "*.yml")) {
353 contextFileListPatched.add(x)
354 }
355 }
356 // Generate over 2env's - for patchset, and for HEAD
azvyagintsev9df82e52018-09-06 19:17:18 +0300357 def paralellEnvs = [:]
azvyagintsev5c0313d2018-08-13 17:13:35 +0300358 paralellEnvs.failFast = true
azvyagintsev30bc82e2018-08-22 12:26:06 +0300359 paralellEnvs['GenerateEnvHead'] = StepGenerateModels(contextFileListHead, vEnv, templateEnvHead)
azvyagintsev3a80e342018-09-19 19:22:48 +0300360 if (gerritDataCC.get('gerritRefSpec', null)) {
361 paralellEnvs['GenerateEnvPatched'] = StepGenerateModels(contextFileListPatched, vEnv, templateEnvPatched)
362 parallel paralellEnvs
363 } else {
364 paralellEnvs['GenerateEnvPatched'] = { common.warningMsg('No need to process: GenerateEnvPatched') }
365 parallel paralellEnvs
366 sh("rsync -a --exclude '*@tmp' ${templateEnvHead} ${templateEnvPatched}")
azvyagintsev5c0313d2018-08-13 17:13:35 +0300367 }
368
azvyagintsev3a80e342018-09-19 19:22:48 +0300369 // We need 2 git's, one for HEAD, one for PATCHed.
370 // if no patch, use head for both
371 RSHeadDir = common.GetBaseName(headReclassArtifactName, '.tar.gz')
372 RSPatchedDir = common.GetBaseName(patchedReclassArtifactName, '.tar.gz')
373 common.infoMsg("gerritDataRS= ${gerritDataRS}")
374 common.infoMsg("gerritDataRSHEAD= ${gerritDataRSHEAD}")
375 if (gerritDataRS.get('gerritRefSpec', null)) {
376 StepPrepareGit("${env.WORKSPACE}/${RSPatchedDir}/", gerritDataRS).call()
377 StepPrepareGit("${env.WORKSPACE}/${RSHeadDir}/", gerritDataRSHEAD).call()
378 } else {
379 StepPrepareGit("${env.WORKSPACE}/${RSHeadDir}/", gerritDataRS).call()
380 sh("cd ${env.WORKSPACE} ; ln -svf ${RSHeadDir} ${RSPatchedDir}")
381 }
azvyagintsev5c0313d2018-08-13 17:13:35 +0300382 // link all models, to use one global reclass
383 // For HEAD
Denis Egorenkod8748942018-09-07 12:26:20 +0400384 linkReclassModels(contextFileListHead, templateEnvHead, headReclassArtifactName)
azvyagintsev5c0313d2018-08-13 17:13:35 +0300385 // For patched
Denis Egorenkod8748942018-09-07 12:26:20 +0400386 linkReclassModels(contextFileListPatched, templateEnvPatched, patchedReclassArtifactName)
azvyagintsev5c0313d2018-08-13 17:13:35 +0300387 }
388
Denis Egorenkod8748942018-09-07 12:26:20 +0400389 stage("Compare cluster lvl Head/Patched") {
azvyagintsev5c0313d2018-08-13 17:13:35 +0300390 // Compare patched and HEAD reclass pillars
Denis Egorenkod8748942018-09-07 12:26:20 +0400391 compareRoot = "${env.WORKSPACE}/cluster_compare/"
Denis Egorenko7aa5ac52018-10-17 15:14:56 +0400392 // extract archive and drop all copied classes/system before comparing
azvyagintsev5c0313d2018-08-13 17:13:35 +0300393 sh(script: """
394 mkdir -pv ${compareRoot}/new ${compareRoot}/old
Denis Egorenkod8748942018-09-07 12:26:20 +0400395 tar -xzf ${patchedReclassArtifactName} --directory ${compareRoot}/new
396 tar -xzf ${headReclassArtifactName} --directory ${compareRoot}/old
Denis Egorenko7aa5ac52018-10-17 15:14:56 +0400397 find ${compareRoot} -name classes -type d -exec rm -rf '{}/system' \\;
azvyagintsev5c0313d2018-08-13 17:13:35 +0300398 """)
399 common.warningMsg('infra/secrets.yml has been skipped from compare!')
azvyagintsev3a80e342018-09-19 19:22:48 +0300400 result = '\n' + common.comparePillars(compareRoot, env.BUILD_URL, "-Ev \'infra/secrets.yml|\\.git\'")
Denis Egorenkod8748942018-09-07 12:26:20 +0400401 currentBuild.description = currentBuild.description ? currentBuild.description + result : result
azvyagintsev5c0313d2018-08-13 17:13:35 +0300402 }
azvyagintsev7d982812019-04-15 23:21:35 +0300403 stage('TestContexts Head/Patched') {
Denis Egorenkod8748942018-09-07 12:26:20 +0400404 def stepsForParallel = [:]
405 stepsForParallel.failFast = true
406 common.infoMsg("Found: ${contextFileListHead.size()} HEAD contexts to test.")
407 for (String context : contextFileListHead) {
408 def basename = common.GetBaseName(context, '.yml')
409 stepsForParallel.put("ContextHeadTest:${basename}", StepTestModel(basename, headReclassArtifactName, reclassInfoHeadPath))
410 }
azvyagintsev5c0313d2018-08-13 17:13:35 +0300411 common.infoMsg("Found: ${contextFileListPatched.size()} patched contexts to test.")
412 for (String context : contextFileListPatched) {
413 def basename = common.GetBaseName(context, '.yml')
Denis Egorenko086aff12018-10-18 17:54:59 +0400414 stepsForParallel.put("ContextPatchedTest:${basename}", StepTestModel(basename, patchedReclassArtifactName, reclassInfoPatchedPath, true))
azvyagintsev5c0313d2018-08-13 17:13:35 +0300415 }
416 parallel stepsForParallel
Denis Egorenkod8748942018-09-07 12:26:20 +0400417 common.infoMsg('All TestContexts tests done')
418 }
azvyagintsev7d982812019-04-15 23:21:35 +0300419 stage('Compare NodesInfo Head/Patched') {
Denis Egorenkod8748942018-09-07 12:26:20 +0400420 // Download all artifacts
421 def stepsForParallel = [:]
422 stepsForParallel.failFast = true
423 common.infoMsg("Found: ${testModelBuildsData.size()} nodeinfo artifacts to download.")
424 testModelBuildsData.each { bname, bdata ->
425 stepsForParallel.put("FetchData:${bname}",
426 getAndUnpackNodesInfoArtifact(bdata.jobname, bdata.copyToDir, bdata.buildId))
427 }
428 parallel stepsForParallel
Denis Egorenkoa6117fc2018-09-11 13:40:00 +0400429 // remove timestamp field from rendered files
430 sh("find ${reclassNodeInfoDir} -type f -exec sed -i '/ timestamp: .*/d' {} \\;")
Denis Egorenkod8748942018-09-07 12:26:20 +0400431 // Compare patched and HEAD reclass pillars
432 result = '\n' + common.comparePillars(reclassNodeInfoDir, env.BUILD_URL, '')
433 currentBuild.description = currentBuild.description ? currentBuild.description + result : result
azvyagintsev5c0313d2018-08-13 17:13:35 +0300434 }
Denis Egorenko04ca9f22018-09-19 16:06:49 +0400435 stage('Check include order') {
azvyagintseve7b8e792018-09-21 17:27:01 +0300436 if (!checkIncludeOrder) {
437 common.infoMsg('Check include order require to much time, and currently disabled!')
438
439 } else {
440 def correctIncludeOrder = ["service", "system", "cluster"]
441 dir(reclassInfoPatchedPath) {
442 def nodeInfoFiles = findFiles(glob: "**/*.reclass.nodeinfo")
443 def messages = ["<b>Wrong include ordering found</b><ul>"]
444 def stepsForParallel = [:]
445 nodeInfoFiles.each { nodeInfo ->
446 stepsForParallel.put("Checking ${nodeInfo.path}:", {
447 def node = readYaml file: nodeInfo.path
448 def classes = node['classes']
449 def curClassID = 0
450 def prevClassID = 0
451 def wrongOrder = false
452 for (String className in classes) {
453 def currentClass = className.tokenize('.')[0]
454 curClassID = correctIncludeOrder.indexOf(currentClass)
455 if (currentClass != correctIncludeOrder[prevClassID]) {
456 if (prevClassID > curClassID) {
457 wrongOrder = true
458 common.warningMsg("File ${nodeInfo.path} contains wrong order of classes including: Includes for ${className} should be declared before ${correctIncludeOrder[prevClassID]} includes")
459 } else {
460 prevClassID = curClassID
461 }
Denis Egorenko04ca9f22018-09-19 16:06:49 +0400462 }
463 }
azvyagintseve7b8e792018-09-21 17:27:01 +0300464 if (wrongOrder) {
465 messages.add("<li>${nodeInfo.path} contains wrong order of classes including</li>")
466 }
467 })
468 }
469 parallel stepsForParallel
470 def includerOrder = '<b>No wrong include order</b>'
471 if (messages.size() != 1) {
472 includerOrder = messages.join('')
473 }
474 currentBuild.description = currentBuild.description ? currentBuild.description + includerOrder : includerOrder
Denis Egorenko04ca9f22018-09-19 16:06:49 +0400475 }
Denis Egorenko04ca9f22018-09-19 16:06:49 +0400476 }
477 }
azvyagintsev5c0313d2018-08-13 17:13:35 +0300478 sh(script: 'find . -mindepth 1 -delete > /dev/null || true')
479
480 } catch (Throwable e) {
481 currentBuild.result = "FAILURE"
482 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
483 throw e
484 } finally {
Denis Egorenko34c4a3b2019-03-12 12:48:15 +0400485 stage('Save artifacts to Artifactory') {
486 def artifactory = new com.mirantis.mcp.MCPArtifactory()
487 def artifactoryLink = artifactory.uploadJobArtifactsToArtifactory(['artifactory': 'mcp-ci', 'artifactoryRepo': "drivetrain-local/${JOB_NAME}/${BUILD_NUMBER}"])
488 currentBuild.description += "<br/>${artifactoryLink}"
489 }
azvyagintsev5c0313d2018-08-13 17:13:35 +0300490 }
491 }
chnydae80bb922017-05-29 17:48:40 +0200492}
azvyagintsev7d982812019-04-15 23:21:35 +0300493