blob: 0d86f2204863bcc97f5ee43fb8ef2bff8243ecd5 [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ál7ded3642017-03-27 15:52:51 +020014
azvyagintsevb8ddb492018-09-12 14:59:45 +030015slaveNode = env.SLAVE_NODE ?: 'python&&docker'
Sergey Galkin8b87f6e2018-10-24 18:40:13 +040016gerritCredentials = env.CREDENTIALS_ID ?: 'gerrit'
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 }
azvyagintsev636493c2018-09-12 17:17:05 +030045 currentBuild.description = clusterName
46 print("Using context:\n" + COOKIECUTTER_TEMPLATE_CONTEXT)
Richard Felkle77b0912018-01-31 17:12:23 +010047
azvyagintsev636493c2018-09-12 17:17:05 +030048 stage('Download Cookiecutter template') {
49 sh(script: 'find . -mindepth 1 -delete > /dev/null || true')
50 def cookiecutterTemplateUrl = templateContext.default_context.cookiecutter_template_url
51 def cookiecutterTemplateBranch = templateContext.default_context.cookiecutter_template_branch
Leontii Istomin7cde6e22018-10-24 17:16:37 +020052 // Use mcpVersion git tag if not specified branch for cookiecutter-templates
53 if (cookiecutterTemplateBranch == '') {
54 cookiecutterTemplateBranch = mcpVersion
55 // Don't have nightly/testing/stable for cookiecutter-templates repo, therefore use master
56 if (["nightly", "testing", "stable"].contains(mcpVersion)) {
57 cookiecutterTemplateBranch = 'master'
azvyagintsev636493c2018-09-12 17:17:05 +030058 }
azvyagintsev636493c2018-09-12 17:17:05 +030059 }
Leontii Istomin7cde6e22018-10-24 17:16:37 +020060 checkout([
61 $class : 'GitSCM',
62 branches : [[name: 'FETCH_HEAD'],],
63 extensions : [[$class: 'RelativeTargetDirectory', relativeTargetDir: templateEnv]],
64 userRemoteConfigs: [[url: cookiecutterTemplateUrl, refspec: cookiecutterTemplateBranch, credentialsId: gerritCredentials],],
65 ])
azvyagintsev636493c2018-09-12 17:17:05 +030066 }
azvyagintsev636493c2018-09-12 17:17:05 +030067 stage('Create empty reclass model') {
68 dir(path: modelEnv) {
69 sh "rm -rfv .git"
70 sh "git init"
Leontii Istomin7cde6e22018-10-24 17:16:37 +020071 if (sharedReclassUrl.startsWith("ssh")){
72 error("You can't use ssh proto to fetch reclass-system. Please use https protocol")
73 } else {
74 sh "git submodule add ${sharedReclassUrl} 'classes/system'"
75 }
azvyagintsev636493c2018-09-12 17:17:05 +030076 }
Jakub Josefa63f9862018-01-11 17:58:38 +010077
azvyagintsev636493c2018-09-12 17:17:05 +030078 def sharedReclassBranch = templateContext.default_context.shared_reclass_branch
Leontii Istomin7cde6e22018-10-24 17:16:37 +020079 // Use mcpVersion git tag if not specified branch for reclass-system
80 if (sharedReclassBranch == '') {
81 sharedReclassBranch = mcpVersion
82 // Don't have nightly/testing for reclass-system repo, therefore use master
83 if (["nightly", "testing", "stable"].contains(mcpVersion)) {
84 common.warningMsg("Fetching reclass-system from master!")
85 sharedReclassBranch = 'master'
azvyagintsev636493c2018-09-12 17:17:05 +030086 }
azvyagintsev636493c2018-09-12 17:17:05 +030087 }
Leontii Istomin7cde6e22018-10-24 17:16:37 +020088 checkout([
89 $class : 'GitSCM',
90 branches : [[name: 'FETCH_HEAD'],],
91 extensions : [[$class: 'RelativeTargetDirectory', relativeTargetDir: systemEnv]],
92 userRemoteConfigs: [[url: sharedReclassUrl, refspec: sharedReclassBranch, credentialsId: gerritCredentials],],
93 ])
azvyagintsev636493c2018-09-12 17:17:05 +030094 git.commitGitChanges(modelEnv, "Added new shared reclass submodule", "${user}@localhost", "${user}")
95 }
Jakub Josefa63f9862018-01-11 17:58:38 +010096
Denis Egorenkof3469092018-10-17 17:05:50 +040097 stage('Generate model') {
98 python.setupCookiecutterVirtualenv(cutterEnv)
99 python.generateModel(COOKIECUTTER_TEMPLATE_CONTEXT, 'default_context', saltMaster, cutterEnv, modelEnv, templateEnv, false)
azvyagintsev636493c2018-09-12 17:17:05 +0300100 git.commitGitChanges(modelEnv, "Create model ${clusterName}", "${user}@localhost", "${user}")
101 }
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400102
azvyagintsev636493c2018-09-12 17:17:05 +0300103 stage("Test") {
104 if (TEST_MODEL.toBoolean() && sharedReclassUrl != '') {
azvyagintsev1bcadc42018-10-05 20:13:15 +0300105 distribRevision = mcpVersion
106 if (['master'].contains(mcpVersion)) {
107 distribRevision = 'nightly'
108 }
109 if (distribRevision.contains('/')) {
110 distribRevision = distribRevision.split('/')[-1]
111 }
112 // Check if we are going to test bleeding-edge release, which doesn't have binary release yet
113 if (!common.checkRemoteBinary([apt_mk_version: distribRevision]).linux_system_repo_url) {
114 common.errorMsg("Binary release: ${distribRevision} not exist. Fallback to 'proposed'! ")
115 distribRevision = 'proposed'
116 }
azvyagintsev636493c2018-09-12 17:17:05 +0300117 sh("cp -r ${modelEnv} ${testEnv}")
118 def DockerCName = "${env.JOB_NAME.toLowerCase()}_${env.BUILD_TAG.toLowerCase()}"
azvyagintsev1bcadc42018-10-05 20:13:15 +0300119 common.infoMsg("Attempt to run test against distribRevision: ${distribRevision}")
Denis Egorenko032b8ca2018-09-13 17:00:23 +0400120 try {
121 def config = [
azvyagintsev7cdcc7a2018-09-25 16:47:24 +0300122 'dockerHostname' : "${saltMaster}.${clusterDomain}",
123 'reclassEnv' : testEnv,
azvyagintsev1bcadc42018-10-05 20:13:15 +0300124 'distribRevision' : distribRevision,
Denis Egorenko032b8ca2018-09-13 17:00:23 +0400125 'dockerContainerName': DockerCName,
azvyagintsev7cdcc7a2018-09-25 16:47:24 +0300126 'testContext' : 'salt-model-node'
Denis Egorenko032b8ca2018-09-13 17:00:23 +0400127 ]
128 testResult = saltModelTesting.testNode(config)
azvyagintsev636493c2018-09-12 17:17:05 +0300129 common.infoMsg("Test finished: SUCCESS")
Denis Egorenko032b8ca2018-09-13 17:00:23 +0400130 } catch (Exception ex) {
131 common.warningMsg("Test finished: FAILED")
132 testResult = false
azvyagintsev636493c2018-09-12 17:17:05 +0300133 }
134 } else {
135 common.warningMsg("Test stage has been skipped!")
136 }
137 }
138 stage("Generate config drives") {
139 // apt package genisoimage is required for this stage
140
141 // download create-config-drive
142 // FIXME: that should be refactored, to use git clone - to be able download it from custom repo.
Ivan Berezovskiy95ec9272018-09-18 16:41:18 +0400143 def mcpCommonScriptsBranch = templateContext['default_context']['mcp_common_scripts_branch']
azvyagintsev636493c2018-09-12 17:17:05 +0300144 if (mcpCommonScriptsBranch == '') {
145 mcpCommonScriptsBranch = mcpVersion
146 // Don't have n/t/s for mcp-common-scripts repo, therefore use master
147 if (["nightly", "testing", "stable"].contains(mcpVersion)) {
148 common.warningMsg("Fetching mcp-common-scripts from master!")
149 mcpCommonScriptsBranch = 'master'
150 }
151 }
Leontii Istomin7cde6e22018-10-24 17:16:37 +0200152 def commonScriptsRepoUrl = 'ssh://gerrit.mcp.mirantis.com:29418/mcp/mcp-common-scripts'
Ivan Berezovskiy95ec9272018-09-18 16:41:18 +0400153 checkout([
azvyagintsev7cdcc7a2018-09-25 16:47:24 +0300154 $class : 'GitSCM',
155 branches : [[name: 'FETCH_HEAD'],],
156 extensions : [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'mcp-common-scripts']],
Leontii Istomin7cde6e22018-10-24 17:16:37 +0200157 userRemoteConfigs: [[url: commonScriptsRepoUrl, refspec: mcpCommonScriptsBranch, credentialsId: gerritCredentials],],
Ivan Berezovskiy95ec9272018-09-18 16:41:18 +0400158 ])
159
160 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 +0400161 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 +0300162
163 sh "git clone --mirror https://github.com/Mirantis/mk-pipelines.git ${pipelineEnv}/mk-pipelines"
164 sh "git clone --mirror https://github.com/Mirantis/pipeline-library.git ${pipelineEnv}/pipeline-library"
Ivan Berezovskiy3379c7a2018-09-14 19:02:46 +0400165 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 +0300166
167 // load data from model
168 def smc = [:]
169 smc['SALT_MASTER_MINION_ID'] = "${saltMaster}.${clusterDomain}"
170 smc['SALT_MASTER_DEPLOY_IP'] = templateContext['default_context']['salt_master_management_address']
171 smc['DEPLOY_NETWORK_GW'] = templateContext['default_context']['deploy_network_gateway']
172 smc['DEPLOY_NETWORK_NETMASK'] = templateContext['default_context']['deploy_network_netmask']
173 if (templateContext['default_context'].get('deploy_network_mtu')) {
174 smc['DEPLOY_NETWORK_MTU'] = templateContext['default_context']['deploy_network_mtu']
175 }
176 smc['DNS_SERVERS'] = templateContext['default_context']['dns_server01']
177 smc['MCP_VERSION'] = "${mcpVersion}"
178 if (templateContext['default_context']['local_repositories'] == 'True') {
179 def localRepoIP = templateContext['default_context']['local_repo_url']
180 smc['MCP_SALT_REPO_KEY'] = "http://${localRepoIP}/public.gpg"
181 smc['MCP_SALT_REPO_URL'] = "http://${localRepoIP}/ubuntu-xenial"
182 smc['PIPELINES_FROM_ISO'] = 'false'
183 smc['PIPELINE_REPO_URL'] = "http://${localRepoIP}:8088"
184 smc['LOCAL_REPOS'] = 'true'
185 }
186 if (templateContext['default_context']['upstream_proxy_enabled'] == 'True') {
187 if (templateContext['default_context']['upstream_proxy_auth_enabled'] == 'True') {
188 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']
189 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']
190 } else {
191 smc['http_proxy'] = 'http://' + templateContext['default_context']['upstream_proxy_address'] + ':' + templateContext['default_context']['upstream_proxy_port']
192 smc['https_proxy'] = 'http://' + templateContext['default_context']['upstream_proxy_address'] + ':' + templateContext['default_context']['upstream_proxy_port']
193 }
194 }
195
196 for (i in common.entries(smc)) {
Ivan Berezovskiy3379c7a2018-09-14 19:02:46 +0400197 sh "sed -i 's,${i[0]}=.*,${i[0]}=${i[1]},' user_data"
azvyagintsev636493c2018-09-12 17:17:05 +0300198 }
199
200 // create cfg config-drive
201 sh "./create-config-drive ${args}"
202 sh("mkdir output-${clusterName} && mv ${saltMaster}.${clusterDomain}-config.iso output-${clusterName}/")
203
204 // save cfg iso to artifacts
205 archiveArtifacts artifacts: "output-${clusterName}/${saltMaster}.${clusterDomain}-config.iso"
206
207 if (templateContext['default_context']['local_repositories'] == 'True') {
208 def aptlyServerHostname = templateContext.default_context.aptly_server_hostname
Ivan Berezovskiyffff3302018-09-21 19:02:39 +0400209 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 +0300210
211 def smc_apt = [:]
212 smc_apt['SALT_MASTER_DEPLOY_IP'] = templateContext['default_context']['salt_master_management_address']
213 smc_apt['APTLY_DEPLOY_IP'] = templateContext['default_context']['aptly_server_deploy_address']
214 smc_apt['APTLY_DEPLOY_NETMASK'] = templateContext['default_context']['deploy_network_netmask']
215 smc_apt['APTLY_MINION_ID'] = "${aptlyServerHostname}.${clusterDomain}"
216
217 for (i in common.entries(smc_apt)) {
Ivan Berezovskiyffff3302018-09-21 19:02:39 +0400218 sh "sed -i \"s,export ${i[0]}=.*,export ${i[0]}=${i[1]},\" mirror_config"
azvyagintsev636493c2018-09-12 17:17:05 +0300219 }
220
221 // create apt config-drive
Ivan Berezovskiyffff3302018-09-21 19:02:39 +0400222 sh "./create-config-drive --user-data mirror_config --hostname ${aptlyServerHostname} ${aptlyServerHostname}.${clusterDomain}-config.iso"
azvyagintsev636493c2018-09-12 17:17:05 +0300223 sh("mv ${aptlyServerHostname}.${clusterDomain}-config.iso output-${clusterName}/")
224
225 // save apt iso to artifacts
226 archiveArtifacts artifacts: "output-${clusterName}/${aptlyServerHostname}.${clusterDomain}-config.iso"
227 }
228 }
229
230 stage('Save changes reclass model') {
231 sh(returnStatus: true, script: "tar -czf output-${clusterName}/${clusterName}.tar.gz --exclude='*@tmp' -C ${modelEnv} .")
232 archiveArtifacts artifacts: "output-${clusterName}/${clusterName}.tar.gz"
233
azvyagintsev636493c2018-09-12 17:17:05 +0300234 if (EMAIL_ADDRESS != null && EMAIL_ADDRESS != "") {
235 emailext(to: EMAIL_ADDRESS,
236 attachmentsPattern: "output-${clusterName}/*",
237 body: "Mirantis Jenkins\n\nRequested reclass model ${clusterName} has been created and attached to this email.\nEnjoy!\n\nMirantis",
238 subject: "Your Salt model ${clusterName}")
239 }
240 dir("output-${clusterName}") {
241 deleteDir()
242 }
243 }
244
245 // Fail, but leave possibility to get failed artifacts
246 if (!testResult && TEST_MODEL.toBoolean()) {
247 common.warningMsg('Test finished: FAILURE. Please check logs and\\or debug failed model manually!')
248 error('Test stage finished: FAILURE')
249 }
250
251 } catch (Throwable e) {
252 currentBuild.result = "FAILURE"
253 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
254 throw e
255 } finally {
256 stage('Clean workspace directories') {
257 sh(script: 'find . -mindepth 1 -delete > /dev/null || true')
258 }
259 // common.sendNotification(currentBuild.result,"",["slack"])
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400260 }
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200261 }
Mikhail Ivanov9f812922017-11-07 18:52:02 +0400262}