blob: fd09fe5421a6ef0b7936728c0c7e29224d9a43ee [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
Denis Egorenko47169272018-10-11 16:01:38 +040020def extraVarsYAML = env.EXTRA_VARIABLES_YAML.trim() ?: ''
Denis Egorenko535d5b62018-09-28 11:28:10 +040021if (extraVarsYAML) {
22 common.mergeEnv(env, extraVarsYAML)
23}
24
azvyagintsev3a80e342018-09-19 19:22:48 +030025slaveNode = env.SLAVE_NODE ?: 'docker'
azvyagintseve7b8e792018-09-21 17:27:01 +030026checkIncludeOrder = env.CHECK_INCLUDE_ORDER ?: false
azvyagintsev5c0313d2018-08-13 17:13:35 +030027
azvyagintsev9df82e52018-09-06 19:17:18 +030028// Global var's
29alreadyMerged = false
30gerritConData = [credentialsId : env.CREDENTIALS_ID,
31 gerritName : env.GERRIT_NAME ?: 'mcp-jenkins',
Roman Vyalovf0c596e2018-10-01 17:56:09 +030032 gerritHost : env.GERRIT_HOST ?: 'gerrit.mcp.mirantis.com',
azvyagintsevb266ad22018-09-11 12:11:11 +030033 gerritScheme : env.GERRIT_SCHEME ?: 'ssh',
34 gerritPort : env.GERRIT_PORT ?: '29418',
azvyagintsev9df82e52018-09-06 19:17:18 +030035 gerritRefSpec : null,
36 gerritProject : null,
37 withWipeOut : true,
38 GERRIT_CHANGE_NUMBER: null]
39//
Roman Vyalovf0c596e2018-10-01 17:56:09 +030040//ccTemplatesRepo = env.COOKIECUTTER_TEMPLATE_URL ?: 'ssh://mcp-jenkins@gerrit.mcp.mirantis.com:29418/mk/cookiecutter-templates'
azvyagintsev3a80e342018-09-19 19:22:48 +030041gerritDataCCHEAD = [:]
azvyagintsev9df82e52018-09-06 19:17:18 +030042gerritDataCC = [:]
43gerritDataCC << gerritConData
44gerritDataCC['gerritBranch'] = env.COOKIECUTTER_TEMPLATE_BRANCH ?: 'master'
azvyagintsev458278c2018-09-25 18:38:30 +030045gerritDataCC['gerritRefSpec'] = env.COOKIECUTTER_TEMPLATE_REF ?: null
azvyagintsev9df82e52018-09-06 19:17:18 +030046gerritDataCC['gerritProject'] = 'mk/cookiecutter-templates'
47//
Roman Vyalovf0c596e2018-10-01 17:56:09 +030048//reclassSystemRepo = env.RECLASS_SYSTEM_URL ?: 'ssh://mcp-jenkins@gerrit.mcp.mirantis.com:29418/salt-models/reclass-system'
azvyagintsev3a80e342018-09-19 19:22:48 +030049gerritDataRSHEAD = [:]
azvyagintsev9df82e52018-09-06 19:17:18 +030050gerritDataRS = [:]
51gerritDataRS << gerritConData
52gerritDataRS['gerritBranch'] = env.RECLASS_MODEL_BRANCH ?: 'master'
azvyagintsev458278c2018-09-25 18:38:30 +030053gerritDataRS['gerritRefSpec'] = env.RECLASS_SYSTEM_GIT_REF ?: null
azvyagintsev9df82e52018-09-06 19:17:18 +030054gerritDataRS['gerritProject'] = 'salt-models/reclass-system'
55
azvyagintsevec055b42018-10-11 12:59:05 +030056// version of debRepos, aka formulas|reclass|ubuntu
azvyagintsev10e24012018-09-10 21:36:32 +030057testDistribRevision = env.DISTRIB_REVISION ?: 'nightly'
Denis Egorenko47169272018-10-11 16:01:38 +040058
Denis Egorenkod8748942018-09-07 12:26:20 +040059// Name of sub-test chunk job
60chunkJobName = "test-mk-cookiecutter-templates-chunk"
61testModelBuildsData = [:]
Vasyl Saienko772e1232018-07-23 14:42:24 +030062
chnyda467f10f2017-05-30 17:25:07 +020063def generateSaltMaster(modEnv, clusterDomain, clusterName) {
azvyagintsev5c0313d2018-08-13 17:13:35 +030064 def nodeFile = "${modEnv}/nodes/cfg01.${clusterDomain}.yml"
65 def nodeString = """classes:
chnydae80bb922017-05-29 17:48:40 +020066- cluster.${clusterName}.infra.config
67parameters:
68 _param:
69 linux_system_codename: xenial
70 reclass_data_revision: master
71 linux:
72 system:
73 name: cfg01
74 domain: ${clusterDomain}
75"""
azvyagintsev5c0313d2018-08-13 17:13:35 +030076 sh "mkdir -p ${modEnv}/nodes/"
77 println "Create file ${nodeFile}"
78 writeFile(file: nodeFile, text: nodeString)
azvyagintsev87985532018-07-10 20:49:38 +030079}
80
azvyagintsev30bc82e2018-08-22 12:26:06 +030081/**
82 *
83 * @param contextFile - path to `contexts/XXX.yaml file`
azvyagintsev9df82e52018-09-06 19:17:18 +030084 * @param virtualenv - pyvenv with CC and dep's
azvyagintsev30bc82e2018-08-22 12:26:06 +030085 * @param templateEnvDir - root of CookieCutter
86 * @return
87 */
88
azvyagintsev5c0313d2018-08-13 17:13:35 +030089def generateModel(contextFile, virtualenv, templateEnvDir) {
90 def modelEnv = "${templateEnvDir}/model"
91 def basename = common.GetBaseName(contextFile, '.yml')
92 def generatedModel = "${modelEnv}/${basename}"
93 def content = readFile(file: "${templateEnvDir}/contexts/${contextFile}")
94 def templateContext = readYaml text: content
95 def clusterDomain = templateContext.default_context.cluster_domain
96 def clusterName = templateContext.default_context.cluster_name
97 def outputDestination = "${generatedModel}/classes/cluster/${clusterName}"
98 def templateBaseDir = templateEnvDir
99 def templateDir = "${templateEnvDir}/dir"
100 def templateOutputDir = templateBaseDir
azvyagintsev30bc82e2018-08-22 12:26:06 +0300101 dir(templateEnvDir) {
102 sh(script: "rm -rf ${generatedModel} || true")
103 common.infoMsg("Generating model from context ${contextFile}")
104 def productList = ["infra", "cicd", "opencontrail", "kubernetes", "openstack", "oss", "stacklight", "ceph"]
105 for (product in productList) {
chnydae80bb922017-05-29 17:48:40 +0200106
azvyagintsev30bc82e2018-08-22 12:26:06 +0300107 // get templateOutputDir and productDir
Michal Kobus197f6112018-09-21 14:30:16 +0200108 templateOutputDir = "${templateEnvDir}/output/${product}"
109 productDir = product
azvyagintsev7cdcc7a2018-09-25 16:47:24 +0300110 templateDir = "${templateEnvDir}/cluster_product/${productDir}"
111 // Bw for 2018.8.1 and older releases
112 if (product.startsWith("stacklight") && (!fileExists(templateDir))) {
113 common.warningMsg("Old release detected! productDir => 'stacklight2' ")
114 productDir = "stacklight2"
115 templateDir = "${templateEnvDir}/cluster_product/${productDir}"
116 }
azvyagintsev30bc82e2018-08-22 12:26:06 +0300117 if (product == "infra" || (templateContext.default_context["${product}_enabled"]
118 && templateContext.default_context["${product}_enabled"].toBoolean())) {
119
azvyagintsev30bc82e2018-08-22 12:26:06 +0300120 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 Egorenko47169272018-10-11 16:01:38 +0400146def testModel(modelFile, reclassArtifactName, artifactCopyPath, useExtraRepos = false) {
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}"
Denis Egorenko47169272018-10-11 16:01:38 +0400157 useExtraRepos: ${useExtraRepos}
158 ${extraVarsYAML}
azvyagintsev87985532018-07-10 20:49:38 +0300159 """
Denis Egorenkod8748942018-09-07 12:26:20 +0400160 def chunkJob = build job: chunkJobName, parameters: [
azvyagintsev08b34e32018-09-12 12:29:42 +0300161 [$class: 'TextParameterValue', name: 'EXTRA_VARIABLES_YAML',
azvyagintsev30bc82e2018-08-22 12:26:06 +0300162 value : _values_string.stripIndent()],
azvyagintsev5c0313d2018-08-13 17:13:35 +0300163 ]
Denis Egorenkod8748942018-09-07 12:26:20 +0400164 // Put sub-job info into global map.
165 testModelBuildsData.put(_uuid, ['jobname' : chunkJob.fullProjectName,
166 'copyToDir': "${artifactCopyPath}/${modelFile}",
167 'buildId' : "${chunkJob.number}"])
chnydabc63c9a2017-05-30 15:37:54 +0200168}
169
Denis Egorenko47169272018-10-11 16:01:38 +0400170def StepTestModel(basename, reclassArtifactName, artifactCopyPath, useExtraRepos = false) {
azvyagintsev5c0313d2018-08-13 17:13:35 +0300171 // We need to wrap what we return in a Groovy closure, or else it's invoked
172 // when this method is called, not when we pass it to parallel.
173 // To do this, you need to wrap the code below in { }, and either return
174 // that explicitly, or use { -> } syntax.
175 // return node object
176 return {
177 node(slaveNode) {
Denis Egorenko47169272018-10-11 16:01:38 +0400178 testModel(basename, reclassArtifactName, artifactCopyPath, useExtraRepos)
azvyagintsev5c0313d2018-08-13 17:13:35 +0300179 }
chnydae80bb922017-05-29 17:48:40 +0200180 }
azvyagintsev87985532018-07-10 20:49:38 +0300181}
182
azvyagintsev9df82e52018-09-06 19:17:18 +0300183def StepPrepareGit(templateEnvFolder, gerrit_data) {
azvyagintsev5c0313d2018-08-13 17:13:35 +0300184 // return git clone object
185 return {
azvyagintsev9df82e52018-09-06 19:17:18 +0300186 def checkouted = false
187 common.infoMsg("StepPrepareGit: ${gerrit_data}")
azvyagintsev5c0313d2018-08-13 17:13:35 +0300188 // fetch needed sources
189 dir(templateEnvFolder) {
azvyagintsev9df82e52018-09-06 19:17:18 +0300190 if (gerrit_data['gerritRefSpec']) {
191 // Those part might be not work,in case manual var's pass
192 def gerritChange = gerrit.getGerritChange(gerrit_data['gerritName'], gerrit_data['gerritHost'],
193 gerrit_data['GERRIT_CHANGE_NUMBER'], gerrit_data['credentialsId'])
azvyagintsev5c0313d2018-08-13 17:13:35 +0300194 merged = gerritChange.status == "MERGED"
195 if (!merged) {
azvyagintsev9df82e52018-09-06 19:17:18 +0300196 checkouted = gerrit.gerritPatchsetCheckout(gerrit_data)
azvyagintsev5c0313d2018-08-13 17:13:35 +0300197 } else {
azvyagintsev9df82e52018-09-06 19:17:18 +0300198 // update global variable for pretty return from pipeline
199 alreadyMerged = true
200 common.successMsg("Change ${gerrit_data['GERRIT_CHANGE_NUMBER']} is already merged, no need to gate them")
201 error('change already merged')
azvyagintsev87985532018-07-10 20:49:38 +0300202 }
azvyagintsev5c0313d2018-08-13 17:13:35 +0300203 } else {
azvyagintsev9df82e52018-09-06 19:17:18 +0300204 // Get clean HEAD
205 gerrit_data['useGerritTriggerBuildChooser'] = false
206 checkouted = gerrit.gerritPatchsetCheckout(gerrit_data)
207 if (!checkouted) {
208 error("Failed to get repo:${gerrit_data}")
209 }
azvyagintsev87985532018-07-10 20:49:38 +0300210 }
azvyagintsev5c0313d2018-08-13 17:13:35 +0300211 }
azvyagintsev87985532018-07-10 20:49:38 +0300212 }
azvyagintsev5c0313d2018-08-13 17:13:35 +0300213}
214
215def StepGenerateModels(_contextFileList, _virtualenv, _templateEnvDir) {
216 return {
217 for (contextFile in _contextFileList) {
218 generateModel(contextFile, _virtualenv, _templateEnvDir)
219 }
220 }
221}
222
azvyagintsev9df82e52018-09-06 19:17:18 +0300223def globalVariatorsUpdate() {
224 // Simple function, to check and define branch-around variables
225 // In general, simply make transition updates for non-master branch
226 // based on magic logic
Denis Egorenkofe9a3362018-10-17 15:33:25 +0400227 def newline = '<br/>'
228 def messages = []
azvyagintsevb266ad22018-09-11 12:11:11 +0300229 if (env.GERRIT_PROJECT) {
Denis Egorenkofe9a3362018-10-17 15:33:25 +0400230 messages.add("<font color='red'>GerritTrigger detected! We are in auto-mode:</font>")
231 messages.add("Test env variables has been changed:")
232 messages.add("COOKIECUTTER_TEMPLATE_BRANCH => ${gerritDataCC['gerritBranch']}")
233 messages.add("RECLASS_MODEL_BRANCH => ${gerritDataRS['gerritBranch']}")
azvyagintseve0eb6cd2018-09-12 13:00:40 +0300234 // TODO are we going to have such branches?
azvyagintsev9df82e52018-09-06 19:17:18 +0300235 if (!['nightly', 'testing', 'stable', 'proposed', 'master'].contains(env.GERRIT_BRANCH)) {
236 gerritDataCC['gerritBranch'] = env.GERRIT_BRANCH
237 gerritDataRS['gerritBranch'] = env.GERRIT_BRANCH
azvyagintsev458278c2018-09-25 18:38:30 +0300238 testDistribRevision = env.GERRIT_BRANCH
azvyagintsev9df82e52018-09-06 19:17:18 +0300239 }
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
Denis Egorenkofe9a3362018-10-17 15:33:25 +0400244 messages.add("RECLASS_SYSTEM_GIT_REF => ${gerritDataRS['gerritRefSpec']}")
azvyagintsev9df82e52018-09-06 19:17:18 +0300245 } else if (env.GERRIT_PROJECT == 'mk/cookiecutter-templates') {
246 gerritDataCC['gerritRefSpec'] = env.GERRIT_REFSPEC
247 gerritDataCC['GERRIT_CHANGE_NUMBER'] = env.GERRIT_CHANGE_NUMBER
Denis Egorenkofe9a3362018-10-17 15:33:25 +0400248 messages.add("COOKIECUTTER_TEMPLATE_REF => ${gerritDataCC['gerritRefSpec']}")
azvyagintsev9df82e52018-09-06 19:17:18 +0300249 } else {
250 error("Unsuported gerrit-project triggered:${env.GERRIT_PROJECT}")
251 }
azvyagintsev9df82e52018-09-06 19:17:18 +0300252 } else {
Denis Egorenkofe9a3362018-10-17 15:33:25 +0400253 messages.add("<font color='red'>Non-gerrit trigger run detected!</font>")
azvyagintsev9df82e52018-09-06 19:17:18 +0300254 }
azvyagintsev3a80e342018-09-19 19:22:48 +0300255 gerritDataCCHEAD << gerritDataCC
256 gerritDataCCHEAD['gerritRefSpec'] = null
257 gerritDataCCHEAD['GERRIT_CHANGE_NUMBER'] = null
258 gerritDataRSHEAD << gerritDataRS
259 gerritDataRSHEAD['gerritRefSpec'] = null
260 gerritDataRSHEAD['GERRIT_CHANGE_NUMBER'] = null
azvyagintsev458278c2018-09-25 18:38:30 +0300261 // 'binary' branch logic w\o 'release/' prefix
262 if (testDistribRevision.contains('/')) {
263 testDistribRevision = testDistribRevision.split('/')[-1]
264 }
265 // Check if we are going to test bleeding-edge release, which doesn't have binary release yet
266 if (!common.checkRemoteBinary([apt_mk_version: testDistribRevision]).linux_system_repo_url) {
267 common.errorMsg("Binary release: ${testDistribRevision} not exist. Fallback to 'proposed'! ")
268 testDistribRevision = 'proposed'
Denis Egorenkofe9a3362018-10-17 15:33:25 +0400269 messages.add("DISTRIB_REVISION => ${testDistribRevision}")
azvyagintsev458278c2018-09-25 18:38:30 +0300270 }
Denis Egorenkofe9a3362018-10-17 15:33:25 +0400271 def message = messages.join(newline) + newline
azvyagintsev458278c2018-09-25 18:38:30 +0300272 currentBuild.description = currentBuild.description ? message + currentBuild.description : message
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
Denis Egorenko7aa5ac52018-10-17 15:14:56 +0400301 // ├── classes-system <<< reclass system
Denis Egorenkod8748942018-09-07 12:26:20 +0400302 // ├── model
303 // │   └── ceph <<< from `context basename`
304 // │   ├── classes
305 // │   │   ├── cluster
Denis Egorenko7aa5ac52018-10-17 15:14:56 +0400306 // │   │   └── system -> ../../../classes-system
Denis Egorenkod8748942018-09-07 12:26:20 +0400307 // │   └── nodes
308 // │   └── cfg01.ceph-cluster-domain.local.yml
Denis Egorenko7aa5ac52018-10-17 15:14:56 +0400309 def archiveBaseName = common.GetBaseName(archiveName, '.tar.gz')
310 def classesSystemDir = 'classes-system'
311 // copy reclass system under envPath with -R and trailing / to support symlinks direct copy
312 sh("cp -R ${archiveBaseName}/ ${envPath}/${classesSystemDir}")
Denis Egorenkod8748942018-09-07 12:26:20 +0400313 dir(envPath) {
314 for (String context : contextList) {
315 def basename = common.GetBaseName(context, '.yml')
Denis Egorenko7aa5ac52018-10-17 15:14:56 +0400316 dir("${envPath}/model/${basename}/classes") {
317 sh(script: "ln -sfv ../../../${classesSystemDir} system ")
Denis Egorenkod8748942018-09-07 12:26:20 +0400318 }
319 }
Denis Egorenkoa6117fc2018-09-11 13:40:00 +0400320 // replace all generated passwords/secrets/keys with hardcode value for infra/secrets.yaml
321 replaceGeneratedValues("${envPath}/model")
azvyagintsev3a80e342018-09-19 19:22:48 +0300322 // Save all models and all contexts. Warning! `h` flag must be used!
Denis Egorenko7aa5ac52018-10-17 15:14:56 +0400323 sh(script: "set -ex; tar -czhf ${env.WORKSPACE}/${archiveName} --exclude='*@tmp' contexts model ${classesSystemDir}", returnStatus: true)
Denis Egorenkod8748942018-09-07 12:26:20 +0400324 }
azvyagintsev3a80e342018-09-19 19:22:48 +0300325 archiveArtifacts artifacts: archiveName
azvyagintsev9df82e52018-09-06 19:17:18 +0300326}
327
azvyagintsev5c0313d2018-08-13 17:13:35 +0300328timeout(time: 1, unit: 'HOURS') {
329 node(slaveNode) {
azvyagintsev9df82e52018-09-06 19:17:18 +0300330 globalVariatorsUpdate()
azvyagintsev30bc82e2018-08-22 12:26:06 +0300331 def templateEnvHead = "${env.WORKSPACE}/EnvHead/"
332 def templateEnvPatched = "${env.WORKSPACE}/EnvPatched/"
azvyagintsev5c0313d2018-08-13 17:13:35 +0300333 def contextFileListHead = []
334 def contextFileListPatched = []
335 def vEnv = "${env.WORKSPACE}/venv"
Denis Egorenkod8748942018-09-07 12:26:20 +0400336 def headReclassArtifactName = "head_reclass.tar.gz"
337 def patchedReclassArtifactName = "patched_reclass.tar.gz"
338 def reclassNodeInfoDir = "${env.WORKSPACE}/reclassNodeInfo_compare/"
339 def reclassInfoHeadPath = "${reclassNodeInfoDir}/old"
340 def reclassInfoPatchedPath = "${reclassNodeInfoDir}/new"
azvyagintsev5c0313d2018-08-13 17:13:35 +0300341 try {
342 sh(script: 'find . -mindepth 1 -delete > /dev/null || true')
343 stage('Download and prepare CC env') {
344 // Prepare 2 env - for patchset, and for HEAD
azvyagintsev9df82e52018-09-06 19:17:18 +0300345 def paralellEnvs = [:]
azvyagintsev5c0313d2018-08-13 17:13:35 +0300346 paralellEnvs.failFast = true
azvyagintsev9df82e52018-09-06 19:17:18 +0300347 paralellEnvs['downloadEnvHead'] = StepPrepareGit(templateEnvHead, gerritDataCCHEAD)
azvyagintsev3a80e342018-09-19 19:22:48 +0300348 if (gerritDataCC.get('gerritRefSpec', null)) {
349 paralellEnvs['downloadEnvPatched'] = StepPrepareGit(templateEnvPatched, gerritDataCC)
350 parallel paralellEnvs
351 } else {
352 paralellEnvs['downloadEnvPatched'] = { common.warningMsg('No need to process: downloadEnvPatched') }
353 parallel paralellEnvs
354 sh("rsync -a --exclude '*@tmp' ${templateEnvHead} ${templateEnvPatched}")
355 }
azvyagintsev5c0313d2018-08-13 17:13:35 +0300356 }
357 stage("Check workflow_definition") {
358 // Check only for patchset
359 python.setupVirtualenv(vEnv, 'python2', [], "${templateEnvPatched}/requirements.txt")
azvyagintsev3a80e342018-09-19 19:22:48 +0300360 if (gerritDataCC.get('gerritRefSpec', null)) {
361 common.infoMsg(python.runVirtualenvCommand(vEnv, "python ${templateEnvPatched}/workflow_definition_test.py"))
362 } else {
363 common.infoMsg('No need to process: workflow_definition')
364 }
azvyagintsev5c0313d2018-08-13 17:13:35 +0300365 }
366
367 stage("generate models") {
368 dir("${templateEnvHead}/contexts") {
369 for (String x : findFiles(glob: "*.yml")) {
370 contextFileListHead.add(x)
371 }
372 }
373 dir("${templateEnvPatched}/contexts") {
374 for (String x : findFiles(glob: "*.yml")) {
375 contextFileListPatched.add(x)
376 }
377 }
378 // Generate over 2env's - for patchset, and for HEAD
azvyagintsev9df82e52018-09-06 19:17:18 +0300379 def paralellEnvs = [:]
azvyagintsev5c0313d2018-08-13 17:13:35 +0300380 paralellEnvs.failFast = true
azvyagintsev30bc82e2018-08-22 12:26:06 +0300381 paralellEnvs['GenerateEnvHead'] = StepGenerateModels(contextFileListHead, vEnv, templateEnvHead)
azvyagintsev3a80e342018-09-19 19:22:48 +0300382 if (gerritDataCC.get('gerritRefSpec', null)) {
383 paralellEnvs['GenerateEnvPatched'] = StepGenerateModels(contextFileListPatched, vEnv, templateEnvPatched)
384 parallel paralellEnvs
385 } else {
386 paralellEnvs['GenerateEnvPatched'] = { common.warningMsg('No need to process: GenerateEnvPatched') }
387 parallel paralellEnvs
388 sh("rsync -a --exclude '*@tmp' ${templateEnvHead} ${templateEnvPatched}")
azvyagintsev5c0313d2018-08-13 17:13:35 +0300389 }
390
azvyagintsev3a80e342018-09-19 19:22:48 +0300391 // We need 2 git's, one for HEAD, one for PATCHed.
392 // if no patch, use head for both
393 RSHeadDir = common.GetBaseName(headReclassArtifactName, '.tar.gz')
394 RSPatchedDir = common.GetBaseName(patchedReclassArtifactName, '.tar.gz')
395 common.infoMsg("gerritDataRS= ${gerritDataRS}")
396 common.infoMsg("gerritDataRSHEAD= ${gerritDataRSHEAD}")
397 if (gerritDataRS.get('gerritRefSpec', null)) {
398 StepPrepareGit("${env.WORKSPACE}/${RSPatchedDir}/", gerritDataRS).call()
399 StepPrepareGit("${env.WORKSPACE}/${RSHeadDir}/", gerritDataRSHEAD).call()
400 } else {
401 StepPrepareGit("${env.WORKSPACE}/${RSHeadDir}/", gerritDataRS).call()
402 sh("cd ${env.WORKSPACE} ; ln -svf ${RSHeadDir} ${RSPatchedDir}")
403 }
azvyagintsev5c0313d2018-08-13 17:13:35 +0300404 // link all models, to use one global reclass
405 // For HEAD
Denis Egorenkod8748942018-09-07 12:26:20 +0400406 linkReclassModels(contextFileListHead, templateEnvHead, headReclassArtifactName)
azvyagintsev5c0313d2018-08-13 17:13:35 +0300407 // For patched
Denis Egorenkod8748942018-09-07 12:26:20 +0400408 linkReclassModels(contextFileListPatched, templateEnvPatched, patchedReclassArtifactName)
azvyagintsev5c0313d2018-08-13 17:13:35 +0300409 }
410
Denis Egorenkod8748942018-09-07 12:26:20 +0400411 stage("Compare cluster lvl Head/Patched") {
azvyagintsev5c0313d2018-08-13 17:13:35 +0300412 // Compare patched and HEAD reclass pillars
Denis Egorenkod8748942018-09-07 12:26:20 +0400413 compareRoot = "${env.WORKSPACE}/cluster_compare/"
Denis Egorenko7aa5ac52018-10-17 15:14:56 +0400414 // extract archive and drop all copied classes/system before comparing
azvyagintsev5c0313d2018-08-13 17:13:35 +0300415 sh(script: """
416 mkdir -pv ${compareRoot}/new ${compareRoot}/old
Denis Egorenkod8748942018-09-07 12:26:20 +0400417 tar -xzf ${patchedReclassArtifactName} --directory ${compareRoot}/new
418 tar -xzf ${headReclassArtifactName} --directory ${compareRoot}/old
Denis Egorenko7aa5ac52018-10-17 15:14:56 +0400419 find ${compareRoot} -name classes -type d -exec rm -rf '{}/system' \\;
azvyagintsev5c0313d2018-08-13 17:13:35 +0300420 """)
421 common.warningMsg('infra/secrets.yml has been skipped from compare!')
azvyagintsev3a80e342018-09-19 19:22:48 +0300422 result = '\n' + common.comparePillars(compareRoot, env.BUILD_URL, "-Ev \'infra/secrets.yml|\\.git\'")
Denis Egorenkod8748942018-09-07 12:26:20 +0400423 currentBuild.description = currentBuild.description ? currentBuild.description + result : result
azvyagintsev5c0313d2018-08-13 17:13:35 +0300424 }
Denis Egorenkod8748942018-09-07 12:26:20 +0400425 stage("TestContexts Head/Patched") {
426 def stepsForParallel = [:]
427 stepsForParallel.failFast = true
428 common.infoMsg("Found: ${contextFileListHead.size()} HEAD contexts to test.")
429 for (String context : contextFileListHead) {
430 def basename = common.GetBaseName(context, '.yml')
431 stepsForParallel.put("ContextHeadTest:${basename}", StepTestModel(basename, headReclassArtifactName, reclassInfoHeadPath))
432 }
azvyagintsev5c0313d2018-08-13 17:13:35 +0300433 common.infoMsg("Found: ${contextFileListPatched.size()} patched contexts to test.")
434 for (String context : contextFileListPatched) {
435 def basename = common.GetBaseName(context, '.yml')
Denis Egorenko47169272018-10-11 16:01:38 +0400436 stepsForParallel.put("ContextPatchedTest:${basename}", StepTestModel(basename, patchedReclassArtifactName, reclassInfoPatchedPath, true))
azvyagintsev5c0313d2018-08-13 17:13:35 +0300437 }
438 parallel stepsForParallel
Denis Egorenkod8748942018-09-07 12:26:20 +0400439 common.infoMsg('All TestContexts tests done')
440 }
441 stage("Compare NodesInfo Head/Patched") {
442 // Download all artifacts
443 def stepsForParallel = [:]
444 stepsForParallel.failFast = true
445 common.infoMsg("Found: ${testModelBuildsData.size()} nodeinfo artifacts to download.")
446 testModelBuildsData.each { bname, bdata ->
447 stepsForParallel.put("FetchData:${bname}",
448 getAndUnpackNodesInfoArtifact(bdata.jobname, bdata.copyToDir, bdata.buildId))
449 }
450 parallel stepsForParallel
Denis Egorenkoa6117fc2018-09-11 13:40:00 +0400451 // remove timestamp field from rendered files
452 sh("find ${reclassNodeInfoDir} -type f -exec sed -i '/ timestamp: .*/d' {} \\;")
Denis Egorenkod8748942018-09-07 12:26:20 +0400453 // Compare patched and HEAD reclass pillars
454 result = '\n' + common.comparePillars(reclassNodeInfoDir, env.BUILD_URL, '')
455 currentBuild.description = currentBuild.description ? currentBuild.description + result : result
azvyagintsev5c0313d2018-08-13 17:13:35 +0300456 }
Denis Egorenko04ca9f22018-09-19 16:06:49 +0400457 stage('Check include order') {
azvyagintseve7b8e792018-09-21 17:27:01 +0300458 if (!checkIncludeOrder) {
459 common.infoMsg('Check include order require to much time, and currently disabled!')
460
461 } else {
462 def correctIncludeOrder = ["service", "system", "cluster"]
463 dir(reclassInfoPatchedPath) {
464 def nodeInfoFiles = findFiles(glob: "**/*.reclass.nodeinfo")
465 def messages = ["<b>Wrong include ordering found</b><ul>"]
466 def stepsForParallel = [:]
467 nodeInfoFiles.each { nodeInfo ->
468 stepsForParallel.put("Checking ${nodeInfo.path}:", {
469 def node = readYaml file: nodeInfo.path
470 def classes = node['classes']
471 def curClassID = 0
472 def prevClassID = 0
473 def wrongOrder = false
474 for (String className in classes) {
475 def currentClass = className.tokenize('.')[0]
476 curClassID = correctIncludeOrder.indexOf(currentClass)
477 if (currentClass != correctIncludeOrder[prevClassID]) {
478 if (prevClassID > curClassID) {
479 wrongOrder = true
480 common.warningMsg("File ${nodeInfo.path} contains wrong order of classes including: Includes for ${className} should be declared before ${correctIncludeOrder[prevClassID]} includes")
481 } else {
482 prevClassID = curClassID
483 }
Denis Egorenko04ca9f22018-09-19 16:06:49 +0400484 }
485 }
azvyagintseve7b8e792018-09-21 17:27:01 +0300486 if (wrongOrder) {
487 messages.add("<li>${nodeInfo.path} contains wrong order of classes including</li>")
488 }
489 })
490 }
491 parallel stepsForParallel
492 def includerOrder = '<b>No wrong include order</b>'
493 if (messages.size() != 1) {
494 includerOrder = messages.join('')
495 }
496 currentBuild.description = currentBuild.description ? currentBuild.description + includerOrder : includerOrder
Denis Egorenko04ca9f22018-09-19 16:06:49 +0400497 }
Denis Egorenko04ca9f22018-09-19 16:06:49 +0400498 }
499 }
azvyagintsev5c0313d2018-08-13 17:13:35 +0300500 sh(script: 'find . -mindepth 1 -delete > /dev/null || true')
501
502 } catch (Throwable e) {
azvyagintsev9df82e52018-09-06 19:17:18 +0300503 if (alreadyMerged) {
504 currentBuild.result = 'ABORTED'
505 currentBuild.description = "Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to gate them"
506 return
507 }
azvyagintsev5c0313d2018-08-13 17:13:35 +0300508 currentBuild.result = "FAILURE"
509 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
510 throw e
511 } finally {
512 def dummy = "dummy"
azvyagintsev5c0313d2018-08-13 17:13:35 +0300513 }
514 }
chnydae80bb922017-05-29 17:48:40 +0200515}