blob: 90640cb251b91c300e38b7bb20e65b8d8791fe2a [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')) {
azvyagintsev636493c2018-09-12 17:17:05 +030018 reclassVersion = RECLASS_VERSION
Vasyl Saienko772e1232018-07-23 14:42:24 +030019}
azvyagintsevf252b592018-08-13 18:39:14 +030020slaveNode = (env.SLAVE_NODE ?: 'python&&docker')
Vasyl Saienko772e1232018-07-23 14:42:24 +030021
azvyagintsevf252b592018-08-13 18:39:14 +030022// install extra formulas required only for rendering cfg01. All others - should be fetched automatically via
23// salt.master.env state, during salt-master bootstrap.
24// TODO: In the best - those data should fetched somewhere from CC, per env\context. Like option, process _enabled
25// options from CC contexts
26// currently, just mix them together in one set
27def testCfg01ExtraFormulas = 'glusterfs jenkins logrotate maas ntp rsyslog fluentd telegraf prometheus ' +
azvyagintsev636493c2018-09-12 17:17:05 +030028 'grafana backupninja'
azvyagintsevf252b592018-08-13 18:39:14 +030029
30
31timeout(time: 2, unit: 'HOURS') {
azvyagintsev636493c2018-09-12 17:17:05 +030032 node(slaveNode) {
33 def templateEnv = "${env.WORKSPACE}/template"
34 def modelEnv = "${env.WORKSPACE}/model"
35 def testEnv = "${env.WORKSPACE}/test"
36 def pipelineEnv = "${env.WORKSPACE}/pipelines"
Tomáš Kukrál9f6260f2017-03-29 23:58:26 +020037
azvyagintsev636493c2018-09-12 17:17:05 +030038 try {
39 def templateContext = readYaml text: COOKIECUTTER_TEMPLATE_CONTEXT
40 def mcpVersion = templateContext.default_context.mcp_version
41 def sharedReclassUrl = templateContext.default_context.shared_reclass_url
42 def clusterDomain = templateContext.default_context.cluster_domain
43 def clusterName = templateContext.default_context.cluster_name
44 def saltMaster = templateContext.default_context.salt_master_hostname
45 def localRepositories = templateContext.default_context.local_repositories.toBoolean()
46 def offlineDeployment = templateContext.default_context.offline_deployment.toBoolean()
47 def cutterEnv = "${env.WORKSPACE}/cutter"
48 def jinjaEnv = "${env.WORKSPACE}/jinja"
49 def outputDestination = "${modelEnv}/classes/cluster/${clusterName}"
50 def systemEnv = "${modelEnv}/classes/system"
51 def targetBranch = "feature/${clusterName}"
52 def templateBaseDir = "${env.WORKSPACE}/template"
53 def templateDir = "${templateEnv}/template/dir"
54 def templateOutputDir = templateBaseDir
55 def user
56 def testResult = false
57 wrap([$class: 'BuildUser']) {
58 user = env.BUILD_USER_ID
Tomáš Kukrála3f4ba72017-08-03 15:40:22 +020059 }
60
azvyagintsev636493c2018-09-12 17:17:05 +030061 if (mcpVersion != '2018.4.0') {
62 testCfg01ExtraFormulas += ' auditd'
Jakub Josefa63f9862018-01-11 17:58:38 +010063 }
64
azvyagintsev636493c2018-09-12 17:17:05 +030065 currentBuild.description = clusterName
66 print("Using context:\n" + COOKIECUTTER_TEMPLATE_CONTEXT)
Richard Felkle77b0912018-01-31 17:12:23 +010067
azvyagintsev636493c2018-09-12 17:17:05 +030068 stage('Download Cookiecutter template') {
69 sh(script: 'find . -mindepth 1 -delete > /dev/null || true')
70 def cookiecutterTemplateUrl = templateContext.default_context.cookiecutter_template_url
71 def cookiecutterTemplateBranch = templateContext.default_context.cookiecutter_template_branch
72 git.checkoutGitRepository(templateEnv, cookiecutterTemplateUrl, 'master')
73 // Use refspec if exists first of all
74 if (cookiecutterTemplateBranch.toString().startsWith('refs/')) {
75 dir(templateEnv) {
76 ssh.agentSh("git fetch ${cookiecutterTemplateUrl} ${cookiecutterTemplateBranch} && git checkout FETCH_HEAD")
77 }
78 } else {
79 // Use mcpVersion git tag if not specified branch for cookiecutter-templates
80 if (cookiecutterTemplateBranch == '') {
81 cookiecutterTemplateBranch = mcpVersion
82 // Don't have nightly/testing/stable for cookiecutter-templates repo, therefore use master
83 if (["nightly", "testing", "stable"].contains(mcpVersion)) {
84 cookiecutterTemplateBranch = 'master'
85 }
86 }
87 git.changeGitBranch(templateEnv, cookiecutterTemplateBranch)
88 }
89 }
Jakub Josefa63f9862018-01-11 17:58:38 +010090
azvyagintsev636493c2018-09-12 17:17:05 +030091 stage('Create empty reclass model') {
92 dir(path: modelEnv) {
93 sh "rm -rfv .git"
94 sh "git init"
95 ssh.agentSh("git submodule add ${sharedReclassUrl} 'classes/system'")
96 }
Jakub Josefa63f9862018-01-11 17:58:38 +010097
azvyagintsev636493c2018-09-12 17:17:05 +030098 def sharedReclassBranch = templateContext.default_context.shared_reclass_branch
99 // Use refspec if exists first of all
100 if (sharedReclassBranch.toString().startsWith('refs/')) {
101 dir(systemEnv) {
102 ssh.agentSh("git fetch ${sharedReclassUrl} ${sharedReclassBranch} && git checkout FETCH_HEAD")
103 }
104 } else {
105 // Use mcpVersion git tag if not specified branch for reclass-system
106 if (sharedReclassBranch == '') {
107 sharedReclassBranch = mcpVersion
108 // Don't have nightly/testing for reclass-system repo, therefore use master
109 if (["nightly", "testing", "stable"].contains(mcpVersion)) {
110 common.warningMsg("Fetching reclass-system from master!")
111 sharedReclassBranch = 'master'
112 }
113 }
114 git.changeGitBranch(systemEnv, sharedReclassBranch)
115 }
116 git.commitGitChanges(modelEnv, "Added new shared reclass submodule", "${user}@localhost", "${user}")
117 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100118
azvyagintsev636493c2018-09-12 17:17:05 +0300119 def productList = ["infra", "cicd", "opencontrail", "kubernetes", "openstack", "oss", "stacklight", "ceph"]
120 for (product in productList) {
Richard Felkl9543faa2018-01-11 09:47:35 +0100121
azvyagintsev636493c2018-09-12 17:17:05 +0300122 // get templateOutputDir and productDir
123 if (product.startsWith("stacklight")) {
124 templateOutputDir = "${env.WORKSPACE}/output/stacklight"
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200125
azvyagintsev636493c2018-09-12 17:17:05 +0300126 def stacklightVersion
127 try {
128 stacklightVersion = templateContext.default_context['stacklight_version']
129 } catch (Throwable e) {
130 common.warningMsg('Stacklight version loading failed')
131 }
Tomáš Kukrála3f4ba72017-08-03 15:40:22 +0200132
azvyagintsev636493c2018-09-12 17:17:05 +0300133 if (stacklightVersion) {
134 productDir = "stacklight" + stacklightVersion
135 } else {
136 productDir = "stacklight1"
137 }
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200138
azvyagintsev636493c2018-09-12 17:17:05 +0300139 } else {
140 templateOutputDir = "${env.WORKSPACE}/output/${product}"
141 productDir = product
142 }
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400143
azvyagintsev636493c2018-09-12 17:17:05 +0300144 if (product == "infra" || (templateContext.default_context["${product}_enabled"]
145 && templateContext.default_context["${product}_enabled"].toBoolean())) {
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400146
azvyagintsev636493c2018-09-12 17:17:05 +0300147 templateDir = "${templateEnv}/cluster_product/${productDir}"
148 common.infoMsg("Generating product " + product + " from " + templateDir + " to " + templateOutputDir)
149
150 sh "rm -rf ${templateOutputDir} || true"
151 sh "mkdir -p ${templateOutputDir}"
152 sh "mkdir -p ${outputDestination}"
153
154 python.setupCookiecutterVirtualenv(cutterEnv)
155 python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv, templateBaseDir)
156 sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
157 } else {
158 common.warningMsg("Product " + product + " is disabled")
159 }
160 }
161
162 if (localRepositories && !offlineDeployment) {
163 def aptlyModelUrl = templateContext.default_context.local_model_url
164 dir(path: modelEnv) {
165 ssh.agentSh "git submodule add \"${aptlyModelUrl}\" \"classes/cluster/${clusterName}/cicd/aptly\""
166 if (!(mcpVersion in ["nightly", "testing", "stable"])) {
167 ssh.agentSh "cd \"classes/cluster/${clusterName}/cicd/aptly\";git fetch --tags;git checkout ${mcpVersion}"
168 }
169 }
170 }
171
172 stage('Generate new SaltMaster node') {
173 def nodeFile = "${modelEnv}/nodes/${saltMaster}.${clusterDomain}.yml"
174 def nodeString = """classes:
Leontii Istomin56b6b112018-01-15 12:14:24 +0300175- cluster.${clusterName}.infra.config
176parameters:
177 _param:
178 linux_system_codename: xenial
179 reclass_data_revision: master
180 linux:
181 system:
182 name: ${saltMaster}
183 domain: ${clusterDomain}
Jakub Josefa63f9862018-01-11 17:58:38 +0100184 """
azvyagintsev636493c2018-09-12 17:17:05 +0300185 sh "mkdir -p ${modelEnv}/nodes/"
186 writeFile(file: nodeFile, text: nodeString)
Jakub Josefa63f9862018-01-11 17:58:38 +0100187
azvyagintsev636493c2018-09-12 17:17:05 +0300188 git.commitGitChanges(modelEnv, "Create model ${clusterName}", "${user}@localhost", "${user}")
189 }
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400190
azvyagintsev636493c2018-09-12 17:17:05 +0300191 stage("Test") {
192 if (TEST_MODEL.toBoolean() && sharedReclassUrl != '') {
193 sh("cp -r ${modelEnv} ${testEnv}")
194 def DockerCName = "${env.JOB_NAME.toLowerCase()}_${env.BUILD_TAG.toLowerCase()}"
195 common.infoMsg("Attempt to run test against formula-version: ${mcpVersion}")
196 testResult = saltModelTesting.setupAndTestNode(
197 "${saltMaster}.${clusterDomain}",
198 "",
199 testCfg01ExtraFormulas,
200 testEnv,
201 'pkg',
202 mcpVersion,
203 reclassVersion,
204 0,
205 false,
206 false,
207 '',
208 '',
209 DockerCName)
210 if (testResult) {
211 common.infoMsg("Test finished: SUCCESS")
212 } else {
213 common.warningMsg('Test finished: FAILURE')
214 }
215 } else {
216 common.warningMsg("Test stage has been skipped!")
217 }
218 }
219 stage("Generate config drives") {
220 // apt package genisoimage is required for this stage
221
222 // download create-config-drive
223 // FIXME: that should be refactored, to use git clone - to be able download it from custom repo.
224 def mcpCommonScriptsBranch = templateContext.default_context.mcp_common_scripts_branch
225 if (mcpCommonScriptsBranch == '') {
226 mcpCommonScriptsBranch = mcpVersion
227 // Don't have n/t/s for mcp-common-scripts repo, therefore use master
228 if (["nightly", "testing", "stable"].contains(mcpVersion)) {
229 common.warningMsg("Fetching mcp-common-scripts from master!")
230 mcpCommonScriptsBranch = 'master'
231 }
232 }
233 def config_drive_script_url = "https://raw.githubusercontent.com/Mirantis/mcp-common-scripts/${mcpCommonScriptsBranch}/config-drive/create_config_drive.sh"
234 def user_data_script_url = "https://raw.githubusercontent.com/Mirantis/mcp-common-scripts/${mcpCommonScriptsBranch}/config-drive/master_config.sh"
235 common.retry(3, 5) {
236 sh "wget -O create-config-drive ${config_drive_script_url} && chmod +x create-config-drive"
237 sh "wget -O user_data.sh ${user_data_script_url}"
238 }
239
240 sh "git clone --mirror https://github.com/Mirantis/mk-pipelines.git ${pipelineEnv}/mk-pipelines"
241 sh "git clone --mirror https://github.com/Mirantis/pipeline-library.git ${pipelineEnv}/pipeline-library"
242 args = "--user-data user_data.sh --hostname ${saltMaster} --model ${modelEnv} --mk-pipelines ${pipelineEnv}/mk-pipelines/ --pipeline-library ${pipelineEnv}/pipeline-library/ ${saltMaster}.${clusterDomain}-config.iso"
243
244 // load data from model
245 def smc = [:]
246 smc['SALT_MASTER_MINION_ID'] = "${saltMaster}.${clusterDomain}"
247 smc['SALT_MASTER_DEPLOY_IP'] = templateContext['default_context']['salt_master_management_address']
248 smc['DEPLOY_NETWORK_GW'] = templateContext['default_context']['deploy_network_gateway']
249 smc['DEPLOY_NETWORK_NETMASK'] = templateContext['default_context']['deploy_network_netmask']
250 if (templateContext['default_context'].get('deploy_network_mtu')) {
251 smc['DEPLOY_NETWORK_MTU'] = templateContext['default_context']['deploy_network_mtu']
252 }
253 smc['DNS_SERVERS'] = templateContext['default_context']['dns_server01']
254 smc['MCP_VERSION'] = "${mcpVersion}"
255 if (templateContext['default_context']['local_repositories'] == 'True') {
256 def localRepoIP = templateContext['default_context']['local_repo_url']
257 smc['MCP_SALT_REPO_KEY'] = "http://${localRepoIP}/public.gpg"
258 smc['MCP_SALT_REPO_URL'] = "http://${localRepoIP}/ubuntu-xenial"
259 smc['PIPELINES_FROM_ISO'] = 'false'
260 smc['PIPELINE_REPO_URL'] = "http://${localRepoIP}:8088"
261 smc['LOCAL_REPOS'] = 'true'
262 }
263 if (templateContext['default_context']['upstream_proxy_enabled'] == 'True') {
264 if (templateContext['default_context']['upstream_proxy_auth_enabled'] == 'True') {
265 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']
266 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']
267 } else {
268 smc['http_proxy'] = 'http://' + templateContext['default_context']['upstream_proxy_address'] + ':' + templateContext['default_context']['upstream_proxy_port']
269 smc['https_proxy'] = 'http://' + templateContext['default_context']['upstream_proxy_address'] + ':' + templateContext['default_context']['upstream_proxy_port']
270 }
271 }
272
273 for (i in common.entries(smc)) {
274 sh "sed -i 's,export ${i[0]}=.*,export ${i[0]}=${i[1]},' user_data.sh"
275 }
276
277 // create cfg config-drive
278 sh "./create-config-drive ${args}"
279 sh("mkdir output-${clusterName} && mv ${saltMaster}.${clusterDomain}-config.iso output-${clusterName}/")
280
281 // save cfg iso to artifacts
282 archiveArtifacts artifacts: "output-${clusterName}/${saltMaster}.${clusterDomain}-config.iso"
283
284 if (templateContext['default_context']['local_repositories'] == 'True') {
285 def aptlyServerHostname = templateContext.default_context.aptly_server_hostname
286 def user_data_script_apt_url = "https://raw.githubusercontent.com/Mirantis/mcp-common-scripts/master/config-drive/mirror_config.sh"
287 sh "wget -O mirror_config.sh ${user_data_script_apt_url}"
288
289 def smc_apt = [:]
290 smc_apt['SALT_MASTER_DEPLOY_IP'] = templateContext['default_context']['salt_master_management_address']
291 smc_apt['APTLY_DEPLOY_IP'] = templateContext['default_context']['aptly_server_deploy_address']
292 smc_apt['APTLY_DEPLOY_NETMASK'] = templateContext['default_context']['deploy_network_netmask']
293 smc_apt['APTLY_MINION_ID'] = "${aptlyServerHostname}.${clusterDomain}"
294
295 for (i in common.entries(smc_apt)) {
296 sh "sed -i \"s,export ${i[0]}=.*,export ${i[0]}=${i[1]},\" mirror_config.sh"
297 }
298
299 // create apt config-drive
300 sh "./create-config-drive --user-data mirror_config.sh --hostname ${aptlyServerHostname} ${aptlyServerHostname}.${clusterDomain}-config.iso"
301 sh("mv ${aptlyServerHostname}.${clusterDomain}-config.iso output-${clusterName}/")
302
303 // save apt iso to artifacts
304 archiveArtifacts artifacts: "output-${clusterName}/${aptlyServerHostname}.${clusterDomain}-config.iso"
305 }
306 }
307
308 stage('Save changes reclass model') {
309 sh(returnStatus: true, script: "tar -czf output-${clusterName}/${clusterName}.tar.gz --exclude='*@tmp' -C ${modelEnv} .")
310 archiveArtifacts artifacts: "output-${clusterName}/${clusterName}.tar.gz"
311
312
313 if (EMAIL_ADDRESS != null && EMAIL_ADDRESS != "") {
314 emailext(to: EMAIL_ADDRESS,
315 attachmentsPattern: "output-${clusterName}/*",
316 body: "Mirantis Jenkins\n\nRequested reclass model ${clusterName} has been created and attached to this email.\nEnjoy!\n\nMirantis",
317 subject: "Your Salt model ${clusterName}")
318 }
319 dir("output-${clusterName}") {
320 deleteDir()
321 }
322 }
323
324 // Fail, but leave possibility to get failed artifacts
325 if (!testResult && TEST_MODEL.toBoolean()) {
326 common.warningMsg('Test finished: FAILURE. Please check logs and\\or debug failed model manually!')
327 error('Test stage finished: FAILURE')
328 }
329
330 } catch (Throwable e) {
331 currentBuild.result = "FAILURE"
332 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
333 throw e
334 } finally {
335 stage('Clean workspace directories') {
336 sh(script: 'find . -mindepth 1 -delete > /dev/null || true')
337 }
338 // common.sendNotification(currentBuild.result,"",["slack"])
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400339 }
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200340 }
Mikhail Ivanov9f812922017-11-07 18:52:02 +0400341}