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