blob: 42e4cb4a4e6586489c59e1eb327345762609eecb [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) {
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}"
chnydae80bb922017-05-29 17:48:40 +020054 }
55 }
chnyda467f10f2017-05-30 17:25:07 +020056 generateSaltMaster(generatedModel, clusterDomain, clusterName)
chnydae80bb922017-05-29 17:48:40 +020057}
58
chnyda467f10f2017-05-30 17:25:07 +020059def testModel(modelFile, testEnv) {
chnydabc63c9a2017-05-30 15:37:54 +020060 def templateEnv = "${env.WORKSPACE}"
chnyda467f10f2017-05-30 17:25:07 +020061 def content = readFile(file: "${templateEnv}/contexts/${modelFile}.yml")
chnydabc63c9a2017-05-30 15:37:54 +020062 def templateContext = readYaml text: content
63 def clusterDomain = templateContext.default_context.cluster_domain
64 git.checkoutGitRepository("${testEnv}/classes/system", RECLASS_MODEL_URL, RECLASS_MODEL_BRANCH, CREDENTIALS_ID)
65 saltModelTesting.setupAndTestNode("cfg01.${clusterDomain}", "", testEnv)
66}
67
68def gerritRef
69try {
70 gerritRef = GERRIT_REFSPEC
71} catch (MissingPropertyException e) {
72 gerritRef = null
chnydae80bb922017-05-29 17:48:40 +020073}
74
75timestamps {
76 node("python&&docker") {
chnydabc63c9a2017-05-30 15:37:54 +020077 def templateEnv = "${env.WORKSPACE}"
78 def cutterEnv = "${env.WORKSPACE}/cutter"
79 def jinjaEnv = "${env.WORKSPACE}/jinja"
chnydae80bb922017-05-29 17:48:40 +020080
81 try {
chnydabc63c9a2017-05-30 15:37:54 +020082 stage("Cleanup") {
83 sh("rm -rf * || true")
84 }
85
chnydae80bb922017-05-29 17:48:40 +020086 stage ('Download Cookiecutter template') {
87 if (gerritRef) {
88 def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID)
89 merged = gerritChange.status == "MERGED"
90 if(!merged){
91 checkouted = gerrit.gerritPatchsetCheckout ([
92 credentialsId : CREDENTIALS_ID
93 ])
94 } else{
95 common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to gate them")
96 }
97 } else {
chnydabc63c9a2017-05-30 15:37:54 +020098 git.checkoutGitRepository(templateEnv, "ssh://jenkins-mk@gerrit.mcp.mirantis.net:29418/mk/cookiecutter-templates", COOKIECUTTER_TEMPLATE_BRANCH, CREDENTIALS_ID)
chnydae80bb922017-05-29 17:48:40 +020099 }
100 }
101
chnydabc63c9a2017-05-30 15:37:54 +0200102 stage("Setup") {
103 python.setupCookiecutterVirtualenv(cutterEnv)
104 }
105
chnydae80bb922017-05-29 17:48:40 +0200106 def contextFiles
chnydabc63c9a2017-05-30 15:37:54 +0200107 dir("${templateEnv}/contexts") {
chnydae80bb922017-05-29 17:48:40 +0200108 contextFiles = findFiles(glob: "*.yml")
109 }
110
chnydabc63c9a2017-05-30 15:37:54 +0200111 def contextFileList = []
112 for (int i = 0; i < contextFiles.size(); i++) {
chnydabc63c9a2017-05-30 15:37:54 +0200113 contextFileList << contextFiles[i]
114 }
115
116 stage("generate-model") {
chnydabc63c9a2017-05-30 15:37:54 +0200117 for (contextFile in contextFileList) {
chnyda467f10f2017-05-30 17:25:07 +0200118 generateModel(contextFile, cutterEnv)
chnydabc63c9a2017-05-30 15:37:54 +0200119 }
chnydae80bb922017-05-29 17:48:40 +0200120 }
121
chnyda467f10f2017-05-30 17:25:07 +0200122 sh("ls -lRa")
123
chnydae80bb922017-05-29 17:48:40 +0200124 stage("test-nodes") {
chnydabc63c9a2017-05-30 15:37:54 +0200125 def partitions = common.partitionList(contextFileList, 3)
chnydae80bb922017-05-29 17:48:40 +0200126 def buildSteps = [:]
127 for (int i = 0; i < partitions.size(); i++) {
128 def partition = partitions[i]
129 buildSteps.put("partition-${i}", new HashMap<String,org.jenkinsci.plugins.workflow.cps.CpsClosure2>())
130 for(int k = 0; k < partition.size; k++){
131 def basename = sh(script: "basename ${partition[k]} .yml", returnStdout: true).trim()
chnydabc63c9a2017-05-30 15:37:54 +0200132 def testEnv = "${env.WORKSPACE}/model/${basename}"
133 buildSteps.get("partition-${i}").put(basename, { testModel(basename, testEnv) })
chnydae80bb922017-05-29 17:48:40 +0200134 }
135 }
136 common.serial(buildSteps)
137 }
138
139 } catch (Throwable e) {
140 currentBuild.result = "FAILURE"
141 throw e
142 } finally {
143 stage ('Clean workspace directories') {
144 sh(returnStatus: true, script: "rm -rfv *")
145 }
146 common.sendNotification(currentBuild.result,"",["slack"])
147 }
148 }
149}