Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 1 | /** |
| 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ál | 91e4925 | 2017-05-09 14:40:26 +0200 | [diff] [blame] | 9 | * EMAIL_ADDRESS Email to send a created tar file |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 10 | * 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ál | 65f8f75 | 2017-03-30 17:51:46 +0200 | [diff] [blame] | 13 | * COMMIT_CHANGES Commit model to repo |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 14 | * |
| 15 | **/ |
| 16 | |
| 17 | common = new com.mirantis.mk.Common() |
| 18 | git = new com.mirantis.mk.Git() |
| 19 | python = new com.mirantis.mk.Python() |
chnyda | 8919101 | 2017-05-29 15:38:35 +0200 | [diff] [blame^] | 20 | saltModelTesting = new com.mirantis.mk.SaltModelTesting() |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 21 | |
| 22 | timestamps { |
chnyda | 8919101 | 2017-05-29 15:38:35 +0200 | [diff] [blame^] | 23 | node("python&&docker") { |
Tomáš Kukrál | 9f6260f | 2017-03-29 23:58:26 +0200 | [diff] [blame] | 24 | def templateEnv = "${env.WORKSPACE}/template" |
| 25 | def modelEnv = "${env.WORKSPACE}/model" |
chnyda | 8919101 | 2017-05-29 15:38:35 +0200 | [diff] [blame^] | 26 | def testEnv = "${env.WORKSPACE}/test" |
Tomáš Kukrál | 9f6260f | 2017-03-29 23:58:26 +0200 | [diff] [blame] | 27 | |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 28 | try { |
Tomáš Kukrál | f2a74ab | 2017-05-09 17:53:32 +0200 | [diff] [blame] | 29 | def templateContext = readYaml text: COOKIECUTTER_TEMPLATE_CONTEXT |
Tomáš Kukrál | 91e4925 | 2017-05-09 14:40:26 +0200 | [diff] [blame] | 30 | def clusterDomain = templateContext.default_context.cluster_domain |
| 31 | def clusterName = templateContext.default_context.cluster_name |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 32 | def cutterEnv = "${env.WORKSPACE}/cutter" |
| 33 | def jinjaEnv = "${env.WORKSPACE}/jinja" |
Tomáš Kukrál | 931f997 | 2017-03-30 23:44:57 +0200 | [diff] [blame] | 34 | def outputDestination = "${modelEnv}/classes/cluster/${clusterName}" |
Tomáš Kukrál | 91e4925 | 2017-05-09 14:40:26 +0200 | [diff] [blame] | 35 | def targetBranch = "feature/${clusterName}" |
| 36 | def templateBaseDir = "${env.WORKSPACE}/template" |
Tomáš Kukrál | 91e4925 | 2017-05-09 14:40:26 +0200 | [diff] [blame] | 37 | def templateDir = "${templateEnv}/template/dir" |
| 38 | def templateOutputDir = templateBaseDir |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 39 | |
Tomáš Kukrál | 08bfb95 | 2017-04-12 13:10:52 +0200 | [diff] [blame] | 40 | currentBuild.description = clusterName |
Tomáš Kukrál | 2a8fc32 | 2017-04-12 18:06:20 +0200 | [diff] [blame] | 41 | print("Using context:\n" + COOKIECUTTER_TEMPLATE_CONTEXT) |
| 42 | |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 43 | 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ál | 4aac3aa | 2017-04-04 14:04:02 +0200 | [diff] [blame] | 48 | if (RECLASS_MODEL_URL != '') { |
| 49 | git.checkoutGitRepository(modelEnv, RECLASS_MODEL_URL, RECLASS_MODEL_BRANCH, RECLASS_MODEL_CREDENTIALS) |
| 50 | } |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 51 | } |
| 52 | |
chnyda | 8919101 | 2017-05-29 15:38:35 +0200 | [diff] [blame^] | 53 | 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ál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 67 | } |
| 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 |
| 74 | parameters: |
| 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ál | 4aac3aa | 2017-04-04 14:04:02 +0200 | [diff] [blame] | 83 | sh "mkdir -p ${modelEnv}/nodes/" |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 84 | writeFile(file: nodeFile, text: nodeString) |
| 85 | } |
| 86 | |
chnyda | 8919101 | 2017-05-29 15:38:35 +0200 | [diff] [blame^] | 87 | 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ál | 6c5957e | 2017-03-30 18:04:38 +0200 | [diff] [blame] | 96 | stage ('Save changes to Reclass model') { |
Tomáš Kukrál | 91e4925 | 2017-05-09 14:40:26 +0200 | [diff] [blame] | 97 | if (env.getEnvironment().containsKey('COMMIT_CHANGES') && COMMIT_CHANGES.toBoolean() && RECLASS_MODEL_URL != null && RECLASS_MODEL_URL != "") { |
Tomáš Kukrál | 4aac3aa | 2017-04-04 14:04:02 +0200 | [diff] [blame] | 98 | git.changeGitBranch(modelEnv, targetBranch) |
Tomáš Kukrál | 6c5957e | 2017-03-30 18:04:38 +0200 | [diff] [blame] | 99 | git.commitGitChanges(modelEnv, "Added new cluster ${clusterName}") |
Tomáš Kukrál | 65f8f75 | 2017-03-30 17:51:46 +0200 | [diff] [blame] | 100 | git.pushGitChanges(modelEnv, targetBranch, 'origin', RECLASS_MODEL_CREDENTIALS) |
| 101 | } |
Tomáš Kukrál | 91e4925 | 2017-05-09 14:40:26 +0200 | [diff] [blame] | 102 | |
Tomáš Kukrál | 931f997 | 2017-03-30 23:44:57 +0200 | [diff] [blame] | 103 | sh(returnStatus: true, script: "tar -zcvf ${clusterName}.tar.gz -C ${modelEnv} .") |
Tomáš Kukrál | 4c98b58 | 2017-03-30 18:49:06 +0200 | [diff] [blame] | 104 | archiveArtifacts artifacts: "${clusterName}.tar.gz" |
Tomáš Kukrál | 91e4925 | 2017-05-09 14:40:26 +0200 | [diff] [blame] | 105 | 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ál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 111 | } |
| 112 | |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 113 | } 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ál | 8b91db9 | 2017-03-29 08:30:49 +0200 | [diff] [blame] | 118 | stage ('Clean workspace directories') { |
Tomáš Kukrál | 2b983c3 | 2017-03-30 18:09:02 +0200 | [diff] [blame] | 119 | sh(returnStatus: true, script: "rm -rfv ${templateEnv}") |
| 120 | sh(returnStatus: true, script: "rm -rfv ${modelEnv}") |
Tomáš Kukrál | 8b91db9 | 2017-03-29 08:30:49 +0200 | [diff] [blame] | 121 | } |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 122 | // common.sendNotification(currentBuild.result,"",["slack"]) |
| 123 | } |
| 124 | } |
| 125 | } |