blob: 8826bc1f5cf84d3a20608c39deaf229f05956758 [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 Egorenko535d5b62018-09-28 11:28:10 +040020def extraVarsYAML = env.EXTRA_VARIABLES_YAML ?: false
21if (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 Egorenkod8748942018-09-07 12:26:20 +040058// Name of sub-test chunk job
59chunkJobName = "test-mk-cookiecutter-templates-chunk"
60testModelBuildsData = [:]
Vasyl Saienko772e1232018-07-23 14:42:24 +030061
chnyda467f10f2017-05-30 17:25:07 +020062def generateSaltMaster(modEnv, clusterDomain, clusterName) {
azvyagintsev5c0313d2018-08-13 17:13:35 +030063 def nodeFile = "${modEnv}/nodes/cfg01.${clusterDomain}.yml"
64 def nodeString = """classes:
chnydae80bb922017-05-29 17:48:40 +020065- cluster.${clusterName}.infra.config
66parameters:
67 _param:
68 linux_system_codename: xenial
69 reclass_data_revision: master
70 linux:
71 system:
72 name: cfg01
73 domain: ${clusterDomain}
74"""
azvyagintsev5c0313d2018-08-13 17:13:35 +030075 sh "mkdir -p ${modEnv}/nodes/"
76 println "Create file ${nodeFile}"
77 writeFile(file: nodeFile, text: nodeString)
azvyagintsev87985532018-07-10 20:49:38 +030078}
79
azvyagintsev30bc82e2018-08-22 12:26:06 +030080/**
81 *
82 * @param contextFile - path to `contexts/XXX.yaml file`
azvyagintsev9df82e52018-09-06 19:17:18 +030083 * @param virtualenv - pyvenv with CC and dep's
azvyagintsev30bc82e2018-08-22 12:26:06 +030084 * @param templateEnvDir - root of CookieCutter
85 * @return
86 */
87
azvyagintsev5c0313d2018-08-13 17:13:35 +030088def generateModel(contextFile, virtualenv, templateEnvDir) {
89 def modelEnv = "${templateEnvDir}/model"
90 def basename = common.GetBaseName(contextFile, '.yml')
91 def generatedModel = "${modelEnv}/${basename}"
92 def content = readFile(file: "${templateEnvDir}/contexts/${contextFile}")
93 def templateContext = readYaml text: content
94 def clusterDomain = templateContext.default_context.cluster_domain
95 def clusterName = templateContext.default_context.cluster_name
96 def outputDestination = "${generatedModel}/classes/cluster/${clusterName}"
97 def templateBaseDir = templateEnvDir
98 def templateDir = "${templateEnvDir}/dir"
99 def templateOutputDir = templateBaseDir
azvyagintsev30bc82e2018-08-22 12:26:06 +0300100 dir(templateEnvDir) {
101 sh(script: "rm -rf ${generatedModel} || true")
102 common.infoMsg("Generating model from context ${contextFile}")
103 def productList = ["infra", "cicd", "opencontrail", "kubernetes", "openstack", "oss", "stacklight", "ceph"]
104 for (product in productList) {
chnydae80bb922017-05-29 17:48:40 +0200105
azvyagintsev30bc82e2018-08-22 12:26:06 +0300106 // get templateOutputDir and productDir
Michal Kobus197f6112018-09-21 14:30:16 +0200107 templateOutputDir = "${templateEnvDir}/output/${product}"
108 productDir = product
azvyagintsev7cdcc7a2018-09-25 16:47:24 +0300109 templateDir = "${templateEnvDir}/cluster_product/${productDir}"
110 // Bw for 2018.8.1 and older releases
111 if (product.startsWith("stacklight") && (!fileExists(templateDir))) {
112 common.warningMsg("Old release detected! productDir => 'stacklight2' ")
113 productDir = "stacklight2"
114 templateDir = "${templateEnvDir}/cluster_product/${productDir}"
115 }
azvyagintsev30bc82e2018-08-22 12:26:06 +0300116 if (product == "infra" || (templateContext.default_context["${product}_enabled"]
117 && templateContext.default_context["${product}_enabled"].toBoolean())) {
118
azvyagintsev30bc82e2018-08-22 12:26:06 +0300119 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 """
Denis Egorenkod8748942018-09-07 12:26:20 +0400157 def chunkJob = build job: chunkJobName, parameters: [
azvyagintsev08b34e32018-09-12 12:29:42 +0300158 [$class: 'TextParameterValue', name: 'EXTRA_VARIABLES_YAML',
azvyagintsev30bc82e2018-08-22 12:26:06 +0300159 value : _values_string.stripIndent()],
azvyagintsev5c0313d2018-08-13 17:13:35 +0300160 ]
Denis Egorenkod8748942018-09-07 12:26:20 +0400161 // Put sub-job info into global map.
162 testModelBuildsData.put(_uuid, ['jobname' : chunkJob.fullProjectName,
163 'copyToDir': "${artifactCopyPath}/${modelFile}",
164 'buildId' : "${chunkJob.number}"])
chnydabc63c9a2017-05-30 15:37:54 +0200165}
166
Denis Egorenkod8748942018-09-07 12:26:20 +0400167def StepTestModel(basename, reclassArtifactName, artifactCopyPath) {
azvyagintsev5c0313d2018-08-13 17:13:35 +0300168 // We need to wrap what we return in a Groovy closure, or else it's invoked
169 // when this method is called, not when we pass it to parallel.
170 // To do this, you need to wrap the code below in { }, and either return
171 // that explicitly, or use { -> } syntax.
172 // return node object
173 return {
174 node(slaveNode) {
Denis Egorenkod8748942018-09-07 12:26:20 +0400175 testModel(basename, reclassArtifactName, artifactCopyPath)
azvyagintsev5c0313d2018-08-13 17:13:35 +0300176 }
chnydae80bb922017-05-29 17:48:40 +0200177 }
azvyagintsev87985532018-07-10 20:49:38 +0300178}
179
azvyagintsev9df82e52018-09-06 19:17:18 +0300180def StepPrepareGit(templateEnvFolder, gerrit_data) {
azvyagintsev5c0313d2018-08-13 17:13:35 +0300181 // return git clone object
182 return {
azvyagintsev9df82e52018-09-06 19:17:18 +0300183 def checkouted = false
184 common.infoMsg("StepPrepareGit: ${gerrit_data}")
azvyagintsev5c0313d2018-08-13 17:13:35 +0300185 // fetch needed sources
186 dir(templateEnvFolder) {
azvyagintsev9df82e52018-09-06 19:17:18 +0300187 if (gerrit_data['gerritRefSpec']) {
188 // Those part might be not work,in case manual var's pass
189 def gerritChange = gerrit.getGerritChange(gerrit_data['gerritName'], gerrit_data['gerritHost'],
190 gerrit_data['GERRIT_CHANGE_NUMBER'], gerrit_data['credentialsId'])
azvyagintsev5c0313d2018-08-13 17:13:35 +0300191 merged = gerritChange.status == "MERGED"
192 if (!merged) {
azvyagintsev9df82e52018-09-06 19:17:18 +0300193 checkouted = gerrit.gerritPatchsetCheckout(gerrit_data)
azvyagintsev5c0313d2018-08-13 17:13:35 +0300194 } else {
azvyagintsev9df82e52018-09-06 19:17:18 +0300195 // update global variable for pretty return from pipeline
196 alreadyMerged = true
197 common.successMsg("Change ${gerrit_data['GERRIT_CHANGE_NUMBER']} is already merged, no need to gate them")
198 error('change already merged')
azvyagintsev87985532018-07-10 20:49:38 +0300199 }
azvyagintsev5c0313d2018-08-13 17:13:35 +0300200 } else {
azvyagintsev9df82e52018-09-06 19:17:18 +0300201 // Get clean HEAD
202 gerrit_data['useGerritTriggerBuildChooser'] = false
203 checkouted = gerrit.gerritPatchsetCheckout(gerrit_data)
204 if (!checkouted) {
205 error("Failed to get repo:${gerrit_data}")
206 }
azvyagintsev87985532018-07-10 20:49:38 +0300207 }
azvyagintsev5c0313d2018-08-13 17:13:35 +0300208 }
azvyagintsev87985532018-07-10 20:49:38 +0300209 }
azvyagintsev5c0313d2018-08-13 17:13:35 +0300210}
211
212def StepGenerateModels(_contextFileList, _virtualenv, _templateEnvDir) {
213 return {
214 for (contextFile in _contextFileList) {
215 generateModel(contextFile, _virtualenv, _templateEnvDir)
216 }
217 }
218}
219
azvyagintsev9df82e52018-09-06 19:17:18 +0300220def globalVariatorsUpdate() {
221 // Simple function, to check and define branch-around variables
222 // In general, simply make transition updates for non-master branch
223 // based on magic logic
Denis Egorenkofe9a3362018-10-17 15:33:25 +0400224 def newline = '<br/>'
225 def messages = []
azvyagintsevb266ad22018-09-11 12:11:11 +0300226 if (env.GERRIT_PROJECT) {
Denis Egorenkofe9a3362018-10-17 15:33:25 +0400227 messages.add("<font color='red'>GerritTrigger detected! We are in auto-mode:</font>")
228 messages.add("Test env variables has been changed:")
229 messages.add("COOKIECUTTER_TEMPLATE_BRANCH => ${gerritDataCC['gerritBranch']}")
230 messages.add("RECLASS_MODEL_BRANCH => ${gerritDataRS['gerritBranch']}")
azvyagintseve0eb6cd2018-09-12 13:00:40 +0300231 // TODO are we going to have such branches?
azvyagintsev9df82e52018-09-06 19:17:18 +0300232 if (!['nightly', 'testing', 'stable', 'proposed', 'master'].contains(env.GERRIT_BRANCH)) {
233 gerritDataCC['gerritBranch'] = env.GERRIT_BRANCH
234 gerritDataRS['gerritBranch'] = env.GERRIT_BRANCH
azvyagintsev458278c2018-09-25 18:38:30 +0300235 testDistribRevision = env.GERRIT_BRANCH
azvyagintsev9df82e52018-09-06 19:17:18 +0300236 }
237 // Identify, who triggered. To whom we should pass refspec
238 if (env.GERRIT_PROJECT == 'salt-models/reclass-system') {
239 gerritDataRS['gerritRefSpec'] = env.GERRIT_REFSPEC
240 gerritDataRS['GERRIT_CHANGE_NUMBER'] = env.GERRIT_CHANGE_NUMBER
Denis Egorenkofe9a3362018-10-17 15:33:25 +0400241 messages.add("RECLASS_SYSTEM_GIT_REF => ${gerritDataRS['gerritRefSpec']}")
azvyagintsev9df82e52018-09-06 19:17:18 +0300242 } else if (env.GERRIT_PROJECT == 'mk/cookiecutter-templates') {
243 gerritDataCC['gerritRefSpec'] = env.GERRIT_REFSPEC
244 gerritDataCC['GERRIT_CHANGE_NUMBER'] = env.GERRIT_CHANGE_NUMBER
Denis Egorenkofe9a3362018-10-17 15:33:25 +0400245 messages.add("COOKIECUTTER_TEMPLATE_REF => ${gerritDataCC['gerritRefSpec']}")
azvyagintsev9df82e52018-09-06 19:17:18 +0300246 } else {
247 error("Unsuported gerrit-project triggered:${env.GERRIT_PROJECT}")
248 }
azvyagintsev9df82e52018-09-06 19:17:18 +0300249 } else {
Denis Egorenkofe9a3362018-10-17 15:33:25 +0400250 messages.add("<font color='red'>Non-gerrit trigger run detected!</font>")
azvyagintsev9df82e52018-09-06 19:17:18 +0300251 }
azvyagintsev3a80e342018-09-19 19:22:48 +0300252 gerritDataCCHEAD << gerritDataCC
253 gerritDataCCHEAD['gerritRefSpec'] = null
254 gerritDataCCHEAD['GERRIT_CHANGE_NUMBER'] = null
255 gerritDataRSHEAD << gerritDataRS
256 gerritDataRSHEAD['gerritRefSpec'] = null
257 gerritDataRSHEAD['GERRIT_CHANGE_NUMBER'] = null
azvyagintsev458278c2018-09-25 18:38:30 +0300258 // 'binary' branch logic w\o 'release/' prefix
259 if (testDistribRevision.contains('/')) {
260 testDistribRevision = testDistribRevision.split('/')[-1]
261 }
262 // Check if we are going to test bleeding-edge release, which doesn't have binary release yet
263 if (!common.checkRemoteBinary([apt_mk_version: testDistribRevision]).linux_system_repo_url) {
264 common.errorMsg("Binary release: ${testDistribRevision} not exist. Fallback to 'proposed'! ")
265 testDistribRevision = 'proposed'
Denis Egorenkofe9a3362018-10-17 15:33:25 +0400266 messages.add("DISTRIB_REVISION => ${testDistribRevision}")
azvyagintsev458278c2018-09-25 18:38:30 +0300267 }
Denis Egorenkofe9a3362018-10-17 15:33:25 +0400268 def message = messages.join(newline) + newline
azvyagintsev458278c2018-09-25 18:38:30 +0300269 currentBuild.description = currentBuild.description ? message + currentBuild.description : message
Denis Egorenkod8748942018-09-07 12:26:20 +0400270}
azvyagintsev9df82e52018-09-06 19:17:18 +0300271
Denis Egorenkoa6117fc2018-09-11 13:40:00 +0400272def replaceGeneratedValues(path) {
273 def files = sh(script: "find ${path} -name 'secrets.yml'", returnStdout: true)
274 def stepsForParallel = [:]
275 stepsForParallel.failFast = true
276 files.tokenize().each {
277 stepsForParallel.put("Removing generated passwords/secrets from ${it}",
278 {
279 def secrets = readYaml file: it
azvyagintsev08b34e32018-09-12 12:29:42 +0300280 for (String key in secrets['parameters']['_param'].keySet()) {
Denis Egorenkoa6117fc2018-09-11 13:40:00 +0400281 secrets['parameters']['_param'][key] = 'generated'
282 }
283 // writeYaml can't write to already existing file
284 writeYaml file: "${it}.tmp", data: secrets
285 sh "mv ${it}.tmp ${it}"
286 })
287 }
288 parallel stepsForParallel
289}
290
Denis Egorenkod8748942018-09-07 12:26:20 +0400291def linkReclassModels(contextList, envPath, archiveName) {
292 // to be able share reclass for all subenvs
293 // Also, makes artifact test more solid - use one reclass for all of sub-models.
294 // Archive Structure will be:
295 // tar.gz
296 // ├── contexts
297 // │   └── ceph.yml
Denis Egorenko7aa5ac52018-10-17 15:14:56 +0400298 // ├── classes-system <<< reclass system
Denis Egorenkod8748942018-09-07 12:26:20 +0400299 // ├── model
300 // │   └── ceph <<< from `context basename`
301 // │   ├── classes
302 // │   │   ├── cluster
Denis Egorenko7aa5ac52018-10-17 15:14:56 +0400303 // │   │   └── system -> ../../../classes-system
Denis Egorenkod8748942018-09-07 12:26:20 +0400304 // │   └── nodes
305 // │   └── cfg01.ceph-cluster-domain.local.yml
Denis Egorenko7aa5ac52018-10-17 15:14:56 +0400306 def archiveBaseName = common.GetBaseName(archiveName, '.tar.gz')
307 def classesSystemDir = 'classes-system'
308 // copy reclass system under envPath with -R and trailing / to support symlinks direct copy
309 sh("cp -R ${archiveBaseName}/ ${envPath}/${classesSystemDir}")
Denis Egorenkod8748942018-09-07 12:26:20 +0400310 dir(envPath) {
311 for (String context : contextList) {
312 def basename = common.GetBaseName(context, '.yml')
Denis Egorenko7aa5ac52018-10-17 15:14:56 +0400313 dir("${envPath}/model/${basename}/classes") {
314 sh(script: "ln -sfv ../../../${classesSystemDir} 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!
Denis Egorenko7aa5ac52018-10-17 15:14:56 +0400320 sh(script: "set -ex; tar -czhf ${env.WORKSPACE}/${archiveName} --exclude='*@tmp' contexts model ${classesSystemDir}", 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/"
Denis Egorenko7aa5ac52018-10-17 15:14:56 +0400411 // extract archive and drop all copied classes/system before comparing
azvyagintsev5c0313d2018-08-13 17:13:35 +0300412 sh(script: """
413 mkdir -pv ${compareRoot}/new ${compareRoot}/old
Denis Egorenkod8748942018-09-07 12:26:20 +0400414 tar -xzf ${patchedReclassArtifactName} --directory ${compareRoot}/new
415 tar -xzf ${headReclassArtifactName} --directory ${compareRoot}/old
Denis Egorenko7aa5ac52018-10-17 15:14:56 +0400416 find ${compareRoot} -name classes -type d -exec rm -rf '{}/system' \\;
azvyagintsev5c0313d2018-08-13 17:13:35 +0300417 """)
418 common.warningMsg('infra/secrets.yml has been skipped from compare!')
azvyagintsev3a80e342018-09-19 19:22:48 +0300419 result = '\n' + common.comparePillars(compareRoot, env.BUILD_URL, "-Ev \'infra/secrets.yml|\\.git\'")
Denis Egorenkod8748942018-09-07 12:26:20 +0400420 currentBuild.description = currentBuild.description ? currentBuild.description + result : result
azvyagintsev5c0313d2018-08-13 17:13:35 +0300421 }
Denis Egorenkod8748942018-09-07 12:26:20 +0400422 stage("TestContexts Head/Patched") {
423 def stepsForParallel = [:]
424 stepsForParallel.failFast = true
425 common.infoMsg("Found: ${contextFileListHead.size()} HEAD contexts to test.")
426 for (String context : contextFileListHead) {
427 def basename = common.GetBaseName(context, '.yml')
428 stepsForParallel.put("ContextHeadTest:${basename}", StepTestModel(basename, headReclassArtifactName, reclassInfoHeadPath))
429 }
azvyagintsev5c0313d2018-08-13 17:13:35 +0300430 common.infoMsg("Found: ${contextFileListPatched.size()} patched contexts to test.")
431 for (String context : contextFileListPatched) {
432 def basename = common.GetBaseName(context, '.yml')
Denis Egorenkod8748942018-09-07 12:26:20 +0400433 stepsForParallel.put("ContextPatchedTest:${basename}", StepTestModel(basename, patchedReclassArtifactName, reclassInfoPatchedPath))
azvyagintsev5c0313d2018-08-13 17:13:35 +0300434 }
435 parallel stepsForParallel
Denis Egorenkod8748942018-09-07 12:26:20 +0400436 common.infoMsg('All TestContexts tests done')
437 }
438 stage("Compare NodesInfo Head/Patched") {
439 // Download all artifacts
440 def stepsForParallel = [:]
441 stepsForParallel.failFast = true
442 common.infoMsg("Found: ${testModelBuildsData.size()} nodeinfo artifacts to download.")
443 testModelBuildsData.each { bname, bdata ->
444 stepsForParallel.put("FetchData:${bname}",
445 getAndUnpackNodesInfoArtifact(bdata.jobname, bdata.copyToDir, bdata.buildId))
446 }
447 parallel stepsForParallel
Denis Egorenkoa6117fc2018-09-11 13:40:00 +0400448 // remove timestamp field from rendered files
449 sh("find ${reclassNodeInfoDir} -type f -exec sed -i '/ timestamp: .*/d' {} \\;")
Denis Egorenkod8748942018-09-07 12:26:20 +0400450 // Compare patched and HEAD reclass pillars
451 result = '\n' + common.comparePillars(reclassNodeInfoDir, env.BUILD_URL, '')
452 currentBuild.description = currentBuild.description ? currentBuild.description + result : result
azvyagintsev5c0313d2018-08-13 17:13:35 +0300453 }
Denis Egorenko04ca9f22018-09-19 16:06:49 +0400454 stage('Check include order') {
azvyagintseve7b8e792018-09-21 17:27:01 +0300455 if (!checkIncludeOrder) {
456 common.infoMsg('Check include order require to much time, and currently disabled!')
457
458 } else {
459 def correctIncludeOrder = ["service", "system", "cluster"]
460 dir(reclassInfoPatchedPath) {
461 def nodeInfoFiles = findFiles(glob: "**/*.reclass.nodeinfo")
462 def messages = ["<b>Wrong include ordering found</b><ul>"]
463 def stepsForParallel = [:]
464 nodeInfoFiles.each { nodeInfo ->
465 stepsForParallel.put("Checking ${nodeInfo.path}:", {
466 def node = readYaml file: nodeInfo.path
467 def classes = node['classes']
468 def curClassID = 0
469 def prevClassID = 0
470 def wrongOrder = false
471 for (String className in classes) {
472 def currentClass = className.tokenize('.')[0]
473 curClassID = correctIncludeOrder.indexOf(currentClass)
474 if (currentClass != correctIncludeOrder[prevClassID]) {
475 if (prevClassID > curClassID) {
476 wrongOrder = true
477 common.warningMsg("File ${nodeInfo.path} contains wrong order of classes including: Includes for ${className} should be declared before ${correctIncludeOrder[prevClassID]} includes")
478 } else {
479 prevClassID = curClassID
480 }
Denis Egorenko04ca9f22018-09-19 16:06:49 +0400481 }
482 }
azvyagintseve7b8e792018-09-21 17:27:01 +0300483 if (wrongOrder) {
484 messages.add("<li>${nodeInfo.path} contains wrong order of classes including</li>")
485 }
486 })
487 }
488 parallel stepsForParallel
489 def includerOrder = '<b>No wrong include order</b>'
490 if (messages.size() != 1) {
491 includerOrder = messages.join('')
492 }
493 currentBuild.description = currentBuild.description ? currentBuild.description + includerOrder : includerOrder
Denis Egorenko04ca9f22018-09-19 16:06:49 +0400494 }
Denis Egorenko04ca9f22018-09-19 16:06:49 +0400495 }
496 }
azvyagintsev5c0313d2018-08-13 17:13:35 +0300497 sh(script: 'find . -mindepth 1 -delete > /dev/null || true')
498
499 } catch (Throwable e) {
azvyagintsev9df82e52018-09-06 19:17:18 +0300500 if (alreadyMerged) {
501 currentBuild.result = 'ABORTED'
502 currentBuild.description = "Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to gate them"
503 return
504 }
azvyagintsev5c0313d2018-08-13 17:13:35 +0300505 currentBuild.result = "FAILURE"
506 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
507 throw e
508 } finally {
509 def dummy = "dummy"
azvyagintsev5c0313d2018-08-13 17:13:35 +0300510 }
511 }
chnydae80bb922017-05-29 17:48:40 +0200512}