blob: 2cbbad033dea080b7628bce9eaaaaea5a0103a05 [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
Sergey Galkin8b87f6e2018-10-24 18:40:13 +04007 * CREDENTIALS_ID Credentials id for git
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'
Sergey Galkin8b87f6e2018-10-24 18:40:13 +040017gerritCredentials = env.CREDENTIALS_ID ?: 'gerrit'
azvyagintsevf252b592018-08-13 18:39:14 +030018
19timeout(time: 2, unit: 'HOURS') {
azvyagintsev636493c2018-09-12 17:17:05 +030020 node(slaveNode) {
21 def templateEnv = "${env.WORKSPACE}/template"
22 def modelEnv = "${env.WORKSPACE}/model"
23 def testEnv = "${env.WORKSPACE}/test"
24 def pipelineEnv = "${env.WORKSPACE}/pipelines"
Tomáš Kukrál9f6260f2017-03-29 23:58:26 +020025
azvyagintsev636493c2018-09-12 17:17:05 +030026 try {
27 def templateContext = readYaml text: COOKIECUTTER_TEMPLATE_CONTEXT
28 def mcpVersion = templateContext.default_context.mcp_version
29 def sharedReclassUrl = templateContext.default_context.shared_reclass_url
30 def clusterDomain = templateContext.default_context.cluster_domain
31 def clusterName = templateContext.default_context.cluster_name
32 def saltMaster = templateContext.default_context.salt_master_hostname
azvyagintsev636493c2018-09-12 17:17:05 +030033 def cutterEnv = "${env.WORKSPACE}/cutter"
34 def jinjaEnv = "${env.WORKSPACE}/jinja"
35 def outputDestination = "${modelEnv}/classes/cluster/${clusterName}"
36 def systemEnv = "${modelEnv}/classes/system"
37 def targetBranch = "feature/${clusterName}"
38 def templateBaseDir = "${env.WORKSPACE}/template"
39 def templateDir = "${templateEnv}/template/dir"
40 def templateOutputDir = templateBaseDir
41 def user
42 def testResult = false
43 wrap([$class: 'BuildUser']) {
44 user = env.BUILD_USER_ID
Tomáš Kukrála3f4ba72017-08-03 15:40:22 +020045 }
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
Sergey Galkin8b87f6e2018-10-24 18:40:13 +040053 git.checkoutGitRepository(templateEnv, cookiecutterTemplateUrl, 'master', gerritCredentials)
azvyagintsev636493c2018-09-12 17:17:05 +030054 // Use refspec if exists first of all
55 if (cookiecutterTemplateBranch.toString().startsWith('refs/')) {
56 dir(templateEnv) {
Sergey Galkin8b87f6e2018-10-24 18:40:13 +040057 withCredentials(gerritCredentials){
58 ssh.agentSh("git fetch ${cookiecutterTemplateUrl} ${cookiecutterTemplateBranch} && git checkout FETCH_HEAD")
59 }
azvyagintsev636493c2018-09-12 17:17:05 +030060 }
61 } else {
62 // Use mcpVersion git tag if not specified branch for cookiecutter-templates
63 if (cookiecutterTemplateBranch == '') {
64 cookiecutterTemplateBranch = mcpVersion
65 // Don't have nightly/testing/stable for cookiecutter-templates repo, therefore use master
66 if (["nightly", "testing", "stable"].contains(mcpVersion)) {
67 cookiecutterTemplateBranch = 'master'
68 }
69 }
70 git.changeGitBranch(templateEnv, cookiecutterTemplateBranch)
71 }
72 }
Jakub Josefa63f9862018-01-11 17:58:38 +010073
azvyagintsev636493c2018-09-12 17:17:05 +030074 stage('Create empty reclass model') {
75 dir(path: modelEnv) {
76 sh "rm -rfv .git"
77 sh "git init"
78 ssh.agentSh("git submodule add ${sharedReclassUrl} 'classes/system'")
79 }
Jakub Josefa63f9862018-01-11 17:58:38 +010080
azvyagintsev636493c2018-09-12 17:17:05 +030081 def sharedReclassBranch = templateContext.default_context.shared_reclass_branch
82 // Use refspec if exists first of all
83 if (sharedReclassBranch.toString().startsWith('refs/')) {
84 dir(systemEnv) {
85 ssh.agentSh("git fetch ${sharedReclassUrl} ${sharedReclassBranch} && git checkout FETCH_HEAD")
86 }
87 } else {
88 // Use mcpVersion git tag if not specified branch for reclass-system
89 if (sharedReclassBranch == '') {
90 sharedReclassBranch = mcpVersion
91 // Don't have nightly/testing for reclass-system repo, therefore use master
92 if (["nightly", "testing", "stable"].contains(mcpVersion)) {
93 common.warningMsg("Fetching reclass-system from master!")
94 sharedReclassBranch = 'master'
95 }
96 }
97 git.changeGitBranch(systemEnv, sharedReclassBranch)
98 }
99 git.commitGitChanges(modelEnv, "Added new shared reclass submodule", "${user}@localhost", "${user}")
100 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100101
Denis Egorenkof3469092018-10-17 17:05:50 +0400102 stage('Generate model') {
103 python.setupCookiecutterVirtualenv(cutterEnv)
104 python.generateModel(COOKIECUTTER_TEMPLATE_CONTEXT, 'default_context', saltMaster, cutterEnv, modelEnv, templateEnv, false)
azvyagintsev636493c2018-09-12 17:17:05 +0300105 git.commitGitChanges(modelEnv, "Create model ${clusterName}", "${user}@localhost", "${user}")
106 }
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400107
azvyagintsev636493c2018-09-12 17:17:05 +0300108 stage("Test") {
109 if (TEST_MODEL.toBoolean() && sharedReclassUrl != '') {
azvyagintsev1bcadc42018-10-05 20:13:15 +0300110 distribRevision = mcpVersion
111 if (['master'].contains(mcpVersion)) {
112 distribRevision = 'nightly'
113 }
114 if (distribRevision.contains('/')) {
115 distribRevision = distribRevision.split('/')[-1]
116 }
117 // Check if we are going to test bleeding-edge release, which doesn't have binary release yet
118 if (!common.checkRemoteBinary([apt_mk_version: distribRevision]).linux_system_repo_url) {
119 common.errorMsg("Binary release: ${distribRevision} not exist. Fallback to 'proposed'! ")
120 distribRevision = 'proposed'
121 }
azvyagintsev636493c2018-09-12 17:17:05 +0300122 sh("cp -r ${modelEnv} ${testEnv}")
123 def DockerCName = "${env.JOB_NAME.toLowerCase()}_${env.BUILD_TAG.toLowerCase()}"
azvyagintsev1bcadc42018-10-05 20:13:15 +0300124 common.infoMsg("Attempt to run test against distribRevision: ${distribRevision}")
Denis Egorenko032b8ca2018-09-13 17:00:23 +0400125 try {
126 def config = [
azvyagintsev7cdcc7a2018-09-25 16:47:24 +0300127 'dockerHostname' : "${saltMaster}.${clusterDomain}",
128 'reclassEnv' : testEnv,
azvyagintsev1bcadc42018-10-05 20:13:15 +0300129 'distribRevision' : distribRevision,
Denis Egorenko032b8ca2018-09-13 17:00:23 +0400130 'dockerContainerName': DockerCName,
azvyagintsev7cdcc7a2018-09-25 16:47:24 +0300131 'testContext' : 'salt-model-node'
Denis Egorenko032b8ca2018-09-13 17:00:23 +0400132 ]
133 testResult = saltModelTesting.testNode(config)
azvyagintsev636493c2018-09-12 17:17:05 +0300134 common.infoMsg("Test finished: SUCCESS")
Denis Egorenko032b8ca2018-09-13 17:00:23 +0400135 } catch (Exception ex) {
136 common.warningMsg("Test finished: FAILED")
137 testResult = false
azvyagintsev636493c2018-09-12 17:17:05 +0300138 }
139 } else {
140 common.warningMsg("Test stage has been skipped!")
141 }
142 }
143 stage("Generate config drives") {
144 // apt package genisoimage is required for this stage
145
146 // download create-config-drive
147 // FIXME: that should be refactored, to use git clone - to be able download it from custom repo.
Ivan Berezovskiy95ec9272018-09-18 16:41:18 +0400148 def mcpCommonScriptsBranch = templateContext['default_context']['mcp_common_scripts_branch']
azvyagintsev636493c2018-09-12 17:17:05 +0300149 if (mcpCommonScriptsBranch == '') {
150 mcpCommonScriptsBranch = mcpVersion
151 // Don't have n/t/s for mcp-common-scripts repo, therefore use master
152 if (["nightly", "testing", "stable"].contains(mcpVersion)) {
153 common.warningMsg("Fetching mcp-common-scripts from master!")
154 mcpCommonScriptsBranch = 'master'
155 }
156 }
Ivan Berezovskiy95ec9272018-09-18 16:41:18 +0400157
Roman Vyalovf0c596e2018-10-01 17:56:09 +0300158 def commonScriptsRepoUrl = 'https://gerrit.mcp.mirantis.com/mcp/mcp-common-scripts'
Ivan Berezovskiy95ec9272018-09-18 16:41:18 +0400159 checkout([
azvyagintsev7cdcc7a2018-09-25 16:47:24 +0300160 $class : 'GitSCM',
161 branches : [[name: 'FETCH_HEAD'],],
162 extensions : [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'mcp-common-scripts']],
163 userRemoteConfigs: [[url: commonScriptsRepoUrl, refspec: mcpCommonScriptsBranch],],
Ivan Berezovskiy95ec9272018-09-18 16:41:18 +0400164 ])
165
166 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 +0400167 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 +0300168
169 sh "git clone --mirror https://github.com/Mirantis/mk-pipelines.git ${pipelineEnv}/mk-pipelines"
170 sh "git clone --mirror https://github.com/Mirantis/pipeline-library.git ${pipelineEnv}/pipeline-library"
Ivan Berezovskiy3379c7a2018-09-14 19:02:46 +0400171 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 +0300172
173 // load data from model
174 def smc = [:]
175 smc['SALT_MASTER_MINION_ID'] = "${saltMaster}.${clusterDomain}"
176 smc['SALT_MASTER_DEPLOY_IP'] = templateContext['default_context']['salt_master_management_address']
177 smc['DEPLOY_NETWORK_GW'] = templateContext['default_context']['deploy_network_gateway']
178 smc['DEPLOY_NETWORK_NETMASK'] = templateContext['default_context']['deploy_network_netmask']
179 if (templateContext['default_context'].get('deploy_network_mtu')) {
180 smc['DEPLOY_NETWORK_MTU'] = templateContext['default_context']['deploy_network_mtu']
181 }
182 smc['DNS_SERVERS'] = templateContext['default_context']['dns_server01']
183 smc['MCP_VERSION'] = "${mcpVersion}"
184 if (templateContext['default_context']['local_repositories'] == 'True') {
185 def localRepoIP = templateContext['default_context']['local_repo_url']
186 smc['MCP_SALT_REPO_KEY'] = "http://${localRepoIP}/public.gpg"
187 smc['MCP_SALT_REPO_URL'] = "http://${localRepoIP}/ubuntu-xenial"
188 smc['PIPELINES_FROM_ISO'] = 'false'
189 smc['PIPELINE_REPO_URL'] = "http://${localRepoIP}:8088"
190 smc['LOCAL_REPOS'] = 'true'
191 }
192 if (templateContext['default_context']['upstream_proxy_enabled'] == 'True') {
193 if (templateContext['default_context']['upstream_proxy_auth_enabled'] == 'True') {
194 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']
195 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']
196 } else {
197 smc['http_proxy'] = 'http://' + templateContext['default_context']['upstream_proxy_address'] + ':' + templateContext['default_context']['upstream_proxy_port']
198 smc['https_proxy'] = 'http://' + templateContext['default_context']['upstream_proxy_address'] + ':' + templateContext['default_context']['upstream_proxy_port']
199 }
200 }
201
202 for (i in common.entries(smc)) {
Ivan Berezovskiy3379c7a2018-09-14 19:02:46 +0400203 sh "sed -i 's,${i[0]}=.*,${i[0]}=${i[1]},' user_data"
azvyagintsev636493c2018-09-12 17:17:05 +0300204 }
205
206 // create cfg config-drive
207 sh "./create-config-drive ${args}"
208 sh("mkdir output-${clusterName} && mv ${saltMaster}.${clusterDomain}-config.iso output-${clusterName}/")
209
210 // save cfg iso to artifacts
211 archiveArtifacts artifacts: "output-${clusterName}/${saltMaster}.${clusterDomain}-config.iso"
212
213 if (templateContext['default_context']['local_repositories'] == 'True') {
214 def aptlyServerHostname = templateContext.default_context.aptly_server_hostname
Ivan Berezovskiyffff3302018-09-21 19:02:39 +0400215 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 +0300216
217 def smc_apt = [:]
218 smc_apt['SALT_MASTER_DEPLOY_IP'] = templateContext['default_context']['salt_master_management_address']
219 smc_apt['APTLY_DEPLOY_IP'] = templateContext['default_context']['aptly_server_deploy_address']
220 smc_apt['APTLY_DEPLOY_NETMASK'] = templateContext['default_context']['deploy_network_netmask']
221 smc_apt['APTLY_MINION_ID'] = "${aptlyServerHostname}.${clusterDomain}"
222
223 for (i in common.entries(smc_apt)) {
Ivan Berezovskiyffff3302018-09-21 19:02:39 +0400224 sh "sed -i \"s,export ${i[0]}=.*,export ${i[0]}=${i[1]},\" mirror_config"
azvyagintsev636493c2018-09-12 17:17:05 +0300225 }
226
227 // create apt config-drive
Ivan Berezovskiyffff3302018-09-21 19:02:39 +0400228 sh "./create-config-drive --user-data mirror_config --hostname ${aptlyServerHostname} ${aptlyServerHostname}.${clusterDomain}-config.iso"
azvyagintsev636493c2018-09-12 17:17:05 +0300229 sh("mv ${aptlyServerHostname}.${clusterDomain}-config.iso output-${clusterName}/")
230
231 // save apt iso to artifacts
232 archiveArtifacts artifacts: "output-${clusterName}/${aptlyServerHostname}.${clusterDomain}-config.iso"
233 }
234 }
235
236 stage('Save changes reclass model') {
237 sh(returnStatus: true, script: "tar -czf output-${clusterName}/${clusterName}.tar.gz --exclude='*@tmp' -C ${modelEnv} .")
238 archiveArtifacts artifacts: "output-${clusterName}/${clusterName}.tar.gz"
239
azvyagintsev636493c2018-09-12 17:17:05 +0300240 if (EMAIL_ADDRESS != null && EMAIL_ADDRESS != "") {
241 emailext(to: EMAIL_ADDRESS,
242 attachmentsPattern: "output-${clusterName}/*",
243 body: "Mirantis Jenkins\n\nRequested reclass model ${clusterName} has been created and attached to this email.\nEnjoy!\n\nMirantis",
244 subject: "Your Salt model ${clusterName}")
245 }
246 dir("output-${clusterName}") {
247 deleteDir()
248 }
249 }
250
251 // Fail, but leave possibility to get failed artifacts
252 if (!testResult && TEST_MODEL.toBoolean()) {
253 common.warningMsg('Test finished: FAILURE. Please check logs and\\or debug failed model manually!')
254 error('Test stage finished: FAILURE')
255 }
256
257 } catch (Throwable e) {
258 currentBuild.result = "FAILURE"
259 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
260 throw e
261 } finally {
262 stage('Clean workspace directories') {
263 sh(script: 'find . -mindepth 1 -delete > /dev/null || true')
264 }
265 // common.sendNotification(currentBuild.result,"",["slack"])
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400266 }
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200267 }
Mikhail Ivanov9f812922017-11-07 18:52:02 +0400268}