blob: b1e9d2f16fc21f6f8c641326e22d9180834d7f12 [file] [log] [blame]
chnydae80bb922017-05-29 17:48:40 +02001common = new com.mirantis.mk.Common()
chnydabc63c9a2017-05-30 15:37:54 +02002gerrit = new com.mirantis.mk.Gerrit()
chnydae80bb922017-05-29 17:48:40 +02003git = new com.mirantis.mk.Git()
4python = new com.mirantis.mk.Python()
5saltModelTesting = new com.mirantis.mk.SaltModelTesting()
6
chnyda467f10f2017-05-30 17:25:07 +02007def generateSaltMaster(modEnv, clusterDomain, clusterName) {
8 def nodeFile = "${modEnv}/nodes/cfg01.${clusterDomain}.yml"
chnydae80bb922017-05-29 17:48:40 +02009 def nodeString = """classes:
10- cluster.${clusterName}.infra.config
11parameters:
12 _param:
13 linux_system_codename: xenial
14 reclass_data_revision: master
15 linux:
16 system:
17 name: cfg01
18 domain: ${clusterDomain}
19"""
chnyda467f10f2017-05-30 17:25:07 +020020 sh "mkdir -p ${modEnv}/nodes/"
21 println "Create file ${nodeFile}"
chnydae80bb922017-05-29 17:48:40 +020022 writeFile(file: nodeFile, text: nodeString)
23}
24
chnyda467f10f2017-05-30 17:25:07 +020025def generateModel(modelFile, cutterEnv) {
chnydabc63c9a2017-05-30 15:37:54 +020026 def templateEnv = "${env.WORKSPACE}"
27 def modelEnv = "${env.WORKSPACE}/model"
chnyda467f10f2017-05-30 17:25:07 +020028 def basename = sh(script: "basename ${modelFile} .yml", returnStdout: true).trim()
chnydabc63c9a2017-05-30 15:37:54 +020029 def generatedModel = "${modelEnv}/${basename}"
30 def testEnv = "${env.WORKSPACE}/test"
chnyda467f10f2017-05-30 17:25:07 +020031 def content = readFile(file: "${templateEnv}/contexts/${modelFile}")
chnydabc63c9a2017-05-30 15:37:54 +020032 def templateContext = readYaml text: content
chnydae80bb922017-05-29 17:48:40 +020033 def clusterDomain = templateContext.default_context.cluster_domain
34 def clusterName = templateContext.default_context.cluster_name
chnydabc63c9a2017-05-30 15:37:54 +020035 def outputDestination = "${generatedModel}/classes/cluster/${clusterName}"
chnydae80bb922017-05-29 17:48:40 +020036 def targetBranch = "feature/${clusterName}"
chnydabc63c9a2017-05-30 15:37:54 +020037 def templateBaseDir = "${env.WORKSPACE}"
38 def templateDir = "${templateEnv}/dir"
chnydae80bb922017-05-29 17:48:40 +020039 def templateOutputDir = templateBaseDir
chnydabc63c9a2017-05-30 15:37:54 +020040 sh "rm -rf ${generatedModel} || true"
chnydae80bb922017-05-29 17:48:40 +020041
chnyda467f10f2017-05-30 17:25:07 +020042 def productList = ["infra", "cicd", "opencontrail", "kubernetes", "openstack", "stacklight"]
43 for (product in productList) {
chnyda467f10f2017-05-30 17:25:07 +020044 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}"
chnydae80bb922017-05-29 17:48:40 +020053 }
54 }
chnyda467f10f2017-05-30 17:25:07 +020055 generateSaltMaster(generatedModel, clusterDomain, clusterName)
chnydae80bb922017-05-29 17:48:40 +020056}
57
chnyda467f10f2017-05-30 17:25:07 +020058def testModel(modelFile, testEnv) {
chnydabc63c9a2017-05-30 15:37:54 +020059 def templateEnv = "${env.WORKSPACE}"
chnyda467f10f2017-05-30 17:25:07 +020060 def content = readFile(file: "${templateEnv}/contexts/${modelFile}.yml")
chnydabc63c9a2017-05-30 15:37:54 +020061 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)
64 saltModelTesting.setupAndTestNode("cfg01.${clusterDomain}", "", testEnv)
65}
66
67def gerritRef
68try {
69 gerritRef = GERRIT_REFSPEC
70} catch (MissingPropertyException e) {
71 gerritRef = null
chnydae80bb922017-05-29 17:48:40 +020072}
73
74timestamps {
75 node("python&&docker") {
chnydabc63c9a2017-05-30 15:37:54 +020076 def templateEnv = "${env.WORKSPACE}"
77 def cutterEnv = "${env.WORKSPACE}/cutter"
78 def jinjaEnv = "${env.WORKSPACE}/jinja"
chnydae80bb922017-05-29 17:48:40 +020079
80 try {
chnydabc63c9a2017-05-30 15:37:54 +020081 stage("Cleanup") {
82 sh("rm -rf * || true")
83 }
84
chnydae80bb922017-05-29 17:48:40 +020085 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 {
chnyda0381bc52017-05-31 11:19:15 +020097 git.checkoutGitRepository(templateEnv, COOKIECUTTER_TEMPLATE_URL, COOKIECUTTER_TEMPLATE_BRANCH, CREDENTIALS_ID)
chnydae80bb922017-05-29 17:48:40 +020098 }
99 }
100
chnydabc63c9a2017-05-30 15:37:54 +0200101 stage("Setup") {
102 python.setupCookiecutterVirtualenv(cutterEnv)
103 }
104
chnydae80bb922017-05-29 17:48:40 +0200105 def contextFiles
chnydabc63c9a2017-05-30 15:37:54 +0200106 dir("${templateEnv}/contexts") {
chnydae80bb922017-05-29 17:48:40 +0200107 contextFiles = findFiles(glob: "*.yml")
108 }
109
chnydabc63c9a2017-05-30 15:37:54 +0200110 def contextFileList = []
111 for (int i = 0; i < contextFiles.size(); i++) {
chnydabc63c9a2017-05-30 15:37:54 +0200112 contextFileList << contextFiles[i]
113 }
114
115 stage("generate-model") {
chnydabc63c9a2017-05-30 15:37:54 +0200116 for (contextFile in contextFileList) {
chnyda467f10f2017-05-30 17:25:07 +0200117 generateModel(contextFile, cutterEnv)
chnydabc63c9a2017-05-30 15:37:54 +0200118 }
chnydae80bb922017-05-29 17:48:40 +0200119 }
120
chnyda30e04c42017-06-01 10:24:36 +0200121 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) })
chnydae80bb922017-05-29 17:48:40 +0200131 }
132 }
chnyda30e04c42017-06-01 10:24:36 +0200133 common.serial(buildSteps)
chnydae80bb922017-05-29 17:48:40 +0200134 }
135
136 } catch (Throwable e) {
137 currentBuild.result = "FAILURE"
138 throw e
139 } finally {
140 stage ('Clean workspace directories') {
chnyda0381bc52017-05-31 11:19:15 +0200141 sh(returnStatus: true, script: "rm -rfv * > /dev/null || true")
chnydae80bb922017-05-29 17:48:40 +0200142 }
143 common.sendNotification(currentBuild.result,"",["slack"])
144 }
145 }
146}