blob: 568b5a42ffdb4a97597794ccdbe878d01ab31794 [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
Tomáš Kukrál9f6260f2017-03-29 23:58:26 +020020
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +040021node("python&&docker") {
22 def templateEnv = "${env.WORKSPACE}/template"
23 def modelEnv = "${env.WORKSPACE}/model"
24 def testEnv = "${env.WORKSPACE}/test"
Tomáš Kukrálc84d1f52017-07-19 17:37:00 +020025
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +040026 try {
27 def templateContext = readYaml text: COOKIECUTTER_TEMPLATE_CONTEXT
28 def clusterDomain = templateContext.default_context.cluster_domain
29 def clusterName = templateContext.default_context.cluster_name
30 def cutterEnv = "${env.WORKSPACE}/cutter"
31 def jinjaEnv = "${env.WORKSPACE}/jinja"
32 def outputDestination = "${modelEnv}/classes/cluster/${clusterName}"
33 def targetBranch = "feature/${clusterName}"
34 def templateBaseDir = "${env.WORKSPACE}/template"
35 def templateDir = "${templateEnv}/template/dir"
36 def templateOutputDir = templateBaseDir
37 def user
38 wrap([$class: 'BuildUser']) {
39 user = env.BUILD_USER_ID
40 }
Tomáš Kukrál2a8fc322017-04-12 18:06:20 +020041
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +040042 currentBuild.description = clusterName
43 print("Using context:\n" + COOKIECUTTER_TEMPLATE_CONTEXT)
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020044
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +040045 stage ('Download Cookiecutter template') {
Tomáš Kukrála3f4ba72017-08-03 15:40:22 +020046 if (COOKIECUTTER_TEMPLATE_BRANCH.startsWith('refs/changes/')) {
47 git.checkoutGitRepository(templateEnv, COOKIECUTTER_TEMPLATE_URL, 'master', COOKIECUTTER_TEMPLATE_CREDENTIALS)
48
49 dir(templateEnv) {
50 ssh.agentSh("git fetch ${COOKIECUTTER_TEMPLATE_URL} ${COOKIECUTTER_TEMPLATE_BRANCH} && git checkout FETCH_HEAD")
51 }
52 } else {
53 git.checkoutGitRepository(templateEnv, COOKIECUTTER_TEMPLATE_URL, COOKIECUTTER_TEMPLATE_BRANCH, COOKIECUTTER_TEMPLATE_CREDENTIALS)
54 }
55
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +040056 }
Tomáš Kukrál0c6835d2017-07-18 16:00:27 +020057
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +040058 stage ('Create empty reclass model') {
59 dir(path: modelEnv) {
60 sh "rm -rfv .git"
61 sh "git init"
62
63 if (SHARED_RECLASS_URL != '') {
64 ssh.agentSh "git submodule add \"${SHARED_RECLASS_URL}\" \"classes/system\""
65 git.commitGitChanges(modelEnv, "Added new shared reclass submodule", "${user}@localhost", "${user}")
Tomáš Kukrál4aac3aa2017-04-04 14:04:02 +020066 }
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020067 }
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +040068 }
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020069
Ondrej Smola1639d212017-08-30 13:37:02 +020070 def productList = ["infra", "cicd", "opencontrail", "kubernetes", "openstack", "stacklight", "ceph"]
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +040071 for (product in productList) {
Tomáš Kukrál9a6821e2017-07-24 11:07:01 +020072
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +040073 // get templateOutputDir and productDir
74 if (product.startsWith("stacklight")) {
75 templateOutputDir = "${env.WORKSPACE}/output/stacklight"
Tomáš Kukrála3f4ba72017-08-03 15:40:22 +020076
77 def stacklightVersion
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +040078 try {
Tomáš Kukrála3f4ba72017-08-03 15:40:22 +020079 stacklightVersion = templateContext.default_context['stacklight_version']
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +040080 } catch (Throwable e) {
Tomáš Kukrála3f4ba72017-08-03 15:40:22 +020081 common.warningMsg('Stacklight version loading failed')
82 }
83
84 if (stacklightVersion) {
85 productDir = "stacklight" + stacklightVersion
86 } else {
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +040087 productDir = "stacklight1"
Tomáš Kukrál9a6821e2017-07-24 11:07:01 +020088 }
Tomáš Kukrála3f4ba72017-08-03 15:40:22 +020089
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +040090 } else {
91 templateOutputDir = "${env.WORKSPACE}/output/${product}"
92 productDir = product
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020093 }
94
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +040095 if (product == "infra" || (templateContext.default_context["${product}_enabled"]
96 && templateContext.default_context["${product}_enabled"].toBoolean())) {
97
98 templateDir = "${templateEnv}/cluster_product/${productDir}"
99 common.infoMsg("Generating product " + product + " from " + templateDir + " to " + templateOutputDir)
100
101 sh "rm -rf ${templateOutputDir} || true"
102 sh "mkdir -p ${templateOutputDir}"
103 sh "mkdir -p ${outputDestination}"
104
105 python.setupCookiecutterVirtualenv(cutterEnv)
106 python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv, templateBaseDir)
107 sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
108 } else {
109 common.warningMsg("Product " + product + " is disabled")
110 }
111 }
112
113 stage('Generate new SaltMaster node') {
114 def nodeFile = "${modelEnv}/nodes/cfg01.${clusterDomain}.yml"
115 def nodeString = """classes:
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200116- cluster.${clusterName}.infra.config
117parameters:
Tomáš Kukrálc0bffae2017-06-07 14:40:32 +0200118 _param:
119 linux_system_codename: xenial
120 reclass_data_revision: master
121 linux:
122 system:
123 name: cfg01
124 domain: ${clusterDomain}
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200125"""
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400126 sh "mkdir -p ${modelEnv}/nodes/"
127 writeFile(file: nodeFile, text: nodeString)
Tomáš Kukrál0c6835d2017-07-18 16:00:27 +0200128
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400129 git.commitGitChanges(modelEnv, "Create model ${clusterName}", "${user}@localhost", "${user}")
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200130 }
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400131
132 stage("Test") {
133 if (SHARED_RECLASS_URL != "" && TEST_MODEL && TEST_MODEL.toBoolean()) {
134 sh("cp -r ${modelEnv} ${testEnv}")
135 saltModelTesting.setupAndTestNode("cfg01.${clusterDomain}", "", testEnv)
136 }
137 }
138
139 stage("Generate config drive") {
140 // apt package genisoimage is required for this stage
141
142 // download create-config-drive
Jiri Broulik85068ff2017-09-05 11:49:09 +0200143 def config_drive_script_url = "https://raw.githubusercontent.com/jiribroulik/scripts/master/create_config_drive.sh"
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400144 sh "wget -O create-config-drive ${config_drive_script_url} && chmod +x create-config-drive"
Jiri Broulik85068ff2017-09-05 11:49:09 +0200145 def user_data_script_url = "https://raw.githubusercontent.com/mceloud/scripts/master/master_config.sh"
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400146 sh "wget -O user_data.sh ${user_data_script_url}"
147
Jiri Broulik85068ff2017-09-05 11:49:09 +0200148 sh "git clone https://github.com/Mirantis/mk-pipelines.git"
149 sh "git clone https://github.com/Mirantis/pipeline-library.git"
150 args = "--user-data user_data.sh --hostname cfg01 --model ${modelEnv} --mk-pipelines ${env.WORKSPACE}/mk-pipelines/ --pipeline-library ${env.WORKSPACE}/pipeline-library/ cfg01.${clusterDomain}-config.iso"
151
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400152 // load data from model
153 def smc = [:]
Jiri Broulik85068ff2017-09-05 11:49:09 +0200154 smc['SALT_MASTER_MINION_ID'] = "cfg01.${clusterDomain}"
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400155 smc['SALT_MASTER_DEPLOY_IP'] = templateContext['default_context']['salt_master_management_address']
156 smc['DEPLOY_NETWORK_GW'] = templateContext['default_context']['deploy_network_gateway']
157 smc['DEPLOY_NETWORK_NETMASK'] = templateContext['default_context']['deploy_network_netmask']
158 smc['DNS_SERVERS'] = templateContext['default_context']['dns_server01']
Jiri Broulik58f65af2017-08-25 17:44:50 +0200159 smc['CICD_CONTROL_ADDRESS'] = templateContext['default_context']['cicd_control_vip_address']
160 smc['INFRA_CONFIG_ADDRESS'] = templateContext['default_context']['salt_master_address']
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400161
162 for (i in common.entries(smc)) {
163 sh "sed -i \"s,export ${i[0]}=.*,export ${i[0]}=${i[1]},\" user_data.sh"
164 }
165
166 // create config-drive
Jiri Broulik85068ff2017-09-05 11:49:09 +0200167 sh "./create-config-drive ${args}"
Jiri Broulika052aec2017-09-05 16:02:03 +0200168 sh("mkdir output-${clusterName} && mv cfg01.${clusterDomain}-config.iso output-${clusterName}/")
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400169 // save iso to artifacts
Jiri Broulika052aec2017-09-05 16:02:03 +0200170 archiveArtifacts artifacts: "output-${clusterName}/cfg01.${clusterDomain}-config.iso"
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400171 }
172
173 stage ('Save changes reclass model') {
174
Jakub Josef0dbf4522017-08-31 14:39:50 +0200175 sh(returnStatus: true, script: "tar -zcf output-${clusterName}/${clusterName}.tar.gz -C ${modelEnv} .")
176 archiveArtifacts artifacts: "output-${clusterName}/${clusterName}.tar.gz"
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400177
178
179 if (EMAIL_ADDRESS != null && EMAIL_ADDRESS != "") {
180 emailext(to: EMAIL_ADDRESS,
Jakub Josef0dbf4522017-08-31 14:39:50 +0200181 attachmentsPattern: "output-${clusterName}/*",
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400182 body: "Mirantis Jenkins\n\nRequested reclass model ${clusterName} has been created and attached to this email.\nEnjoy!\n\nMirantis",
183 subject: "Your Salt model ${clusterName}")
184 }
Jakub Josef0dbf4522017-08-31 14:39:50 +0200185 dir("output-${clusterName}"){
186 deleteDir()
187 }
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400188 }
189
190 } catch (Throwable e) {
191 // If there was an error or exception thrown, the build failed
192 currentBuild.result = "FAILURE"
Jakub Josefd2efd7d2017-08-22 17:49:57 +0200193 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400194 throw e
195 } finally {
196 stage ('Clean workspace directories') {
197 sh(returnStatus: true, script: "rm -rf ${templateEnv}")
198 sh(returnStatus: true, script: "rm -rf ${modelEnv}")
199 }
200 // common.sendNotification(currentBuild.result,"",["slack"])
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200201 }
Jakub Josef0dbf4522017-08-31 14:39:50 +0200202}