blob: 25473fb8a3440c8ba9545078619063d9b9b24882 [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
azvyagintsevb8ddb492018-09-12 14:59:45 +030016slaveNode = env.SLAVE_NODE ?: 'python&&docker'
azvyagintsevf252b592018-08-13 18:39:14 +030017
18timeout(time: 2, unit: 'HOURS') {
azvyagintsev636493c2018-09-12 17:17:05 +030019 node(slaveNode) {
20 def templateEnv = "${env.WORKSPACE}/template"
21 def modelEnv = "${env.WORKSPACE}/model"
22 def testEnv = "${env.WORKSPACE}/test"
23 def pipelineEnv = "${env.WORKSPACE}/pipelines"
Tomáš Kukrál9f6260f2017-03-29 23:58:26 +020024
azvyagintsev636493c2018-09-12 17:17:05 +030025 try {
26 def templateContext = readYaml text: COOKIECUTTER_TEMPLATE_CONTEXT
27 def mcpVersion = templateContext.default_context.mcp_version
28 def sharedReclassUrl = templateContext.default_context.shared_reclass_url
29 def clusterDomain = templateContext.default_context.cluster_domain
30 def clusterName = templateContext.default_context.cluster_name
31 def saltMaster = templateContext.default_context.salt_master_hostname
azvyagintsev636493c2018-09-12 17:17:05 +030032 def cutterEnv = "${env.WORKSPACE}/cutter"
33 def jinjaEnv = "${env.WORKSPACE}/jinja"
34 def outputDestination = "${modelEnv}/classes/cluster/${clusterName}"
35 def systemEnv = "${modelEnv}/classes/system"
36 def targetBranch = "feature/${clusterName}"
37 def templateBaseDir = "${env.WORKSPACE}/template"
38 def templateDir = "${templateEnv}/template/dir"
39 def templateOutputDir = templateBaseDir
40 def user
41 def testResult = false
42 wrap([$class: 'BuildUser']) {
43 user = env.BUILD_USER_ID
Tomáš Kukrála3f4ba72017-08-03 15:40:22 +020044 }
45
azvyagintsev636493c2018-09-12 17:17:05 +030046 currentBuild.description = clusterName
47 print("Using context:\n" + COOKIECUTTER_TEMPLATE_CONTEXT)
Richard Felkle77b0912018-01-31 17:12:23 +010048
azvyagintsev636493c2018-09-12 17:17:05 +030049 stage('Download Cookiecutter template') {
50 sh(script: 'find . -mindepth 1 -delete > /dev/null || true')
51 def cookiecutterTemplateUrl = templateContext.default_context.cookiecutter_template_url
52 def cookiecutterTemplateBranch = templateContext.default_context.cookiecutter_template_branch
53 git.checkoutGitRepository(templateEnv, cookiecutterTemplateUrl, 'master')
54 // Use refspec if exists first of all
55 if (cookiecutterTemplateBranch.toString().startsWith('refs/')) {
56 dir(templateEnv) {
57 ssh.agentSh("git fetch ${cookiecutterTemplateUrl} ${cookiecutterTemplateBranch} && git checkout FETCH_HEAD")
58 }
59 } else {
60 // Use mcpVersion git tag if not specified branch for cookiecutter-templates
61 if (cookiecutterTemplateBranch == '') {
62 cookiecutterTemplateBranch = mcpVersion
63 // Don't have nightly/testing/stable for cookiecutter-templates repo, therefore use master
64 if (["nightly", "testing", "stable"].contains(mcpVersion)) {
65 cookiecutterTemplateBranch = 'master'
66 }
67 }
68 git.changeGitBranch(templateEnv, cookiecutterTemplateBranch)
69 }
70 }
Jakub Josefa63f9862018-01-11 17:58:38 +010071
azvyagintsev636493c2018-09-12 17:17:05 +030072 stage('Create empty reclass model') {
73 dir(path: modelEnv) {
74 sh "rm -rfv .git"
75 sh "git init"
76 ssh.agentSh("git submodule add ${sharedReclassUrl} 'classes/system'")
77 }
Jakub Josefa63f9862018-01-11 17:58:38 +010078
azvyagintsev636493c2018-09-12 17:17:05 +030079 def sharedReclassBranch = templateContext.default_context.shared_reclass_branch
80 // Use refspec if exists first of all
81 if (sharedReclassBranch.toString().startsWith('refs/')) {
82 dir(systemEnv) {
83 ssh.agentSh("git fetch ${sharedReclassUrl} ${sharedReclassBranch} && git checkout FETCH_HEAD")
84 }
85 } else {
86 // Use mcpVersion git tag if not specified branch for reclass-system
87 if (sharedReclassBranch == '') {
88 sharedReclassBranch = mcpVersion
89 // Don't have nightly/testing for reclass-system repo, therefore use master
90 if (["nightly", "testing", "stable"].contains(mcpVersion)) {
91 common.warningMsg("Fetching reclass-system from master!")
92 sharedReclassBranch = 'master'
93 }
94 }
95 git.changeGitBranch(systemEnv, sharedReclassBranch)
96 }
97 git.commitGitChanges(modelEnv, "Added new shared reclass submodule", "${user}@localhost", "${user}")
98 }
Jakub Josefa63f9862018-01-11 17:58:38 +010099
Denis Egorenkof3469092018-10-17 17:05:50 +0400100 stage('Generate model') {
101 python.setupCookiecutterVirtualenv(cutterEnv)
102 python.generateModel(COOKIECUTTER_TEMPLATE_CONTEXT, 'default_context', saltMaster, cutterEnv, modelEnv, templateEnv, false)
azvyagintsev636493c2018-09-12 17:17:05 +0300103 git.commitGitChanges(modelEnv, "Create model ${clusterName}", "${user}@localhost", "${user}")
104 }
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400105
azvyagintsev636493c2018-09-12 17:17:05 +0300106 stage("Test") {
107 if (TEST_MODEL.toBoolean() && sharedReclassUrl != '') {
azvyagintsev1bcadc42018-10-05 20:13:15 +0300108 distribRevision = mcpVersion
109 if (['master'].contains(mcpVersion)) {
110 distribRevision = 'nightly'
111 }
112 if (distribRevision.contains('/')) {
113 distribRevision = distribRevision.split('/')[-1]
114 }
115 // Check if we are going to test bleeding-edge release, which doesn't have binary release yet
116 if (!common.checkRemoteBinary([apt_mk_version: distribRevision]).linux_system_repo_url) {
117 common.errorMsg("Binary release: ${distribRevision} not exist. Fallback to 'proposed'! ")
118 distribRevision = 'proposed'
119 }
azvyagintsev636493c2018-09-12 17:17:05 +0300120 sh("cp -r ${modelEnv} ${testEnv}")
121 def DockerCName = "${env.JOB_NAME.toLowerCase()}_${env.BUILD_TAG.toLowerCase()}"
azvyagintsev1bcadc42018-10-05 20:13:15 +0300122 common.infoMsg("Attempt to run test against distribRevision: ${distribRevision}")
Denis Egorenko032b8ca2018-09-13 17:00:23 +0400123 try {
124 def config = [
azvyagintsev7cdcc7a2018-09-25 16:47:24 +0300125 'dockerHostname' : "${saltMaster}.${clusterDomain}",
126 'reclassEnv' : testEnv,
azvyagintsev1bcadc42018-10-05 20:13:15 +0300127 'distribRevision' : distribRevision,
Denis Egorenko032b8ca2018-09-13 17:00:23 +0400128 'dockerContainerName': DockerCName,
azvyagintsev7cdcc7a2018-09-25 16:47:24 +0300129 'testContext' : 'salt-model-node'
Denis Egorenko032b8ca2018-09-13 17:00:23 +0400130 ]
131 testResult = saltModelTesting.testNode(config)
azvyagintsev636493c2018-09-12 17:17:05 +0300132 common.infoMsg("Test finished: SUCCESS")
Denis Egorenko032b8ca2018-09-13 17:00:23 +0400133 } catch (Exception ex) {
134 common.warningMsg("Test finished: FAILED")
135 testResult = false
azvyagintsev636493c2018-09-12 17:17:05 +0300136 }
137 } else {
138 common.warningMsg("Test stage has been skipped!")
139 }
140 }
141 stage("Generate config drives") {
142 // apt package genisoimage is required for this stage
143
144 // download create-config-drive
145 // FIXME: that should be refactored, to use git clone - to be able download it from custom repo.
Ivan Berezovskiy95ec9272018-09-18 16:41:18 +0400146 def mcpCommonScriptsBranch = templateContext['default_context']['mcp_common_scripts_branch']
azvyagintsev636493c2018-09-12 17:17:05 +0300147 if (mcpCommonScriptsBranch == '') {
148 mcpCommonScriptsBranch = mcpVersion
149 // Don't have n/t/s for mcp-common-scripts repo, therefore use master
150 if (["nightly", "testing", "stable"].contains(mcpVersion)) {
151 common.warningMsg("Fetching mcp-common-scripts from master!")
152 mcpCommonScriptsBranch = 'master'
153 }
154 }
Ivan Berezovskiy95ec9272018-09-18 16:41:18 +0400155
Roman Vyalovf0c596e2018-10-01 17:56:09 +0300156 def commonScriptsRepoUrl = 'https://gerrit.mcp.mirantis.com/mcp/mcp-common-scripts'
Ivan Berezovskiy95ec9272018-09-18 16:41:18 +0400157 checkout([
azvyagintsev7cdcc7a2018-09-25 16:47:24 +0300158 $class : 'GitSCM',
159 branches : [[name: 'FETCH_HEAD'],],
160 extensions : [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'mcp-common-scripts']],
161 userRemoteConfigs: [[url: commonScriptsRepoUrl, refspec: mcpCommonScriptsBranch],],
Ivan Berezovskiy95ec9272018-09-18 16:41:18 +0400162 ])
163
164 sh "cp mcp-common-scripts/config-drive/create_config_drive.sh create-config-drive && chmod +x create-config-drive"
Ivan Berezovskiy3379c7a2018-09-14 19:02:46 +0400165 sh "[ -f mcp-common-scripts/config-drive/master_config.sh ] && cp mcp-common-scripts/config-drive/master_config.sh user_data || cp mcp-common-scripts/config-drive/master_config.yaml user_data"
azvyagintsev636493c2018-09-12 17:17:05 +0300166
167 sh "git clone --mirror https://github.com/Mirantis/mk-pipelines.git ${pipelineEnv}/mk-pipelines"
168 sh "git clone --mirror https://github.com/Mirantis/pipeline-library.git ${pipelineEnv}/pipeline-library"
Ivan Berezovskiy3379c7a2018-09-14 19:02:46 +0400169 args = "--user-data user_data --hostname ${saltMaster} --model ${modelEnv} --mk-pipelines ${pipelineEnv}/mk-pipelines/ --pipeline-library ${pipelineEnv}/pipeline-library/ ${saltMaster}.${clusterDomain}-config.iso"
azvyagintsev636493c2018-09-12 17:17:05 +0300170
171 // load data from model
172 def smc = [:]
173 smc['SALT_MASTER_MINION_ID'] = "${saltMaster}.${clusterDomain}"
174 smc['SALT_MASTER_DEPLOY_IP'] = templateContext['default_context']['salt_master_management_address']
175 smc['DEPLOY_NETWORK_GW'] = templateContext['default_context']['deploy_network_gateway']
176 smc['DEPLOY_NETWORK_NETMASK'] = templateContext['default_context']['deploy_network_netmask']
177 if (templateContext['default_context'].get('deploy_network_mtu')) {
178 smc['DEPLOY_NETWORK_MTU'] = templateContext['default_context']['deploy_network_mtu']
179 }
180 smc['DNS_SERVERS'] = templateContext['default_context']['dns_server01']
181 smc['MCP_VERSION'] = "${mcpVersion}"
182 if (templateContext['default_context']['local_repositories'] == 'True') {
183 def localRepoIP = templateContext['default_context']['local_repo_url']
184 smc['MCP_SALT_REPO_KEY'] = "http://${localRepoIP}/public.gpg"
185 smc['MCP_SALT_REPO_URL'] = "http://${localRepoIP}/ubuntu-xenial"
186 smc['PIPELINES_FROM_ISO'] = 'false'
187 smc['PIPELINE_REPO_URL'] = "http://${localRepoIP}:8088"
188 smc['LOCAL_REPOS'] = 'true'
189 }
190 if (templateContext['default_context']['upstream_proxy_enabled'] == 'True') {
191 if (templateContext['default_context']['upstream_proxy_auth_enabled'] == 'True') {
192 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']
193 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']
194 } else {
195 smc['http_proxy'] = 'http://' + templateContext['default_context']['upstream_proxy_address'] + ':' + templateContext['default_context']['upstream_proxy_port']
196 smc['https_proxy'] = 'http://' + templateContext['default_context']['upstream_proxy_address'] + ':' + templateContext['default_context']['upstream_proxy_port']
197 }
198 }
199
200 for (i in common.entries(smc)) {
Ivan Berezovskiy3379c7a2018-09-14 19:02:46 +0400201 sh "sed -i 's,${i[0]}=.*,${i[0]}=${i[1]},' user_data"
azvyagintsev636493c2018-09-12 17:17:05 +0300202 }
203
204 // create cfg config-drive
205 sh "./create-config-drive ${args}"
206 sh("mkdir output-${clusterName} && mv ${saltMaster}.${clusterDomain}-config.iso output-${clusterName}/")
207
208 // save cfg iso to artifacts
209 archiveArtifacts artifacts: "output-${clusterName}/${saltMaster}.${clusterDomain}-config.iso"
210
211 if (templateContext['default_context']['local_repositories'] == 'True') {
212 def aptlyServerHostname = templateContext.default_context.aptly_server_hostname
Ivan Berezovskiyffff3302018-09-21 19:02:39 +0400213 sh "[ -f mcp-common-scripts/config-drive/mirror_config.yaml ] && cp mcp-common-scripts/config-drive/mirror_config.yaml mirror_config || cp mcp-common-scripts/config-drive/mirror_config.sh mirror_config"
azvyagintsev636493c2018-09-12 17:17:05 +0300214
215 def smc_apt = [:]
216 smc_apt['SALT_MASTER_DEPLOY_IP'] = templateContext['default_context']['salt_master_management_address']
217 smc_apt['APTLY_DEPLOY_IP'] = templateContext['default_context']['aptly_server_deploy_address']
218 smc_apt['APTLY_DEPLOY_NETMASK'] = templateContext['default_context']['deploy_network_netmask']
219 smc_apt['APTLY_MINION_ID'] = "${aptlyServerHostname}.${clusterDomain}"
220
221 for (i in common.entries(smc_apt)) {
Ivan Berezovskiyffff3302018-09-21 19:02:39 +0400222 sh "sed -i \"s,export ${i[0]}=.*,export ${i[0]}=${i[1]},\" mirror_config"
azvyagintsev636493c2018-09-12 17:17:05 +0300223 }
224
225 // create apt config-drive
Ivan Berezovskiyffff3302018-09-21 19:02:39 +0400226 sh "./create-config-drive --user-data mirror_config --hostname ${aptlyServerHostname} ${aptlyServerHostname}.${clusterDomain}-config.iso"
azvyagintsev636493c2018-09-12 17:17:05 +0300227 sh("mv ${aptlyServerHostname}.${clusterDomain}-config.iso output-${clusterName}/")
228
229 // save apt iso to artifacts
230 archiveArtifacts artifacts: "output-${clusterName}/${aptlyServerHostname}.${clusterDomain}-config.iso"
231 }
232 }
233
234 stage('Save changes reclass model') {
235 sh(returnStatus: true, script: "tar -czf output-${clusterName}/${clusterName}.tar.gz --exclude='*@tmp' -C ${modelEnv} .")
236 archiveArtifacts artifacts: "output-${clusterName}/${clusterName}.tar.gz"
237
azvyagintsev636493c2018-09-12 17:17:05 +0300238 if (EMAIL_ADDRESS != null && EMAIL_ADDRESS != "") {
239 emailext(to: EMAIL_ADDRESS,
240 attachmentsPattern: "output-${clusterName}/*",
241 body: "Mirantis Jenkins\n\nRequested reclass model ${clusterName} has been created and attached to this email.\nEnjoy!\n\nMirantis",
242 subject: "Your Salt model ${clusterName}")
243 }
244 dir("output-${clusterName}") {
245 deleteDir()
246 }
247 }
248
249 // Fail, but leave possibility to get failed artifacts
250 if (!testResult && TEST_MODEL.toBoolean()) {
251 common.warningMsg('Test finished: FAILURE. Please check logs and\\or debug failed model manually!')
252 error('Test stage finished: FAILURE')
253 }
254
255 } catch (Throwable e) {
256 currentBuild.result = "FAILURE"
257 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
258 throw e
259 } finally {
260 stage('Clean workspace directories') {
261 sh(script: 'find . -mindepth 1 -delete > /dev/null || true')
262 }
263 // common.sendNotification(currentBuild.result,"",["slack"])
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400264 }
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200265 }
Mikhail Ivanov9f812922017-11-07 18:52:02 +0400266}