blob: 4326e7e4b2394fcae320d16b8a00318953eee34e [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
Jiri Broulik19f85192017-09-18 13:55:14 +020031 def saltMaster = templateContext.default_context.salt_master_hostname
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +040032 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
42 }
Tomáš Kukrál2a8fc322017-04-12 18:06:20 +020043
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +040044 currentBuild.description = clusterName
45 print("Using context:\n" + COOKIECUTTER_TEMPLATE_CONTEXT)
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020046
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +040047 stage ('Download Cookiecutter template') {
Tomáš Kukrála3f4ba72017-08-03 15:40:22 +020048 if (COOKIECUTTER_TEMPLATE_BRANCH.startsWith('refs/changes/')) {
49 git.checkoutGitRepository(templateEnv, COOKIECUTTER_TEMPLATE_URL, 'master', COOKIECUTTER_TEMPLATE_CREDENTIALS)
50
51 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 }
57
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +040058 }
Tomáš Kukrál0c6835d2017-07-18 16:00:27 +020059
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +040060 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 git.commitGitChanges(modelEnv, "Added new shared reclass submodule", "${user}@localhost", "${user}")
Tomáš Kukrál4aac3aa2017-04-04 14:04:02 +020068 }
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020069 }
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +040070 }
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020071
Ondrej Smola1639d212017-08-30 13:37:02 +020072 def productList = ["infra", "cicd", "opencontrail", "kubernetes", "openstack", "stacklight", "ceph"]
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +040073 for (product in productList) {
Tomáš Kukrál9a6821e2017-07-24 11:07:01 +020074
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +040075 // get templateOutputDir and productDir
76 if (product.startsWith("stacklight")) {
77 templateOutputDir = "${env.WORKSPACE}/output/stacklight"
Tomáš Kukrála3f4ba72017-08-03 15:40:22 +020078
79 def stacklightVersion
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +040080 try {
Tomáš Kukrála3f4ba72017-08-03 15:40:22 +020081 stacklightVersion = templateContext.default_context['stacklight_version']
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +040082 } catch (Throwable e) {
Tomáš Kukrála3f4ba72017-08-03 15:40:22 +020083 common.warningMsg('Stacklight version loading failed')
84 }
85
86 if (stacklightVersion) {
87 productDir = "stacklight" + stacklightVersion
88 } else {
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +040089 productDir = "stacklight1"
Tomáš Kukrál9a6821e2017-07-24 11:07:01 +020090 }
Tomáš Kukrála3f4ba72017-08-03 15:40:22 +020091
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +040092 } else {
93 templateOutputDir = "${env.WORKSPACE}/output/${product}"
94 productDir = product
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020095 }
96
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +040097 if (product == "infra" || (templateContext.default_context["${product}_enabled"]
98 && templateContext.default_context["${product}_enabled"].toBoolean())) {
99
100 templateDir = "${templateEnv}/cluster_product/${productDir}"
101 common.infoMsg("Generating product " + product + " from " + templateDir + " to " + templateOutputDir)
102
103 sh "rm -rf ${templateOutputDir} || true"
104 sh "mkdir -p ${templateOutputDir}"
105 sh "mkdir -p ${outputDestination}"
106
107 python.setupCookiecutterVirtualenv(cutterEnv)
108 python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv, templateBaseDir)
109 sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
110 } else {
111 common.warningMsg("Product " + product + " is disabled")
112 }
113 }
114
115 stage('Generate new SaltMaster node') {
Jiri Broulik19f85192017-09-18 13:55:14 +0200116 def nodeFile = "${modelEnv}/nodes/${saltMaster}.${clusterDomain}.yml"
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400117 def nodeString = """classes:
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200118- cluster.${clusterName}.infra.config
119parameters:
Tomáš Kukrálc0bffae2017-06-07 14:40:32 +0200120 _param:
121 linux_system_codename: xenial
122 reclass_data_revision: master
123 linux:
124 system:
Jiri Broulik19f85192017-09-18 13:55:14 +0200125 name: ${saltMaster}
Tomáš Kukrálc0bffae2017-06-07 14:40:32 +0200126 domain: ${clusterDomain}
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200127"""
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400128 sh "mkdir -p ${modelEnv}/nodes/"
129 writeFile(file: nodeFile, text: nodeString)
Tomáš Kukrál0c6835d2017-07-18 16:00:27 +0200130
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400131 git.commitGitChanges(modelEnv, "Create model ${clusterName}", "${user}@localhost", "${user}")
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200132 }
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400133
134 stage("Test") {
135 if (SHARED_RECLASS_URL != "" && TEST_MODEL && TEST_MODEL.toBoolean()) {
136 sh("cp -r ${modelEnv} ${testEnv}")
Jiri Broulik19f85192017-09-18 13:55:14 +0200137 saltModelTesting.setupAndTestNode("${saltMaster}.${clusterDomain}", "", testEnv)
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400138 }
139 }
140
141 stage("Generate config drive") {
142 // apt package genisoimage is required for this stage
143
144 // download create-config-drive
Jiri Broulik85068ff2017-09-05 11:49:09 +0200145 def config_drive_script_url = "https://raw.githubusercontent.com/jiribroulik/scripts/master/create_config_drive.sh"
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400146 sh "wget -O create-config-drive ${config_drive_script_url} && chmod +x create-config-drive"
Jiri Broulik85068ff2017-09-05 11:49:09 +0200147 def user_data_script_url = "https://raw.githubusercontent.com/mceloud/scripts/master/master_config.sh"
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400148 sh "wget -O user_data.sh ${user_data_script_url}"
149
Jiri Broulikf6725282017-09-05 16:23:35 +0200150 sh "git clone https://github.com/Mirantis/mk-pipelines.git ${pipelineEnv}/mk-pipelines"
151 sh "git clone https://github.com/Mirantis/pipeline-library.git ${pipelineEnv}/pipeline-library"
Jiri Broulik19f85192017-09-18 13:55:14 +0200152 args = "--user-data user_data.sh --hostname ${saltMaster} --model ${modelEnv} --mk-pipelines ${pipelineEnv}/mk-pipelines/ --pipeline-library ${pipelineEnv}/pipeline-library/ ${saltMaster}.${clusterDomain}-config.iso"
Jiri Broulik85068ff2017-09-05 11:49:09 +0200153
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400154 // load data from model
155 def smc = [:]
Jiri Broulik19f85192017-09-18 13:55:14 +0200156 smc['SALT_MASTER_MINION_ID'] = "${saltMaster}.${clusterDomain}"
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400157 smc['SALT_MASTER_DEPLOY_IP'] = templateContext['default_context']['salt_master_management_address']
158 smc['DEPLOY_NETWORK_GW'] = templateContext['default_context']['deploy_network_gateway']
159 smc['DEPLOY_NETWORK_NETMASK'] = templateContext['default_context']['deploy_network_netmask']
160 smc['DNS_SERVERS'] = templateContext['default_context']['dns_server01']
161
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 Broulik19f85192017-09-18 13:55:14 +0200168 sh("mkdir output-${clusterName} && mv ${saltMaster}.${clusterDomain}-config.iso output-${clusterName}/")
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400169 // save iso to artifacts
Jiri Broulik19f85192017-09-18 13:55:14 +0200170 archiveArtifacts artifacts: "output-${clusterName}/${saltMaster}.${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}")
Jiri Broulikf6725282017-09-05 16:23:35 +0200199 sh(returnStatus: true, script: "rm -rf ${pipelineEnv}")
Ruslan Kamaldinov6feef402017-08-02 16:55:58 +0400200 }
201 // common.sendNotification(currentBuild.result,"",["slack"])
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200202 }
Jakub Josef0dbf4522017-08-31 14:39:50 +0200203}