blob: e59f0ce1f5860df6eb2af0b9af394c821465e8a4 [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
Tomáš Kukrál9a6821e2017-07-24 11:07:01 +020042 common.infoMsg("Generating model from context ${modelFile}")
chnydae79b94e2017-07-10 09:46:25 +020043
chnyda467f10f2017-05-30 17:25:07 +020044 def productList = ["infra", "cicd", "opencontrail", "kubernetes", "openstack", "stacklight"]
45 for (product in productList) {
Tomáš Kukrál9a6821e2017-07-24 11:07:01 +020046
47 // get templateOutputDir and productDir
48 if (product.startsWith("stacklight")) {
49 templateOutputDir = "${env.WORKSPACE}/output/stacklight"
50 try {
51 productDir = "stacklight" + templateContext.default_context['stacklight_version']
52 } catch (Throwable e) {
53 productDir = "stacklight1"
54 }
55 } else {
56 templateOutputDir = "${env.WORKSPACE}/output/${product}"
57 productDir = product
58 }
59
chnyda467f10f2017-05-30 17:25:07 +020060 if (product == "infra" || (templateContext.default_context["${product}_enabled"]
61 && templateContext.default_context["${product}_enabled"].toBoolean())) {
Tomáš Kukrál9a6821e2017-07-24 11:07:01 +020062
63 templateDir = "${templateEnv}/cluster_product/${productDir}"
64 common.infoMsg("Generating product " + product + " from " + templateDir + " to " + templateOutputDir)
65
chnyda467f10f2017-05-30 17:25:07 +020066 sh "rm -rf ${templateOutputDir} || true"
67 sh "mkdir -p ${templateOutputDir}"
68 sh "mkdir -p ${outputDestination}"
Tomáš Kukrál9a6821e2017-07-24 11:07:01 +020069
chnyda467f10f2017-05-30 17:25:07 +020070 python.buildCookiecutterTemplate(templateDir, content, templateOutputDir, cutterEnv, templateBaseDir)
71 sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
Tomáš Kukrál9a6821e2017-07-24 11:07:01 +020072 } else {
73 common.warningMsg("Product " + product + " is disabled")
chnydae80bb922017-05-29 17:48:40 +020074 }
75 }
chnyda467f10f2017-05-30 17:25:07 +020076 generateSaltMaster(generatedModel, clusterDomain, clusterName)
chnydae80bb922017-05-29 17:48:40 +020077}
78
chnyda467f10f2017-05-30 17:25:07 +020079def testModel(modelFile, testEnv) {
chnydabc63c9a2017-05-30 15:37:54 +020080 def templateEnv = "${env.WORKSPACE}"
chnyda467f10f2017-05-30 17:25:07 +020081 def content = readFile(file: "${templateEnv}/contexts/${modelFile}.yml")
chnydabc63c9a2017-05-30 15:37:54 +020082 def templateContext = readYaml text: content
Jakub Josef1308e282017-08-18 14:08:54 +020083 def clusterName = templateContext.default_context.cluster_name
chnydabc63c9a2017-05-30 15:37:54 +020084 def clusterDomain = templateContext.default_context.cluster_domain
Jakub Josef5d972eb2017-08-22 11:51:09 +020085 git.checkoutGitRepository("${testEnv}/classes/system", RECLASS_MODEL_URL, RECLASS_MODEL_BRANCH, CREDENTIALS_ID)
86 saltModelTesting.setupAndTestNode("cfg01.${clusterDomain}", clusterName, EXTRA_FORMULAS, testEnv)
chnydabc63c9a2017-05-30 15:37:54 +020087}
88
89def gerritRef
90try {
91 gerritRef = GERRIT_REFSPEC
92} catch (MissingPropertyException e) {
93 gerritRef = null
chnydae80bb922017-05-29 17:48:40 +020094}
95
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +040096node("python&&docker") {
97 def templateEnv = "${env.WORKSPACE}"
98 def cutterEnv = "${env.WORKSPACE}/cutter"
99 def jinjaEnv = "${env.WORKSPACE}/jinja"
chnydae80bb922017-05-29 17:48:40 +0200100
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400101 try {
102 stage("Cleanup") {
103 sh("rm -rf * || true")
chnydae80bb922017-05-29 17:48:40 +0200104 }
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400105
106 stage ('Download Cookiecutter template') {
107 if (gerritRef) {
108 def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID)
109 merged = gerritChange.status == "MERGED"
110 if(!merged){
111 checkouted = gerrit.gerritPatchsetCheckout ([
112 credentialsId : CREDENTIALS_ID
113 ])
114 } else{
115 common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to gate them")
116 }
117 } else {
118 git.checkoutGitRepository(templateEnv, COOKIECUTTER_TEMPLATE_URL, COOKIECUTTER_TEMPLATE_BRANCH, CREDENTIALS_ID)
119 }
120 }
121
122 stage("Setup") {
123 python.setupCookiecutterVirtualenv(cutterEnv)
124 }
125
chnyda0cfaf7b2017-08-29 10:35:28 +0200126 stage("Check workflow_definition") {
127 sh "python ${env.WORKSPACE}/workflow_definition_test.py"
128 }
129
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400130 def contextFiles
131 dir("${templateEnv}/contexts") {
132 contextFiles = findFiles(glob: "*.yml")
133 }
134
135 def contextFileList = []
136 for (int i = 0; i < contextFiles.size(); i++) {
137 contextFileList << contextFiles[i]
138 }
139
140 stage("generate-model") {
141 for (contextFile in contextFileList) {
142 generateModel(contextFile, cutterEnv)
143 }
144 }
145
146 dir("${env.WORKSPACE}") {
147 sh(returnStatus: true, script: "tar -zcvf model.tar.gz -C model .")
148 archiveArtifacts artifacts: "model.tar.gz"
149 }
150
151 stage("test-nodes") {
152 def partitions = common.partitionList(contextFileList, PARALLEL_NODE_GROUP_SIZE.toInteger())
153 def buildSteps = [:]
154 for (int i = 0; i < partitions.size(); i++) {
155 def partition = partitions[i]
156 buildSteps.put("partition-${i}", new HashMap<String,org.jenkinsci.plugins.workflow.cps.CpsClosure2>())
157 for(int k = 0; k < partition.size; k++){
158 def basename = sh(script: "basename ${partition[k]} .yml", returnStdout: true).trim()
159 def testEnv = "${env.WORKSPACE}/model/${basename}"
160 buildSteps.get("partition-${i}").put(basename, { testModel(basename, testEnv) })
161 }
162 }
163 common.serial(buildSteps)
164 }
165
166 stage ('Clean workspace directories') {
167 sh(returnStatus: true, script: "rm -rfv * > /dev/null || true")
168 }
169
170 } catch (Throwable e) {
171 currentBuild.result = "FAILURE"
Jakub Josefd2efd7d2017-08-22 17:49:57 +0200172 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400173 throw e
174 } finally {
Jakub Josef5603d6d2017-08-10 13:11:46 +0200175 def dummy = "dummy"
176 //FAILING common.sendNotification(currentBuild.result,"",["slack"])
chnydae80bb922017-05-29 17:48:40 +0200177 }
178}