blob: b984d531e9e6047a9af03b59ca095462ee0aa214 [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
chnydabc63c9a2017-05-30 15:37:54 +02007def generateSaltMaster(modelEnv, clusterDomain, clusterName) {
chnydae80bb922017-05-29 17:48:40 +02008 def nodeFile = "${modelEnv}/nodes/cfg01.${clusterDomain}.yml"
9 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"""
20 sh "mkdir -p ${modelEnv}/nodes/"
21 writeFile(file: nodeFile, text: nodeString)
22}
23
chnydabc63c9a2017-05-30 15:37:54 +020024def generateModel(contextFile, cutterEnv) {
25 def templateEnv = "${env.WORKSPACE}"
26 def modelEnv = "${env.WORKSPACE}/model"
27 def basename = sh(script: "basename ${contextFile} .yml", returnStdout: true).trim()
28 def generatedModel = "${modelEnv}/${basename}"
29 def testEnv = "${env.WORKSPACE}/test"
30 def content = readFile(file: "${templateEnv}/contexts/${contextFile}")
31 def templateContext = readYaml text: content
chnydae80bb922017-05-29 17:48:40 +020032 def clusterDomain = templateContext.default_context.cluster_domain
33 def clusterName = templateContext.default_context.cluster_name
chnydabc63c9a2017-05-30 15:37:54 +020034 def outputDestination = "${generatedModel}/classes/cluster/${clusterName}"
chnydae80bb922017-05-29 17:48:40 +020035 def targetBranch = "feature/${clusterName}"
chnydabc63c9a2017-05-30 15:37:54 +020036 def templateBaseDir = "${env.WORKSPACE}"
37 def templateDir = "${templateEnv}/dir"
chnydae80bb922017-05-29 17:48:40 +020038 def templateOutputDir = templateBaseDir
chnydabc63c9a2017-05-30 15:37:54 +020039 sh "rm -rf ${generatedModel} || true"
chnydae80bb922017-05-29 17:48:40 +020040
chnydabc63c9a2017-05-30 15:37:54 +020041 stage("Generate model from ${contextFile}") {
42 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}"
54 }
chnydae80bb922017-05-29 17:48:40 +020055 }
chnydabc63c9a2017-05-30 15:37:54 +020056 generateSaltMaster(generatedModel, clusterDomain, clusterName)
chnydae80bb922017-05-29 17:48:40 +020057 }
chnydae80bb922017-05-29 17:48:40 +020058}
59
chnydabc63c9a2017-05-30 15:37:54 +020060def testModel(contextFile, testEnv) {
61 def templateEnv = "${env.WORKSPACE}"
62 def content = readFile(file: "${templateEnv}/contexts/${contextFile}.yml")
63 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)
66 saltModelTesting.setupAndTestNode("cfg01.${clusterDomain}", "", testEnv)
67}
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 {
chnydabc63c9a2017-05-30 15:37:54 +020099 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 +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++) {
114 //generateModel(contextFiles[i], cutterEnv)
115 contextFileList << contextFiles[i]
116 }
117
118 stage("generate-model") {
119 def buildSteps = [:]
120 for (contextFile in contextFileList) {
121 buildSteps[contextFile] = { generateModel(contextFile, cutterEnv) }
122 }
123 common.serial(buildSteps)
chnydae80bb922017-05-29 17:48:40 +0200124 }
125
126 stage("test-nodes") {
chnydabc63c9a2017-05-30 15:37:54 +0200127 def partitions = common.partitionList(contextFileList, 3)
chnydae80bb922017-05-29 17:48:40 +0200128 def buildSteps = [:]
129 for (int i = 0; i < partitions.size(); i++) {
130 def partition = partitions[i]
131 buildSteps.put("partition-${i}", new HashMap<String,org.jenkinsci.plugins.workflow.cps.CpsClosure2>())
132 for(int k = 0; k < partition.size; k++){
133 def basename = sh(script: "basename ${partition[k]} .yml", returnStdout: true).trim()
chnydabc63c9a2017-05-30 15:37:54 +0200134 def testEnv = "${env.WORKSPACE}/model/${basename}"
135 buildSteps.get("partition-${i}").put(basename, { testModel(basename, testEnv) })
chnydae80bb922017-05-29 17:48:40 +0200136 }
137 }
138 common.serial(buildSteps)
139 }
140
141 } catch (Throwable e) {
142 currentBuild.result = "FAILURE"
143 throw e
144 } finally {
145 stage ('Clean workspace directories') {
146 sh(returnStatus: true, script: "rm -rfv *")
147 }
148 common.sendNotification(currentBuild.result,"",["slack"])
149 }
150 }
151}