blob: 09249511134e5fde7402eb07d8ae714de8044e95 [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}
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 ' +
Andrei Danincd633092018-08-28 15:26:43 -070028 'grafana backupninja'
azvyagintsevf252b592018-08-13 18:39:14 +030029
30
31timeout(time: 2, unit: 'HOURS') {
32 node(slaveNode) {
azvyagintsev3ed704f2018-07-09 15:49:27 +030033 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
azvyagintsev3ed704f2018-07-09 15:49:27 +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
azvyagintsevf252b592018-08-13 18:39:14 +030056 def testResult = false
azvyagintsev3ed704f2018-07-09 15:49:27 +030057 wrap([$class: 'BuildUser']) {
58 user = env.BUILD_USER_ID
59 }
60
Andrei Danincd633092018-08-28 15:26:43 -070061 if (mcpVersion != '2018.4.0') {
62 testCfg01ExtraFormulas += ' auditd'
63 }
64
azvyagintsev3ed704f2018-07-09 15:49:27 +030065 currentBuild.description = clusterName
66 print("Using context:\n" + COOKIECUTTER_TEMPLATE_CONTEXT)
67
azvyagintsevf252b592018-08-13 18:39:14 +030068 stage('Download Cookiecutter template') {
69 sh(script: 'find . -mindepth 1 -delete > /dev/null || true')
azvyagintsev3ed704f2018-07-09 15:49:27 +030070 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
azvyagintsevf252b592018-08-13 18:39:14 +030083 if ([ "nightly" , "testing", "stable" ].contains(mcpVersion)) {
azvyagintsev3ed704f2018-07-09 15:49:27 +030084 cookiecutterTemplateBranch = 'master'
Tomáš Kukrála3f4ba72017-08-03 15:40:22 +020085 }
azvyagintsev3ed704f2018-07-09 15:49:27 +030086 }
87 git.changeGitBranch(templateEnv, cookiecutterTemplateBranch)
88 }
89 }
Tomáš Kukrála3f4ba72017-08-03 15:40:22 +020090
azvyagintsevf252b592018-08-13 18:39:14 +030091 stage('Create empty reclass model') {
azvyagintsev3ed704f2018-07-09 15:49:27 +030092 dir(path: modelEnv) {
93 sh "rm -rfv .git"
94 sh "git init"
95 ssh.agentSh("git submodule add ${sharedReclassUrl} 'classes/system'")
96 }
Tomáš Kukrál0c6835d2017-07-18 16:00:27 +020097
azvyagintsev3ed704f2018-07-09 15:49:27 +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
azvyagintsevf252b592018-08-13 18:39:14 +0300108 // 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!")
azvyagintsev3ed704f2018-07-09 15:49:27 +0300111 sharedReclassBranch = 'master'
Jakub Josefa63f9862018-01-11 17:58:38 +0100112 }
azvyagintsev3ed704f2018-07-09 15:49:27 +0300113 }
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
azvyagintsev3ed704f2018-07-09 15:49:27 +0300119 def productList = ["infra", "cicd", "opencontrail", "kubernetes", "openstack", "oss", "stacklight", "ceph"]
120 for (product in productList) {
Richard Felkle77b0912018-01-31 17:12:23 +0100121
azvyagintsev3ed704f2018-07-09 15:49:27 +0300122 // get templateOutputDir and productDir
123 if (product.startsWith("stacklight")) {
124 templateOutputDir = "${env.WORKSPACE}/output/stacklight"
Jakub Josefa63f9862018-01-11 17:58:38 +0100125
azvyagintsev3ed704f2018-07-09 15:49:27 +0300126 def stacklightVersion
127 try {
128 stacklightVersion = templateContext.default_context['stacklight_version']
129 } catch (Throwable e) {
130 common.warningMsg('Stacklight version loading failed')
131 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100132
azvyagintsev3ed704f2018-07-09 15:49:27 +0300133 if (stacklightVersion) {
134 productDir = "stacklight" + stacklightVersion
135 } else {
136 productDir = "stacklight1"
137 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100138
azvyagintsev3ed704f2018-07-09 15:49:27 +0300139 } else {
140 templateOutputDir = "${env.WORKSPACE}/output/${product}"
141 productDir = product
142 }
Richard Felkl9543faa2018-01-11 09:47:35 +0100143
azvyagintsev3ed704f2018-07-09 15:49:27 +0300144 if (product == "infra" || (templateContext.default_context["${product}_enabled"]
145 && templateContext.default_context["${product}_enabled"].toBoolean())) {
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200146
azvyagintsev3ed704f2018-07-09 15:49:27 +0300147 templateDir = "${templateEnv}/cluster_product/${productDir}"
148 common.infoMsg("Generating product " + product + " from " + templateDir + " to " + templateOutputDir)
Tomáš Kukrála3f4ba72017-08-03 15:40:22 +0200149
azvyagintsev3ed704f2018-07-09 15:49:27 +0300150 sh "rm -rf ${templateOutputDir} || true"
151 sh "mkdir -p ${templateOutputDir}"
152 sh "mkdir -p ${outputDestination}"
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200153
azvyagintsev3ed704f2018-07-09 15:49:27 +0300154 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 }
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400161
azvyagintsevf252b592018-08-13 18:39:14 +0300162 if (localRepositories && !offlineDeployment) {
azvyagintsev3ed704f2018-07-09 15:49:27 +0300163 def aptlyModelUrl = templateContext.default_context.local_model_url
164 dir(path: modelEnv) {
165 ssh.agentSh "git submodule add \"${aptlyModelUrl}\" \"classes/cluster/${clusterName}/cicd/aptly\""
azvyagintsevf252b592018-08-13 18:39:14 +0300166 if (!(mcpVersion in ["nightly", "testing", "stable"])) {
azvyagintsev3ed704f2018-07-09 15:49:27 +0300167 ssh.agentSh "cd \"classes/cluster/${clusterName}/cicd/aptly\";git fetch --tags;git checkout ${mcpVersion}"
168 }
169 }
170 }
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400171
azvyagintsev3ed704f2018-07-09 15:49:27 +0300172 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 """
azvyagintsev3ed704f2018-07-09 15:49:27 +0300185 sh "mkdir -p ${modelEnv}/nodes/"
186 writeFile(file: nodeFile, text: nodeString)
Jakub Josefa63f9862018-01-11 17:58:38 +0100187
azvyagintsev3ed704f2018-07-09 15:49:27 +0300188 git.commitGitChanges(modelEnv, "Create model ${clusterName}", "${user}@localhost", "${user}")
189 }
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400190
azvyagintsev3ed704f2018-07-09 15:49:27 +0300191 stage("Test") {
192 if (TEST_MODEL.toBoolean() && sharedReclassUrl != '') {
azvyagintsev3ed704f2018-07-09 15:49:27 +0300193 sh("cp -r ${modelEnv} ${testEnv}")
194 def DockerCName = "${env.JOB_NAME.toLowerCase()}_${env.BUILD_TAG.toLowerCase()}"
azvyagintsevf252b592018-08-13 18:39:14 +0300195 common.infoMsg("Attempt to run test against formula-version: ${mcpVersion}")
azvyagintsev3ed704f2018-07-09 15:49:27 +0300196 testResult = saltModelTesting.setupAndTestNode(
197 "${saltMaster}.${clusterDomain}",
198 "",
azvyagintsevf252b592018-08-13 18:39:14 +0300199 testCfg01ExtraFormulas,
azvyagintsev3ed704f2018-07-09 15:49:27 +0300200 testEnv,
201 'pkg',
azvyagintsevf252b592018-08-13 18:39:14 +0300202 mcpVersion,
Vasyl Saienko772e1232018-07-23 14:42:24 +0300203 reclassVersion,
azvyagintsev3ed704f2018-07-09 15:49:27 +0300204 0,
205 false,
206 false,
207 '',
208 '',
209 DockerCName)
210 if (testResult) {
211 common.infoMsg("Test finished: SUCCESS")
212 } else {
azvyagintsevf252b592018-08-13 18:39:14 +0300213 common.warningMsg('Test finished: FAILURE')
azvyagintsev7c4a3cf2018-07-08 08:43:33 +0300214 }
azvyagintsev3ed704f2018-07-09 15:49:27 +0300215 } else {
216 common.warningMsg("Test stage has been skipped!")
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400217 }
azvyagintsev3ed704f2018-07-09 15:49:27 +0300218 }
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
azvyagintsevf252b592018-08-13 18:39:14 +0300227 // 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!")
azvyagintsev3ed704f2018-07-09 15:49:27 +0300230 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"
azvyagintsevf252b592018-08-13 18:39:14 +0300235 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 }
azvyagintsev3ed704f2018-07-09 15:49:27 +0300239
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 smc['DNS_SERVERS'] = templateContext['default_context']['dns_server01']
251 smc['MCP_VERSION'] = "${mcpVersion}"
azvyagintsevf252b592018-08-13 18:39:14 +0300252 if (templateContext['default_context']['local_repositories'] == 'True') {
azvyagintsev3ed704f2018-07-09 15:49:27 +0300253 def localRepoIP = templateContext['default_context']['local_repo_url']
254 smc['MCP_SALT_REPO_KEY'] = "http://${localRepoIP}/public.gpg"
255 smc['MCP_SALT_REPO_URL'] = "http://${localRepoIP}/ubuntu-xenial"
256 smc['PIPELINES_FROM_ISO'] = 'false'
257 smc['PIPELINE_REPO_URL'] = "http://${localRepoIP}:8088"
258 smc['LOCAL_REPOS'] = 'true'
259 }
azvyagintsevf252b592018-08-13 18:39:14 +0300260 if (templateContext['default_context']['upstream_proxy_enabled'] == 'True') {
261 if (templateContext['default_context']['upstream_proxy_auth_enabled'] == 'True') {
azvyagintsev3ed704f2018-07-09 15:49:27 +0300262 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']
263 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']
264 } else {
265 smc['http_proxy'] = 'http://' + templateContext['default_context']['upstream_proxy_address'] + ':' + templateContext['default_context']['upstream_proxy_port']
266 smc['https_proxy'] = 'http://' + templateContext['default_context']['upstream_proxy_address'] + ':' + templateContext['default_context']['upstream_proxy_port']
267 }
268 }
269
270 for (i in common.entries(smc)) {
271 sh "sed -i 's,export ${i[0]}=.*,export ${i[0]}=${i[1]},' user_data.sh"
272 }
273
274 // create cfg config-drive
275 sh "./create-config-drive ${args}"
276 sh("mkdir output-${clusterName} && mv ${saltMaster}.${clusterDomain}-config.iso output-${clusterName}/")
277
278 // save cfg iso to artifacts
279 archiveArtifacts artifacts: "output-${clusterName}/${saltMaster}.${clusterDomain}-config.iso"
280
azvyagintsevf252b592018-08-13 18:39:14 +0300281 if (templateContext['default_context']['local_repositories'] == 'True') {
azvyagintsev3ed704f2018-07-09 15:49:27 +0300282 def aptlyServerHostname = templateContext.default_context.aptly_server_hostname
283 def user_data_script_apt_url = "https://raw.githubusercontent.com/Mirantis/mcp-common-scripts/master/config-drive/mirror_config.sh"
284 sh "wget -O mirror_config.sh ${user_data_script_apt_url}"
285
286 def smc_apt = [:]
287 smc_apt['SALT_MASTER_DEPLOY_IP'] = templateContext['default_context']['salt_master_management_address']
288 smc_apt['APTLY_DEPLOY_IP'] = templateContext['default_context']['aptly_server_deploy_address']
289 smc_apt['APTLY_DEPLOY_NETMASK'] = templateContext['default_context']['deploy_network_netmask']
290 smc_apt['APTLY_MINION_ID'] = "${aptlyServerHostname}.${clusterDomain}"
291
292 for (i in common.entries(smc_apt)) {
293 sh "sed -i \"s,export ${i[0]}=.*,export ${i[0]}=${i[1]},\" mirror_config.sh"
294 }
295
296 // create apt config-drive
297 sh "./create-config-drive --user-data mirror_config.sh --hostname ${aptlyServerHostname} ${aptlyServerHostname}.${clusterDomain}-config.iso"
298 sh("mv ${aptlyServerHostname}.${clusterDomain}-config.iso output-${clusterName}/")
299
300 // save apt iso to artifacts
301 archiveArtifacts artifacts: "output-${clusterName}/${aptlyServerHostname}.${clusterDomain}-config.iso"
302 }
303 }
304
azvyagintsevf252b592018-08-13 18:39:14 +0300305 stage('Save changes reclass model') {
306 sh(returnStatus: true, script: "tar -czf output-${clusterName}/${clusterName}.tar.gz --exclude='*@tmp' -C ${modelEnv} .")
azvyagintsev3ed704f2018-07-09 15:49:27 +0300307 archiveArtifacts artifacts: "output-${clusterName}/${clusterName}.tar.gz"
308
309
310 if (EMAIL_ADDRESS != null && EMAIL_ADDRESS != "") {
311 emailext(to: EMAIL_ADDRESS,
312 attachmentsPattern: "output-${clusterName}/*",
313 body: "Mirantis Jenkins\n\nRequested reclass model ${clusterName} has been created and attached to this email.\nEnjoy!\n\nMirantis",
314 subject: "Your Salt model ${clusterName}")
315 }
azvyagintsevf252b592018-08-13 18:39:14 +0300316 dir("output-${clusterName}") {
azvyagintsev3ed704f2018-07-09 15:49:27 +0300317 deleteDir()
318 }
319 }
320
azvyagintsevf252b592018-08-13 18:39:14 +0300321 // Fail, but leave possibility to get failed artifacts
322 if (!testResult && TEST_MODEL.toBoolean()) {
323 common.warningMsg('Test finished: FAILURE. Please check logs and\\or debug failed model manually!')
324 error('Test stage finished: FAILURE')
325 }
326
azvyagintsev3ed704f2018-07-09 15:49:27 +0300327 } catch (Throwable e) {
azvyagintsev3ed704f2018-07-09 15:49:27 +0300328 currentBuild.result = "FAILURE"
329 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
330 throw e
331 } finally {
azvyagintsevf252b592018-08-13 18:39:14 +0300332 stage('Clean workspace directories') {
333 sh(script: 'find . -mindepth 1 -delete > /dev/null || true')
azvyagintsev3ed704f2018-07-09 15:49:27 +0300334 }
335 // common.sendNotification(currentBuild.result,"",["slack"])
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200336 }
azvyagintsev3ed704f2018-07-09 15:49:27 +0300337 }
Mikhail Ivanov9f812922017-11-07 18:52:02 +0400338}