chnyda | e80bb92 | 2017-05-29 17:48:40 +0200 | [diff] [blame^] | 1 | common = new com.mirantis.mk.Common() |
| 2 | git = new com.mirantis.mk.Git() |
| 3 | python = new com.mirantis.mk.Python() |
| 4 | saltModelTesting = new com.mirantis.mk.SaltModelTesting() |
| 5 | |
| 6 | def generateSaltMaster(modelEnv) { |
| 7 | def nodeFile = "${modelEnv}/nodes/cfg01.${clusterDomain}.yml" |
| 8 | def nodeString = """classes: |
| 9 | - cluster.${clusterName}.infra.config |
| 10 | parameters: |
| 11 | _param: |
| 12 | linux_system_codename: xenial |
| 13 | reclass_data_revision: master |
| 14 | linux: |
| 15 | system: |
| 16 | name: cfg01 |
| 17 | domain: ${clusterDomain} |
| 18 | """ |
| 19 | sh "mkdir -p ${modelEnv}/nodes/" |
| 20 | writeFile(file: nodeFile, text: nodeString) |
| 21 | } |
| 22 | |
| 23 | def generate(contextFile) { |
| 24 | def templateEnv = "${env.WORKSPACE}/template" |
| 25 | def baseName = sh(script: "basename ${contextFile} .yml", returnStdout: true) |
| 26 | def modelEnv = "${env.WORKSPACE}/model-${baseName}" |
| 27 | def cookiecutterTemplateContext = readFile(file: "${env.WORKSPACE}/contexts/contextFile") |
| 28 | def templateContext = readYaml text: cookiecutterTemplateContext |
| 29 | def clusterDomain = templateContext.default_context.cluster_domain |
| 30 | def clusterName = templateContext.default_context.cluster_name |
| 31 | def cutterEnv = "${env.WORKSPACE}/cutter" |
| 32 | def jinjaEnv = "${env.WORKSPACE}/jinja" |
| 33 | def outputDestination = "${modelEnv}/classes/cluster/${clusterName}" |
| 34 | def targetBranch = "feature/${clusterName}" |
| 35 | def templateBaseDir = "${env.WORKSPACE}/template" |
| 36 | def templateDir = "${templateEnv}/template/dir" |
| 37 | def templateOutputDir = templateBaseDir |
| 38 | sh("rm -rf ${templateBaseDir} || true") |
| 39 | |
| 40 | def productList = ["infra", "cicd", "opencontrail", "kubernetes", "openstack", "stacklight"] |
| 41 | for (product in productList) { |
| 42 | def stagename = (product == "infra") ? "Generate base infrastructure" : "Generate product ${product}" |
| 43 | println stagename |
| 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}/template/output/${product}" |
| 48 | sh "mkdir -p ${templateOutputDir}" |
| 49 | sh "mkdir -p ${outputDestination}" |
| 50 | python.setupCookiecutterVirtualenv(cutterEnv) |
| 51 | python.buildCookiecutterTemplate(templateDir, cookiecutterTemplateContext, templateOutputDir, cutterEnv, templateBaseDir) |
| 52 | sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}" |
| 53 | } |
| 54 | } |
| 55 | generateSaltMaster(modelEnv) |
| 56 | } |
| 57 | |
| 58 | def testModel(contextFile) { |
| 59 | def baseName = sh(script: "basename ${contextFile} .yml", returnStdout: true) |
| 60 | def modelEnv = "${env.WORKSPACE}/model-${baseName}" |
| 61 | git.checkoutGitRepository("${modelEnv}/classes/system", RECLASS_MODEL_URL, RECLASS_MODEL_BRANCH, RECLASS_MODEL_CREDENTIALS) |
| 62 | saltModelTesting.setupAndTestNode("cfg01.${clusterDomain}", "", modelEnv) |
| 63 | } |
| 64 | |
| 65 | timestamps { |
| 66 | node("python&&docker") { |
| 67 | def templateEnv = "${env.WORKSPACE}/template" |
| 68 | |
| 69 | try { |
| 70 | stage ('Download Cookiecutter template') { |
| 71 | if (gerritRef) { |
| 72 | def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID) |
| 73 | merged = gerritChange.status == "MERGED" |
| 74 | if(!merged){ |
| 75 | checkouted = gerrit.gerritPatchsetCheckout ([ |
| 76 | credentialsId : CREDENTIALS_ID |
| 77 | ]) |
| 78 | } else{ |
| 79 | common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to gate them") |
| 80 | } |
| 81 | } else { |
| 82 | gerrit.gerritPatchsetCheckout(COOKIECUTTER_TEMPLATE_URL, COOKIECUTTER_TEMPLATE_BRANCH, "HEAD", CREDENTIALS_ID) |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | def contextFiles |
| 87 | dir("contexts") { |
| 88 | contextFiles = findFiles(glob: "*.yml") |
| 89 | } |
| 90 | |
| 91 | for (contextFile in contextFiles) { |
| 92 | generate(contextFile) |
| 93 | } |
| 94 | |
| 95 | stage("test-nodes") { |
| 96 | def partitions = common.partitionList(contextFiles, 3) |
| 97 | def buildSteps = [:] |
| 98 | for (int i = 0; i < partitions.size(); i++) { |
| 99 | def partition = partitions[i] |
| 100 | buildSteps.put("partition-${i}", new HashMap<String,org.jenkinsci.plugins.workflow.cps.CpsClosure2>()) |
| 101 | for(int k = 0; k < partition.size; k++){ |
| 102 | def basename = sh(script: "basename ${partition[k]} .yml", returnStdout: true).trim() |
| 103 | def modelEnv = "${env.WORKSPACE}/model-${baseName}" |
| 104 | buildSteps.get("partition-${i}").put(basename, { saltModelTesting.setupAndTestNode(basename, "", modelEnv) }) |
| 105 | } |
| 106 | } |
| 107 | common.serial(buildSteps) |
| 108 | } |
| 109 | |
| 110 | } catch (Throwable e) { |
| 111 | currentBuild.result = "FAILURE" |
| 112 | throw e |
| 113 | } finally { |
| 114 | stage ('Clean workspace directories') { |
| 115 | sh(returnStatus: true, script: "rm -rfv *") |
| 116 | } |
| 117 | common.sendNotification(currentBuild.result,"",["slack"]) |
| 118 | } |
| 119 | } |
| 120 | } |