blob: cc8cfff3e900ae1b337288dba87f9c165920b909 [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ál7ded3642017-03-27 15:52:51 +020036
Tomáš Kukrál08bfb952017-04-12 13:10:52 +020037 currentBuild.description = clusterName
Tomáš Kukrál2a8fc322017-04-12 18:06:20 +020038 print("Using context:\n" + COOKIECUTTER_TEMPLATE_CONTEXT)
39
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020040 stage ('Download Cookiecutter template') {
41 git.checkoutGitRepository(templateEnv, COOKIECUTTER_TEMPLATE_URL, COOKIECUTTER_TEMPLATE_BRANCH, COOKIECUTTER_TEMPLATE_CREDENTIALS)
42 }
43
Tomáš Kukrál0c6835d2017-07-18 16:00:27 +020044 stage ('Create empty reclass model') {
45 sh "git init"
46
47 if (SHARED_RECLASS_URL != '') {
48 sh "git submodule add ${SHARED_RECLASS_URL} classes/system"
49 git.commitGitChanges(modelEnv, "Added new shared reclass submodule")
Tomáš Kukrál4aac3aa2017-04-04 14:04:02 +020050 }
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020051 }
52
chnyda89191012017-05-29 15:38:35 +020053 def productList = ["infra", "cicd", "opencontrail", "kubernetes", "openstack", "stacklight"]
54 for (product in productList) {
55 def stagename = (product == "infra") ? "Generate base infrastructure" : "Generate product ${product}"
56 stage(stagename) {
57 if (product == "infra" || (templateContext.default_context["${product}_enabled"]
58 && templateContext.default_context["${product}_enabled"].toBoolean())) {
59 templateDir = "${templateEnv}/cluster_product/${product}"
60 templateOutputDir = "${env.WORKSPACE}/template/output/${product}"
61 sh "mkdir -p ${templateOutputDir}"
62 sh "mkdir -p ${outputDestination}"
63 python.setupCookiecutterVirtualenv(cutterEnv)
64 python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv, templateBaseDir)
65 sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}"
66 }
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020067 }
68 }
69
70 stage('Generate new SaltMaster node') {
71 def nodeFile = "${modelEnv}/nodes/cfg01.${clusterDomain}.yml"
72 def nodeString = """classes:
73- cluster.${clusterName}.infra.config
74parameters:
Tomáš Kukrálc0bffae2017-06-07 14:40:32 +020075 _param:
76 linux_system_codename: xenial
77 reclass_data_revision: master
78 linux:
79 system:
80 name: cfg01
81 domain: ${clusterDomain}
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020082"""
Tomáš Kukrál4aac3aa2017-04-04 14:04:02 +020083 sh "mkdir -p ${modelEnv}/nodes/"
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020084 writeFile(file: nodeFile, text: nodeString)
Tomáš Kukrál0c6835d2017-07-18 16:00:27 +020085
86 git.commitGitChanges(modelEnv, "Create model ${clusterDomain}")
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020087 }
88
chnyda89191012017-05-29 15:38:35 +020089 stage("Test") {
Tomáš Kukrál0c6835d2017-07-18 16:00:27 +020090 if (SHARED_RECLASS_URL != "" && TEST_MODEL && TEST_MODEL.toBoolean()) {
chnyda89191012017-05-29 15:38:35 +020091 sh("cp -r ${modelEnv} ${testEnv}")
chnyda89191012017-05-29 15:38:35 +020092 saltModelTesting.setupAndTestNode("cfg01.${clusterDomain}", "", testEnv)
93 }
94 }
95
Tomáš Kukrál0c6835d2017-07-18 16:00:27 +020096 stage("Generate config drive") {
97 // download create-config-drive
98 // generate user-data.sh
99 // create config-drive
100
101 }
102
103 stage ('Save changes reclass model') {
Tomáš Kukrál91e49252017-05-09 14:40:26 +0200104
Tomáš Kukrál931f9972017-03-30 23:44:57 +0200105 sh(returnStatus: true, script: "tar -zcvf ${clusterName}.tar.gz -C ${modelEnv} .")
Tomáš Kukrál4c98b582017-03-30 18:49:06 +0200106 archiveArtifacts artifacts: "${clusterName}.tar.gz"
Tomáš Kukrál0c6835d2017-07-18 16:00:27 +0200107
Tomáš Kukrál91e49252017-05-09 14:40:26 +0200108 if (EMAIl_ADDRESS != null && EMAIL_ADDRESS != ""){
109 emailext(to: EMAIL_ADDRESS,
110 attachmentsPattern: "${clusterName}.tar.gz",
111 body: "Mirantis Jenkins\n\nRequested reclass model ${clusterName} has been created and attached to this email.\nEnjoy!\n\nMirantis",
112 subject: "Your Salt model ${clusterName}")
113 }
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200114 }
115
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200116 } catch (Throwable e) {
117 // If there was an error or exception thrown, the build failed
118 currentBuild.result = "FAILURE"
119 throw e
120 } finally {
Tomáš Kukrál8b91db92017-03-29 08:30:49 +0200121 stage ('Clean workspace directories') {
Tomáš Kukrál2b983c32017-03-30 18:09:02 +0200122 sh(returnStatus: true, script: "rm -rfv ${templateEnv}")
123 sh(returnStatus: true, script: "rm -rfv ${modelEnv}")
Tomáš Kukrál8b91db92017-03-29 08:30:49 +0200124 }
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200125 // common.sendNotification(currentBuild.result,"",["slack"])
126 }
127 }
128}