blob: 5da97f38e94470b9171b54b403ace8a3dccde4c8 [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
chnydae402e682017-05-31 13:03:44 +020074def testOption
75try {
76 testOption = TEST_OPTION
77} catch (MissingPropertyException e) {
78 testOption = "salt-model"
79}
80
chnydae80bb922017-05-29 17:48:40 +020081timestamps {
82 node("python&&docker") {
chnydabc63c9a2017-05-30 15:37:54 +020083 def templateEnv = "${env.WORKSPACE}"
84 def cutterEnv = "${env.WORKSPACE}/cutter"
85 def jinjaEnv = "${env.WORKSPACE}/jinja"
chnydae80bb922017-05-29 17:48:40 +020086
87 try {
chnydabc63c9a2017-05-30 15:37:54 +020088 stage("Cleanup") {
89 sh("rm -rf * || true")
90 }
91
chnydae80bb922017-05-29 17:48:40 +020092 stage ('Download Cookiecutter template') {
93 if (gerritRef) {
94 def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID)
95 merged = gerritChange.status == "MERGED"
96 if(!merged){
97 checkouted = gerrit.gerritPatchsetCheckout ([
98 credentialsId : CREDENTIALS_ID
99 ])
100 } else{
101 common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to gate them")
102 }
103 } else {
chnyda0381bc52017-05-31 11:19:15 +0200104 git.checkoutGitRepository(templateEnv, COOKIECUTTER_TEMPLATE_URL, COOKIECUTTER_TEMPLATE_BRANCH, CREDENTIALS_ID)
chnydae80bb922017-05-29 17:48:40 +0200105 }
106 }
107
chnydabc63c9a2017-05-30 15:37:54 +0200108 stage("Setup") {
109 python.setupCookiecutterVirtualenv(cutterEnv)
110 }
111
chnydae80bb922017-05-29 17:48:40 +0200112 def contextFiles
chnydabc63c9a2017-05-30 15:37:54 +0200113 dir("${templateEnv}/contexts") {
chnydae80bb922017-05-29 17:48:40 +0200114 contextFiles = findFiles(glob: "*.yml")
115 }
116
chnydabc63c9a2017-05-30 15:37:54 +0200117 def contextFileList = []
118 for (int i = 0; i < contextFiles.size(); i++) {
chnydabc63c9a2017-05-30 15:37:54 +0200119 contextFileList << contextFiles[i]
120 }
121
122 stage("generate-model") {
chnydabc63c9a2017-05-30 15:37:54 +0200123 for (contextFile in contextFileList) {
chnyda467f10f2017-05-30 17:25:07 +0200124 generateModel(contextFile, cutterEnv)
chnydabc63c9a2017-05-30 15:37:54 +0200125 }
chnydae80bb922017-05-29 17:48:40 +0200126 }
127
chnydae402e682017-05-31 13:03:44 +0200128 if (testOption == "salt-model") {
129 stage("test-nodes") {
130 def partitions = common.partitionList(contextFileList, PARALLEL_NODE_GROUP_SIZE.toInteger())
131 def buildSteps = [:]
132 for (int i = 0; i < partitions.size(); i++) {
133 def partition = partitions[i]
134 buildSteps.put("partition-${i}", new HashMap<String,org.jenkinsci.plugins.workflow.cps.CpsClosure2>())
135 for(int k = 0; k < partition.size; k++){
136 def basename = sh(script: "basename ${partition[k]} .yml", returnStdout: true).trim()
137 def testEnv = "${env.WORKSPACE}/model/${basename}"
138 buildSteps.get("partition-${i}").put(basename, { testModel(basename, testEnv) })
139 }
chnydae80bb922017-05-29 17:48:40 +0200140 }
chnydae402e682017-05-31 13:03:44 +0200141 common.serial(buildSteps)
chnydae80bb922017-05-29 17:48:40 +0200142 }
chnydae80bb922017-05-29 17:48:40 +0200143 }
144
145 } catch (Throwable e) {
146 currentBuild.result = "FAILURE"
147 throw e
148 } finally {
149 stage ('Clean workspace directories') {
chnyda0381bc52017-05-31 11:19:15 +0200150 sh(returnStatus: true, script: "rm -rfv * > /dev/null || true")
chnydae80bb922017-05-29 17:48:40 +0200151 }
152 common.sendNotification(currentBuild.result,"",["slack"])
153 }
154 }
155}