blob: 288a4a5687f6ba72fb14d6e79fc40cc9cf909771 [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"
Jiri Broulikf6725282017-09-05 16:23:35 +020025 def pipelineEnv = "${env.WORKSPACE}/pipelines"
Tomáš Kukrálc84d1f52017-07-19 17:37:00 +020026
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +040027 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 cutterEnv = "${env.WORKSPACE}/cutter"
32 def jinjaEnv = "${env.WORKSPACE}/jinja"
33 def outputDestination = "${modelEnv}/classes/cluster/${clusterName}"
34 def targetBranch = "feature/${clusterName}"
35 def templateBaseDir = "${env.WORKSPACE}/template"
36 def templateDir = "${templateEnv}/template/dir"
37 def templateOutputDir = templateBaseDir
38 def user
39 wrap([$class: 'BuildUser']) {
40 user = env.BUILD_USER_ID
41 }
Tomáš Kukrál2a8fc322017-04-12 18:06:20 +020042
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +040043 currentBuild.description = clusterName
44 print("Using context:\n" + COOKIECUTTER_TEMPLATE_CONTEXT)
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020045
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +040046 stage ('Download Cookiecutter template') {
Tomáš Kukrála3f4ba72017-08-03 15:40:22 +020047 if (COOKIECUTTER_TEMPLATE_BRANCH.startsWith('refs/changes/')) {
48 git.checkoutGitRepository(templateEnv, COOKIECUTTER_TEMPLATE_URL, 'master', COOKIECUTTER_TEMPLATE_CREDENTIALS)
49
50 dir(templateEnv) {
51 ssh.agentSh("git fetch ${COOKIECUTTER_TEMPLATE_URL} ${COOKIECUTTER_TEMPLATE_BRANCH} && git checkout FETCH_HEAD")
52 }
53 } else {
54 git.checkoutGitRepository(templateEnv, COOKIECUTTER_TEMPLATE_URL, COOKIECUTTER_TEMPLATE_BRANCH, COOKIECUTTER_TEMPLATE_CREDENTIALS)
55 }
56
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +040057 }
Tomáš Kukrál0c6835d2017-07-18 16:00:27 +020058
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +040059 stage ('Create empty reclass model') {
60 dir(path: modelEnv) {
61 sh "rm -rfv .git"
62 sh "git init"
63
64 if (SHARED_RECLASS_URL != '') {
65 ssh.agentSh "git submodule add \"${SHARED_RECLASS_URL}\" \"classes/system\""
66 git.commitGitChanges(modelEnv, "Added new shared reclass submodule", "${user}@localhost", "${user}")
Tomáš Kukrál4aac3aa2017-04-04 14:04:02 +020067 }
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020068 }
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +040069 }
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020070
Ondrej Smola1639d212017-08-30 13:37:02 +020071 def productList = ["infra", "cicd", "opencontrail", "kubernetes", "openstack", "stacklight", "ceph"]
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +040072 for (product in productList) {
Tomáš Kukrál9a6821e2017-07-24 11:07:01 +020073
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +040074 // get templateOutputDir and productDir
75 if (product.startsWith("stacklight")) {
76 templateOutputDir = "${env.WORKSPACE}/output/stacklight"
Tomáš Kukrála3f4ba72017-08-03 15:40:22 +020077
78 def stacklightVersion
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +040079 try {
Tomáš Kukrála3f4ba72017-08-03 15:40:22 +020080 stacklightVersion = templateContext.default_context['stacklight_version']
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +040081 } catch (Throwable e) {
Tomáš Kukrála3f4ba72017-08-03 15:40:22 +020082 common.warningMsg('Stacklight version loading failed')
83 }
84
85 if (stacklightVersion) {
86 productDir = "stacklight" + stacklightVersion
87 } else {
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +040088 productDir = "stacklight1"
Tomáš Kukrál9a6821e2017-07-24 11:07:01 +020089 }
Tomáš Kukrála3f4ba72017-08-03 15:40:22 +020090
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +040091 } else {
92 templateOutputDir = "${env.WORKSPACE}/output/${product}"
93 productDir = product
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020094 }
95
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +040096 if (product == "infra" || (templateContext.default_context["${product}_enabled"]
97 && templateContext.default_context["${product}_enabled"].toBoolean())) {
98
99 templateDir = "${templateEnv}/cluster_product/${productDir}"
100 common.infoMsg("Generating product " + product + " from " + templateDir + " to " + templateOutputDir)
101
102 sh "rm -rf ${templateOutputDir} || true"
103 sh "mkdir -p ${templateOutputDir}"
104 sh "mkdir -p ${outputDestination}"
105
106 python.setupCookiecutterVirtualenv(cutterEnv)
107 python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv, templateBaseDir)
108 sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
109 } else {
110 common.warningMsg("Product " + product + " is disabled")
111 }
112 }
113
114 stage('Generate new SaltMaster node') {
115 def nodeFile = "${modelEnv}/nodes/cfg01.${clusterDomain}.yml"
116 def nodeString = """classes:
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200117- cluster.${clusterName}.infra.config
118parameters:
Tomáš Kukrálc0bffae2017-06-07 14:40:32 +0200119 _param:
120 linux_system_codename: xenial
121 reclass_data_revision: master
122 linux:
123 system:
124 name: cfg01
125 domain: ${clusterDomain}
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200126"""
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400127 sh "mkdir -p ${modelEnv}/nodes/"
128 writeFile(file: nodeFile, text: nodeString)
Tomáš Kukrál0c6835d2017-07-18 16:00:27 +0200129
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400130 git.commitGitChanges(modelEnv, "Create model ${clusterName}", "${user}@localhost", "${user}")
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200131 }
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400132
133 stage("Test") {
134 if (SHARED_RECLASS_URL != "" && TEST_MODEL && TEST_MODEL.toBoolean()) {
135 sh("cp -r ${modelEnv} ${testEnv}")
136 saltModelTesting.setupAndTestNode("cfg01.${clusterDomain}", "", testEnv)
137 }
138 }
139
140 stage("Generate config drive") {
141 // apt package genisoimage is required for this stage
142
143 // download create-config-drive
Jiri Broulik85068ff2017-09-05 11:49:09 +0200144 def config_drive_script_url = "https://raw.githubusercontent.com/jiribroulik/scripts/master/create_config_drive.sh"
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400145 sh "wget -O create-config-drive ${config_drive_script_url} && chmod +x create-config-drive"
Jiri Broulik85068ff2017-09-05 11:49:09 +0200146 def user_data_script_url = "https://raw.githubusercontent.com/mceloud/scripts/master/master_config.sh"
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400147 sh "wget -O user_data.sh ${user_data_script_url}"
148
Jiri Broulikf6725282017-09-05 16:23:35 +0200149 sh "git clone https://github.com/Mirantis/mk-pipelines.git ${pipelineEnv}/mk-pipelines"
150 sh "git clone https://github.com/Mirantis/pipeline-library.git ${pipelineEnv}/pipeline-library"
151 args = "--user-data user_data.sh --hostname cfg01 --model ${modelEnv} --mk-pipelines ${pipelineEnv}/mk-pipelines/ --pipeline-library ${pipelineEnv}/pipeline-library/ cfg01.${clusterDomain}-config.iso"
Jiri Broulik85068ff2017-09-05 11:49:09 +0200152
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400153 // load data from model
154 def smc = [:]
Jiri Broulik85068ff2017-09-05 11:49:09 +0200155 smc['SALT_MASTER_MINION_ID'] = "cfg01.${clusterDomain}"
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400156 smc['SALT_MASTER_DEPLOY_IP'] = templateContext['default_context']['salt_master_management_address']
157 smc['DEPLOY_NETWORK_GW'] = templateContext['default_context']['deploy_network_gateway']
158 smc['DEPLOY_NETWORK_NETMASK'] = templateContext['default_context']['deploy_network_netmask']
159 smc['DNS_SERVERS'] = templateContext['default_context']['dns_server01']
160
161 for (i in common.entries(smc)) {
162 sh "sed -i \"s,export ${i[0]}=.*,export ${i[0]}=${i[1]},\" user_data.sh"
163 }
164
165 // create config-drive
Jiri Broulik85068ff2017-09-05 11:49:09 +0200166 sh "./create-config-drive ${args}"
Jiri Broulika052aec2017-09-05 16:02:03 +0200167 sh("mkdir output-${clusterName} && mv cfg01.${clusterDomain}-config.iso output-${clusterName}/")
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400168 // save iso to artifacts
Jiri Broulika052aec2017-09-05 16:02:03 +0200169 archiveArtifacts artifacts: "output-${clusterName}/cfg01.${clusterDomain}-config.iso"
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400170 }
171
172 stage ('Save changes reclass model') {
173
Jakub Josef0dbf4522017-08-31 14:39:50 +0200174 sh(returnStatus: true, script: "tar -zcf output-${clusterName}/${clusterName}.tar.gz -C ${modelEnv} .")
175 archiveArtifacts artifacts: "output-${clusterName}/${clusterName}.tar.gz"
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400176
177
178 if (EMAIL_ADDRESS != null && EMAIL_ADDRESS != "") {
179 emailext(to: EMAIL_ADDRESS,
Jakub Josef0dbf4522017-08-31 14:39:50 +0200180 attachmentsPattern: "output-${clusterName}/*",
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400181 body: "Mirantis Jenkins\n\nRequested reclass model ${clusterName} has been created and attached to this email.\nEnjoy!\n\nMirantis",
182 subject: "Your Salt model ${clusterName}")
183 }
Jakub Josef0dbf4522017-08-31 14:39:50 +0200184 dir("output-${clusterName}"){
185 deleteDir()
186 }
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400187 }
188
189 } catch (Throwable e) {
190 // If there was an error or exception thrown, the build failed
191 currentBuild.result = "FAILURE"
Jakub Josefd2efd7d2017-08-22 17:49:57 +0200192 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400193 throw e
194 } finally {
195 stage ('Clean workspace directories') {
196 sh(returnStatus: true, script: "rm -rf ${templateEnv}")
197 sh(returnStatus: true, script: "rm -rf ${modelEnv}")
Jiri Broulikf6725282017-09-05 16:23:35 +0200198 sh(returnStatus: true, script: "rm -rf ${pipelineEnv}")
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400199 }
200 // common.sendNotification(currentBuild.result,"",["slack"])
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200201 }
Jakub Josef0dbf4522017-08-31 14:39:50 +0200202}