blob: 674d70c54fb06c6361214297d1320ed4e79f3f93 [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'
azvyagintsev5c0313d2018-08-13 17:13:35 +030021
azvyagintsev9df82e52018-09-06 19:17:18 +030022// Global var's
23alreadyMerged = false
24gerritConData = [credentialsId : env.CREDENTIALS_ID,
25 gerritName : env.GERRIT_NAME ?: 'mcp-jenkins',
26 gerritHost : env.GERRIT_HOST ?: 'gerrit.mcp.mirantis.net',
azvyagintsevb266ad22018-09-11 12:11:11 +030027 gerritScheme : env.GERRIT_SCHEME ?: 'ssh',
28 gerritPort : env.GERRIT_PORT ?: '29418',
azvyagintsev9df82e52018-09-06 19:17:18 +030029 gerritRefSpec : null,
30 gerritProject : null,
31 withWipeOut : true,
32 GERRIT_CHANGE_NUMBER: null]
33//
34//ccTemplatesRepo = env.COOKIECUTTER_TEMPLATE_URL ?: 'ssh://mcp-jenkins@gerrit.mcp.mirantis.net:29418/mk/cookiecutter-templates'
azvyagintsev3a80e342018-09-19 19:22:48 +030035gerritDataCCHEAD = [:]
azvyagintsev9df82e52018-09-06 19:17:18 +030036gerritDataCC = [:]
37gerritDataCC << gerritConData
38gerritDataCC['gerritBranch'] = env.COOKIECUTTER_TEMPLATE_BRANCH ?: 'master'
39gerritDataCC['gerritProject'] = 'mk/cookiecutter-templates'
40//
41//reclassSystemRepo = env.RECLASS_SYSTEM_URL ?: 'ssh://mcp-jenkins@gerrit.mcp.mirantis.net:29418/salt-models/reclass-system'
azvyagintsev3a80e342018-09-19 19:22:48 +030042gerritDataRSHEAD = [:]
azvyagintsev9df82e52018-09-06 19:17:18 +030043gerritDataRS = [:]
44gerritDataRS << gerritConData
45gerritDataRS['gerritBranch'] = env.RECLASS_MODEL_BRANCH ?: 'master'
46gerritDataRS['gerritProject'] = 'salt-models/reclass-system'
47
48// version of debRepos, aka formulas\reclass
azvyagintsev10e24012018-09-10 21:36:32 +030049testDistribRevision = env.DISTRIB_REVISION ?: 'nightly'
50reclassVersion = 'v1.5.4'
azvyagintsevb266ad22018-09-11 12:11:11 +030051if (env.RECLASS_VERSION) {
azvyagintsev9df82e52018-09-06 19:17:18 +030052 reclassVersion = env.RECLASS_VERSION
Vasyl Saienko772e1232018-07-23 14:42:24 +030053}
Denis Egorenkod8748942018-09-07 12:26:20 +040054// Name of sub-test chunk job
55chunkJobName = "test-mk-cookiecutter-templates-chunk"
56testModelBuildsData = [:]
Vasyl Saienko772e1232018-07-23 14:42:24 +030057
chnyda467f10f2017-05-30 17:25:07 +020058def generateSaltMaster(modEnv, clusterDomain, clusterName) {
azvyagintsev5c0313d2018-08-13 17:13:35 +030059 def nodeFile = "${modEnv}/nodes/cfg01.${clusterDomain}.yml"
60 def nodeString = """classes:
chnydae80bb922017-05-29 17:48:40 +020061- cluster.${clusterName}.infra.config
62parameters:
63 _param:
64 linux_system_codename: xenial
65 reclass_data_revision: master
66 linux:
67 system:
68 name: cfg01
69 domain: ${clusterDomain}
70"""
azvyagintsev5c0313d2018-08-13 17:13:35 +030071 sh "mkdir -p ${modEnv}/nodes/"
72 println "Create file ${nodeFile}"
73 writeFile(file: nodeFile, text: nodeString)
azvyagintsev87985532018-07-10 20:49:38 +030074}
75
azvyagintsev30bc82e2018-08-22 12:26:06 +030076/**
77 *
78 * @param contextFile - path to `contexts/XXX.yaml file`
azvyagintsev9df82e52018-09-06 19:17:18 +030079 * @param virtualenv - pyvenv with CC and dep's
azvyagintsev30bc82e2018-08-22 12:26:06 +030080 * @param templateEnvDir - root of CookieCutter
81 * @return
82 */
83
azvyagintsev5c0313d2018-08-13 17:13:35 +030084def generateModel(contextFile, virtualenv, templateEnvDir) {
85 def modelEnv = "${templateEnvDir}/model"
86 def basename = common.GetBaseName(contextFile, '.yml')
87 def generatedModel = "${modelEnv}/${basename}"
88 def content = readFile(file: "${templateEnvDir}/contexts/${contextFile}")
89 def templateContext = readYaml text: content
90 def clusterDomain = templateContext.default_context.cluster_domain
91 def clusterName = templateContext.default_context.cluster_name
92 def outputDestination = "${generatedModel}/classes/cluster/${clusterName}"
93 def templateBaseDir = templateEnvDir
94 def templateDir = "${templateEnvDir}/dir"
95 def templateOutputDir = templateBaseDir
azvyagintsev30bc82e2018-08-22 12:26:06 +030096 dir(templateEnvDir) {
97 sh(script: "rm -rf ${generatedModel} || true")
98 common.infoMsg("Generating model from context ${contextFile}")
99 def productList = ["infra", "cicd", "opencontrail", "kubernetes", "openstack", "oss", "stacklight", "ceph"]
100 for (product in productList) {
chnydae80bb922017-05-29 17:48:40 +0200101
azvyagintsev30bc82e2018-08-22 12:26:06 +0300102 // get templateOutputDir and productDir
103 if (product.startsWith("stacklight")) {
104 templateOutputDir = "${templateEnvDir}/output/stacklight"
105 try {
106 productDir = "stacklight" + templateContext.default_context['stacklight_version']
107 } catch (Throwable e) {
108 productDir = "stacklight1"
109 }
110 } else {
111 templateOutputDir = "${templateEnvDir}/output/${product}"
112 productDir = product
azvyagintsev5c0313d2018-08-13 17:13:35 +0300113 }
azvyagintsev30bc82e2018-08-22 12:26:06 +0300114
115 if (product == "infra" || (templateContext.default_context["${product}_enabled"]
116 && templateContext.default_context["${product}_enabled"].toBoolean())) {
117
118 templateDir = "${templateEnvDir}/cluster_product/${productDir}"
119 common.infoMsg("Generating product " + product + " from " + templateDir + " to " + templateOutputDir)
120
121 sh "rm -rf ${templateOutputDir} || true"
122 sh "mkdir -p ${templateOutputDir}"
123 sh "mkdir -p ${outputDestination}"
124
125 python.buildCookiecutterTemplate(templateDir, content, templateOutputDir, virtualenv, templateBaseDir)
126 sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
127 } else {
128 common.warningMsg("Product " + product + " is disabled")
129 }
azvyagintsev5c0313d2018-08-13 17:13:35 +0300130 }
azvyagintsev30bc82e2018-08-22 12:26:06 +0300131 generateSaltMaster(generatedModel, clusterDomain, clusterName)
chnydae80bb922017-05-29 17:48:40 +0200132 }
chnydae80bb922017-05-29 17:48:40 +0200133}
134
Denis Egorenkod8748942018-09-07 12:26:20 +0400135def getAndUnpackNodesInfoArtifact(jobName, copyTo, build) {
136 return {
137 dir(copyTo) {
138 copyArtifacts(projectName: jobName, selector: specific(build), filter: "nodesinfo.tar.gz")
azvyagintsev3a80e342018-09-19 19:22:48 +0300139 sh "tar -xf nodesinfo.tar.gz"
Denis Egorenkod8748942018-09-07 12:26:20 +0400140 sh "rm -v nodesinfo.tar.gz"
141 }
142 }
143}
azvyagintsev87985532018-07-10 20:49:38 +0300144
Denis Egorenkod8748942018-09-07 12:26:20 +0400145def testModel(modelFile, reclassArtifactName, artifactCopyPath) {
azvyagintsev5c0313d2018-08-13 17:13:35 +0300146 // modelFile - `modelfiname` from model/modelfiname/modelfiname.yaml
147 //* Grub all models and send it to check in paralell - by one in thread.
Denis Egorenkod8748942018-09-07 12:26:20 +0400148 def _uuid = "${env.JOB_NAME.toLowerCase()}_${env.BUILD_TAG.toLowerCase()}_${modelFile.toLowerCase()}_" + UUID.randomUUID().toString().take(8)
149 def _values_string = """
azvyagintsev87985532018-07-10 20:49:38 +0300150 ---
Denis Egorenkod8748942018-09-07 12:26:20 +0400151 MODELS_TARGZ: "${env.BUILD_URL}/artifact/${reclassArtifactName}"
152 DockerCName: "${_uuid}"
azvyagintsev87985532018-07-10 20:49:38 +0300153 testReclassEnv: "model/${modelFile}/"
154 modelFile: "contexts/${modelFile}.yml"
azvyagintsev9df82e52018-09-06 19:17:18 +0300155 DISTRIB_REVISION: "${testDistribRevision}"
azvyagintsev87985532018-07-10 20:49:38 +0300156 reclassVersion: "${reclassVersion}"
157 """
Denis Egorenkod8748942018-09-07 12:26:20 +0400158 def chunkJob = build job: chunkJobName, parameters: [
azvyagintsev08b34e32018-09-12 12:29:42 +0300159 [$class: 'TextParameterValue', name: 'EXTRA_VARIABLES_YAML',
azvyagintsev30bc82e2018-08-22 12:26:06 +0300160 value : _values_string.stripIndent()],
azvyagintsev5c0313d2018-08-13 17:13:35 +0300161 ]
Denis Egorenkod8748942018-09-07 12:26:20 +0400162 // Put sub-job info into global map.
163 testModelBuildsData.put(_uuid, ['jobname' : chunkJob.fullProjectName,
164 'copyToDir': "${artifactCopyPath}/${modelFile}",
165 'buildId' : "${chunkJob.number}"])
chnydabc63c9a2017-05-30 15:37:54 +0200166}
167
Denis Egorenkod8748942018-09-07 12:26:20 +0400168def StepTestModel(basename, reclassArtifactName, artifactCopyPath) {
azvyagintsev5c0313d2018-08-13 17:13:35 +0300169 // We need to wrap what we return in a Groovy closure, or else it's invoked
170 // when this method is called, not when we pass it to parallel.
171 // To do this, you need to wrap the code below in { }, and either return
172 // that explicitly, or use { -> } syntax.
173 // return node object
174 return {
175 node(slaveNode) {
Denis Egorenkod8748942018-09-07 12:26:20 +0400176 testModel(basename, reclassArtifactName, artifactCopyPath)
azvyagintsev5c0313d2018-08-13 17:13:35 +0300177 }
chnydae80bb922017-05-29 17:48:40 +0200178 }
azvyagintsev87985532018-07-10 20:49:38 +0300179}
180
azvyagintsev9df82e52018-09-06 19:17:18 +0300181def StepPrepareGit(templateEnvFolder, gerrit_data) {
azvyagintsev5c0313d2018-08-13 17:13:35 +0300182 // return git clone object
183 return {
azvyagintsev9df82e52018-09-06 19:17:18 +0300184 def checkouted = false
185 common.infoMsg("StepPrepareGit: ${gerrit_data}")
azvyagintsev5c0313d2018-08-13 17:13:35 +0300186 // fetch needed sources
187 dir(templateEnvFolder) {
azvyagintsev9df82e52018-09-06 19:17:18 +0300188 if (gerrit_data['gerritRefSpec']) {
189 // Those part might be not work,in case manual var's pass
190 def gerritChange = gerrit.getGerritChange(gerrit_data['gerritName'], gerrit_data['gerritHost'],
191 gerrit_data['GERRIT_CHANGE_NUMBER'], gerrit_data['credentialsId'])
azvyagintsev5c0313d2018-08-13 17:13:35 +0300192 merged = gerritChange.status == "MERGED"
193 if (!merged) {
azvyagintsev9df82e52018-09-06 19:17:18 +0300194 checkouted = gerrit.gerritPatchsetCheckout(gerrit_data)
azvyagintsev5c0313d2018-08-13 17:13:35 +0300195 } else {
azvyagintsev9df82e52018-09-06 19:17:18 +0300196 // update global variable for pretty return from pipeline
197 alreadyMerged = true
198 common.successMsg("Change ${gerrit_data['GERRIT_CHANGE_NUMBER']} is already merged, no need to gate them")
199 error('change already merged')
azvyagintsev87985532018-07-10 20:49:38 +0300200 }
azvyagintsev5c0313d2018-08-13 17:13:35 +0300201 } else {
azvyagintsev9df82e52018-09-06 19:17:18 +0300202 // Get clean HEAD
203 gerrit_data['useGerritTriggerBuildChooser'] = false
204 checkouted = gerrit.gerritPatchsetCheckout(gerrit_data)
205 if (!checkouted) {
206 error("Failed to get repo:${gerrit_data}")
207 }
azvyagintsev87985532018-07-10 20:49:38 +0300208 }
azvyagintsev5c0313d2018-08-13 17:13:35 +0300209 }
azvyagintsev87985532018-07-10 20:49:38 +0300210 }
azvyagintsev5c0313d2018-08-13 17:13:35 +0300211}
212
213def StepGenerateModels(_contextFileList, _virtualenv, _templateEnvDir) {
214 return {
215 for (contextFile in _contextFileList) {
216 generateModel(contextFile, _virtualenv, _templateEnvDir)
217 }
218 }
219}
220
azvyagintsev9df82e52018-09-06 19:17:18 +0300221def globalVariatorsUpdate() {
222 // Simple function, to check and define branch-around variables
223 // In general, simply make transition updates for non-master branch
224 // based on magic logic
225 def message = ''
azvyagintsevb266ad22018-09-11 12:11:11 +0300226 if (env.GERRIT_PROJECT) {
azvyagintseve0eb6cd2018-09-12 13:00:40 +0300227 // TODO are we going to have such branches?
azvyagintsev9df82e52018-09-06 19:17:18 +0300228 if (!['nightly', 'testing', 'stable', 'proposed', 'master'].contains(env.GERRIT_BRANCH)) {
229 gerritDataCC['gerritBranch'] = env.GERRIT_BRANCH
230 gerritDataRS['gerritBranch'] = env.GERRIT_BRANCH
231 // 'binary' branch logic w\o 'release/' prefix
232 testDistribRevision = env.GERRIT_BRANCH.split('/')[-1]
233 // Check if we are going to test bleeding-edge release, which doesn't have binary release yet
234 if (!common.checkRemoteBinary([apt_mk_version: testDistribRevision]).linux_system_repo_url) {
235 common.errorMsg("Binary release: ${testDistribRevision} not exist. Fallback to 'proposed'! ")
236 testDistribRevision = 'proposed'
237 }
238 }
239 // Identify, who triggered. To whom we should pass refspec
240 if (env.GERRIT_PROJECT == 'salt-models/reclass-system') {
241 gerritDataRS['gerritRefSpec'] = env.GERRIT_REFSPEC
242 gerritDataRS['GERRIT_CHANGE_NUMBER'] = env.GERRIT_CHANGE_NUMBER
243 message = "<br/>RECLASS_SYSTEM_GIT_REF =>${gerritDataRS['gerritRefSpec']}"
244 } else if (env.GERRIT_PROJECT == 'mk/cookiecutter-templates') {
245 gerritDataCC['gerritRefSpec'] = env.GERRIT_REFSPEC
246 gerritDataCC['GERRIT_CHANGE_NUMBER'] = env.GERRIT_CHANGE_NUMBER
247 message = "<br/>COOKIECUTTER_TEMPLATE_REF =>${gerritDataCC['gerritRefSpec']}"
248 } else {
249 error("Unsuported gerrit-project triggered:${env.GERRIT_PROJECT}")
250 }
251
252 message = "<font color='red'>GerritTrigger detected! We are in auto-mode:</font>" +
253 "<br/>Test env variables has been changed:" +
254 "<br/>COOKIECUTTER_TEMPLATE_BRANCH => ${gerritDataCC['gerritBranch']}" +
255 "<br/>DISTRIB_REVISION =>${testDistribRevision}" +
Denis Egorenko04ca9f22018-09-19 16:06:49 +0400256 "<br/>RECLASS_MODEL_BRANCH=> ${gerritDataRS['gerritBranch']}" + message + "<br/>"
azvyagintsev9df82e52018-09-06 19:17:18 +0300257 common.warningMsg(message)
258 currentBuild.description = currentBuild.description ? message + "<br/>" + currentBuild.description : message
259 } else {
260 // Check for passed variables:
azvyagintsev3a80e342018-09-19 19:22:48 +0300261 gerritDataRS['gerritRefSpec'] = env.RECLASS_SYSTEM_GIT_REF ?: null
262 gerritDataCC['gerritRefSpec'] = env.COOKIECUTTER_TEMPLATE_REF ?: null
263 message = "<font color='red'>Non-gerrit trigger run detected!</font>" + "<br/>"
azvyagintsev9df82e52018-09-06 19:17:18 +0300264 currentBuild.description = currentBuild.description ? message + "<br/>" + currentBuild.description : message
265 }
azvyagintsev3a80e342018-09-19 19:22:48 +0300266 gerritDataCCHEAD << gerritDataCC
267 gerritDataCCHEAD['gerritRefSpec'] = null
268 gerritDataCCHEAD['GERRIT_CHANGE_NUMBER'] = null
269 gerritDataRSHEAD << gerritDataRS
270 gerritDataRSHEAD['gerritRefSpec'] = null
271 gerritDataRSHEAD['GERRIT_CHANGE_NUMBER'] = null
272
Denis Egorenkod8748942018-09-07 12:26:20 +0400273}
azvyagintsev9df82e52018-09-06 19:17:18 +0300274
Denis Egorenkoa6117fc2018-09-11 13:40:00 +0400275def replaceGeneratedValues(path) {
276 def files = sh(script: "find ${path} -name 'secrets.yml'", returnStdout: true)
277 def stepsForParallel = [:]
278 stepsForParallel.failFast = true
279 files.tokenize().each {
280 stepsForParallel.put("Removing generated passwords/secrets from ${it}",
281 {
282 def secrets = readYaml file: it
azvyagintsev08b34e32018-09-12 12:29:42 +0300283 for (String key in secrets['parameters']['_param'].keySet()) {
Denis Egorenkoa6117fc2018-09-11 13:40:00 +0400284 secrets['parameters']['_param'][key] = 'generated'
285 }
286 // writeYaml can't write to already existing file
287 writeYaml file: "${it}.tmp", data: secrets
288 sh "mv ${it}.tmp ${it}"
289 })
290 }
291 parallel stepsForParallel
292}
293
Denis Egorenkod8748942018-09-07 12:26:20 +0400294def linkReclassModels(contextList, envPath, archiveName) {
295 // to be able share reclass for all subenvs
296 // Also, makes artifact test more solid - use one reclass for all of sub-models.
297 // Archive Structure will be:
298 // tar.gz
299 // ├── contexts
300 // │   └── ceph.yml
azvyagintsev3a80e342018-09-19 19:22:48 +0300301 // ├── ${reclassDirName} <<< reclass system
Denis Egorenkod8748942018-09-07 12:26:20 +0400302 // ├── model
303 // │   └── ceph <<< from `context basename`
304 // │   ├── classes
305 // │   │   ├── cluster
azvyagintsev3a80e342018-09-19 19:22:48 +0300306 // │   │   └── system -> ../../../${reclassDirName}
Denis Egorenkod8748942018-09-07 12:26:20 +0400307 // │   └── nodes
308 // │   └── cfg01.ceph-cluster-domain.local.yml
309 dir(envPath) {
310 for (String context : contextList) {
311 def basename = common.GetBaseName(context, '.yml')
312 dir("${envPath}/model/${basename}") {
azvyagintsev3a80e342018-09-19 19:22:48 +0300313 sh(script: "mkdir -p classes/; ln -sfv ../../../../${common.GetBaseName(archiveName, '.tar.gz')} classes/system ")
Denis Egorenkod8748942018-09-07 12:26:20 +0400314 }
315 }
Denis Egorenkoa6117fc2018-09-11 13:40:00 +0400316 // replace all generated passwords/secrets/keys with hardcode value for infra/secrets.yaml
317 replaceGeneratedValues("${envPath}/model")
azvyagintsev3a80e342018-09-19 19:22:48 +0300318 // Save all models and all contexts. Warning! `h` flag must be used!
319 sh(script: "set -ex; tar -czhf ${env.WORKSPACE}/${archiveName} --exclude='*@tmp' model contexts", returnStatus: true)
Denis Egorenkod8748942018-09-07 12:26:20 +0400320 }
azvyagintsev3a80e342018-09-19 19:22:48 +0300321 archiveArtifacts artifacts: archiveName
azvyagintsev9df82e52018-09-06 19:17:18 +0300322}
323
azvyagintsev5c0313d2018-08-13 17:13:35 +0300324timeout(time: 1, unit: 'HOURS') {
325 node(slaveNode) {
azvyagintsev9df82e52018-09-06 19:17:18 +0300326 globalVariatorsUpdate()
azvyagintsev30bc82e2018-08-22 12:26:06 +0300327 def templateEnvHead = "${env.WORKSPACE}/EnvHead/"
328 def templateEnvPatched = "${env.WORKSPACE}/EnvPatched/"
azvyagintsev5c0313d2018-08-13 17:13:35 +0300329 def contextFileListHead = []
330 def contextFileListPatched = []
331 def vEnv = "${env.WORKSPACE}/venv"
Denis Egorenkod8748942018-09-07 12:26:20 +0400332 def headReclassArtifactName = "head_reclass.tar.gz"
333 def patchedReclassArtifactName = "patched_reclass.tar.gz"
334 def reclassNodeInfoDir = "${env.WORKSPACE}/reclassNodeInfo_compare/"
335 def reclassInfoHeadPath = "${reclassNodeInfoDir}/old"
336 def reclassInfoPatchedPath = "${reclassNodeInfoDir}/new"
azvyagintsev5c0313d2018-08-13 17:13:35 +0300337 try {
338 sh(script: 'find . -mindepth 1 -delete > /dev/null || true')
339 stage('Download and prepare CC env') {
340 // Prepare 2 env - for patchset, and for HEAD
azvyagintsev9df82e52018-09-06 19:17:18 +0300341 def paralellEnvs = [:]
azvyagintsev5c0313d2018-08-13 17:13:35 +0300342 paralellEnvs.failFast = true
azvyagintsev9df82e52018-09-06 19:17:18 +0300343 paralellEnvs['downloadEnvHead'] = StepPrepareGit(templateEnvHead, gerritDataCCHEAD)
azvyagintsev3a80e342018-09-19 19:22:48 +0300344 if (gerritDataCC.get('gerritRefSpec', null)) {
345 paralellEnvs['downloadEnvPatched'] = StepPrepareGit(templateEnvPatched, gerritDataCC)
346 parallel paralellEnvs
347 } else {
348 paralellEnvs['downloadEnvPatched'] = { common.warningMsg('No need to process: downloadEnvPatched') }
349 parallel paralellEnvs
350 sh("rsync -a --exclude '*@tmp' ${templateEnvHead} ${templateEnvPatched}")
351 }
azvyagintsev5c0313d2018-08-13 17:13:35 +0300352 }
353 stage("Check workflow_definition") {
354 // Check only for patchset
355 python.setupVirtualenv(vEnv, 'python2', [], "${templateEnvPatched}/requirements.txt")
azvyagintsev3a80e342018-09-19 19:22:48 +0300356 if (gerritDataCC.get('gerritRefSpec', null)) {
357 common.infoMsg(python.runVirtualenvCommand(vEnv, "python ${templateEnvPatched}/workflow_definition_test.py"))
358 } else {
359 common.infoMsg('No need to process: workflow_definition')
360 }
azvyagintsev5c0313d2018-08-13 17:13:35 +0300361 }
362
363 stage("generate models") {
364 dir("${templateEnvHead}/contexts") {
365 for (String x : findFiles(glob: "*.yml")) {
366 contextFileListHead.add(x)
367 }
368 }
369 dir("${templateEnvPatched}/contexts") {
370 for (String x : findFiles(glob: "*.yml")) {
371 contextFileListPatched.add(x)
372 }
373 }
374 // Generate over 2env's - for patchset, and for HEAD
azvyagintsev9df82e52018-09-06 19:17:18 +0300375 def paralellEnvs = [:]
azvyagintsev5c0313d2018-08-13 17:13:35 +0300376 paralellEnvs.failFast = true
azvyagintsev30bc82e2018-08-22 12:26:06 +0300377 paralellEnvs['GenerateEnvHead'] = StepGenerateModels(contextFileListHead, vEnv, templateEnvHead)
azvyagintsev3a80e342018-09-19 19:22:48 +0300378 if (gerritDataCC.get('gerritRefSpec', null)) {
379 paralellEnvs['GenerateEnvPatched'] = StepGenerateModels(contextFileListPatched, vEnv, templateEnvPatched)
380 parallel paralellEnvs
381 } else {
382 paralellEnvs['GenerateEnvPatched'] = { common.warningMsg('No need to process: GenerateEnvPatched') }
383 parallel paralellEnvs
384 sh("rsync -a --exclude '*@tmp' ${templateEnvHead} ${templateEnvPatched}")
azvyagintsev5c0313d2018-08-13 17:13:35 +0300385 }
386
azvyagintsev3a80e342018-09-19 19:22:48 +0300387 // We need 2 git's, one for HEAD, one for PATCHed.
388 // if no patch, use head for both
389 RSHeadDir = common.GetBaseName(headReclassArtifactName, '.tar.gz')
390 RSPatchedDir = common.GetBaseName(patchedReclassArtifactName, '.tar.gz')
391 common.infoMsg("gerritDataRS= ${gerritDataRS}")
392 common.infoMsg("gerritDataRSHEAD= ${gerritDataRSHEAD}")
393 if (gerritDataRS.get('gerritRefSpec', null)) {
394 StepPrepareGit("${env.WORKSPACE}/${RSPatchedDir}/", gerritDataRS).call()
395 StepPrepareGit("${env.WORKSPACE}/${RSHeadDir}/", gerritDataRSHEAD).call()
396 } else {
397 StepPrepareGit("${env.WORKSPACE}/${RSHeadDir}/", gerritDataRS).call()
398 sh("cd ${env.WORKSPACE} ; ln -svf ${RSHeadDir} ${RSPatchedDir}")
399 }
azvyagintsev5c0313d2018-08-13 17:13:35 +0300400 // link all models, to use one global reclass
401 // For HEAD
Denis Egorenkod8748942018-09-07 12:26:20 +0400402 linkReclassModels(contextFileListHead, templateEnvHead, headReclassArtifactName)
azvyagintsev5c0313d2018-08-13 17:13:35 +0300403 // For patched
Denis Egorenkod8748942018-09-07 12:26:20 +0400404 linkReclassModels(contextFileListPatched, templateEnvPatched, patchedReclassArtifactName)
azvyagintsev5c0313d2018-08-13 17:13:35 +0300405 }
406
Denis Egorenkod8748942018-09-07 12:26:20 +0400407 stage("Compare cluster lvl Head/Patched") {
azvyagintsev5c0313d2018-08-13 17:13:35 +0300408 // Compare patched and HEAD reclass pillars
Denis Egorenkod8748942018-09-07 12:26:20 +0400409 compareRoot = "${env.WORKSPACE}/cluster_compare/"
azvyagintsev5c0313d2018-08-13 17:13:35 +0300410 sh(script: """
411 mkdir -pv ${compareRoot}/new ${compareRoot}/old
Denis Egorenkod8748942018-09-07 12:26:20 +0400412 tar -xzf ${patchedReclassArtifactName} --directory ${compareRoot}/new
413 tar -xzf ${headReclassArtifactName} --directory ${compareRoot}/old
azvyagintsev5c0313d2018-08-13 17:13:35 +0300414 """)
415 common.warningMsg('infra/secrets.yml has been skipped from compare!')
azvyagintsev3a80e342018-09-19 19:22:48 +0300416 result = '\n' + common.comparePillars(compareRoot, env.BUILD_URL, "-Ev \'infra/secrets.yml|\\.git\'")
Denis Egorenkod8748942018-09-07 12:26:20 +0400417 currentBuild.description = currentBuild.description ? currentBuild.description + result : result
azvyagintsev5c0313d2018-08-13 17:13:35 +0300418 }
Denis Egorenkod8748942018-09-07 12:26:20 +0400419 stage("TestContexts Head/Patched") {
420 def stepsForParallel = [:]
421 stepsForParallel.failFast = true
422 common.infoMsg("Found: ${contextFileListHead.size()} HEAD contexts to test.")
423 for (String context : contextFileListHead) {
424 def basename = common.GetBaseName(context, '.yml')
425 stepsForParallel.put("ContextHeadTest:${basename}", StepTestModel(basename, headReclassArtifactName, reclassInfoHeadPath))
426 }
azvyagintsev5c0313d2018-08-13 17:13:35 +0300427 common.infoMsg("Found: ${contextFileListPatched.size()} patched contexts to test.")
428 for (String context : contextFileListPatched) {
429 def basename = common.GetBaseName(context, '.yml')
Denis Egorenkod8748942018-09-07 12:26:20 +0400430 stepsForParallel.put("ContextPatchedTest:${basename}", StepTestModel(basename, patchedReclassArtifactName, reclassInfoPatchedPath))
azvyagintsev5c0313d2018-08-13 17:13:35 +0300431 }
432 parallel stepsForParallel
Denis Egorenkod8748942018-09-07 12:26:20 +0400433 common.infoMsg('All TestContexts tests done')
434 }
435 stage("Compare NodesInfo Head/Patched") {
436 // Download all artifacts
437 def stepsForParallel = [:]
438 stepsForParallel.failFast = true
439 common.infoMsg("Found: ${testModelBuildsData.size()} nodeinfo artifacts to download.")
440 testModelBuildsData.each { bname, bdata ->
441 stepsForParallel.put("FetchData:${bname}",
442 getAndUnpackNodesInfoArtifact(bdata.jobname, bdata.copyToDir, bdata.buildId))
443 }
444 parallel stepsForParallel
Denis Egorenkoa6117fc2018-09-11 13:40:00 +0400445 // remove timestamp field from rendered files
446 sh("find ${reclassNodeInfoDir} -type f -exec sed -i '/ timestamp: .*/d' {} \\;")
Denis Egorenkod8748942018-09-07 12:26:20 +0400447 // Compare patched and HEAD reclass pillars
448 result = '\n' + common.comparePillars(reclassNodeInfoDir, env.BUILD_URL, '')
449 currentBuild.description = currentBuild.description ? currentBuild.description + result : result
azvyagintsev5c0313d2018-08-13 17:13:35 +0300450 }
Denis Egorenko04ca9f22018-09-19 16:06:49 +0400451 stage('Check include order') {
452 def correctIncludeOrder = ["service", "system", "cluster"]
453 dir(reclassInfoPatchedPath) {
454 def nodeInfoFiles = findFiles(glob: "**/*.reclass.nodeinfo")
455 def messages = ["<b>Wrong include ordering found</b><ul>"]
456 def stepsForParallel = [:]
457 nodeInfoFiles.each { nodeInfo ->
458 stepsForParallel.put("Checking ${nodeInfo.path}:", {
459 def node = readYaml file: nodeInfo.path
460 def classes = node['classes']
461 def curClassID = 0
462 def prevClassID = 0
463 def wrongOrder = false
464 for(String className in classes) {
465 def currentClass = className.tokenize('.')[0]
466 curClassID = correctIncludeOrder.indexOf(currentClass)
467 if (currentClass != correctIncludeOrder[prevClassID]) {
468 if (prevClassID > curClassID) {
469 wrongOrder = true
470 common.warningMsg("File ${nodeInfo.path} contains wrong order of classes including: Includes for ${className} should be declared before ${correctIncludeOrder[prevClassID]} includes")
471 } else {
472 prevClassID = curClassID
473 }
474 }
475 }
476 if(wrongOrder) {
477 messages.add("<li>${nodeInfo.path} contains wrong order of classes including</li>")
478 }
479 })
480 }
481 parallel stepsForParallel
482 def includerOrder = '<b>No wrong include order</b>'
483 if (messages.size() != 1) {
484 includerOrder = messages.join('')
485 }
486 currentBuild.description = currentBuild.description ? currentBuild.description + includerOrder : includerOrder
487 }
488 }
azvyagintsev5c0313d2018-08-13 17:13:35 +0300489 sh(script: 'find . -mindepth 1 -delete > /dev/null || true')
490
491 } catch (Throwable e) {
azvyagintsev9df82e52018-09-06 19:17:18 +0300492 if (alreadyMerged) {
493 currentBuild.result = 'ABORTED'
494 currentBuild.description = "Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to gate them"
495 return
496 }
azvyagintsev5c0313d2018-08-13 17:13:35 +0300497 currentBuild.result = "FAILURE"
498 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
499 throw e
500 } finally {
501 def dummy = "dummy"
azvyagintsev5c0313d2018-08-13 17:13:35 +0300502 }
503 }
chnydae80bb922017-05-29 17:48:40 +0200504}