blob: 783bd67a87b44065fd57760a98e797a570f88c4d [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ál7ded3642017-03-27 15:52:51 +020018
19timestamps {
chnyda89191012017-05-29 15:38:35 +020020 node("python&&docker") {
Tomáš Kukrál9f6260f2017-03-29 23:58:26 +020021 def templateEnv = "${env.WORKSPACE}/template"
22 def modelEnv = "${env.WORKSPACE}/model"
chnyda89191012017-05-29 15:38:35 +020023 def testEnv = "${env.WORKSPACE}/test"
Tomáš Kukrál9f6260f2017-03-29 23:58:26 +020024
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020025 try {
Tomáš Kukrálf2a74ab2017-05-09 17:53:32 +020026 def templateContext = readYaml text: COOKIECUTTER_TEMPLATE_CONTEXT
Tomáš Kukrál91e49252017-05-09 14:40:26 +020027 def clusterDomain = templateContext.default_context.cluster_domain
28 def clusterName = templateContext.default_context.cluster_name
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020029 def cutterEnv = "${env.WORKSPACE}/cutter"
30 def jinjaEnv = "${env.WORKSPACE}/jinja"
Tomáš Kukrál931f9972017-03-30 23:44:57 +020031 def outputDestination = "${modelEnv}/classes/cluster/${clusterName}"
Tomáš Kukrál91e49252017-05-09 14:40:26 +020032 def targetBranch = "feature/${clusterName}"
33 def templateBaseDir = "${env.WORKSPACE}/template"
Tomáš Kukrál91e49252017-05-09 14:40:26 +020034 def templateDir = "${templateEnv}/template/dir"
35 def templateOutputDir = templateBaseDir
Tomáš Kukrálc84d1f52017-07-19 17:37:00 +020036 def user
37 wrap([$class: 'BuildUser']) {
38 user = env.BUILD_USER_ID
39 }
40
Tomáš Kukrál08bfb952017-04-12 13:10:52 +020041 currentBuild.description = clusterName
Tomáš Kukrál2a8fc322017-04-12 18:06:20 +020042 print("Using context:\n" + COOKIECUTTER_TEMPLATE_CONTEXT)
43
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020044 stage ('Download Cookiecutter template') {
45 git.checkoutGitRepository(templateEnv, COOKIECUTTER_TEMPLATE_URL, COOKIECUTTER_TEMPLATE_BRANCH, COOKIECUTTER_TEMPLATE_CREDENTIALS)
46 }
47
Tomáš Kukrál0c6835d2017-07-18 16:00:27 +020048 stage ('Create empty reclass model') {
Tomáš Kukrálc84d1f52017-07-19 17:37:00 +020049 dir(path: modelEnv) {
50 sh "rm -rfv .git"
51 sh "git init"
Tomáš Kukrál0c6835d2017-07-18 16:00:27 +020052
Tomáš Kukrálc84d1f52017-07-19 17:37:00 +020053 if (SHARED_RECLASS_URL != '') {
54 sh "git submodule add ${SHARED_RECLASS_URL} '${modelEnv}/classes/system'"
55 git.commitGitChanges(modelEnv, "Added new shared reclass submodule", "${user}@localhost", "${user}")
56 }
Tomáš Kukrál4aac3aa2017-04-04 14:04:02 +020057 }
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020058 }
59
chnyda89191012017-05-29 15:38:35 +020060 def productList = ["infra", "cicd", "opencontrail", "kubernetes", "openstack", "stacklight"]
61 for (product in productList) {
Tomáš Kukrál9a6821e2017-07-24 11:07:01 +020062
63 // get templateOutputDir and productDir
64 if (product.startsWith("stacklight")) {
65 templateOutputDir = "${env.WORKSPACE}/output/stacklight"
66 try {
67 productDir = "stacklight" + templateContext.default_context['stacklight_version']
68 } catch (Throwable e) {
69 productDir = "stacklight1"
chnyda89191012017-05-29 15:38:35 +020070 }
Tomáš Kukrál9a6821e2017-07-24 11:07:01 +020071 } else {
72 templateOutputDir = "${env.WORKSPACE}/output/${product}"
73 productDir = product
74 }
75
76 if (product == "infra" || (templateContext.default_context["${product}_enabled"]
77 && templateContext.default_context["${product}_enabled"].toBoolean())) {
78
79 templateDir = "${templateEnv}/cluster_product/${productDir}"
80 common.infoMsg("Generating product " + product + " from " + templateDir + " to " + templateOutputDir)
81
82 sh "rm -rf ${templateOutputDir} || true"
83 sh "mkdir -p ${templateOutputDir}"
84 sh "mkdir -p ${outputDestination}"
85
86 python.buildCookiecutterTemplate(templateDir, content, templateOutputDir, cutterEnv, templateBaseDir)
87 sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
88 } else {
89 common.warningMsg("Product " + product + " is disabled")
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020090 }
91 }
92
93 stage('Generate new SaltMaster node') {
94 def nodeFile = "${modelEnv}/nodes/cfg01.${clusterDomain}.yml"
95 def nodeString = """classes:
96- cluster.${clusterName}.infra.config
97parameters:
Tomáš Kukrálc0bffae2017-06-07 14:40:32 +020098 _param:
99 linux_system_codename: xenial
100 reclass_data_revision: master
101 linux:
102 system:
103 name: cfg01
104 domain: ${clusterDomain}
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200105"""
Tomáš Kukrál4aac3aa2017-04-04 14:04:02 +0200106 sh "mkdir -p ${modelEnv}/nodes/"
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200107 writeFile(file: nodeFile, text: nodeString)
Tomáš Kukrál0c6835d2017-07-18 16:00:27 +0200108
Tomáš Kukrálc84d1f52017-07-19 17:37:00 +0200109 git.commitGitChanges(modelEnv, "Create model ${clusterName}", "${user}@localhost", "${user}")
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200110 }
111
chnyda89191012017-05-29 15:38:35 +0200112 stage("Test") {
Tomáš Kukrál0c6835d2017-07-18 16:00:27 +0200113 if (SHARED_RECLASS_URL != "" && TEST_MODEL && TEST_MODEL.toBoolean()) {
chnyda89191012017-05-29 15:38:35 +0200114 sh("cp -r ${modelEnv} ${testEnv}")
chnyda89191012017-05-29 15:38:35 +0200115 saltModelTesting.setupAndTestNode("cfg01.${clusterDomain}", "", testEnv)
116 }
117 }
118
Tomáš Kukrál0c6835d2017-07-18 16:00:27 +0200119 stage("Generate config drive") {
Tomáš Kukrálc84d1f52017-07-19 17:37:00 +0200120 // apt package genisoimage is required for this stage
Tomáš Kukrál0c6835d2017-07-18 16:00:27 +0200121
Tomáš Kukrálc84d1f52017-07-19 17:37:00 +0200122 // download create-config-drive
123 def config_drive_script_url = "https://raw.githubusercontent.com/pupapaik/virt-utils/master/create-config-drive"
124 def user_data_script_url = "https://raw.githubusercontent.com/mceloud/scripts/master/master_config.sh"
125
126 sh "wget -O create-config-drive ${config_drive_script_url} && chmod +x create-config-drive"
127 sh "wget -O user_data.sh ${user_data_script_url}"
128
129
130 // load data from model
131 def smc = [:]
132 smc['SALT_MASTER_MINION_ID'] = "cfg.${clusterDomain}"
133 smc['SALT_MASTER_DEPLOY_IP'] = templateContext['default_context']['salt_master_management_address']
134 smc['DEPLOY_NETWORK_GW'] = templateContext['default_context']['deploy_network_gateway']
135 smc['DEPLOY_NETWORK_NETMASK'] = templateContext['default_context']['deploy_network_netmask']
136 smc['DNS_SERVERS'] = templateContext['default_context']['dns_server01']
137
138 for (i in common.entries(smc)) {
139 sh "sed -i \"s,export ${i[0]}=.*,export ${i[0]}=${i[1]},\" user_data.sh"
140 }
141
142 // create config-drive
143 sh "./create-config-drive --user-data user_data.sh --hostname cfg --model ${modelEnv} cfg.${clusterDomain}-config.iso"
144
145 // save iso to artifacts
146 archiveArtifacts artifacts: "cfg.${clusterDomain}-config.iso"
Tomáš Kukrál0c6835d2017-07-18 16:00:27 +0200147 }
148
149 stage ('Save changes reclass model') {
Tomáš Kukrál91e49252017-05-09 14:40:26 +0200150
Tomáš Kukrálc84d1f52017-07-19 17:37:00 +0200151 sh(returnStatus: true, script: "tar -zcf ${clusterName}.tar.gz -C ${modelEnv} .")
Tomáš Kukrál4c98b582017-03-30 18:49:06 +0200152 archiveArtifacts artifacts: "${clusterName}.tar.gz"
Tomáš Kukrál0c6835d2017-07-18 16:00:27 +0200153
Tomáš Kukrálc84d1f52017-07-19 17:37:00 +0200154
155 if (EMAIL_ADDRESS != null && EMAIL_ADDRESS != "") {
Tomáš Kukrál91e49252017-05-09 14:40:26 +0200156 emailext(to: EMAIL_ADDRESS,
157 attachmentsPattern: "${clusterName}.tar.gz",
158 body: "Mirantis Jenkins\n\nRequested reclass model ${clusterName} has been created and attached to this email.\nEnjoy!\n\nMirantis",
159 subject: "Your Salt model ${clusterName}")
160 }
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200161 }
162
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200163 } catch (Throwable e) {
164 // If there was an error or exception thrown, the build failed
165 currentBuild.result = "FAILURE"
166 throw e
167 } finally {
Tomáš Kukrál8b91db92017-03-29 08:30:49 +0200168 stage ('Clean workspace directories') {
Tomáš Kukrálc84d1f52017-07-19 17:37:00 +0200169 sh(returnStatus: true, script: "rm -rf ${templateEnv}")
170 sh(returnStatus: true, script: "rm -rf ${modelEnv}")
Tomáš Kukrál8b91db92017-03-29 08:30:49 +0200171 }
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200172 // common.sendNotification(currentBuild.result,"",["slack"])
173 }
174 }
175}