blob: 12dc88d191e8afb608f970e9d9ea8f0208460a99 [file] [log] [blame]
Tomáš Kukrál7ded3642017-03-27 15:52:51 +02001/**
2 * Generate cookiecutter cluster by individual products
3 *
4 * Expected parameters:
Tomáš Kukrál7ded3642017-03-27 15:52:51 +02005 * COOKIECUTTER_TEMPLATE_CONTEXT Context parameters for the template generation.
Tomáš Kukrál91e49252017-05-09 14:40:26 +02006 * EMAIL_ADDRESS Email to send a created tar file
Tomáš Kukrál7ded3642017-03-27 15:52:51 +02007 *
azvyagintsev3ed704f2018-07-09 15:49:27 +03008 **/
Tomáš Kukrál7ded3642017-03-27 15:52:51 +02009
10common = new com.mirantis.mk.Common()
11git = new com.mirantis.mk.Git()
12python = new com.mirantis.mk.Python()
chnyda89191012017-05-29 15:38:35 +020013saltModelTesting = new com.mirantis.mk.SaltModelTesting()
Tomáš Kukrálcebc1a02017-07-25 16:10:37 +020014ssh = new com.mirantis.mk.Ssh()
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020015
Vasyl Saienko682043d2018-07-23 16:04:10 +030016def reclassVersion = 'v1.5.4'
Vasyl Saienko772e1232018-07-23 14:42:24 +030017if (common.validInputParam('RECLASS_VERSION')) {
18 reclassVersion = RECLASS_VERSION
19}
20
Jakub Josefa63f9862018-01-11 17:58:38 +010021timeout(time: 12, unit: 'HOURS') {
azvyagintsev3ed704f2018-07-09 15:49:27 +030022 node("python&&docker") {
23 def templateEnv = "${env.WORKSPACE}/template"
24 def modelEnv = "${env.WORKSPACE}/model"
25 def testEnv = "${env.WORKSPACE}/test"
26 def pipelineEnv = "${env.WORKSPACE}/pipelines"
Tomáš Kukrál9f6260f2017-03-29 23:58:26 +020027
azvyagintsev3ed704f2018-07-09 15:49:27 +030028 try {
29 def templateContext = readYaml text: COOKIECUTTER_TEMPLATE_CONTEXT
30 def mcpVersion = templateContext.default_context.mcp_version
31 def sharedReclassUrl = templateContext.default_context.shared_reclass_url
32 def clusterDomain = templateContext.default_context.cluster_domain
33 def clusterName = templateContext.default_context.cluster_name
34 def saltMaster = templateContext.default_context.salt_master_hostname
35 def localRepositories = templateContext.default_context.local_repositories.toBoolean()
36 def offlineDeployment = templateContext.default_context.offline_deployment.toBoolean()
37 def cutterEnv = "${env.WORKSPACE}/cutter"
38 def jinjaEnv = "${env.WORKSPACE}/jinja"
39 def outputDestination = "${modelEnv}/classes/cluster/${clusterName}"
40 def systemEnv = "${modelEnv}/classes/system"
41 def targetBranch = "feature/${clusterName}"
42 def templateBaseDir = "${env.WORKSPACE}/template"
43 def templateDir = "${templateEnv}/template/dir"
44 def templateOutputDir = templateBaseDir
45 def user
46 wrap([$class: 'BuildUser']) {
47 user = env.BUILD_USER_ID
48 }
49
50 currentBuild.description = clusterName
51 print("Using context:\n" + COOKIECUTTER_TEMPLATE_CONTEXT)
52
53 stage ('Download Cookiecutter template') {
54 def cookiecutterTemplateUrl = templateContext.default_context.cookiecutter_template_url
55 def cookiecutterTemplateBranch = templateContext.default_context.cookiecutter_template_branch
56 git.checkoutGitRepository(templateEnv, cookiecutterTemplateUrl, 'master')
57 // Use refspec if exists first of all
58 if (cookiecutterTemplateBranch.toString().startsWith('refs/')) {
59 dir(templateEnv) {
60 ssh.agentSh("git fetch ${cookiecutterTemplateUrl} ${cookiecutterTemplateBranch} && git checkout FETCH_HEAD")
61 }
62 } else {
63 // Use mcpVersion git tag if not specified branch for cookiecutter-templates
64 if (cookiecutterTemplateBranch == '') {
65 cookiecutterTemplateBranch = mcpVersion
66 // Don't have nightly/testing/stable for cookiecutter-templates repo, therefore use master
67 if(mcpVersion == "nightly" || mcpVersion == "testing" || mcpVersion == "stable"){
68 cookiecutterTemplateBranch = 'master'
Tomáš Kukrála3f4ba72017-08-03 15:40:22 +020069 }
azvyagintsev3ed704f2018-07-09 15:49:27 +030070 }
71 git.changeGitBranch(templateEnv, cookiecutterTemplateBranch)
72 }
73 }
Tomáš Kukrála3f4ba72017-08-03 15:40:22 +020074
azvyagintsev3ed704f2018-07-09 15:49:27 +030075 stage ('Create empty reclass model') {
76 dir(path: modelEnv) {
77 sh "rm -rfv .git"
78 sh "git init"
79 ssh.agentSh("git submodule add ${sharedReclassUrl} 'classes/system'")
80 }
Tomáš Kukrál0c6835d2017-07-18 16:00:27 +020081
azvyagintsev3ed704f2018-07-09 15:49:27 +030082 def sharedReclassBranch = templateContext.default_context.shared_reclass_branch
83 // Use refspec if exists first of all
84 if (sharedReclassBranch.toString().startsWith('refs/')) {
85 dir(systemEnv) {
86 ssh.agentSh("git fetch ${sharedReclassUrl} ${sharedReclassBranch} && git checkout FETCH_HEAD")
87 }
88 } else {
89 // Use mcpVersion git tag if not specified branch for reclass-system
90 if (sharedReclassBranch == '') {
91 sharedReclassBranch = mcpVersion
92 // Don't have nightly/testing/stable for reclass-system repo, therefore use master
93 if(mcpVersion == "nightly" || mcpVersion == "testing" || mcpVersion == "stable"){
94 sharedReclassBranch = 'master'
Jakub Josefa63f9862018-01-11 17:58:38 +010095 }
azvyagintsev3ed704f2018-07-09 15:49:27 +030096 }
97 git.changeGitBranch(systemEnv, sharedReclassBranch)
98 }
99 git.commitGitChanges(modelEnv, "Added new shared reclass submodule", "${user}@localhost", "${user}")
100 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100101
azvyagintsev3ed704f2018-07-09 15:49:27 +0300102 def productList = ["infra", "cicd", "opencontrail", "kubernetes", "openstack", "oss", "stacklight", "ceph"]
103 for (product in productList) {
Richard Felkle77b0912018-01-31 17:12:23 +0100104
azvyagintsev3ed704f2018-07-09 15:49:27 +0300105 // get templateOutputDir and productDir
106 if (product.startsWith("stacklight")) {
107 templateOutputDir = "${env.WORKSPACE}/output/stacklight"
Jakub Josefa63f9862018-01-11 17:58:38 +0100108
azvyagintsev3ed704f2018-07-09 15:49:27 +0300109 def stacklightVersion
110 try {
111 stacklightVersion = templateContext.default_context['stacklight_version']
112 } catch (Throwable e) {
113 common.warningMsg('Stacklight version loading failed')
114 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100115
azvyagintsev3ed704f2018-07-09 15:49:27 +0300116 if (stacklightVersion) {
117 productDir = "stacklight" + stacklightVersion
118 } else {
119 productDir = "stacklight1"
120 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100121
azvyagintsev3ed704f2018-07-09 15:49:27 +0300122 } else {
123 templateOutputDir = "${env.WORKSPACE}/output/${product}"
124 productDir = product
125 }
Richard Felkl9543faa2018-01-11 09:47:35 +0100126
azvyagintsev3ed704f2018-07-09 15:49:27 +0300127 if (product == "infra" || (templateContext.default_context["${product}_enabled"]
128 && templateContext.default_context["${product}_enabled"].toBoolean())) {
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200129
azvyagintsev3ed704f2018-07-09 15:49:27 +0300130 templateDir = "${templateEnv}/cluster_product/${productDir}"
131 common.infoMsg("Generating product " + product + " from " + templateDir + " to " + templateOutputDir)
Tomáš Kukrála3f4ba72017-08-03 15:40:22 +0200132
azvyagintsev3ed704f2018-07-09 15:49:27 +0300133 sh "rm -rf ${templateOutputDir} || true"
134 sh "mkdir -p ${templateOutputDir}"
135 sh "mkdir -p ${outputDestination}"
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200136
azvyagintsev3ed704f2018-07-09 15:49:27 +0300137 python.setupCookiecutterVirtualenv(cutterEnv)
138 python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv, templateBaseDir)
139 sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
140 } else {
141 common.warningMsg("Product " + product + " is disabled")
142 }
143 }
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400144
azvyagintsev3ed704f2018-07-09 15:49:27 +0300145 if(localRepositories && !offlineDeployment){
146 def aptlyModelUrl = templateContext.default_context.local_model_url
147 dir(path: modelEnv) {
148 ssh.agentSh "git submodule add \"${aptlyModelUrl}\" \"classes/cluster/${clusterName}/cicd/aptly\""
149 if(!(mcpVersion in ["nightly", "testing", "stable"])){
150 ssh.agentSh "cd \"classes/cluster/${clusterName}/cicd/aptly\";git fetch --tags;git checkout ${mcpVersion}"
151 }
152 }
153 }
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400154
azvyagintsev3ed704f2018-07-09 15:49:27 +0300155 stage('Generate new SaltMaster node') {
156 def nodeFile = "${modelEnv}/nodes/${saltMaster}.${clusterDomain}.yml"
157 def nodeString = """classes:
Leontii Istomin56b6b112018-01-15 12:14:24 +0300158- cluster.${clusterName}.infra.config
159parameters:
160 _param:
161 linux_system_codename: xenial
162 reclass_data_revision: master
163 linux:
164 system:
165 name: ${saltMaster}
166 domain: ${clusterDomain}
Jakub Josefa63f9862018-01-11 17:58:38 +0100167 """
azvyagintsev3ed704f2018-07-09 15:49:27 +0300168 sh "mkdir -p ${modelEnv}/nodes/"
169 writeFile(file: nodeFile, text: nodeString)
Jakub Josefa63f9862018-01-11 17:58:38 +0100170
azvyagintsev3ed704f2018-07-09 15:49:27 +0300171 git.commitGitChanges(modelEnv, "Create model ${clusterName}", "${user}@localhost", "${user}")
172 }
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400173
azvyagintsev3ed704f2018-07-09 15:49:27 +0300174 stage("Test") {
175 if (TEST_MODEL.toBoolean() && sharedReclassUrl != '') {
176 def testResult = false
177 sh("cp -r ${modelEnv} ${testEnv}")
178 def DockerCName = "${env.JOB_NAME.toLowerCase()}_${env.BUILD_TAG.toLowerCase()}"
179 testResult = saltModelTesting.setupAndTestNode(
180 "${saltMaster}.${clusterDomain}",
181 "",
182 "",
183 testEnv,
184 'pkg',
185 'stable',
Vasyl Saienko772e1232018-07-23 14:42:24 +0300186 reclassVersion,
azvyagintsev3ed704f2018-07-09 15:49:27 +0300187 0,
188 false,
189 false,
190 '',
191 '',
192 DockerCName)
193 if (testResult) {
194 common.infoMsg("Test finished: SUCCESS")
195 } else {
196 common.infoMsg('Test finished: FAILURE')
197 throw new RuntimeException('Test stage finished: FAILURE')
azvyagintsev7c4a3cf2018-07-08 08:43:33 +0300198 }
azvyagintsev3ed704f2018-07-09 15:49:27 +0300199 } else {
200 common.warningMsg("Test stage has been skipped!")
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400201 }
azvyagintsev3ed704f2018-07-09 15:49:27 +0300202 }
203 stage("Generate config drives") {
204 // apt package genisoimage is required for this stage
205
206 // download create-config-drive
207 // FIXME: that should be refactored, to use git clone - to be able download it from custom repo.
208 def mcpCommonScriptsBranch = templateContext.default_context.mcp_common_scripts_branch
209 if (mcpCommonScriptsBranch == '') {
210 mcpCommonScriptsBranch = mcpVersion
211 // Don't have nightly for mcp-common-scripts repo, therefore use master
212 if(mcpVersion == "nightly"){
213 mcpCommonScriptsBranch = 'master'
214 }
215 }
216 def config_drive_script_url = "https://raw.githubusercontent.com/Mirantis/mcp-common-scripts/${mcpCommonScriptsBranch}/config-drive/create_config_drive.sh"
217 def user_data_script_url = "https://raw.githubusercontent.com/Mirantis/mcp-common-scripts/${mcpCommonScriptsBranch}/config-drive/master_config.sh"
218
219 sh "wget -O create-config-drive ${config_drive_script_url} && chmod +x create-config-drive"
220 sh "wget -O user_data.sh ${user_data_script_url}"
221
222 sh "git clone --mirror https://github.com/Mirantis/mk-pipelines.git ${pipelineEnv}/mk-pipelines"
223 sh "git clone --mirror https://github.com/Mirantis/pipeline-library.git ${pipelineEnv}/pipeline-library"
224 args = "--user-data user_data.sh --hostname ${saltMaster} --model ${modelEnv} --mk-pipelines ${pipelineEnv}/mk-pipelines/ --pipeline-library ${pipelineEnv}/pipeline-library/ ${saltMaster}.${clusterDomain}-config.iso"
225
226 // load data from model
227 def smc = [:]
228 smc['SALT_MASTER_MINION_ID'] = "${saltMaster}.${clusterDomain}"
229 smc['SALT_MASTER_DEPLOY_IP'] = templateContext['default_context']['salt_master_management_address']
230 smc['DEPLOY_NETWORK_GW'] = templateContext['default_context']['deploy_network_gateway']
231 smc['DEPLOY_NETWORK_NETMASK'] = templateContext['default_context']['deploy_network_netmask']
232 smc['DNS_SERVERS'] = templateContext['default_context']['dns_server01']
233 smc['MCP_VERSION'] = "${mcpVersion}"
234 if (templateContext['default_context']['local_repositories'] == 'True'){
235 def localRepoIP = templateContext['default_context']['local_repo_url']
236 smc['MCP_SALT_REPO_KEY'] = "http://${localRepoIP}/public.gpg"
237 smc['MCP_SALT_REPO_URL'] = "http://${localRepoIP}/ubuntu-xenial"
238 smc['PIPELINES_FROM_ISO'] = 'false'
239 smc['PIPELINE_REPO_URL'] = "http://${localRepoIP}:8088"
240 smc['LOCAL_REPOS'] = 'true'
241 }
242 if (templateContext['default_context']['upstream_proxy_enabled'] == 'True'){
243 if (templateContext['default_context']['upstream_proxy_auth_enabled'] == 'True'){
244 smc['http_proxy'] = 'http://' + templateContext['default_context']['upstream_proxy_user'] + ':' + templateContext['default_context']['upstream_proxy_password'] + '@' + templateContext['default_context']['upstream_proxy_address'] + ':' + templateContext['default_context']['upstream_proxy_port']
245 smc['https_proxy'] = 'http://' + templateContext['default_context']['upstream_proxy_user'] + ':' + templateContext['default_context']['upstream_proxy_password'] + '@' + templateContext['default_context']['upstream_proxy_address'] + ':' + templateContext['default_context']['upstream_proxy_port']
246 } else {
247 smc['http_proxy'] = 'http://' + templateContext['default_context']['upstream_proxy_address'] + ':' + templateContext['default_context']['upstream_proxy_port']
248 smc['https_proxy'] = 'http://' + templateContext['default_context']['upstream_proxy_address'] + ':' + templateContext['default_context']['upstream_proxy_port']
249 }
250 }
251
252 for (i in common.entries(smc)) {
253 sh "sed -i 's,export ${i[0]}=.*,export ${i[0]}=${i[1]},' user_data.sh"
254 }
255
256 // create cfg config-drive
257 sh "./create-config-drive ${args}"
258 sh("mkdir output-${clusterName} && mv ${saltMaster}.${clusterDomain}-config.iso output-${clusterName}/")
259
260 // save cfg iso to artifacts
261 archiveArtifacts artifacts: "output-${clusterName}/${saltMaster}.${clusterDomain}-config.iso"
262
263 if (templateContext['default_context']['local_repositories'] == 'True'){
264 def aptlyServerHostname = templateContext.default_context.aptly_server_hostname
265 def user_data_script_apt_url = "https://raw.githubusercontent.com/Mirantis/mcp-common-scripts/master/config-drive/mirror_config.sh"
266 sh "wget -O mirror_config.sh ${user_data_script_apt_url}"
267
268 def smc_apt = [:]
269 smc_apt['SALT_MASTER_DEPLOY_IP'] = templateContext['default_context']['salt_master_management_address']
270 smc_apt['APTLY_DEPLOY_IP'] = templateContext['default_context']['aptly_server_deploy_address']
271 smc_apt['APTLY_DEPLOY_NETMASK'] = templateContext['default_context']['deploy_network_netmask']
272 smc_apt['APTLY_MINION_ID'] = "${aptlyServerHostname}.${clusterDomain}"
273
274 for (i in common.entries(smc_apt)) {
275 sh "sed -i \"s,export ${i[0]}=.*,export ${i[0]}=${i[1]},\" mirror_config.sh"
276 }
277
278 // create apt config-drive
279 sh "./create-config-drive --user-data mirror_config.sh --hostname ${aptlyServerHostname} ${aptlyServerHostname}.${clusterDomain}-config.iso"
280 sh("mv ${aptlyServerHostname}.${clusterDomain}-config.iso output-${clusterName}/")
281
282 // save apt iso to artifacts
283 archiveArtifacts artifacts: "output-${clusterName}/${aptlyServerHostname}.${clusterDomain}-config.iso"
284 }
285 }
286
287 stage ('Save changes reclass model') {
288 sh(returnStatus: true, script: "tar -zcf output-${clusterName}/${clusterName}.tar.gz -C ${modelEnv} .")
289 archiveArtifacts artifacts: "output-${clusterName}/${clusterName}.tar.gz"
290
291
292 if (EMAIL_ADDRESS != null && EMAIL_ADDRESS != "") {
293 emailext(to: EMAIL_ADDRESS,
294 attachmentsPattern: "output-${clusterName}/*",
295 body: "Mirantis Jenkins\n\nRequested reclass model ${clusterName} has been created and attached to this email.\nEnjoy!\n\nMirantis",
296 subject: "Your Salt model ${clusterName}")
297 }
298 dir("output-${clusterName}"){
299 deleteDir()
300 }
301 }
302
303 } catch (Throwable e) {
304 // If there was an error or exception thrown, the build failed
305 currentBuild.result = "FAILURE"
306 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
307 throw e
308 } finally {
309 stage ('Clean workspace directories') {
310 sh(returnStatus: true, script: "rm -rf ${templateEnv}")
311 sh(returnStatus: true, script: "rm -rf ${modelEnv}")
312 sh(returnStatus: true, script: "rm -rf ${pipelineEnv}")
313 }
314 // common.sendNotification(currentBuild.result,"",["slack"])
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200315 }
azvyagintsev3ed704f2018-07-09 15:49:27 +0300316 }
Mikhail Ivanov9f812922017-11-07 18:52:02 +0400317}