blob: 413e72e11b68c55c5c28e7d37cc9f22dc1766148 [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
chnydae80bb922017-05-29 17:48:40 +020015common = new com.mirantis.mk.Common()
chnydabc63c9a2017-05-30 15:37:54 +020016gerrit = new com.mirantis.mk.Gerrit()
chnydae80bb922017-05-29 17:48:40 +020017git = new com.mirantis.mk.Git()
18python = new com.mirantis.mk.Python()
chnydae80bb922017-05-29 17:48:40 +020019
azvyagintsev3a80e342018-09-19 19:22:48 +030020slaveNode = env.SLAVE_NODE ?: 'docker'
azvyagintseve7b8e792018-09-21 17:27:01 +030021checkIncludeOrder = env.CHECK_INCLUDE_ORDER ?: false
azvyagintsev5c0313d2018-08-13 17:13:35 +030022
azvyagintsev9df82e52018-09-06 19:17:18 +030023// Global var's
24alreadyMerged = false
25gerritConData = [credentialsId : env.CREDENTIALS_ID,
26 gerritName : env.GERRIT_NAME ?: 'mcp-jenkins',
27 gerritHost : env.GERRIT_HOST ?: 'gerrit.mcp.mirantis.net',
azvyagintsevb266ad22018-09-11 12:11:11 +030028 gerritScheme : env.GERRIT_SCHEME ?: 'ssh',
29 gerritPort : env.GERRIT_PORT ?: '29418',
azvyagintsev9df82e52018-09-06 19:17:18 +030030 gerritRefSpec : null,
31 gerritProject : null,
32 withWipeOut : true,
33 GERRIT_CHANGE_NUMBER: null]
34//
35//ccTemplatesRepo = env.COOKIECUTTER_TEMPLATE_URL ?: 'ssh://mcp-jenkins@gerrit.mcp.mirantis.net:29418/mk/cookiecutter-templates'
azvyagintsev3a80e342018-09-19 19:22:48 +030036gerritDataCCHEAD = [:]
azvyagintsev9df82e52018-09-06 19:17:18 +030037gerritDataCC = [:]
38gerritDataCC << gerritConData
39gerritDataCC['gerritBranch'] = env.COOKIECUTTER_TEMPLATE_BRANCH ?: 'master'
40gerritDataCC['gerritProject'] = 'mk/cookiecutter-templates'
41//
42//reclassSystemRepo = env.RECLASS_SYSTEM_URL ?: 'ssh://mcp-jenkins@gerrit.mcp.mirantis.net:29418/salt-models/reclass-system'
azvyagintsev3a80e342018-09-19 19:22:48 +030043gerritDataRSHEAD = [:]
azvyagintsev9df82e52018-09-06 19:17:18 +030044gerritDataRS = [:]
45gerritDataRS << gerritConData
46gerritDataRS['gerritBranch'] = env.RECLASS_MODEL_BRANCH ?: 'master'
47gerritDataRS['gerritProject'] = 'salt-models/reclass-system'
48
49// version of debRepos, aka formulas\reclass
azvyagintsev10e24012018-09-10 21:36:32 +030050testDistribRevision = env.DISTRIB_REVISION ?: 'nightly'
51reclassVersion = 'v1.5.4'
azvyagintsevb266ad22018-09-11 12:11:11 +030052if (env.RECLASS_VERSION) {
azvyagintsev9df82e52018-09-06 19:17:18 +030053 reclassVersion = env.RECLASS_VERSION
Vasyl Saienko772e1232018-07-23 14:42:24 +030054}
Denis Egorenkod8748942018-09-07 12:26:20 +040055// Name of sub-test chunk job
56chunkJobName = "test-mk-cookiecutter-templates-chunk"
57testModelBuildsData = [:]
Vasyl Saienko772e1232018-07-23 14:42:24 +030058
chnyda467f10f2017-05-30 17:25:07 +020059def generateSaltMaster(modEnv, clusterDomain, clusterName) {
azvyagintsev5c0313d2018-08-13 17:13:35 +030060 def nodeFile = "${modEnv}/nodes/cfg01.${clusterDomain}.yml"
61 def nodeString = """classes:
chnydae80bb922017-05-29 17:48:40 +020062- cluster.${clusterName}.infra.config
63parameters:
64 _param:
65 linux_system_codename: xenial
66 reclass_data_revision: master
67 linux:
68 system:
69 name: cfg01
70 domain: ${clusterDomain}
71"""
azvyagintsev5c0313d2018-08-13 17:13:35 +030072 sh "mkdir -p ${modEnv}/nodes/"
73 println "Create file ${nodeFile}"
74 writeFile(file: nodeFile, text: nodeString)
azvyagintsev87985532018-07-10 20:49:38 +030075}
76
azvyagintsev30bc82e2018-08-22 12:26:06 +030077/**
78 *
79 * @param contextFile - path to `contexts/XXX.yaml file`
azvyagintsev9df82e52018-09-06 19:17:18 +030080 * @param virtualenv - pyvenv with CC and dep's
azvyagintsev30bc82e2018-08-22 12:26:06 +030081 * @param templateEnvDir - root of CookieCutter
82 * @return
83 */
84
azvyagintsev5c0313d2018-08-13 17:13:35 +030085def generateModel(contextFile, virtualenv, templateEnvDir) {
86 def modelEnv = "${templateEnvDir}/model"
87 def basename = common.GetBaseName(contextFile, '.yml')
88 def generatedModel = "${modelEnv}/${basename}"
89 def content = readFile(file: "${templateEnvDir}/contexts/${contextFile}")
90 def templateContext = readYaml text: content
91 def clusterDomain = templateContext.default_context.cluster_domain
92 def clusterName = templateContext.default_context.cluster_name
93 def outputDestination = "${generatedModel}/classes/cluster/${clusterName}"
94 def templateBaseDir = templateEnvDir
95 def templateDir = "${templateEnvDir}/dir"
96 def templateOutputDir = templateBaseDir
azvyagintsev30bc82e2018-08-22 12:26:06 +030097 dir(templateEnvDir) {
98 sh(script: "rm -rf ${generatedModel} || true")
99 common.infoMsg("Generating model from context ${contextFile}")
100 def productList = ["infra", "cicd", "opencontrail", "kubernetes", "openstack", "oss", "stacklight", "ceph"]
101 for (product in productList) {
chnydae80bb922017-05-29 17:48:40 +0200102
azvyagintsev30bc82e2018-08-22 12:26:06 +0300103 // get templateOutputDir and productDir
104 if (product.startsWith("stacklight")) {
105 templateOutputDir = "${templateEnvDir}/output/stacklight"
106 try {
107 productDir = "stacklight" + templateContext.default_context['stacklight_version']
108 } catch (Throwable e) {
109 productDir = "stacklight1"
110 }
111 } else {
112 templateOutputDir = "${templateEnvDir}/output/${product}"
113 productDir = product
azvyagintsev5c0313d2018-08-13 17:13:35 +0300114 }
azvyagintsev30bc82e2018-08-22 12:26:06 +0300115
116 if (product == "infra" || (templateContext.default_context["${product}_enabled"]
117 && templateContext.default_context["${product}_enabled"].toBoolean())) {
118
119 templateDir = "${templateEnvDir}/cluster_product/${productDir}"
120 common.infoMsg("Generating product " + product + " from " + templateDir + " to " + templateOutputDir)
121
122 sh "rm -rf ${templateOutputDir} || true"
123 sh "mkdir -p ${templateOutputDir}"
124 sh "mkdir -p ${outputDestination}"
125
126 python.buildCookiecutterTemplate(templateDir, content, templateOutputDir, virtualenv, templateBaseDir)
127 sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
128 } else {
129 common.warningMsg("Product " + product + " is disabled")
130 }
azvyagintsev5c0313d2018-08-13 17:13:35 +0300131 }
azvyagintsev30bc82e2018-08-22 12:26:06 +0300132 generateSaltMaster(generatedModel, clusterDomain, clusterName)
chnydae80bb922017-05-29 17:48:40 +0200133 }
chnydae80bb922017-05-29 17:48:40 +0200134}
135
Denis Egorenkod8748942018-09-07 12:26:20 +0400136def getAndUnpackNodesInfoArtifact(jobName, copyTo, build) {
137 return {
138 dir(copyTo) {
139 copyArtifacts(projectName: jobName, selector: specific(build), filter: "nodesinfo.tar.gz")
azvyagintsev3a80e342018-09-19 19:22:48 +0300140 sh "tar -xf nodesinfo.tar.gz"
Denis Egorenkod8748942018-09-07 12:26:20 +0400141 sh "rm -v nodesinfo.tar.gz"
142 }
143 }
144}
azvyagintsev87985532018-07-10 20:49:38 +0300145
Denis Egorenkod8748942018-09-07 12:26:20 +0400146def testModel(modelFile, reclassArtifactName, artifactCopyPath) {
azvyagintsev5c0313d2018-08-13 17:13:35 +0300147 // modelFile - `modelfiname` from model/modelfiname/modelfiname.yaml
148 //* Grub all models and send it to check in paralell - by one in thread.
Denis Egorenkod8748942018-09-07 12:26:20 +0400149 def _uuid = "${env.JOB_NAME.toLowerCase()}_${env.BUILD_TAG.toLowerCase()}_${modelFile.toLowerCase()}_" + UUID.randomUUID().toString().take(8)
150 def _values_string = """
azvyagintsev87985532018-07-10 20:49:38 +0300151 ---
Denis Egorenkod8748942018-09-07 12:26:20 +0400152 MODELS_TARGZ: "${env.BUILD_URL}/artifact/${reclassArtifactName}"
153 DockerCName: "${_uuid}"
azvyagintsev87985532018-07-10 20:49:38 +0300154 testReclassEnv: "model/${modelFile}/"
155 modelFile: "contexts/${modelFile}.yml"
azvyagintsev9df82e52018-09-06 19:17:18 +0300156 DISTRIB_REVISION: "${testDistribRevision}"
azvyagintsev87985532018-07-10 20:49:38 +0300157 reclassVersion: "${reclassVersion}"
158 """
Denis Egorenkod8748942018-09-07 12:26:20 +0400159 def chunkJob = build job: chunkJobName, parameters: [
azvyagintsev08b34e32018-09-12 12:29:42 +0300160 [$class: 'TextParameterValue', name: 'EXTRA_VARIABLES_YAML',
azvyagintsev30bc82e2018-08-22 12:26:06 +0300161 value : _values_string.stripIndent()],
azvyagintsev5c0313d2018-08-13 17:13:35 +0300162 ]
Denis Egorenkod8748942018-09-07 12:26:20 +0400163 // Put sub-job info into global map.
164 testModelBuildsData.put(_uuid, ['jobname' : chunkJob.fullProjectName,
165 'copyToDir': "${artifactCopyPath}/${modelFile}",
166 'buildId' : "${chunkJob.number}"])
chnydabc63c9a2017-05-30 15:37:54 +0200167}
168
Denis Egorenkod8748942018-09-07 12:26:20 +0400169def StepTestModel(basename, reclassArtifactName, artifactCopyPath) {
azvyagintsev5c0313d2018-08-13 17:13:35 +0300170 // We need to wrap what we return in a Groovy closure, or else it's invoked
171 // when this method is called, not when we pass it to parallel.
172 // To do this, you need to wrap the code below in { }, and either return
173 // that explicitly, or use { -> } syntax.
174 // return node object
175 return {
176 node(slaveNode) {
Denis Egorenkod8748942018-09-07 12:26:20 +0400177 testModel(basename, reclassArtifactName, artifactCopyPath)
azvyagintsev5c0313d2018-08-13 17:13:35 +0300178 }
chnydae80bb922017-05-29 17:48:40 +0200179 }
azvyagintsev87985532018-07-10 20:49:38 +0300180}
181
azvyagintsev9df82e52018-09-06 19:17:18 +0300182def StepPrepareGit(templateEnvFolder, gerrit_data) {
azvyagintsev5c0313d2018-08-13 17:13:35 +0300183 // return git clone object
184 return {
azvyagintsev9df82e52018-09-06 19:17:18 +0300185 def checkouted = false
186 common.infoMsg("StepPrepareGit: ${gerrit_data}")
azvyagintsev5c0313d2018-08-13 17:13:35 +0300187 // fetch needed sources
188 dir(templateEnvFolder) {
azvyagintsev9df82e52018-09-06 19:17:18 +0300189 if (gerrit_data['gerritRefSpec']) {
190 // Those part might be not work,in case manual var's pass
191 def gerritChange = gerrit.getGerritChange(gerrit_data['gerritName'], gerrit_data['gerritHost'],
192 gerrit_data['GERRIT_CHANGE_NUMBER'], gerrit_data['credentialsId'])
azvyagintsev5c0313d2018-08-13 17:13:35 +0300193 merged = gerritChange.status == "MERGED"
194 if (!merged) {
azvyagintsev9df82e52018-09-06 19:17:18 +0300195 checkouted = gerrit.gerritPatchsetCheckout(gerrit_data)
azvyagintsev5c0313d2018-08-13 17:13:35 +0300196 } else {
azvyagintsev9df82e52018-09-06 19:17:18 +0300197 // update global variable for pretty return from pipeline
198 alreadyMerged = true
199 common.successMsg("Change ${gerrit_data['GERRIT_CHANGE_NUMBER']} is already merged, no need to gate them")
200 error('change already merged')
azvyagintsev87985532018-07-10 20:49:38 +0300201 }
azvyagintsev5c0313d2018-08-13 17:13:35 +0300202 } else {
azvyagintsev9df82e52018-09-06 19:17:18 +0300203 // Get clean HEAD
204 gerrit_data['useGerritTriggerBuildChooser'] = false
205 checkouted = gerrit.gerritPatchsetCheckout(gerrit_data)
206 if (!checkouted) {
207 error("Failed to get repo:${gerrit_data}")
208 }
azvyagintsev87985532018-07-10 20:49:38 +0300209 }
azvyagintsev5c0313d2018-08-13 17:13:35 +0300210 }
azvyagintsev87985532018-07-10 20:49:38 +0300211 }
azvyagintsev5c0313d2018-08-13 17:13:35 +0300212}
213
214def StepGenerateModels(_contextFileList, _virtualenv, _templateEnvDir) {
215 return {
216 for (contextFile in _contextFileList) {
217 generateModel(contextFile, _virtualenv, _templateEnvDir)
218 }
219 }
220}
221
azvyagintsev9df82e52018-09-06 19:17:18 +0300222def globalVariatorsUpdate() {
223 // Simple function, to check and define branch-around variables
224 // In general, simply make transition updates for non-master branch
225 // based on magic logic
226 def message = ''
azvyagintsevb266ad22018-09-11 12:11:11 +0300227 if (env.GERRIT_PROJECT) {
azvyagintseve0eb6cd2018-09-12 13:00:40 +0300228 // TODO are we going to have such branches?
azvyagintsev9df82e52018-09-06 19:17:18 +0300229 if (!['nightly', 'testing', 'stable', 'proposed', 'master'].contains(env.GERRIT_BRANCH)) {
230 gerritDataCC['gerritBranch'] = env.GERRIT_BRANCH
231 gerritDataRS['gerritBranch'] = env.GERRIT_BRANCH
232 // 'binary' branch logic w\o 'release/' prefix
233 testDistribRevision = env.GERRIT_BRANCH.split('/')[-1]
234 // Check if we are going to test bleeding-edge release, which doesn't have binary release yet
235 if (!common.checkRemoteBinary([apt_mk_version: testDistribRevision]).linux_system_repo_url) {
236 common.errorMsg("Binary release: ${testDistribRevision} not exist. Fallback to 'proposed'! ")
237 testDistribRevision = 'proposed'
238 }
239 }
240 // Identify, who triggered. To whom we should pass refspec
241 if (env.GERRIT_PROJECT == 'salt-models/reclass-system') {
242 gerritDataRS['gerritRefSpec'] = env.GERRIT_REFSPEC
243 gerritDataRS['GERRIT_CHANGE_NUMBER'] = env.GERRIT_CHANGE_NUMBER
244 message = "<br/>RECLASS_SYSTEM_GIT_REF =>${gerritDataRS['gerritRefSpec']}"
245 } else if (env.GERRIT_PROJECT == 'mk/cookiecutter-templates') {
246 gerritDataCC['gerritRefSpec'] = env.GERRIT_REFSPEC
247 gerritDataCC['GERRIT_CHANGE_NUMBER'] = env.GERRIT_CHANGE_NUMBER
248 message = "<br/>COOKIECUTTER_TEMPLATE_REF =>${gerritDataCC['gerritRefSpec']}"
249 } else {
250 error("Unsuported gerrit-project triggered:${env.GERRIT_PROJECT}")
251 }
252
253 message = "<font color='red'>GerritTrigger detected! We are in auto-mode:</font>" +
254 "<br/>Test env variables has been changed:" +
255 "<br/>COOKIECUTTER_TEMPLATE_BRANCH => ${gerritDataCC['gerritBranch']}" +
256 "<br/>DISTRIB_REVISION =>${testDistribRevision}" +
Denis Egorenko04ca9f22018-09-19 16:06:49 +0400257 "<br/>RECLASS_MODEL_BRANCH=> ${gerritDataRS['gerritBranch']}" + message + "<br/>"
azvyagintsev9df82e52018-09-06 19:17:18 +0300258 common.warningMsg(message)
259 currentBuild.description = currentBuild.description ? message + "<br/>" + currentBuild.description : message
260 } else {
261 // Check for passed variables:
azvyagintsev3a80e342018-09-19 19:22:48 +0300262 gerritDataRS['gerritRefSpec'] = env.RECLASS_SYSTEM_GIT_REF ?: null
263 gerritDataCC['gerritRefSpec'] = env.COOKIECUTTER_TEMPLATE_REF ?: null
264 message = "<font color='red'>Non-gerrit trigger run detected!</font>" + "<br/>"
azvyagintsev9df82e52018-09-06 19:17:18 +0300265 currentBuild.description = currentBuild.description ? message + "<br/>" + currentBuild.description : message
266 }
azvyagintsev3a80e342018-09-19 19:22:48 +0300267 gerritDataCCHEAD << gerritDataCC
268 gerritDataCCHEAD['gerritRefSpec'] = null
269 gerritDataCCHEAD['GERRIT_CHANGE_NUMBER'] = null
270 gerritDataRSHEAD << gerritDataRS
271 gerritDataRSHEAD['gerritRefSpec'] = null
272 gerritDataRSHEAD['GERRIT_CHANGE_NUMBER'] = null
273
Denis Egorenkod8748942018-09-07 12:26:20 +0400274}
azvyagintsev9df82e52018-09-06 19:17:18 +0300275
Denis Egorenkoa6117fc2018-09-11 13:40:00 +0400276def replaceGeneratedValues(path) {
277 def files = sh(script: "find ${path} -name 'secrets.yml'", returnStdout: true)
278 def stepsForParallel = [:]
279 stepsForParallel.failFast = true
280 files.tokenize().each {
281 stepsForParallel.put("Removing generated passwords/secrets from ${it}",
282 {
283 def secrets = readYaml file: it
azvyagintsev08b34e32018-09-12 12:29:42 +0300284 for (String key in secrets['parameters']['_param'].keySet()) {
Denis Egorenkoa6117fc2018-09-11 13:40:00 +0400285 secrets['parameters']['_param'][key] = 'generated'
286 }
287 // writeYaml can't write to already existing file
288 writeYaml file: "${it}.tmp", data: secrets
289 sh "mv ${it}.tmp ${it}"
290 })
291 }
292 parallel stepsForParallel
293}
294
Denis Egorenkod8748942018-09-07 12:26:20 +0400295def linkReclassModels(contextList, envPath, archiveName) {
296 // to be able share reclass for all subenvs
297 // Also, makes artifact test more solid - use one reclass for all of sub-models.
298 // Archive Structure will be:
299 // tar.gz
300 // ├── contexts
301 // │   └── ceph.yml
azvyagintsev3a80e342018-09-19 19:22:48 +0300302 // ├── ${reclassDirName} <<< reclass system
Denis Egorenkod8748942018-09-07 12:26:20 +0400303 // ├── model
304 // │   └── ceph <<< from `context basename`
305 // │   ├── classes
306 // │   │   ├── cluster
azvyagintsev3a80e342018-09-19 19:22:48 +0300307 // │   │   └── system -> ../../../${reclassDirName}
Denis Egorenkod8748942018-09-07 12:26:20 +0400308 // │   └── nodes
309 // │   └── cfg01.ceph-cluster-domain.local.yml
310 dir(envPath) {
311 for (String context : contextList) {
312 def basename = common.GetBaseName(context, '.yml')
313 dir("${envPath}/model/${basename}") {
azvyagintsev3a80e342018-09-19 19:22:48 +0300314 sh(script: "mkdir -p classes/; ln -sfv ../../../../${common.GetBaseName(archiveName, '.tar.gz')} classes/system ")
Denis Egorenkod8748942018-09-07 12:26:20 +0400315 }
316 }
Denis Egorenkoa6117fc2018-09-11 13:40:00 +0400317 // replace all generated passwords/secrets/keys with hardcode value for infra/secrets.yaml
318 replaceGeneratedValues("${envPath}/model")
azvyagintsev3a80e342018-09-19 19:22:48 +0300319 // Save all models and all contexts. Warning! `h` flag must be used!
320 sh(script: "set -ex; tar -czhf ${env.WORKSPACE}/${archiveName} --exclude='*@tmp' model contexts", returnStatus: true)
Denis Egorenkod8748942018-09-07 12:26:20 +0400321 }
azvyagintsev3a80e342018-09-19 19:22:48 +0300322 archiveArtifacts artifacts: archiveName
azvyagintsev9df82e52018-09-06 19:17:18 +0300323}
324
azvyagintsev5c0313d2018-08-13 17:13:35 +0300325timeout(time: 1, unit: 'HOURS') {
326 node(slaveNode) {
azvyagintsev9df82e52018-09-06 19:17:18 +0300327 globalVariatorsUpdate()
azvyagintsev30bc82e2018-08-22 12:26:06 +0300328 def templateEnvHead = "${env.WORKSPACE}/EnvHead/"
329 def templateEnvPatched = "${env.WORKSPACE}/EnvPatched/"
azvyagintsev5c0313d2018-08-13 17:13:35 +0300330 def contextFileListHead = []
331 def contextFileListPatched = []
332 def vEnv = "${env.WORKSPACE}/venv"
Denis Egorenkod8748942018-09-07 12:26:20 +0400333 def headReclassArtifactName = "head_reclass.tar.gz"
334 def patchedReclassArtifactName = "patched_reclass.tar.gz"
335 def reclassNodeInfoDir = "${env.WORKSPACE}/reclassNodeInfo_compare/"
336 def reclassInfoHeadPath = "${reclassNodeInfoDir}/old"
337 def reclassInfoPatchedPath = "${reclassNodeInfoDir}/new"
azvyagintsev5c0313d2018-08-13 17:13:35 +0300338 try {
339 sh(script: 'find . -mindepth 1 -delete > /dev/null || true')
340 stage('Download and prepare CC env') {
341 // Prepare 2 env - for patchset, and for HEAD
azvyagintsev9df82e52018-09-06 19:17:18 +0300342 def paralellEnvs = [:]
azvyagintsev5c0313d2018-08-13 17:13:35 +0300343 paralellEnvs.failFast = true
azvyagintsev9df82e52018-09-06 19:17:18 +0300344 paralellEnvs['downloadEnvHead'] = StepPrepareGit(templateEnvHead, gerritDataCCHEAD)
azvyagintsev3a80e342018-09-19 19:22:48 +0300345 if (gerritDataCC.get('gerritRefSpec', null)) {
346 paralellEnvs['downloadEnvPatched'] = StepPrepareGit(templateEnvPatched, gerritDataCC)
347 parallel paralellEnvs
348 } else {
349 paralellEnvs['downloadEnvPatched'] = { common.warningMsg('No need to process: downloadEnvPatched') }
350 parallel paralellEnvs
351 sh("rsync -a --exclude '*@tmp' ${templateEnvHead} ${templateEnvPatched}")
352 }
azvyagintsev5c0313d2018-08-13 17:13:35 +0300353 }
354 stage("Check workflow_definition") {
355 // Check only for patchset
356 python.setupVirtualenv(vEnv, 'python2', [], "${templateEnvPatched}/requirements.txt")
azvyagintsev3a80e342018-09-19 19:22:48 +0300357 if (gerritDataCC.get('gerritRefSpec', null)) {
358 common.infoMsg(python.runVirtualenvCommand(vEnv, "python ${templateEnvPatched}/workflow_definition_test.py"))
359 } else {
360 common.infoMsg('No need to process: workflow_definition')
361 }
azvyagintsev5c0313d2018-08-13 17:13:35 +0300362 }
363
364 stage("generate models") {
365 dir("${templateEnvHead}/contexts") {
366 for (String x : findFiles(glob: "*.yml")) {
367 contextFileListHead.add(x)
368 }
369 }
370 dir("${templateEnvPatched}/contexts") {
371 for (String x : findFiles(glob: "*.yml")) {
372 contextFileListPatched.add(x)
373 }
374 }
375 // Generate over 2env's - for patchset, and for HEAD
azvyagintsev9df82e52018-09-06 19:17:18 +0300376 def paralellEnvs = [:]
azvyagintsev5c0313d2018-08-13 17:13:35 +0300377 paralellEnvs.failFast = true
azvyagintsev30bc82e2018-08-22 12:26:06 +0300378 paralellEnvs['GenerateEnvHead'] = StepGenerateModels(contextFileListHead, vEnv, templateEnvHead)
azvyagintsev3a80e342018-09-19 19:22:48 +0300379 if (gerritDataCC.get('gerritRefSpec', null)) {
380 paralellEnvs['GenerateEnvPatched'] = StepGenerateModels(contextFileListPatched, vEnv, templateEnvPatched)
381 parallel paralellEnvs
382 } else {
383 paralellEnvs['GenerateEnvPatched'] = { common.warningMsg('No need to process: GenerateEnvPatched') }
384 parallel paralellEnvs
385 sh("rsync -a --exclude '*@tmp' ${templateEnvHead} ${templateEnvPatched}")
azvyagintsev5c0313d2018-08-13 17:13:35 +0300386 }
387
azvyagintsev3a80e342018-09-19 19:22:48 +0300388 // We need 2 git's, one for HEAD, one for PATCHed.
389 // if no patch, use head for both
390 RSHeadDir = common.GetBaseName(headReclassArtifactName, '.tar.gz')
391 RSPatchedDir = common.GetBaseName(patchedReclassArtifactName, '.tar.gz')
392 common.infoMsg("gerritDataRS= ${gerritDataRS}")
393 common.infoMsg("gerritDataRSHEAD= ${gerritDataRSHEAD}")
394 if (gerritDataRS.get('gerritRefSpec', null)) {
395 StepPrepareGit("${env.WORKSPACE}/${RSPatchedDir}/", gerritDataRS).call()
396 StepPrepareGit("${env.WORKSPACE}/${RSHeadDir}/", gerritDataRSHEAD).call()
397 } else {
398 StepPrepareGit("${env.WORKSPACE}/${RSHeadDir}/", gerritDataRS).call()
399 sh("cd ${env.WORKSPACE} ; ln -svf ${RSHeadDir} ${RSPatchedDir}")
400 }
azvyagintsev5c0313d2018-08-13 17:13:35 +0300401 // link all models, to use one global reclass
402 // For HEAD
Denis Egorenkod8748942018-09-07 12:26:20 +0400403 linkReclassModels(contextFileListHead, templateEnvHead, headReclassArtifactName)
azvyagintsev5c0313d2018-08-13 17:13:35 +0300404 // For patched
Denis Egorenkod8748942018-09-07 12:26:20 +0400405 linkReclassModels(contextFileListPatched, templateEnvPatched, patchedReclassArtifactName)
azvyagintsev5c0313d2018-08-13 17:13:35 +0300406 }
407
Denis Egorenkod8748942018-09-07 12:26:20 +0400408 stage("Compare cluster lvl Head/Patched") {
azvyagintsev5c0313d2018-08-13 17:13:35 +0300409 // Compare patched and HEAD reclass pillars
Denis Egorenkod8748942018-09-07 12:26:20 +0400410 compareRoot = "${env.WORKSPACE}/cluster_compare/"
azvyagintsev5c0313d2018-08-13 17:13:35 +0300411 sh(script: """
412 mkdir -pv ${compareRoot}/new ${compareRoot}/old
Denis Egorenkod8748942018-09-07 12:26:20 +0400413 tar -xzf ${patchedReclassArtifactName} --directory ${compareRoot}/new
414 tar -xzf ${headReclassArtifactName} --directory ${compareRoot}/old
azvyagintsev5c0313d2018-08-13 17:13:35 +0300415 """)
416 common.warningMsg('infra/secrets.yml has been skipped from compare!')
azvyagintsev3a80e342018-09-19 19:22:48 +0300417 result = '\n' + common.comparePillars(compareRoot, env.BUILD_URL, "-Ev \'infra/secrets.yml|\\.git\'")
Denis Egorenkod8748942018-09-07 12:26:20 +0400418 currentBuild.description = currentBuild.description ? currentBuild.description + result : result
azvyagintsev5c0313d2018-08-13 17:13:35 +0300419 }
Denis Egorenkod8748942018-09-07 12:26:20 +0400420 stage("TestContexts Head/Patched") {
421 def stepsForParallel = [:]
422 stepsForParallel.failFast = true
423 common.infoMsg("Found: ${contextFileListHead.size()} HEAD contexts to test.")
424 for (String context : contextFileListHead) {
425 def basename = common.GetBaseName(context, '.yml')
426 stepsForParallel.put("ContextHeadTest:${basename}", StepTestModel(basename, headReclassArtifactName, reclassInfoHeadPath))
427 }
azvyagintsev5c0313d2018-08-13 17:13:35 +0300428 common.infoMsg("Found: ${contextFileListPatched.size()} patched contexts to test.")
429 for (String context : contextFileListPatched) {
430 def basename = common.GetBaseName(context, '.yml')
Denis Egorenkod8748942018-09-07 12:26:20 +0400431 stepsForParallel.put("ContextPatchedTest:${basename}", StepTestModel(basename, patchedReclassArtifactName, reclassInfoPatchedPath))
azvyagintsev5c0313d2018-08-13 17:13:35 +0300432 }
433 parallel stepsForParallel
Denis Egorenkod8748942018-09-07 12:26:20 +0400434 common.infoMsg('All TestContexts tests done')
435 }
436 stage("Compare NodesInfo Head/Patched") {
437 // Download all artifacts
438 def stepsForParallel = [:]
439 stepsForParallel.failFast = true
440 common.infoMsg("Found: ${testModelBuildsData.size()} nodeinfo artifacts to download.")
441 testModelBuildsData.each { bname, bdata ->
442 stepsForParallel.put("FetchData:${bname}",
443 getAndUnpackNodesInfoArtifact(bdata.jobname, bdata.copyToDir, bdata.buildId))
444 }
445 parallel stepsForParallel
Denis Egorenkoa6117fc2018-09-11 13:40:00 +0400446 // remove timestamp field from rendered files
447 sh("find ${reclassNodeInfoDir} -type f -exec sed -i '/ timestamp: .*/d' {} \\;")
Denis Egorenkod8748942018-09-07 12:26:20 +0400448 // Compare patched and HEAD reclass pillars
449 result = '\n' + common.comparePillars(reclassNodeInfoDir, env.BUILD_URL, '')
450 currentBuild.description = currentBuild.description ? currentBuild.description + result : result
azvyagintsev5c0313d2018-08-13 17:13:35 +0300451 }
Denis Egorenko04ca9f22018-09-19 16:06:49 +0400452 stage('Check include order') {
azvyagintseve7b8e792018-09-21 17:27:01 +0300453 if (!checkIncludeOrder) {
454 common.infoMsg('Check include order require to much time, and currently disabled!')
455
456 } else {
457 def correctIncludeOrder = ["service", "system", "cluster"]
458 dir(reclassInfoPatchedPath) {
459 def nodeInfoFiles = findFiles(glob: "**/*.reclass.nodeinfo")
460 def messages = ["<b>Wrong include ordering found</b><ul>"]
461 def stepsForParallel = [:]
462 nodeInfoFiles.each { nodeInfo ->
463 stepsForParallel.put("Checking ${nodeInfo.path}:", {
464 def node = readYaml file: nodeInfo.path
465 def classes = node['classes']
466 def curClassID = 0
467 def prevClassID = 0
468 def wrongOrder = false
469 for (String className in classes) {
470 def currentClass = className.tokenize('.')[0]
471 curClassID = correctIncludeOrder.indexOf(currentClass)
472 if (currentClass != correctIncludeOrder[prevClassID]) {
473 if (prevClassID > curClassID) {
474 wrongOrder = true
475 common.warningMsg("File ${nodeInfo.path} contains wrong order of classes including: Includes for ${className} should be declared before ${correctIncludeOrder[prevClassID]} includes")
476 } else {
477 prevClassID = curClassID
478 }
Denis Egorenko04ca9f22018-09-19 16:06:49 +0400479 }
480 }
azvyagintseve7b8e792018-09-21 17:27:01 +0300481 if (wrongOrder) {
482 messages.add("<li>${nodeInfo.path} contains wrong order of classes including</li>")
483 }
484 })
485 }
486 parallel stepsForParallel
487 def includerOrder = '<b>No wrong include order</b>'
488 if (messages.size() != 1) {
489 includerOrder = messages.join('')
490 }
491 currentBuild.description = currentBuild.description ? currentBuild.description + includerOrder : includerOrder
Denis Egorenko04ca9f22018-09-19 16:06:49 +0400492 }
Denis Egorenko04ca9f22018-09-19 16:06:49 +0400493 }
494 }
azvyagintsev5c0313d2018-08-13 17:13:35 +0300495 sh(script: 'find . -mindepth 1 -delete > /dev/null || true')
496
497 } catch (Throwable e) {
azvyagintsev9df82e52018-09-06 19:17:18 +0300498 if (alreadyMerged) {
499 currentBuild.result = 'ABORTED'
500 currentBuild.description = "Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to gate them"
501 return
502 }
azvyagintsev5c0313d2018-08-13 17:13:35 +0300503 currentBuild.result = "FAILURE"
504 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
505 throw e
506 } finally {
507 def dummy = "dummy"
azvyagintsev5c0313d2018-08-13 17:13:35 +0300508 }
509 }
chnydae80bb922017-05-29 17:48:40 +0200510}