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 | |
chnyda | 467f10f | 2017-05-30 17:25:07 +0200 | [diff] [blame] | 42 | def productList = ["infra", "cicd", "opencontrail", "kubernetes", "openstack", "stacklight"] |
| 43 | for (product in productList) { |
chnyda | 467f10f | 2017-05-30 17:25:07 +0200 | [diff] [blame] | 44 | if (product == "infra" || (templateContext.default_context["${product}_enabled"] |
| 45 | && templateContext.default_context["${product}_enabled"].toBoolean())) { |
| 46 | templateDir = "${templateEnv}/cluster_product/${product}" |
| 47 | templateOutputDir = "${env.WORKSPACE}/output/${product}" |
| 48 | sh "rm -rf ${templateOutputDir} || true" |
| 49 | sh "mkdir -p ${templateOutputDir}" |
| 50 | sh "mkdir -p ${outputDestination}" |
| 51 | python.buildCookiecutterTemplate(templateDir, content, templateOutputDir, cutterEnv, templateBaseDir) |
| 52 | sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}" |
chnyda | e80bb92 | 2017-05-29 17:48:40 +0200 | [diff] [blame] | 53 | } |
| 54 | } |
chnyda | 467f10f | 2017-05-30 17:25:07 +0200 | [diff] [blame] | 55 | generateSaltMaster(generatedModel, clusterDomain, clusterName) |
chnyda | e80bb92 | 2017-05-29 17:48:40 +0200 | [diff] [blame] | 56 | } |
| 57 | |
chnyda | 467f10f | 2017-05-30 17:25:07 +0200 | [diff] [blame] | 58 | def testModel(modelFile, testEnv) { |
chnyda | bc63c9a | 2017-05-30 15:37:54 +0200 | [diff] [blame] | 59 | def templateEnv = "${env.WORKSPACE}" |
chnyda | 467f10f | 2017-05-30 17:25:07 +0200 | [diff] [blame] | 60 | def content = readFile(file: "${templateEnv}/contexts/${modelFile}.yml") |
chnyda | bc63c9a | 2017-05-30 15:37:54 +0200 | [diff] [blame] | 61 | def templateContext = readYaml text: content |
| 62 | def clusterDomain = templateContext.default_context.cluster_domain |
| 63 | git.checkoutGitRepository("${testEnv}/classes/system", RECLASS_MODEL_URL, RECLASS_MODEL_BRANCH, CREDENTIALS_ID) |
chnyda | dac9dfd | 2017-06-14 10:57:17 +0200 | [diff] [blame] | 64 | saltModelTesting.setupAndTestNode("cfg01.${clusterDomain}", EXTRA_FORMULAS, testEnv) |
chnyda | bc63c9a | 2017-05-30 15:37:54 +0200 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | def gerritRef |
| 68 | try { |
| 69 | gerritRef = GERRIT_REFSPEC |
| 70 | } catch (MissingPropertyException e) { |
| 71 | gerritRef = null |
chnyda | e80bb92 | 2017-05-29 17:48:40 +0200 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | timestamps { |
| 75 | node("python&&docker") { |
chnyda | bc63c9a | 2017-05-30 15:37:54 +0200 | [diff] [blame] | 76 | def templateEnv = "${env.WORKSPACE}" |
| 77 | def cutterEnv = "${env.WORKSPACE}/cutter" |
| 78 | def jinjaEnv = "${env.WORKSPACE}/jinja" |
chnyda | e80bb92 | 2017-05-29 17:48:40 +0200 | [diff] [blame] | 79 | |
| 80 | try { |
chnyda | bc63c9a | 2017-05-30 15:37:54 +0200 | [diff] [blame] | 81 | stage("Cleanup") { |
| 82 | sh("rm -rf * || true") |
| 83 | } |
| 84 | |
chnyda | e80bb92 | 2017-05-29 17:48:40 +0200 | [diff] [blame] | 85 | stage ('Download Cookiecutter template') { |
| 86 | if (gerritRef) { |
| 87 | def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID) |
| 88 | merged = gerritChange.status == "MERGED" |
| 89 | if(!merged){ |
| 90 | checkouted = gerrit.gerritPatchsetCheckout ([ |
| 91 | credentialsId : CREDENTIALS_ID |
| 92 | ]) |
| 93 | } else{ |
| 94 | common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to gate them") |
| 95 | } |
| 96 | } else { |
chnyda | 0381bc5 | 2017-05-31 11:19:15 +0200 | [diff] [blame] | 97 | git.checkoutGitRepository(templateEnv, COOKIECUTTER_TEMPLATE_URL, COOKIECUTTER_TEMPLATE_BRANCH, CREDENTIALS_ID) |
chnyda | e80bb92 | 2017-05-29 17:48:40 +0200 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
chnyda | bc63c9a | 2017-05-30 15:37:54 +0200 | [diff] [blame] | 101 | stage("Setup") { |
| 102 | python.setupCookiecutterVirtualenv(cutterEnv) |
| 103 | } |
| 104 | |
chnyda | e80bb92 | 2017-05-29 17:48:40 +0200 | [diff] [blame] | 105 | def contextFiles |
chnyda | bc63c9a | 2017-05-30 15:37:54 +0200 | [diff] [blame] | 106 | dir("${templateEnv}/contexts") { |
chnyda | e80bb92 | 2017-05-29 17:48:40 +0200 | [diff] [blame] | 107 | contextFiles = findFiles(glob: "*.yml") |
| 108 | } |
| 109 | |
chnyda | bc63c9a | 2017-05-30 15:37:54 +0200 | [diff] [blame] | 110 | def contextFileList = [] |
| 111 | for (int i = 0; i < contextFiles.size(); i++) { |
chnyda | bc63c9a | 2017-05-30 15:37:54 +0200 | [diff] [blame] | 112 | contextFileList << contextFiles[i] |
| 113 | } |
| 114 | |
| 115 | stage("generate-model") { |
chnyda | bc63c9a | 2017-05-30 15:37:54 +0200 | [diff] [blame] | 116 | for (contextFile in contextFileList) { |
chnyda | 467f10f | 2017-05-30 17:25:07 +0200 | [diff] [blame] | 117 | generateModel(contextFile, cutterEnv) |
chnyda | bc63c9a | 2017-05-30 15:37:54 +0200 | [diff] [blame] | 118 | } |
chnyda | e80bb92 | 2017-05-29 17:48:40 +0200 | [diff] [blame] | 119 | } |
| 120 | |
chnyda | 30e04c4 | 2017-06-01 10:24:36 +0200 | [diff] [blame] | 121 | stage("test-nodes") { |
| 122 | def partitions = common.partitionList(contextFileList, PARALLEL_NODE_GROUP_SIZE.toInteger()) |
| 123 | def buildSteps = [:] |
| 124 | for (int i = 0; i < partitions.size(); i++) { |
| 125 | def partition = partitions[i] |
| 126 | buildSteps.put("partition-${i}", new HashMap<String,org.jenkinsci.plugins.workflow.cps.CpsClosure2>()) |
| 127 | for(int k = 0; k < partition.size; k++){ |
| 128 | def basename = sh(script: "basename ${partition[k]} .yml", returnStdout: true).trim() |
| 129 | def testEnv = "${env.WORKSPACE}/model/${basename}" |
| 130 | buildSteps.get("partition-${i}").put(basename, { testModel(basename, testEnv) }) |
chnyda | e80bb92 | 2017-05-29 17:48:40 +0200 | [diff] [blame] | 131 | } |
| 132 | } |
chnyda | 30e04c4 | 2017-06-01 10:24:36 +0200 | [diff] [blame] | 133 | common.serial(buildSteps) |
chnyda | e80bb92 | 2017-05-29 17:48:40 +0200 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | } catch (Throwable e) { |
| 137 | currentBuild.result = "FAILURE" |
| 138 | throw e |
| 139 | } finally { |
| 140 | stage ('Clean workspace directories') { |
chnyda | 0381bc5 | 2017-05-31 11:19:15 +0200 | [diff] [blame] | 141 | sh(returnStatus: true, script: "rm -rfv * > /dev/null || true") |
chnyda | e80bb92 | 2017-05-29 17:48:40 +0200 | [diff] [blame] | 142 | } |
| 143 | common.sendNotification(currentBuild.result,"",["slack"]) |
| 144 | } |
| 145 | } |
| 146 | } |