blob: fa98b9a07ab37b7b3fd477af1e8f9b97c6de6a43 [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
chnydae79b94e2017-07-10 09:46:25 +020042 println "Generating model from context ${modelFile}"
43
chnyda467f10f2017-05-30 17:25:07 +020044 def productList = ["infra", "cicd", "opencontrail", "kubernetes", "openstack", "stacklight"]
45 for (product in productList) {
chnyda467f10f2017-05-30 17:25:07 +020046 if (product == "infra" || (templateContext.default_context["${product}_enabled"]
47 && templateContext.default_context["${product}_enabled"].toBoolean())) {
48 templateDir = "${templateEnv}/cluster_product/${product}"
49 templateOutputDir = "${env.WORKSPACE}/output/${product}"
50 sh "rm -rf ${templateOutputDir} || true"
51 sh "mkdir -p ${templateOutputDir}"
52 sh "mkdir -p ${outputDestination}"
53 python.buildCookiecutterTemplate(templateDir, content, templateOutputDir, cutterEnv, templateBaseDir)
54 sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
chnydae80bb922017-05-29 17:48:40 +020055 }
56 }
chnyda467f10f2017-05-30 17:25:07 +020057 generateSaltMaster(generatedModel, clusterDomain, clusterName)
chnydae80bb922017-05-29 17:48:40 +020058}
59
chnyda467f10f2017-05-30 17:25:07 +020060def testModel(modelFile, testEnv) {
chnydabc63c9a2017-05-30 15:37:54 +020061 def templateEnv = "${env.WORKSPACE}"
chnyda467f10f2017-05-30 17:25:07 +020062 def content = readFile(file: "${templateEnv}/contexts/${modelFile}.yml")
chnydabc63c9a2017-05-30 15:37:54 +020063 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)
chnydadac9dfd2017-06-14 10:57:17 +020066 saltModelTesting.setupAndTestNode("cfg01.${clusterDomain}", EXTRA_FORMULAS, testEnv)
chnydabc63c9a2017-05-30 15:37:54 +020067}
68
69def gerritRef
70try {
71 gerritRef = GERRIT_REFSPEC
72} catch (MissingPropertyException e) {
73 gerritRef = null
chnydae80bb922017-05-29 17:48:40 +020074}
75
76timestamps {
77 node("python&&docker") {
chnydabc63c9a2017-05-30 15:37:54 +020078 def templateEnv = "${env.WORKSPACE}"
79 def cutterEnv = "${env.WORKSPACE}/cutter"
80 def jinjaEnv = "${env.WORKSPACE}/jinja"
chnydae80bb922017-05-29 17:48:40 +020081
82 try {
chnydabc63c9a2017-05-30 15:37:54 +020083 stage("Cleanup") {
84 sh("rm -rf * || true")
85 }
86
chnydae80bb922017-05-29 17:48:40 +020087 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 {
chnyda0381bc52017-05-31 11:19:15 +020099 git.checkoutGitRepository(templateEnv, COOKIECUTTER_TEMPLATE_URL, COOKIECUTTER_TEMPLATE_BRANCH, CREDENTIALS_ID)
chnydae80bb922017-05-29 17:48:40 +0200100 }
101 }
102
chnydabc63c9a2017-05-30 15:37:54 +0200103 stage("Setup") {
104 python.setupCookiecutterVirtualenv(cutterEnv)
105 }
106
chnydae80bb922017-05-29 17:48:40 +0200107 def contextFiles
chnydabc63c9a2017-05-30 15:37:54 +0200108 dir("${templateEnv}/contexts") {
chnydae80bb922017-05-29 17:48:40 +0200109 contextFiles = findFiles(glob: "*.yml")
110 }
111
chnydabc63c9a2017-05-30 15:37:54 +0200112 def contextFileList = []
113 for (int i = 0; i < contextFiles.size(); i++) {
chnydabc63c9a2017-05-30 15:37:54 +0200114 contextFileList << contextFiles[i]
115 }
116
117 stage("generate-model") {
chnydabc63c9a2017-05-30 15:37:54 +0200118 for (contextFile in contextFileList) {
chnyda467f10f2017-05-30 17:25:07 +0200119 generateModel(contextFile, cutterEnv)
chnydabc63c9a2017-05-30 15:37:54 +0200120 }
chnydae80bb922017-05-29 17:48:40 +0200121 }
122
chnydaed71dcf2017-07-10 17:55:42 +0200123 dir("${env.WORKSPACE}") {
124 sh(returnStatus: true, script: "tar -zcvf model.tar.gz -C model .")
125 archiveArtifacts artifacts: "model.tar.gz"
126 }
127
chnyda30e04c42017-06-01 10:24:36 +0200128 stage("test-nodes") {
129 def partitions = common.partitionList(contextFileList, PARALLEL_NODE_GROUP_SIZE.toInteger())
130 def buildSteps = [:]
131 for (int i = 0; i < partitions.size(); i++) {
132 def partition = partitions[i]
133 buildSteps.put("partition-${i}", new HashMap<String,org.jenkinsci.plugins.workflow.cps.CpsClosure2>())
134 for(int k = 0; k < partition.size; k++){
135 def basename = sh(script: "basename ${partition[k]} .yml", returnStdout: true).trim()
136 def testEnv = "${env.WORKSPACE}/model/${basename}"
137 buildSteps.get("partition-${i}").put(basename, { testModel(basename, testEnv) })
chnydae80bb922017-05-29 17:48:40 +0200138 }
139 }
chnyda30e04c42017-06-01 10:24:36 +0200140 common.serial(buildSteps)
chnydae80bb922017-05-29 17:48:40 +0200141 }
142
chnydaed71dcf2017-07-10 17:55:42 +0200143 stage ('Clean workspace directories') {
144 sh(returnStatus: true, script: "rm -rfv * > /dev/null || true")
145 }
146
chnydae80bb922017-05-29 17:48:40 +0200147 } catch (Throwable e) {
148 currentBuild.result = "FAILURE"
149 throw e
150 } finally {
chnydae80bb922017-05-29 17:48:40 +0200151 common.sendNotification(currentBuild.result,"",["slack"])
152 }
153 }
154}