blob: a4b646e9dd3d7b206651d5ce6a36450aa0c7ec2e [file] [log] [blame]
Tomáš Kukrál7ded3642017-03-27 15:52:51 +02001/**
2 * Generate cookiecutter cluster by individual products
3 *
4 * Expected parameters:
5 * COOKIECUTTER_TEMPLATE_CREDENTIALS Credentials to the Cookiecutter template repo.
6 * COOKIECUTTER_TEMPLATE_URL Cookiecutter template repo address.
7 * COOKIECUTTER_TEMPLATE_BRANCH Branch for the template.
8 * COOKIECUTTER_TEMPLATE_CONTEXT Context parameters for the template generation.
Tomáš Kukrál91e49252017-05-09 14:40:26 +02009 * EMAIL_ADDRESS Email to send a created tar file
Tomáš Kukrál0c6835d2017-07-18 16:00:27 +020010 * SHARED_RECLASS_URL Git repository with shared reclass
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020011 *
12**/
13
14common = new com.mirantis.mk.Common()
15git = new com.mirantis.mk.Git()
16python = new com.mirantis.mk.Python()
chnyda89191012017-05-29 15:38:35 +020017saltModelTesting = new com.mirantis.mk.SaltModelTesting()
Tomáš Kukrálcebc1a02017-07-25 16:10:37 +020018ssh = new com.mirantis.mk.Ssh()
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020019
Jakub Josefa63f9862018-01-11 17:58:38 +010020timeout(time: 12, unit: 'HOURS') {
21 node("python&&docker") {
22 def templateEnv = "${env.WORKSPACE}/template"
23 def modelEnv = "${env.WORKSPACE}/model"
24 def testEnv = "${env.WORKSPACE}/test"
25 def pipelineEnv = "${env.WORKSPACE}/pipelines"
Tomáš Kukrál9f6260f2017-03-29 23:58:26 +020026
Jakub Josefa63f9862018-01-11 17:58:38 +010027 try {
28 def templateContext = readYaml text: COOKIECUTTER_TEMPLATE_CONTEXT
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
32 def cutterEnv = "${env.WORKSPACE}/cutter"
33 def jinjaEnv = "${env.WORKSPACE}/jinja"
34 def outputDestination = "${modelEnv}/classes/cluster/${clusterName}"
35 def targetBranch = "feature/${clusterName}"
36 def templateBaseDir = "${env.WORKSPACE}/template"
37 def templateDir = "${templateEnv}/template/dir"
38 def templateOutputDir = templateBaseDir
39 def user
40 wrap([$class: 'BuildUser']) {
41 user = env.BUILD_USER_ID
Tomáš Kukrála3f4ba72017-08-03 15:40:22 +020042 }
43
Jakub Josefa63f9862018-01-11 17:58:38 +010044 currentBuild.description = clusterName
45 print("Using context:\n" + COOKIECUTTER_TEMPLATE_CONTEXT)
Tomáš Kukrál0c6835d2017-07-18 16:00:27 +020046
Jakub Josefa63f9862018-01-11 17:58:38 +010047 stage ('Download Cookiecutter template') {
Richard Felklc73f2ee2018-01-18 11:09:48 +010048 if (COOKIECUTTER_TEMPLATE_BRANCH.startsWith('refs/')) {
Jakub Josefa63f9862018-01-11 17:58:38 +010049 git.checkoutGitRepository(templateEnv, COOKIECUTTER_TEMPLATE_URL, 'master', COOKIECUTTER_TEMPLATE_CREDENTIALS)
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +040050
Jakub Josefa63f9862018-01-11 17:58:38 +010051 dir(templateEnv) {
52 ssh.agentSh("git fetch ${COOKIECUTTER_TEMPLATE_URL} ${COOKIECUTTER_TEMPLATE_BRANCH} && git checkout FETCH_HEAD")
53 }
54 } else {
55 git.checkoutGitRepository(templateEnv, COOKIECUTTER_TEMPLATE_URL, COOKIECUTTER_TEMPLATE_BRANCH, COOKIECUTTER_TEMPLATE_CREDENTIALS)
56 }
Richard Felkl9543faa2018-01-11 09:47:35 +010057
Jakub Josefa63f9862018-01-11 17:58:38 +010058 }
59
60 stage ('Create empty reclass model') {
61 dir(path: modelEnv) {
62 sh "rm -rfv .git"
63 sh "git init"
64
65 if (SHARED_RECLASS_URL != '') {
66 ssh.agentSh "git submodule add \"${SHARED_RECLASS_URL}\" \"classes/system\""
67
68 def mcpVersion = templateContext['default_context']['mcp_version']
69 if(mcpVersion != "stable" && mcpVersion != "nightly" && mcpVersion != "testing"){
70 ssh.agentSh "cd \"classes/system\";git fetch --tags;git checkout ${mcpVersion}"
71 }
72
73 git.commitGitChanges(modelEnv, "Added new shared reclass submodule", "${user}@localhost", "${user}")
74 }
75 }
76 }
77
78 def productList = ["infra", "cicd", "opencontrail", "kubernetes", "openstack", "oss", "stacklight", "ceph"]
79 for (product in productList) {
80
81 // get templateOutputDir and productDir
82 if (product.startsWith("stacklight")) {
83 templateOutputDir = "${env.WORKSPACE}/output/stacklight"
84
85 def stacklightVersion
86 try {
87 stacklightVersion = templateContext.default_context['stacklight_version']
88 } catch (Throwable e) {
89 common.warningMsg('Stacklight version loading failed')
Richard Felkl9543faa2018-01-11 09:47:35 +010090 }
91
Jakub Josefa63f9862018-01-11 17:58:38 +010092 if (stacklightVersion) {
93 productDir = "stacklight" + stacklightVersion
94 } else {
95 productDir = "stacklight1"
96 }
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020097
Tomáš Kukrála3f4ba72017-08-03 15:40:22 +020098 } else {
Jakub Josefa63f9862018-01-11 17:58:38 +010099 templateOutputDir = "${env.WORKSPACE}/output/${product}"
100 productDir = product
Tomáš Kukrál9a6821e2017-07-24 11:07:01 +0200101 }
Tomáš Kukrála3f4ba72017-08-03 15:40:22 +0200102
Jakub Josefa63f9862018-01-11 17:58:38 +0100103 if (product == "infra" || (templateContext.default_context["${product}_enabled"]
104 && templateContext.default_context["${product}_enabled"].toBoolean())) {
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200105
Jakub Josefa63f9862018-01-11 17:58:38 +0100106 templateDir = "${templateEnv}/cluster_product/${productDir}"
107 common.infoMsg("Generating product " + product + " from " + templateDir + " to " + templateOutputDir)
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400108
Jakub Josefa63f9862018-01-11 17:58:38 +0100109 sh "rm -rf ${templateOutputDir} || true"
110 sh "mkdir -p ${templateOutputDir}"
111 sh "mkdir -p ${outputDestination}"
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400112
Jakub Josefa63f9862018-01-11 17:58:38 +0100113 python.setupCookiecutterVirtualenv(cutterEnv)
114 python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv, templateBaseDir)
115 sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
Jiri Broulikd1375f72018-01-09 10:14:59 +0100116 } else {
Jakub Josefa63f9862018-01-11 17:58:38 +0100117 common.warningMsg("Product " + product + " is disabled")
Jiri Broulikd1375f72018-01-09 10:14:59 +0100118 }
119 }
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400120
Jakub Josefa63f9862018-01-11 17:58:38 +0100121 stage('Generate new SaltMaster node') {
122 def nodeFile = "${modelEnv}/nodes/${saltMaster}.${clusterDomain}.yml"
123 def nodeString = """classes:
Leontii Istomin56b6b112018-01-15 12:14:24 +0300124- cluster.${clusterName}.infra.config
125parameters:
126 _param:
127 linux_system_codename: xenial
128 reclass_data_revision: master
129 linux:
130 system:
131 name: ${saltMaster}
132 domain: ${clusterDomain}
Jakub Josefa63f9862018-01-11 17:58:38 +0100133 """
134 sh "mkdir -p ${modelEnv}/nodes/"
135 writeFile(file: nodeFile, text: nodeString)
136
137 git.commitGitChanges(modelEnv, "Create model ${clusterName}", "${user}@localhost", "${user}")
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400138 }
139
Jakub Josefa63f9862018-01-11 17:58:38 +0100140 stage("Test") {
141 if (SHARED_RECLASS_URL != "" && TEST_MODEL && TEST_MODEL.toBoolean()) {
142 sh("cp -r ${modelEnv} ${testEnv}")
143 saltModelTesting.setupAndTestNode("${saltMaster}.${clusterDomain}", "", testEnv)
144 }
145 }
Richard Felkl9fd47942017-10-20 16:23:29 +0200146
Jakub Josefa63f9862018-01-11 17:58:38 +0100147 stage("Generate config drives") {
148 // apt package genisoimage is required for this stage
Richard Felkl9fd47942017-10-20 16:23:29 +0200149
Jakub Josefa63f9862018-01-11 17:58:38 +0100150 // download create-config-drive
151 // FIXME: that should be refactored, to use git clone - to be able download it from custom repo.
152 def config_drive_script_url = "https://raw.githubusercontent.com/Mirantis/mcp-common-scripts/master/config-drive/create_config_drive.sh"
153 def user_data_script_url = "https://raw.githubusercontent.com/Mirantis/mcp-common-scripts/master/config-drive/master_config.sh"
Richard Felkl9fd47942017-10-20 16:23:29 +0200154
Jakub Josefa63f9862018-01-11 17:58:38 +0100155 sh "wget -O create-config-drive ${config_drive_script_url} && chmod +x create-config-drive"
156 sh "wget -O user_data.sh ${user_data_script_url}"
Richard Felkl9fd47942017-10-20 16:23:29 +0200157
Jakub Josefa63f9862018-01-11 17:58:38 +0100158 sh "git clone --mirror https://github.com/Mirantis/mk-pipelines.git ${pipelineEnv}/mk-pipelines"
159 sh "git clone --mirror https://github.com/Mirantis/pipeline-library.git ${pipelineEnv}/pipeline-library"
160 args = "--user-data user_data.sh --hostname ${saltMaster} --model ${modelEnv} --mk-pipelines ${pipelineEnv}/mk-pipelines/ --pipeline-library ${pipelineEnv}/pipeline-library/ ${saltMaster}.${clusterDomain}-config.iso"
161
162 // load data from model
163 def smc = [:]
164 smc['SALT_MASTER_MINION_ID'] = "${saltMaster}.${clusterDomain}"
165 smc['SALT_MASTER_DEPLOY_IP'] = templateContext['default_context']['salt_master_management_address']
166 smc['DEPLOY_NETWORK_GW'] = templateContext['default_context']['deploy_network_gateway']
167 smc['DEPLOY_NETWORK_NETMASK'] = templateContext['default_context']['deploy_network_netmask']
168 smc['DNS_SERVERS'] = templateContext['default_context']['dns_server01']
169 if (templateContext['default_context']['local_repositories'] == 'True'){
170 smc['PIPELINES_FROM_ISO'] = 'false'
171 smc['PIPELINE_REPO_URL'] = 'http://' + templateContext['default_context']['aptly_server_deploy_address'] + ':8088'
172 }
173 if (templateContext['default_context']['upstream_proxy_enabled'] == 'True'){
174 if (templateContext['default_context']['upstream_proxy_auth_enabled'] == 'True'){
175 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']
176 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']
177 } else {
178 smc['http_proxy'] = 'http://' + templateContext['default_context']['upstream_proxy_address'] + ':' + templateContext['default_context']['upstream_proxy_port']
179 smc['https_proxy'] = 'http://' + templateContext['default_context']['upstream_proxy_address'] + ':' + templateContext['default_context']['upstream_proxy_port']
180 }
Richard Felkl9fd47942017-10-20 16:23:29 +0200181 }
182
Jakub Josefa63f9862018-01-11 17:58:38 +0100183 for (i in common.entries(smc)) {
184 sh "sed -i \"s,export ${i[0]}=.*,export ${i[0]}=${i[1]},\" user_data.sh"
185 }
Richard Felkl9fd47942017-10-20 16:23:29 +0200186
Jakub Josefa63f9862018-01-11 17:58:38 +0100187 // create cfg config-drive
188 sh "./create-config-drive ${args}"
189 sh("mkdir output-${clusterName} && mv ${saltMaster}.${clusterDomain}-config.iso output-${clusterName}/")
190
191 // save cfg iso to artifacts
192 archiveArtifacts artifacts: "output-${clusterName}/${saltMaster}.${clusterDomain}-config.iso"
193
194 if (templateContext['default_context']['local_repositories'] == 'True'){
195 def aptlyServerHostname = templateContext.default_context.aptly_server_hostname
196 def user_data_script_apt_url = "https://raw.githubusercontent.com/Mirantis/mcp-common-scripts/master/config-drive/mirror_config.sh"
197 sh "wget -O mirror_config.sh ${user_data_script_apt_url}"
198
199 def smc_apt = [:]
200 smc_apt['SALT_MASTER_DEPLOY_IP'] = templateContext['default_context']['salt_master_management_address']
201 smc_apt['APTLY_DEPLOY_IP'] = templateContext['default_context']['aptly_server_deploy_address']
202 smc_apt['APTLY_DEPLOY_NETMASK'] = templateContext['default_context']['deploy_network_netmask']
203 smc_apt['APTLY_MINION_ID'] = "${aptlyServerHostname}.${clusterDomain}"
204
205 for (i in common.entries(smc_apt)) {
206 sh "sed -i \"s,export ${i[0]}=.*,export ${i[0]}=${i[1]},\" mirror_config.sh"
207 }
208
209 // create apt config-drive
210 sh "./create-config-drive --user-data mirror_config.sh --hostname ${aptlyServerHostname} ${aptlyServerHostname}.${clusterDomain}-config.iso"
211 sh("mv ${aptlyServerHostname}.${clusterDomain}-config.iso output-${clusterName}/")
212
213 // save apt iso to artifacts
214 archiveArtifacts artifacts: "output-${clusterName}/${aptlyServerHostname}.${clusterDomain}-config.iso"
215 }
Richard Felkl9fd47942017-10-20 16:23:29 +0200216 }
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400217
Jakub Josefa63f9862018-01-11 17:58:38 +0100218 stage ('Save changes reclass model') {
219 sh(returnStatus: true, script: "tar -zcf output-${clusterName}/${clusterName}.tar.gz -C ${modelEnv} .")
220 archiveArtifacts artifacts: "output-${clusterName}/${clusterName}.tar.gz"
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400221
222
Jakub Josefa63f9862018-01-11 17:58:38 +0100223 if (EMAIL_ADDRESS != null && EMAIL_ADDRESS != "") {
224 emailext(to: EMAIL_ADDRESS,
225 attachmentsPattern: "output-${clusterName}/*",
226 body: "Mirantis Jenkins\n\nRequested reclass model ${clusterName} has been created and attached to this email.\nEnjoy!\n\nMirantis",
227 subject: "Your Salt model ${clusterName}")
228 }
229 dir("output-${clusterName}"){
230 deleteDir()
231 }
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400232 }
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400233
Jakub Josefa63f9862018-01-11 17:58:38 +0100234 } catch (Throwable e) {
235 // If there was an error or exception thrown, the build failed
236 currentBuild.result = "FAILURE"
237 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
238 throw e
239 } finally {
240 stage ('Clean workspace directories') {
241 sh(returnStatus: true, script: "rm -rf ${templateEnv}")
242 sh(returnStatus: true, script: "rm -rf ${modelEnv}")
243 sh(returnStatus: true, script: "rm -rf ${pipelineEnv}")
244 }
245 // common.sendNotification(currentBuild.result,"",["slack"])
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400246 }
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200247 }
Mikhail Ivanov9f812922017-11-07 18:52:02 +0400248}