blob: 0a2888e5cce9d4bda0941859c6f984297cc9aef5 [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 *
8**/
9
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
Jakub Josefa63f9862018-01-11 17:58:38 +010016timeout(time: 12, unit: 'HOURS') {
17 node("python&&docker") {
18 def templateEnv = "${env.WORKSPACE}/template"
19 def modelEnv = "${env.WORKSPACE}/model"
20 def testEnv = "${env.WORKSPACE}/test"
21 def pipelineEnv = "${env.WORKSPACE}/pipelines"
Tomáš Kukrál9f6260f2017-03-29 23:58:26 +020022
Jakub Josefa63f9862018-01-11 17:58:38 +010023 try {
24 def templateContext = readYaml text: COOKIECUTTER_TEMPLATE_CONTEXT
Leontii Istomin9adf9182018-02-23 13:16:20 +010025 def mcpVersion = templateContext.default_context.mcp_version
26 def sharedReclassUrl = templateContext.default_context.shared_reclass_url
Jakub Josefa63f9862018-01-11 17:58:38 +010027 def clusterDomain = templateContext.default_context.cluster_domain
28 def clusterName = templateContext.default_context.cluster_name
29 def saltMaster = templateContext.default_context.salt_master_hostname
Richard Felkle77b0912018-01-31 17:12:23 +010030 def localRepositories = templateContext.default_context.local_repositories.toBoolean()
31 def offlineDeployment = templateContext.default_context.offline_deployment.toBoolean()
Jakub Josefa63f9862018-01-11 17:58:38 +010032 def cutterEnv = "${env.WORKSPACE}/cutter"
33 def jinjaEnv = "${env.WORKSPACE}/jinja"
34 def outputDestination = "${modelEnv}/classes/cluster/${clusterName}"
Leontii Istomin9adf9182018-02-23 13:16:20 +010035 def systemEnv = "${modelEnv}/classes/system"
Jakub Josefa63f9862018-01-11 17:58:38 +010036 def targetBranch = "feature/${clusterName}"
37 def templateBaseDir = "${env.WORKSPACE}/template"
38 def templateDir = "${templateEnv}/template/dir"
39 def templateOutputDir = templateBaseDir
40 def user
41 wrap([$class: 'BuildUser']) {
42 user = env.BUILD_USER_ID
Tomáš Kukrála3f4ba72017-08-03 15:40:22 +020043 }
44
Jakub Josefa63f9862018-01-11 17:58:38 +010045 currentBuild.description = clusterName
46 print("Using context:\n" + COOKIECUTTER_TEMPLATE_CONTEXT)
Tomáš Kukrál0c6835d2017-07-18 16:00:27 +020047
Jakub Josefa63f9862018-01-11 17:58:38 +010048 stage ('Download Cookiecutter template') {
Leontii Istomin9adf9182018-02-23 13:16:20 +010049 def cookiecutterTemplateUrl = templateContext.default_context.cookiecutter_template_url
50 def cookiecutterTemplateBranch = templateContext.default_context.cookiecutter_template_branch
51 git.checkoutGitRepository(templateEnv, cookiecutterTemplateUrl, 'master')
52 // Use refspec if exists first of all
53 if (cookiecutterTemplateBranch.toString().startsWith('refs/')) {
Jakub Josefa63f9862018-01-11 17:58:38 +010054 dir(templateEnv) {
Leontii Istomin9adf9182018-02-23 13:16:20 +010055 ssh.agentSh("git fetch ${cookiecutterTemplateUrl} ${cookiecutterTemplateBranch} && git checkout FETCH_HEAD")
Jakub Josefa63f9862018-01-11 17:58:38 +010056 }
57 } else {
Leontii Istomin9adf9182018-02-23 13:16:20 +010058 // Use mcpVersion git tag if not specified branch for cookiecutter-templates
59 if (cookiecutterTemplateBranch == '') {
60 cookiecutterTemplateBranch = mcpVersion
61 // Don't have nightly/testing/stable for cookiecutter-templates repo, therefore use master
62 if(mcpVersion == "nightly" || mcpVersion == "testing" || mcpVersion == "stable"){
63 cookiecutterTemplateBranch = 'master'
64 }
65 }
66 git.changeGitBranch(templateEnv, cookiecutterTemplateBranch)
Jakub Josefa63f9862018-01-11 17:58:38 +010067 }
Jakub Josefa63f9862018-01-11 17:58:38 +010068 }
69
70 stage ('Create empty reclass model') {
71 dir(path: modelEnv) {
72 sh "rm -rfv .git"
73 sh "git init"
Leontii Istomin4bf8bc72018-03-02 10:36:39 +010074 ssh.agentSh("git submodule add ${sharedReclassUrl} 'classes/system'")
Jakub Josefa63f9862018-01-11 17:58:38 +010075 }
Richard Felkle77b0912018-01-31 17:12:23 +010076
Leontii Istomin9adf9182018-02-23 13:16:20 +010077 def sharedReclassBranch = templateContext.default_context.shared_reclass_branch
78 // Use refspec if exists first of all
79 if (sharedReclassBranch.toString().startsWith('refs/')) {
80 dir(systemEnv) {
81 ssh.agentSh("git fetch ${sharedReclassUrl} ${sharedReclassBranch} && git checkout FETCH_HEAD")
82 }
83 } else {
84 // Use mcpVersion git tag if not specified branch for reclass-system
85 if (sharedReclassBranch == '') {
86 sharedReclassBranch = mcpVersion
87 // Don't have nightly/testing/stable for reclass-system repo, therefore use master
88 if(mcpVersion == "nightly" || mcpVersion == "testing" || mcpVersion == "stable"){
89 sharedReclassBranch = 'master'
90 }
91 }
92 git.changeGitBranch(systemEnv, sharedReclassBranch)
93 }
94 git.commitGitChanges(modelEnv, "Added new shared reclass submodule", "${user}@localhost", "${user}")
Jakub Josefa63f9862018-01-11 17:58:38 +010095 }
96
97 def productList = ["infra", "cicd", "opencontrail", "kubernetes", "openstack", "oss", "stacklight", "ceph"]
98 for (product in productList) {
99
100 // get templateOutputDir and productDir
101 if (product.startsWith("stacklight")) {
102 templateOutputDir = "${env.WORKSPACE}/output/stacklight"
103
104 def stacklightVersion
105 try {
106 stacklightVersion = templateContext.default_context['stacklight_version']
107 } catch (Throwable e) {
108 common.warningMsg('Stacklight version loading failed')
Richard Felkl9543faa2018-01-11 09:47:35 +0100109 }
110
Jakub Josefa63f9862018-01-11 17:58:38 +0100111 if (stacklightVersion) {
112 productDir = "stacklight" + stacklightVersion
113 } else {
114 productDir = "stacklight1"
115 }
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200116
Tomáš Kukrála3f4ba72017-08-03 15:40:22 +0200117 } else {
Jakub Josefa63f9862018-01-11 17:58:38 +0100118 templateOutputDir = "${env.WORKSPACE}/output/${product}"
119 productDir = product
Tomáš Kukrál9a6821e2017-07-24 11:07:01 +0200120 }
Tomáš Kukrála3f4ba72017-08-03 15:40:22 +0200121
Jakub Josefa63f9862018-01-11 17:58:38 +0100122 if (product == "infra" || (templateContext.default_context["${product}_enabled"]
123 && templateContext.default_context["${product}_enabled"].toBoolean())) {
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200124
Jakub Josefa63f9862018-01-11 17:58:38 +0100125 templateDir = "${templateEnv}/cluster_product/${productDir}"
126 common.infoMsg("Generating product " + product + " from " + templateDir + " to " + templateOutputDir)
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400127
Jakub Josefa63f9862018-01-11 17:58:38 +0100128 sh "rm -rf ${templateOutputDir} || true"
129 sh "mkdir -p ${templateOutputDir}"
130 sh "mkdir -p ${outputDestination}"
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400131
Jakub Josefa63f9862018-01-11 17:58:38 +0100132 python.setupCookiecutterVirtualenv(cutterEnv)
133 python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv, templateBaseDir)
134 sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
Jiri Broulikd1375f72018-01-09 10:14:59 +0100135 } else {
Jakub Josefa63f9862018-01-11 17:58:38 +0100136 common.warningMsg("Product " + product + " is disabled")
Jiri Broulikd1375f72018-01-09 10:14:59 +0100137 }
138 }
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400139
Richard Felkle77b0912018-01-31 17:12:23 +0100140 if(localRepositories && !offlineDeployment){
141 def aptlyModelUrl = templateContext.default_context.local_model_url
142 dir(path: modelEnv) {
143 ssh.agentSh "git submodule add \"${aptlyModelUrl}\" \"classes/cluster/${clusterName}/cicd/aptly\""
144 if(!(mcpVersion in ["nightly", "testing", "stable"])){
145 ssh.agentSh "cd \"classes/cluster/${clusterName}/cicd/aptly\";git fetch --tags;git checkout ${mcpVersion}"
146 }
147 }
148 }
149
Jakub Josefa63f9862018-01-11 17:58:38 +0100150 stage('Generate new SaltMaster node') {
151 def nodeFile = "${modelEnv}/nodes/${saltMaster}.${clusterDomain}.yml"
152 def nodeString = """classes:
Leontii Istomin56b6b112018-01-15 12:14:24 +0300153- cluster.${clusterName}.infra.config
154parameters:
155 _param:
156 linux_system_codename: xenial
157 reclass_data_revision: master
158 linux:
159 system:
160 name: ${saltMaster}
161 domain: ${clusterDomain}
Jakub Josefa63f9862018-01-11 17:58:38 +0100162 """
163 sh "mkdir -p ${modelEnv}/nodes/"
164 writeFile(file: nodeFile, text: nodeString)
165
166 git.commitGitChanges(modelEnv, "Create model ${clusterName}", "${user}@localhost", "${user}")
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400167 }
168
Jakub Josefa63f9862018-01-11 17:58:38 +0100169 stage("Test") {
Leontii Istomin9adf9182018-02-23 13:16:20 +0100170 if (sharedReclassUrl != "" && TEST_MODEL && TEST_MODEL.toBoolean()) {
Jakub Josefa63f9862018-01-11 17:58:38 +0100171 sh("cp -r ${modelEnv} ${testEnv}")
172 saltModelTesting.setupAndTestNode("${saltMaster}.${clusterDomain}", "", testEnv)
173 }
174 }
Richard Felkl9fd47942017-10-20 16:23:29 +0200175
Jakub Josefa63f9862018-01-11 17:58:38 +0100176 stage("Generate config drives") {
177 // apt package genisoimage is required for this stage
Richard Felkl9fd47942017-10-20 16:23:29 +0200178
Jakub Josefa63f9862018-01-11 17:58:38 +0100179 // download create-config-drive
180 // FIXME: that should be refactored, to use git clone - to be able download it from custom repo.
Leontii Istomin6a2d3692018-04-11 12:15:36 +0200181 def mcpCommonScriptsBranch = templateContext.default_context.mcp_common_scripts_branch
182 if (mcpCommonScriptsBranch == '') {
183 mcpCommonScriptsBranch = mcpVersion
184 // Don't have nightly for mcp-common-scripts repo, therefore use master
185 if(mcpVersion == "nightly"){
186 mcpCommonScriptsBranch = 'master'
187 }
188 }
189 def config_drive_script_url = "https://raw.githubusercontent.com/Mirantis/mcp-common-scripts/${mcpCommonScriptsBranch}/config-drive/create_config_drive.sh"
190 def user_data_script_url = "https://raw.githubusercontent.com/Mirantis/mcp-common-scripts/${mcpCommonScriptsBranch}/config-drive/master_config.sh"
Richard Felkl9fd47942017-10-20 16:23:29 +0200191
Jakub Josefa63f9862018-01-11 17:58:38 +0100192 sh "wget -O create-config-drive ${config_drive_script_url} && chmod +x create-config-drive"
193 sh "wget -O user_data.sh ${user_data_script_url}"
Richard Felkl9fd47942017-10-20 16:23:29 +0200194
Jakub Josefa63f9862018-01-11 17:58:38 +0100195 sh "git clone --mirror https://github.com/Mirantis/mk-pipelines.git ${pipelineEnv}/mk-pipelines"
196 sh "git clone --mirror https://github.com/Mirantis/pipeline-library.git ${pipelineEnv}/pipeline-library"
197 args = "--user-data user_data.sh --hostname ${saltMaster} --model ${modelEnv} --mk-pipelines ${pipelineEnv}/mk-pipelines/ --pipeline-library ${pipelineEnv}/pipeline-library/ ${saltMaster}.${clusterDomain}-config.iso"
198
199 // load data from model
200 def smc = [:]
201 smc['SALT_MASTER_MINION_ID'] = "${saltMaster}.${clusterDomain}"
202 smc['SALT_MASTER_DEPLOY_IP'] = templateContext['default_context']['salt_master_management_address']
203 smc['DEPLOY_NETWORK_GW'] = templateContext['default_context']['deploy_network_gateway']
204 smc['DEPLOY_NETWORK_NETMASK'] = templateContext['default_context']['deploy_network_netmask']
205 smc['DNS_SERVERS'] = templateContext['default_context']['dns_server01']
Richard Felkl2d88c782018-04-09 14:54:05 +0200206 smc['MCP_VERSION'] = "${mcpVersion}"
Jakub Josefa63f9862018-01-11 17:58:38 +0100207 if (templateContext['default_context']['local_repositories'] == 'True'){
Richard Felkl2d88c782018-04-09 14:54:05 +0200208 def localRepoIP = templateContext['default_context']['local_repo_url']
209 smc['MCP_SALT_REPO_KEY'] = "http://${localRepoIP}/public.gpg"
210 smc['MCP_SALT_REPO_URL'] = "http://${localRepoIP}/ubuntu-xenial"
Jakub Josefa63f9862018-01-11 17:58:38 +0100211 smc['PIPELINES_FROM_ISO'] = 'false'
Richard Felkl2d88c782018-04-09 14:54:05 +0200212 smc['PIPELINE_REPO_URL'] = "http://${localRepoIP}:8088"
Jiri Broulik2211c882018-04-12 10:23:09 +0200213 smc['LOCAL_REPOS'] = 'true'
Jakub Josefa63f9862018-01-11 17:58:38 +0100214 }
215 if (templateContext['default_context']['upstream_proxy_enabled'] == 'True'){
216 if (templateContext['default_context']['upstream_proxy_auth_enabled'] == 'True'){
217 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']
218 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']
219 } else {
220 smc['http_proxy'] = 'http://' + templateContext['default_context']['upstream_proxy_address'] + ':' + templateContext['default_context']['upstream_proxy_port']
221 smc['https_proxy'] = 'http://' + templateContext['default_context']['upstream_proxy_address'] + ':' + templateContext['default_context']['upstream_proxy_port']
222 }
Richard Felkl9fd47942017-10-20 16:23:29 +0200223 }
224
Jakub Josefa63f9862018-01-11 17:58:38 +0100225 for (i in common.entries(smc)) {
Adam Tenglerdac9c462018-05-24 16:08:12 +0200226 sh "sed -i 's,export ${i[0]}=.*,export ${i[0]}=${i[1]},' user_data.sh"
Jakub Josefa63f9862018-01-11 17:58:38 +0100227 }
Richard Felkl9fd47942017-10-20 16:23:29 +0200228
Jakub Josefa63f9862018-01-11 17:58:38 +0100229 // create cfg config-drive
230 sh "./create-config-drive ${args}"
231 sh("mkdir output-${clusterName} && mv ${saltMaster}.${clusterDomain}-config.iso output-${clusterName}/")
232
233 // save cfg iso to artifacts
234 archiveArtifacts artifacts: "output-${clusterName}/${saltMaster}.${clusterDomain}-config.iso"
235
236 if (templateContext['default_context']['local_repositories'] == 'True'){
237 def aptlyServerHostname = templateContext.default_context.aptly_server_hostname
238 def user_data_script_apt_url = "https://raw.githubusercontent.com/Mirantis/mcp-common-scripts/master/config-drive/mirror_config.sh"
239 sh "wget -O mirror_config.sh ${user_data_script_apt_url}"
240
241 def smc_apt = [:]
242 smc_apt['SALT_MASTER_DEPLOY_IP'] = templateContext['default_context']['salt_master_management_address']
243 smc_apt['APTLY_DEPLOY_IP'] = templateContext['default_context']['aptly_server_deploy_address']
244 smc_apt['APTLY_DEPLOY_NETMASK'] = templateContext['default_context']['deploy_network_netmask']
245 smc_apt['APTLY_MINION_ID'] = "${aptlyServerHostname}.${clusterDomain}"
246
247 for (i in common.entries(smc_apt)) {
248 sh "sed -i \"s,export ${i[0]}=.*,export ${i[0]}=${i[1]},\" mirror_config.sh"
249 }
250
251 // create apt config-drive
252 sh "./create-config-drive --user-data mirror_config.sh --hostname ${aptlyServerHostname} ${aptlyServerHostname}.${clusterDomain}-config.iso"
253 sh("mv ${aptlyServerHostname}.${clusterDomain}-config.iso output-${clusterName}/")
254
255 // save apt iso to artifacts
256 archiveArtifacts artifacts: "output-${clusterName}/${aptlyServerHostname}.${clusterDomain}-config.iso"
257 }
Richard Felkl9fd47942017-10-20 16:23:29 +0200258 }
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400259
Jakub Josefa63f9862018-01-11 17:58:38 +0100260 stage ('Save changes reclass model') {
261 sh(returnStatus: true, script: "tar -zcf output-${clusterName}/${clusterName}.tar.gz -C ${modelEnv} .")
262 archiveArtifacts artifacts: "output-${clusterName}/${clusterName}.tar.gz"
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400263
264
Jakub Josefa63f9862018-01-11 17:58:38 +0100265 if (EMAIL_ADDRESS != null && EMAIL_ADDRESS != "") {
266 emailext(to: EMAIL_ADDRESS,
267 attachmentsPattern: "output-${clusterName}/*",
268 body: "Mirantis Jenkins\n\nRequested reclass model ${clusterName} has been created and attached to this email.\nEnjoy!\n\nMirantis",
269 subject: "Your Salt model ${clusterName}")
270 }
271 dir("output-${clusterName}"){
272 deleteDir()
273 }
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400274 }
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400275
Jakub Josefa63f9862018-01-11 17:58:38 +0100276 } catch (Throwable e) {
277 // If there was an error or exception thrown, the build failed
278 currentBuild.result = "FAILURE"
279 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
280 throw e
281 } finally {
282 stage ('Clean workspace directories') {
283 sh(returnStatus: true, script: "rm -rf ${templateEnv}")
284 sh(returnStatus: true, script: "rm -rf ${modelEnv}")
285 sh(returnStatus: true, script: "rm -rf ${pipelineEnv}")
286 }
287 // common.sendNotification(currentBuild.result,"",["slack"])
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400288 }
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200289 }
Mikhail Ivanov9f812922017-11-07 18:52:02 +0400290}