blob: e64abdac5eb217c6108d6604a91937e948c4dfba [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ál7ded3642017-03-27 15:52:51 +020010 * RECLASS_MODEL_URL Reclass model repo address
11 * RECLASS_MODEL_CREDENTIALS Credentials to the Reclass model repo.
12 * RECLASS_MODEL_BRANCH Branch for the template to push to model.
Tomáš Kukrál65f8f752017-03-30 17:51:46 +020013 * COMMIT_CHANGES Commit model to repo
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020014 *
15**/
16
17common = new com.mirantis.mk.Common()
18git = new com.mirantis.mk.Git()
19python = new com.mirantis.mk.Python()
chnyda89191012017-05-29 15:38:35 +020020saltModelTesting = new com.mirantis.mk.SaltModelTesting()
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020021
22timestamps {
chnyda89191012017-05-29 15:38:35 +020023 node("python&&docker") {
Tomáš Kukrál9f6260f2017-03-29 23:58:26 +020024 def templateEnv = "${env.WORKSPACE}/template"
25 def modelEnv = "${env.WORKSPACE}/model"
chnyda89191012017-05-29 15:38:35 +020026 def testEnv = "${env.WORKSPACE}/test"
Tomáš Kukrál9f6260f2017-03-29 23:58:26 +020027
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020028 try {
Tomáš Kukrálf2a74ab2017-05-09 17:53:32 +020029 def templateContext = readYaml text: COOKIECUTTER_TEMPLATE_CONTEXT
Tomáš Kukrál91e49252017-05-09 14:40:26 +020030 def clusterDomain = templateContext.default_context.cluster_domain
31 def clusterName = templateContext.default_context.cluster_name
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020032 def cutterEnv = "${env.WORKSPACE}/cutter"
33 def jinjaEnv = "${env.WORKSPACE}/jinja"
Tomáš Kukrál931f9972017-03-30 23:44:57 +020034 def outputDestination = "${modelEnv}/classes/cluster/${clusterName}"
Tomáš Kukrál91e49252017-05-09 14:40:26 +020035 def targetBranch = "feature/${clusterName}"
36 def templateBaseDir = "${env.WORKSPACE}/template"
Tomáš Kukrál91e49252017-05-09 14:40:26 +020037 def templateDir = "${templateEnv}/template/dir"
38 def templateOutputDir = templateBaseDir
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020039
Tomáš Kukrál08bfb952017-04-12 13:10:52 +020040 currentBuild.description = clusterName
Tomáš Kukrál2a8fc322017-04-12 18:06:20 +020041 print("Using context:\n" + COOKIECUTTER_TEMPLATE_CONTEXT)
42
Tomáš Kukrál7ded3642017-03-27 15:52:51 +020043 stage ('Download Cookiecutter template') {
44 git.checkoutGitRepository(templateEnv, COOKIECUTTER_TEMPLATE_URL, COOKIECUTTER_TEMPLATE_BRANCH, COOKIECUTTER_TEMPLATE_CREDENTIALS)
45 }
46
47 stage ('Download full Reclass model') {
Tomáš Kukrál4aac3aa2017-04-04 14:04:02 +020048 if (RECLASS_MODEL_URL != '') {
49 git.checkoutGitRepository(modelEnv, RECLASS_MODEL_URL, RECLASS_MODEL_BRANCH, RECLASS_MODEL_CREDENTIALS)
50 }
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:
75 _param:
76 linux_system_codename: xenial
77 reclass_data_revision: master
78 linux:
79 system:
80 name: cfg01
81 domain: ${clusterDomain}
82"""
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)
85 }
86
chnyda89191012017-05-29 15:38:35 +020087 stage("Test") {
88 if (RECLASS_MODEL_URL == "" && TEST_MODEL && TEST_MODEL.toBoolean()) {
89 sh("cp -r ${modelEnv} ${testEnv}")
90 def defaultReclassModel = "ssh://jenkins-mk@gerrit.mcp.mirantis.net:29418/salt-models/reclass-system"
91 git.checkoutGitRepository("${testEnv}/classes/system", defaultReclassModel, RECLASS_MODEL_BRANCH, RECLASS_MODEL_CREDENTIALS)
92 saltModelTesting.setupAndTestNode("cfg01.${clusterDomain}", "", testEnv)
93 }
94 }
95
Tomáš Kukrál6c5957e2017-03-30 18:04:38 +020096 stage ('Save changes to Reclass model') {
Tomáš Kukrál91e49252017-05-09 14:40:26 +020097 if (env.getEnvironment().containsKey('COMMIT_CHANGES') && COMMIT_CHANGES.toBoolean() && RECLASS_MODEL_URL != null && RECLASS_MODEL_URL != "") {
Tomáš Kukrál4aac3aa2017-04-04 14:04:02 +020098 git.changeGitBranch(modelEnv, targetBranch)
Tomáš Kukrál6c5957e2017-03-30 18:04:38 +020099 git.commitGitChanges(modelEnv, "Added new cluster ${clusterName}")
Tomáš Kukrál65f8f752017-03-30 17:51:46 +0200100 git.pushGitChanges(modelEnv, targetBranch, 'origin', RECLASS_MODEL_CREDENTIALS)
101 }
Tomáš Kukrál91e49252017-05-09 14:40:26 +0200102
Tomáš Kukrál931f9972017-03-30 23:44:57 +0200103 sh(returnStatus: true, script: "tar -zcvf ${clusterName}.tar.gz -C ${modelEnv} .")
Tomáš Kukrál4c98b582017-03-30 18:49:06 +0200104 archiveArtifacts artifacts: "${clusterName}.tar.gz"
Tomáš Kukrál91e49252017-05-09 14:40:26 +0200105 if (EMAIl_ADDRESS != null && EMAIL_ADDRESS != ""){
106 emailext(to: EMAIL_ADDRESS,
107 attachmentsPattern: "${clusterName}.tar.gz",
108 body: "Mirantis Jenkins\n\nRequested reclass model ${clusterName} has been created and attached to this email.\nEnjoy!\n\nMirantis",
109 subject: "Your Salt model ${clusterName}")
110 }
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200111 }
112
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200113 } catch (Throwable e) {
114 // If there was an error or exception thrown, the build failed
115 currentBuild.result = "FAILURE"
116 throw e
117 } finally {
Tomáš Kukrál8b91db92017-03-29 08:30:49 +0200118 stage ('Clean workspace directories') {
Tomáš Kukrál2b983c32017-03-30 18:09:02 +0200119 sh(returnStatus: true, script: "rm -rfv ${templateEnv}")
120 sh(returnStatus: true, script: "rm -rfv ${modelEnv}")
Tomáš Kukrál8b91db92017-03-29 08:30:49 +0200121 }
Tomáš Kukrál7ded3642017-03-27 15:52:51 +0200122 // common.sendNotification(currentBuild.result,"",["slack"])
123 }
124 }
125}