blob: 2a8f0ff251187bde3c3a891f7ebca2120b926d69 [file] [log] [blame]
Jakub Josef4780ae62017-04-25 16:17:56 +02001/**
2 * Generate cookiecutter cluster by individual products
3 *
4 * Expected parameters:
5 * COOKIECUTTER_TEMPLATE_CREDENTIALS Credentials to the Cookiecutter template repo.
6 * COOKIECUTTER_TEMPLATE_URL Cookiecutter template repo address.
7 * COOKIECUTTER_TEMPLATE_BRANCH Branch for the template.
8 * COOKIECUTTER_TEMPLATE_CONTEXT Context parameters for the template generation.
9 * EMAIl_ADDRESS Email to send a created tar file
10 * RECLASS_MODEL_URL Reclass model repo address
11 * RECLASS_MODEL_CREDENTIALS Credentials to the Reclass model repo.
12 * RECLASS_MODEL_BRANCH Branch for the template to push to model.
13 *
14**/
15
16common = new com.mirantis.mk.Common()
17git = new com.mirantis.mk.Git()
18python = new com.mirantis.mk.Python()
19
20timestamps {
21 node() {
22 def templateEnv = "${env.WORKSPACE}/template"
23 def modelEnv = "${env.WORKSPACE}/model"
24
25 try {
26 def templateContext = readYaml text: COOKIECUTTER_TEMPLATE_CONTEXT
27 def templateDir = "${templateEnv}/template/dir"
28 def templateOutputDir = "${env.WORKSPACE}/template"
29 def cutterEnv = "${env.WORKSPACE}/cutter"
30 def jinjaEnv = "${env.WORKSPACE}/jinja"
31 def clusterName = templateContext.default_context.cluster_name
32 def clusterDomain = templateContext.default_context.cluster_domain
33 def targetBranch = "feature/${clusterName}"
34 def outputDestination = "${modelEnv}/classes/cluster/${clusterName}"
35
36 currentBuild.description = clusterName
37 print("Using context:\n" + COOKIECUTTER_TEMPLATE_CONTEXT)
38
39 stage ('Download Cookiecutter template') {
40 git.checkoutGitRepository(templateEnv, COOKIECUTTER_TEMPLATE_URL, COOKIECUTTER_TEMPLATE_BRANCH, COOKIECUTTER_TEMPLATE_CREDENTIALS)
41 }
42
43 stage ('Download full Reclass model') {
44 if (RECLASS_MODEL_URL != '') {
45 git.checkoutGitRepository(modelEnv, RECLASS_MODEL_URL, RECLASS_MODEL_BRANCH, RECLASS_MODEL_CREDENTIALS)
46 }
47 }
48
49 stage('Generate base infrastructure') {
50 templateDir = "${templateEnv}/cluster_product/infra"
51 templateOutputDir = "${env.WORKSPACE}/template/output/infra"
52 sh "mkdir -p ${templateOutputDir}"
53 sh "mkdir -p ${outputDestination}"
54 python.setupCookiecutterVirtualenv(cutterEnv)
55 python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv)
56 sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
57 }
58
59 stage('Generate product CI/CD') {
60 if (templateContext.default_context.cicd_enabled.toBoolean()) {
61 templateDir = "${templateEnv}/cluster_product/cicd"
62 templateOutputDir = "${env.WORKSPACE}/template/output/cicd"
63 sh "mkdir -p ${templateOutputDir}"
64 python.setupCookiecutterVirtualenv(cutterEnv)
65 python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv)
66 sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
67 }
68 }
69
70 stage('Generate product OpenContrail') {
71 if (templateContext.default_context.contrail_enabled.toBoolean()) {
72 templateDir = "${templateEnv}/cluster_product/opencontrail"
73 templateOutputDir = "${env.WORKSPACE}/template/output/opencontrail"
74 sh "mkdir -p ${templateOutputDir}"
75 python.setupCookiecutterVirtualenv(cutterEnv)
76 python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv)
77 sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
78 }
79 }
80
81 stage('Generate product Kubernetes') {
82 if (templateContext.default_context.kubernetes_enabled.toBoolean()) {
83 templateDir = "${templateEnv}/cluster_product/kubernetes"
84 templateOutputDir = "${env.WORKSPACE}/template/output/kubernetes"
85 sh "mkdir -p ${templateOutputDir}"
86 python.setupCookiecutterVirtualenv(cutterEnv)
87 python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv)
88 sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
89 }
90 }
91
92 stage('Generate product OpenStack') {
93 if (templateContext.default_context.openstack_enabled.toBoolean()) {
94 templateDir = "${templateEnv}/cluster_product/openstack"
95 templateOutputDir = "${env.WORKSPACE}/template/output/openstack"
96 sh "mkdir -p ${templateOutputDir}"
97 python.setupCookiecutterVirtualenv(cutterEnv)
98 python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv)
99 sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
100 }
101 }
102
103 stage('Generate product StackLight') {
104 if (templateContext.default_context.stacklight_enabled.toBoolean()) {
105 templateDir = "${templateEnv}/cluster_product/stacklight"
106 templateOutputDir = "${env.WORKSPACE}/template/output/stacklight"
107 sh "mkdir -p ${templateOutputDir}"
108 python.setupCookiecutterVirtualenv(cutterEnv)
109 python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv)
110 sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
111 }
112 }
113
114 stage('Generate new SaltMaster node') {
115 def nodeFile = "${modelEnv}/nodes/cfg01.${clusterDomain}.yml"
116 def nodeString = """classes:
117- cluster.${clusterName}.infra.config
118parameters:
119 _param:
120 linux_system_codename: xenial
121 reclass_data_revision: master
122 linux:
123 system:
124 name: cfg01
125 domain: ${clusterDomain}
126"""
127 sh "mkdir -p ${modelEnv}/nodes/"
128 writeFile(file: nodeFile, text: nodeString)
129 }
130
131 stage ('Save changes to Reclass model') {
132 if (RECLASS_MODEL_URL != null && RECLASS_MODEL_URL != "") {
133 git.changeGitBranch(modelEnv, targetBranch)
134 git.commitGitChanges(modelEnv, "Added new cluster ${clusterName}")
135 git.pushGitChanges(modelEnv, targetBranch, 'origin', RECLASS_MODEL_CREDENTIALS)
136 }
137 sh(returnStatus: true, script: "tar -zcvf ${clusterName}.tar.gz -C ${modelEnv} .")
138 archiveArtifacts artifacts: "${clusterName}.tar.gz"
139 if(EMAIl_ADDRESS.toBoolean()){
140 //TODO: mail
Jakub Josefcd15ce72017-04-25 18:55:23 +0200141 def TODO= "todo"
Jakub Josef4780ae62017-04-25 16:17:56 +0200142 }
143 }
144
145 } catch (Throwable e) {
146 // If there was an error or exception thrown, the build failed
147 currentBuild.result = "FAILURE"
148 throw e
149 } finally {
150 stage ('Clean workspace directories') {
151 sh(returnStatus: true, script: "rm -rfv ${templateEnv}")
152 sh(returnStatus: true, script: "rm -rfv ${modelEnv}")
153 }
154 // common.sendNotification(currentBuild.result,"",["slack"])
155 }
156 }
157}