chnyda | e80bb92 | 2017-05-29 17:48:40 +0200 | [diff] [blame] | 1 | common = new com.mirantis.mk.Common() |
chnyda | bc63c9a | 2017-05-30 15:37:54 +0200 | [diff] [blame] | 2 | gerrit = new com.mirantis.mk.Gerrit() |
chnyda | e80bb92 | 2017-05-29 17:48:40 +0200 | [diff] [blame] | 3 | git = new com.mirantis.mk.Git() |
| 4 | python = new com.mirantis.mk.Python() |
| 5 | saltModelTesting = new com.mirantis.mk.SaltModelTesting() |
| 6 | |
chnyda | 467f10f | 2017-05-30 17:25:07 +0200 | [diff] [blame] | 7 | def generateSaltMaster(modEnv, clusterDomain, clusterName) { |
| 8 | def nodeFile = "${modEnv}/nodes/cfg01.${clusterDomain}.yml" |
chnyda | e80bb92 | 2017-05-29 17:48:40 +0200 | [diff] [blame] | 9 | def nodeString = """classes: |
| 10 | - cluster.${clusterName}.infra.config |
| 11 | parameters: |
| 12 | _param: |
| 13 | linux_system_codename: xenial |
| 14 | reclass_data_revision: master |
| 15 | linux: |
| 16 | system: |
| 17 | name: cfg01 |
| 18 | domain: ${clusterDomain} |
| 19 | """ |
chnyda | 467f10f | 2017-05-30 17:25:07 +0200 | [diff] [blame] | 20 | sh "mkdir -p ${modEnv}/nodes/" |
| 21 | println "Create file ${nodeFile}" |
chnyda | e80bb92 | 2017-05-29 17:48:40 +0200 | [diff] [blame] | 22 | writeFile(file: nodeFile, text: nodeString) |
| 23 | } |
| 24 | |
chnyda | 467f10f | 2017-05-30 17:25:07 +0200 | [diff] [blame] | 25 | def generateModel(modelFile, cutterEnv) { |
chnyda | bc63c9a | 2017-05-30 15:37:54 +0200 | [diff] [blame] | 26 | def templateEnv = "${env.WORKSPACE}" |
| 27 | def modelEnv = "${env.WORKSPACE}/model" |
chnyda | 467f10f | 2017-05-30 17:25:07 +0200 | [diff] [blame] | 28 | def basename = sh(script: "basename ${modelFile} .yml", returnStdout: true).trim() |
chnyda | bc63c9a | 2017-05-30 15:37:54 +0200 | [diff] [blame] | 29 | def generatedModel = "${modelEnv}/${basename}" |
| 30 | def testEnv = "${env.WORKSPACE}/test" |
chnyda | 467f10f | 2017-05-30 17:25:07 +0200 | [diff] [blame] | 31 | def content = readFile(file: "${templateEnv}/contexts/${modelFile}") |
chnyda | bc63c9a | 2017-05-30 15:37:54 +0200 | [diff] [blame] | 32 | def templateContext = readYaml text: content |
chnyda | e80bb92 | 2017-05-29 17:48:40 +0200 | [diff] [blame] | 33 | def clusterDomain = templateContext.default_context.cluster_domain |
| 34 | def clusterName = templateContext.default_context.cluster_name |
chnyda | bc63c9a | 2017-05-30 15:37:54 +0200 | [diff] [blame] | 35 | def outputDestination = "${generatedModel}/classes/cluster/${clusterName}" |
chnyda | e80bb92 | 2017-05-29 17:48:40 +0200 | [diff] [blame] | 36 | def targetBranch = "feature/${clusterName}" |
chnyda | bc63c9a | 2017-05-30 15:37:54 +0200 | [diff] [blame] | 37 | def templateBaseDir = "${env.WORKSPACE}" |
| 38 | def templateDir = "${templateEnv}/dir" |
chnyda | e80bb92 | 2017-05-29 17:48:40 +0200 | [diff] [blame] | 39 | def templateOutputDir = templateBaseDir |
chnyda | bc63c9a | 2017-05-30 15:37:54 +0200 | [diff] [blame] | 40 | sh "rm -rf ${generatedModel} || true" |
chnyda | e80bb92 | 2017-05-29 17:48:40 +0200 | [diff] [blame] | 41 | |
Tomáš Kukrál | 9a6821e | 2017-07-24 11:07:01 +0200 | [diff] [blame] | 42 | common.infoMsg("Generating model from context ${modelFile}") |
chnyda | e79b94e | 2017-07-10 09:46:25 +0200 | [diff] [blame] | 43 | |
Mikhail Ivanov | 30c91f4 | 2017-11-07 20:42:26 +0400 | [diff] [blame] | 44 | def productList = ["infra", "cicd", "opencontrail", "kubernetes", "openstack", "oss", "stacklight", "ceph"] |
chnyda | 467f10f | 2017-05-30 17:25:07 +0200 | [diff] [blame] | 45 | for (product in productList) { |
Tomáš Kukrál | 9a6821e | 2017-07-24 11:07:01 +0200 | [diff] [blame] | 46 | |
| 47 | // get templateOutputDir and productDir |
| 48 | if (product.startsWith("stacklight")) { |
| 49 | templateOutputDir = "${env.WORKSPACE}/output/stacklight" |
| 50 | try { |
| 51 | productDir = "stacklight" + templateContext.default_context['stacklight_version'] |
| 52 | } catch (Throwable e) { |
| 53 | productDir = "stacklight1" |
| 54 | } |
| 55 | } else { |
| 56 | templateOutputDir = "${env.WORKSPACE}/output/${product}" |
| 57 | productDir = product |
| 58 | } |
| 59 | |
chnyda | 467f10f | 2017-05-30 17:25:07 +0200 | [diff] [blame] | 60 | if (product == "infra" || (templateContext.default_context["${product}_enabled"] |
| 61 | && templateContext.default_context["${product}_enabled"].toBoolean())) { |
Tomáš Kukrál | 9a6821e | 2017-07-24 11:07:01 +0200 | [diff] [blame] | 62 | |
| 63 | templateDir = "${templateEnv}/cluster_product/${productDir}" |
| 64 | common.infoMsg("Generating product " + product + " from " + templateDir + " to " + templateOutputDir) |
| 65 | |
chnyda | 467f10f | 2017-05-30 17:25:07 +0200 | [diff] [blame] | 66 | sh "rm -rf ${templateOutputDir} || true" |
| 67 | sh "mkdir -p ${templateOutputDir}" |
| 68 | sh "mkdir -p ${outputDestination}" |
Tomáš Kukrál | 9a6821e | 2017-07-24 11:07:01 +0200 | [diff] [blame] | 69 | |
chnyda | 467f10f | 2017-05-30 17:25:07 +0200 | [diff] [blame] | 70 | python.buildCookiecutterTemplate(templateDir, content, templateOutputDir, cutterEnv, templateBaseDir) |
| 71 | sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}" |
Tomáš Kukrál | 9a6821e | 2017-07-24 11:07:01 +0200 | [diff] [blame] | 72 | } else { |
| 73 | common.warningMsg("Product " + product + " is disabled") |
chnyda | e80bb92 | 2017-05-29 17:48:40 +0200 | [diff] [blame] | 74 | } |
| 75 | } |
chnyda | 467f10f | 2017-05-30 17:25:07 +0200 | [diff] [blame] | 76 | generateSaltMaster(generatedModel, clusterDomain, clusterName) |
chnyda | e80bb92 | 2017-05-29 17:48:40 +0200 | [diff] [blame] | 77 | } |
| 78 | |
chnyda | 467f10f | 2017-05-30 17:25:07 +0200 | [diff] [blame] | 79 | def testModel(modelFile, testEnv) { |
chnyda | bc63c9a | 2017-05-30 15:37:54 +0200 | [diff] [blame] | 80 | def templateEnv = "${env.WORKSPACE}" |
chnyda | 467f10f | 2017-05-30 17:25:07 +0200 | [diff] [blame] | 81 | def content = readFile(file: "${templateEnv}/contexts/${modelFile}.yml") |
chnyda | bc63c9a | 2017-05-30 15:37:54 +0200 | [diff] [blame] | 82 | def templateContext = readYaml text: content |
Jakub Josef | 1308e28 | 2017-08-18 14:08:54 +0200 | [diff] [blame] | 83 | def clusterName = templateContext.default_context.cluster_name |
chnyda | bc63c9a | 2017-05-30 15:37:54 +0200 | [diff] [blame] | 84 | def clusterDomain = templateContext.default_context.cluster_domain |
chnyda | 6f78a9d | 2017-12-19 11:34:26 +0100 | [diff] [blame] | 85 | if (SYSTEM_GIT_URL == "") { |
| 86 | git.checkoutGitRepository("${testEnv}/classes/system", RECLASS_MODEL_URL, RECLASS_MODEL_BRANCH, CREDENTIALS_ID) |
| 87 | } else { |
| 88 | dir("${testEnv}/classes/system") { |
| 89 | if (!gerrit.gerritPatchsetCheckout(SYSTEM_GIT_URL, SYSTEM_GIT_REF, "HEAD", CREDENTIALS_ID)) { |
| 90 | common.errorMsg("Failed to obtain system reclass with url: ${SYSTEM_GIT_URL} and ${SYSTEM_GIT_REF}") |
| 91 | } |
| 92 | } |
| 93 | } |
chnyda | 7dd8cd9 | 2017-12-18 10:19:25 +0100 | [diff] [blame] | 94 | |
| 95 | def nbTry = 0 |
| 96 | while (nbTry < 5) { |
| 97 | nbTry++ |
| 98 | try { |
Kirill Mashchenko | 418b059 | 2018-03-15 14:56:28 +0300 | [diff] [blame] | 99 | saltModelTesting.setupAndTestNode("cfg01.${clusterDomain}", clusterName, EXTRA_FORMULAS, testEnv, "pkg", DISTRIB_REVISION) |
chnyda | 7dd8cd9 | 2017-12-18 10:19:25 +0100 | [diff] [blame] | 100 | break |
| 101 | } catch (Exception e) { |
| 102 | if (e.getMessage() == "script returned exit code 124") { |
| 103 | common.errorMsg("Impossible to test node due to timeout of salt-master, retriggering") |
| 104 | } else { |
| 105 | throw e |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
chnyda | bc63c9a | 2017-05-30 15:37:54 +0200 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | def gerritRef |
| 113 | try { |
| 114 | gerritRef = GERRIT_REFSPEC |
| 115 | } catch (MissingPropertyException e) { |
| 116 | gerritRef = null |
chnyda | e80bb92 | 2017-05-29 17:48:40 +0200 | [diff] [blame] | 117 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 118 | timeout(time: 12, unit: 'HOURS') { |
| 119 | node("python&&docker") { |
| 120 | def templateEnv = "${env.WORKSPACE}" |
| 121 | def cutterEnv = "${env.WORKSPACE}/cutter" |
| 122 | def jinjaEnv = "${env.WORKSPACE}/jinja" |
chnyda | e80bb92 | 2017-05-29 17:48:40 +0200 | [diff] [blame] | 123 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 124 | try { |
| 125 | stage("Cleanup") { |
| 126 | sh("rm -rf * || true") |
Ruslan Kamaldinov | 6feef40 | 2017-08-02 16:55:58 +0400 | [diff] [blame] | 127 | } |
Ruslan Kamaldinov | 6feef40 | 2017-08-02 16:55:58 +0400 | [diff] [blame] | 128 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 129 | stage ('Download Cookiecutter template') { |
| 130 | if (gerritRef) { |
| 131 | def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID) |
| 132 | merged = gerritChange.status == "MERGED" |
| 133 | if(!merged){ |
| 134 | checkouted = gerrit.gerritPatchsetCheckout ([ |
| 135 | credentialsId : CREDENTIALS_ID |
| 136 | ]) |
| 137 | } else{ |
| 138 | common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to gate them") |
| 139 | } |
| 140 | } else { |
| 141 | git.checkoutGitRepository(templateEnv, COOKIECUTTER_TEMPLATE_URL, COOKIECUTTER_TEMPLATE_BRANCH, CREDENTIALS_ID) |
Ruslan Kamaldinov | 6feef40 | 2017-08-02 16:55:58 +0400 | [diff] [blame] | 142 | } |
| 143 | } |
Ruslan Kamaldinov | 6feef40 | 2017-08-02 16:55:58 +0400 | [diff] [blame] | 144 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 145 | stage("Setup") { |
| 146 | python.setupCookiecutterVirtualenv(cutterEnv) |
| 147 | } |
Ruslan Kamaldinov | 6feef40 | 2017-08-02 16:55:58 +0400 | [diff] [blame] | 148 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 149 | stage("Check workflow_definition") { |
| 150 | sh "python ${env.WORKSPACE}/workflow_definition_test.py" |
| 151 | } |
| 152 | |
| 153 | def contextFiles |
| 154 | dir("${templateEnv}/contexts") { |
| 155 | contextFiles = findFiles(glob: "*.yml") |
| 156 | } |
| 157 | |
| 158 | def contextFileList = [] |
| 159 | for (int i = 0; i < contextFiles.size(); i++) { |
| 160 | contextFileList << contextFiles[i] |
| 161 | } |
| 162 | |
| 163 | stage("generate-model") { |
| 164 | for (contextFile in contextFileList) { |
| 165 | generateModel(contextFile, cutterEnv) |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | dir("${env.WORKSPACE}") { |
| 170 | sh(returnStatus: true, script: "tar -zcvf model.tar.gz -C model .") |
| 171 | archiveArtifacts artifacts: "model.tar.gz" |
| 172 | } |
| 173 | |
| 174 | stage("test-nodes") { |
| 175 | def partitions = common.partitionList(contextFileList, PARALLEL_NODE_GROUP_SIZE.toInteger()) |
| 176 | def buildSteps = [:] |
Richard Felkl | 9351d03 | 2018-06-28 15:03:14 +0200 | [diff] [blame^] | 177 | partitions.eachWithIndex { partition, i -> |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 178 | buildSteps.put("partition-${i}", new HashMap<String,org.jenkinsci.plugins.workflow.cps.CpsClosure2>()) |
Richard Felkl | 9351d03 | 2018-06-28 15:03:14 +0200 | [diff] [blame^] | 179 | for(part in partition){ |
| 180 | def basename = sh(script: "basename ${part} .yml", returnStdout: true).trim() |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 181 | def testEnv = "${env.WORKSPACE}/model/${basename}" |
| 182 | buildSteps.get("partition-${i}").put(basename, { testModel(basename, testEnv) }) |
| 183 | } |
| 184 | } |
| 185 | common.serial(buildSteps) |
| 186 | } |
| 187 | |
| 188 | stage ('Clean workspace directories') { |
| 189 | sh(returnStatus: true, script: "rm -rfv * > /dev/null || true") |
| 190 | } |
| 191 | |
| 192 | } catch (Throwable e) { |
| 193 | currentBuild.result = "FAILURE" |
| 194 | currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message |
| 195 | throw e |
| 196 | } finally { |
| 197 | def dummy = "dummy" |
| 198 | //FAILING common.sendNotification(currentBuild.result,"",["slack"]) |
| 199 | } |
chnyda | e80bb92 | 2017-05-29 17:48:40 +0200 | [diff] [blame] | 200 | } |
| 201 | } |