blob: 553029ec091339d50f3f26864667eb4bac5c0a53 [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 ' +
28 'grafana backupninja auditd'
29
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
61 currentBuild.description = clusterName
62 print("Using context:\n" + COOKIECUTTER_TEMPLATE_CONTEXT)
63
azvyagintsevf252b592018-08-13 18:39:14 +030064 stage('Download Cookiecutter template') {
65 sh(script: 'find . -mindepth 1 -delete > /dev/null || true')
azvyagintsev3ed704f2018-07-09 15:49:27 +030066 def cookiecutterTemplateUrl = templateContext.default_context.cookiecutter_template_url
67 def cookiecutterTemplateBranch = templateContext.default_context.cookiecutter_template_branch
68 git.checkoutGitRepository(templateEnv, cookiecutterTemplateUrl, 'master')
69 // Use refspec if exists first of all
70 if (cookiecutterTemplateBranch.toString().startsWith('refs/')) {
71 dir(templateEnv) {
72 ssh.agentSh("git fetch ${cookiecutterTemplateUrl} ${cookiecutterTemplateBranch} && git checkout FETCH_HEAD")
73 }
74 } else {
75 // Use mcpVersion git tag if not specified branch for cookiecutter-templates
76 if (cookiecutterTemplateBranch == '') {
77 cookiecutterTemplateBranch = mcpVersion
78 // Don't have nightly/testing/stable for cookiecutter-templates repo, therefore use master
azvyagintsevf252b592018-08-13 18:39:14 +030079 if ([ "nightly" , "testing", "stable" ].contains(mcpVersion)) {
azvyagintsev3ed704f2018-07-09 15:49:27 +030080 cookiecutterTemplateBranch = 'master'
Tomáš Kukrála3f4ba72017-08-03 15:40:22 +020081 }
azvyagintsev3ed704f2018-07-09 15:49:27 +030082 }
83 git.changeGitBranch(templateEnv, cookiecutterTemplateBranch)
84 }
85 }
Tomáš Kukrála3f4ba72017-08-03 15:40:22 +020086
azvyagintsevf252b592018-08-13 18:39:14 +030087 stage('Create empty reclass model') {
azvyagintsev3ed704f2018-07-09 15:49:27 +030088 dir(path: modelEnv) {
89 sh "rm -rfv .git"
90 sh "git init"
91 ssh.agentSh("git submodule add ${sharedReclassUrl} 'classes/system'")
92 }
Tomáš Kukrál0c6835d2017-07-18 16:00:27 +020093
azvyagintsev3ed704f2018-07-09 15:49:27 +030094 def sharedReclassBranch = templateContext.default_context.shared_reclass_branch
95 // Use refspec if exists first of all
96 if (sharedReclassBranch.toString().startsWith('refs/')) {
97 dir(systemEnv) {
98 ssh.agentSh("git fetch ${sharedReclassUrl} ${sharedReclassBranch} && git checkout FETCH_HEAD")
99 }
100 } else {
101 // Use mcpVersion git tag if not specified branch for reclass-system
102 if (sharedReclassBranch == '') {
103 sharedReclassBranch = mcpVersion
azvyagintsevf252b592018-08-13 18:39:14 +0300104 // Don't have nightly/testing for reclass-system repo, therefore use master
105 if ([ "nightly" , "testing", "stable" ].contains(mcpVersion)) {
106 common.warningMsg("Fetching reclass-system from master!")
azvyagintsev3ed704f2018-07-09 15:49:27 +0300107 sharedReclassBranch = 'master'
Jakub Josefa63f9862018-01-11 17:58:38 +0100108 }
azvyagintsev3ed704f2018-07-09 15:49:27 +0300109 }
110 git.changeGitBranch(systemEnv, sharedReclassBranch)
111 }
112 git.commitGitChanges(modelEnv, "Added new shared reclass submodule", "${user}@localhost", "${user}")
113 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100114
azvyagintsev3ed704f2018-07-09 15:49:27 +0300115 def productList = ["infra", "cicd", "opencontrail", "kubernetes", "openstack", "oss", "stacklight", "ceph"]
116 for (product in productList) {
Richard Felkle77b0912018-01-31 17:12:23 +0100117
azvyagintsev3ed704f2018-07-09 15:49:27 +0300118 // get templateOutputDir and productDir
119 if (product.startsWith("stacklight")) {
120 templateOutputDir = "${env.WORKSPACE}/output/stacklight"
Jakub Josefa63f9862018-01-11 17:58:38 +0100121
azvyagintsev3ed704f2018-07-09 15:49:27 +0300122 def stacklightVersion
123 try {
124 stacklightVersion = templateContext.default_context['stacklight_version']
125 } catch (Throwable e) {
126 common.warningMsg('Stacklight version loading failed')
127 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100128
azvyagintsev3ed704f2018-07-09 15:49:27 +0300129 if (stacklightVersion) {
130 productDir = "stacklight" + stacklightVersion
131 } else {
132 productDir = "stacklight1"
133 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100134
azvyagintsev3ed704f2018-07-09 15:49:27 +0300135 } else {
136 templateOutputDir = "${env.WORKSPACE}/output/${product}"
137 productDir = product
138 }
Richard Felkl9543faa2018-01-11 09:47:35 +0100139
azvyagintsev3ed704f2018-07-09 15:49:27 +0300140 if (product == "infra" || (templateContext.default_context["${product}_enabled"]
141 && templateContext.default_context["${product}_enabled"].toBoolean())) {
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200142
azvyagintsev3ed704f2018-07-09 15:49:27 +0300143 templateDir = "${templateEnv}/cluster_product/${productDir}"
144 common.infoMsg("Generating product " + product + " from " + templateDir + " to " + templateOutputDir)
Tomáš Kukrála3f4ba72017-08-03 15:40:22 +0200145
azvyagintsev3ed704f2018-07-09 15:49:27 +0300146 sh "rm -rf ${templateOutputDir} || true"
147 sh "mkdir -p ${templateOutputDir}"
148 sh "mkdir -p ${outputDestination}"
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200149
azvyagintsev3ed704f2018-07-09 15:49:27 +0300150 python.setupCookiecutterVirtualenv(cutterEnv)
151 python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv, templateBaseDir)
152 sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
153 } else {
154 common.warningMsg("Product " + product + " is disabled")
155 }
156 }
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400157
azvyagintsevf252b592018-08-13 18:39:14 +0300158 if (localRepositories && !offlineDeployment) {
azvyagintsev3ed704f2018-07-09 15:49:27 +0300159 def aptlyModelUrl = templateContext.default_context.local_model_url
160 dir(path: modelEnv) {
161 ssh.agentSh "git submodule add \"${aptlyModelUrl}\" \"classes/cluster/${clusterName}/cicd/aptly\""
azvyagintsevf252b592018-08-13 18:39:14 +0300162 if (!(mcpVersion in ["nightly", "testing", "stable"])) {
azvyagintsev3ed704f2018-07-09 15:49:27 +0300163 ssh.agentSh "cd \"classes/cluster/${clusterName}/cicd/aptly\";git fetch --tags;git checkout ${mcpVersion}"
164 }
165 }
166 }
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400167
azvyagintsev3ed704f2018-07-09 15:49:27 +0300168 stage('Generate new SaltMaster node') {
169 def nodeFile = "${modelEnv}/nodes/${saltMaster}.${clusterDomain}.yml"
170 def nodeString = """classes:
Leontii Istomin56b6b112018-01-15 12:14:24 +0300171- cluster.${clusterName}.infra.config
172parameters:
173 _param:
174 linux_system_codename: xenial
175 reclass_data_revision: master
176 linux:
177 system:
178 name: ${saltMaster}
179 domain: ${clusterDomain}
Jakub Josefa63f9862018-01-11 17:58:38 +0100180 """
azvyagintsev3ed704f2018-07-09 15:49:27 +0300181 sh "mkdir -p ${modelEnv}/nodes/"
182 writeFile(file: nodeFile, text: nodeString)
Jakub Josefa63f9862018-01-11 17:58:38 +0100183
azvyagintsev3ed704f2018-07-09 15:49:27 +0300184 git.commitGitChanges(modelEnv, "Create model ${clusterName}", "${user}@localhost", "${user}")
185 }
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400186
azvyagintsev3ed704f2018-07-09 15:49:27 +0300187 stage("Test") {
188 if (TEST_MODEL.toBoolean() && sharedReclassUrl != '') {
azvyagintsev3ed704f2018-07-09 15:49:27 +0300189 sh("cp -r ${modelEnv} ${testEnv}")
190 def DockerCName = "${env.JOB_NAME.toLowerCase()}_${env.BUILD_TAG.toLowerCase()}"
azvyagintsevf252b592018-08-13 18:39:14 +0300191 common.infoMsg("Attempt to run test against formula-version: ${mcpVersion}")
azvyagintsev3ed704f2018-07-09 15:49:27 +0300192 testResult = saltModelTesting.setupAndTestNode(
193 "${saltMaster}.${clusterDomain}",
194 "",
azvyagintsevf252b592018-08-13 18:39:14 +0300195 testCfg01ExtraFormulas,
azvyagintsev3ed704f2018-07-09 15:49:27 +0300196 testEnv,
197 'pkg',
azvyagintsevf252b592018-08-13 18:39:14 +0300198 mcpVersion,
Vasyl Saienko772e1232018-07-23 14:42:24 +0300199 reclassVersion,
azvyagintsev3ed704f2018-07-09 15:49:27 +0300200 0,
201 false,
202 false,
203 '',
204 '',
205 DockerCName)
206 if (testResult) {
207 common.infoMsg("Test finished: SUCCESS")
208 } else {
azvyagintsevf252b592018-08-13 18:39:14 +0300209 common.warningMsg('Test finished: FAILURE')
azvyagintsev7c4a3cf2018-07-08 08:43:33 +0300210 }
azvyagintsev3ed704f2018-07-09 15:49:27 +0300211 } else {
212 common.warningMsg("Test stage has been skipped!")
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400213 }
azvyagintsev3ed704f2018-07-09 15:49:27 +0300214 }
215 stage("Generate config drives") {
216 // apt package genisoimage is required for this stage
217
218 // download create-config-drive
219 // FIXME: that should be refactored, to use git clone - to be able download it from custom repo.
220 def mcpCommonScriptsBranch = templateContext.default_context.mcp_common_scripts_branch
221 if (mcpCommonScriptsBranch == '') {
222 mcpCommonScriptsBranch = mcpVersion
azvyagintsevf252b592018-08-13 18:39:14 +0300223 // Don't have n/t/s for mcp-common-scripts repo, therefore use master
224 if ([ "nightly" , "testing", "stable" ].contains(mcpVersion)) {
225 common.warningMsg("Fetching mcp-common-scripts from master!")
azvyagintsev3ed704f2018-07-09 15:49:27 +0300226 mcpCommonScriptsBranch = 'master'
227 }
228 }
229 def config_drive_script_url = "https://raw.githubusercontent.com/Mirantis/mcp-common-scripts/${mcpCommonScriptsBranch}/config-drive/create_config_drive.sh"
230 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 +0300231 common.retry(3, 5) {
232 sh "wget -O create-config-drive ${config_drive_script_url} && chmod +x create-config-drive"
233 sh "wget -O user_data.sh ${user_data_script_url}"
234 }
azvyagintsev3ed704f2018-07-09 15:49:27 +0300235
236 sh "git clone --mirror https://github.com/Mirantis/mk-pipelines.git ${pipelineEnv}/mk-pipelines"
237 sh "git clone --mirror https://github.com/Mirantis/pipeline-library.git ${pipelineEnv}/pipeline-library"
238 args = "--user-data user_data.sh --hostname ${saltMaster} --model ${modelEnv} --mk-pipelines ${pipelineEnv}/mk-pipelines/ --pipeline-library ${pipelineEnv}/pipeline-library/ ${saltMaster}.${clusterDomain}-config.iso"
239
240 // load data from model
241 def smc = [:]
242 smc['SALT_MASTER_MINION_ID'] = "${saltMaster}.${clusterDomain}"
243 smc['SALT_MASTER_DEPLOY_IP'] = templateContext['default_context']['salt_master_management_address']
244 smc['DEPLOY_NETWORK_GW'] = templateContext['default_context']['deploy_network_gateway']
245 smc['DEPLOY_NETWORK_NETMASK'] = templateContext['default_context']['deploy_network_netmask']
246 smc['DNS_SERVERS'] = templateContext['default_context']['dns_server01']
247 smc['MCP_VERSION'] = "${mcpVersion}"
azvyagintsevf252b592018-08-13 18:39:14 +0300248 if (templateContext['default_context']['local_repositories'] == 'True') {
azvyagintsev3ed704f2018-07-09 15:49:27 +0300249 def localRepoIP = templateContext['default_context']['local_repo_url']
250 smc['MCP_SALT_REPO_KEY'] = "http://${localRepoIP}/public.gpg"
251 smc['MCP_SALT_REPO_URL'] = "http://${localRepoIP}/ubuntu-xenial"
252 smc['PIPELINES_FROM_ISO'] = 'false'
253 smc['PIPELINE_REPO_URL'] = "http://${localRepoIP}:8088"
254 smc['LOCAL_REPOS'] = 'true'
255 }
azvyagintsevf252b592018-08-13 18:39:14 +0300256 if (templateContext['default_context']['upstream_proxy_enabled'] == 'True') {
257 if (templateContext['default_context']['upstream_proxy_auth_enabled'] == 'True') {
azvyagintsev3ed704f2018-07-09 15:49:27 +0300258 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']
259 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']
260 } else {
261 smc['http_proxy'] = 'http://' + templateContext['default_context']['upstream_proxy_address'] + ':' + templateContext['default_context']['upstream_proxy_port']
262 smc['https_proxy'] = 'http://' + templateContext['default_context']['upstream_proxy_address'] + ':' + templateContext['default_context']['upstream_proxy_port']
263 }
264 }
265
266 for (i in common.entries(smc)) {
267 sh "sed -i 's,export ${i[0]}=.*,export ${i[0]}=${i[1]},' user_data.sh"
268 }
269
270 // create cfg config-drive
271 sh "./create-config-drive ${args}"
272 sh("mkdir output-${clusterName} && mv ${saltMaster}.${clusterDomain}-config.iso output-${clusterName}/")
273
274 // save cfg iso to artifacts
275 archiveArtifacts artifacts: "output-${clusterName}/${saltMaster}.${clusterDomain}-config.iso"
276
azvyagintsevf252b592018-08-13 18:39:14 +0300277 if (templateContext['default_context']['local_repositories'] == 'True') {
azvyagintsev3ed704f2018-07-09 15:49:27 +0300278 def aptlyServerHostname = templateContext.default_context.aptly_server_hostname
279 def user_data_script_apt_url = "https://raw.githubusercontent.com/Mirantis/mcp-common-scripts/master/config-drive/mirror_config.sh"
280 sh "wget -O mirror_config.sh ${user_data_script_apt_url}"
281
282 def smc_apt = [:]
283 smc_apt['SALT_MASTER_DEPLOY_IP'] = templateContext['default_context']['salt_master_management_address']
284 smc_apt['APTLY_DEPLOY_IP'] = templateContext['default_context']['aptly_server_deploy_address']
285 smc_apt['APTLY_DEPLOY_NETMASK'] = templateContext['default_context']['deploy_network_netmask']
286 smc_apt['APTLY_MINION_ID'] = "${aptlyServerHostname}.${clusterDomain}"
287
288 for (i in common.entries(smc_apt)) {
289 sh "sed -i \"s,export ${i[0]}=.*,export ${i[0]}=${i[1]},\" mirror_config.sh"
290 }
291
292 // create apt config-drive
293 sh "./create-config-drive --user-data mirror_config.sh --hostname ${aptlyServerHostname} ${aptlyServerHostname}.${clusterDomain}-config.iso"
294 sh("mv ${aptlyServerHostname}.${clusterDomain}-config.iso output-${clusterName}/")
295
296 // save apt iso to artifacts
297 archiveArtifacts artifacts: "output-${clusterName}/${aptlyServerHostname}.${clusterDomain}-config.iso"
298 }
299 }
300
azvyagintsevf252b592018-08-13 18:39:14 +0300301 stage('Save changes reclass model') {
302 sh(returnStatus: true, script: "tar -czf output-${clusterName}/${clusterName}.tar.gz --exclude='*@tmp' -C ${modelEnv} .")
azvyagintsev3ed704f2018-07-09 15:49:27 +0300303 archiveArtifacts artifacts: "output-${clusterName}/${clusterName}.tar.gz"
304
305
306 if (EMAIL_ADDRESS != null && EMAIL_ADDRESS != "") {
307 emailext(to: EMAIL_ADDRESS,
308 attachmentsPattern: "output-${clusterName}/*",
309 body: "Mirantis Jenkins\n\nRequested reclass model ${clusterName} has been created and attached to this email.\nEnjoy!\n\nMirantis",
310 subject: "Your Salt model ${clusterName}")
311 }
azvyagintsevf252b592018-08-13 18:39:14 +0300312 dir("output-${clusterName}") {
azvyagintsev3ed704f2018-07-09 15:49:27 +0300313 deleteDir()
314 }
315 }
316
azvyagintsevf252b592018-08-13 18:39:14 +0300317 // Fail, but leave possibility to get failed artifacts
318 if (!testResult && TEST_MODEL.toBoolean()) {
319 common.warningMsg('Test finished: FAILURE. Please check logs and\\or debug failed model manually!')
320 error('Test stage finished: FAILURE')
321 }
322
azvyagintsev3ed704f2018-07-09 15:49:27 +0300323 } catch (Throwable e) {
azvyagintsev3ed704f2018-07-09 15:49:27 +0300324 currentBuild.result = "FAILURE"
325 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
326 throw e
327 } finally {
azvyagintsevf252b592018-08-13 18:39:14 +0300328 stage('Clean workspace directories') {
329 sh(script: 'find . -mindepth 1 -delete > /dev/null || true')
azvyagintsev3ed704f2018-07-09 15:49:27 +0300330 }
331 // common.sendNotification(currentBuild.result,"",["slack"])
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200332 }
azvyagintsev3ed704f2018-07-09 15:49:27 +0300333 }
Mikhail Ivanov9f812922017-11-07 18:52:02 +0400334}