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