Jakub Josef | 4780ae6 | 2017-04-25 16:17:56 +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. |
| 9 | * EMAIl_ADDRESS Email to send a created tar file |
| 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. |
| 13 | * |
| 14 | **/ |
| 15 | |
| 16 | common = new com.mirantis.mk.Common() |
| 17 | git = new com.mirantis.mk.Git() |
| 18 | python = new com.mirantis.mk.Python() |
| 19 | |
| 20 | timestamps { |
| 21 | node() { |
| 22 | def templateEnv = "${env.WORKSPACE}/template" |
| 23 | def modelEnv = "${env.WORKSPACE}/model" |
| 24 | |
| 25 | try { |
| 26 | def templateContext = readYaml text: COOKIECUTTER_TEMPLATE_CONTEXT |
Jakub Josef | da18fc4 | 2017-04-26 14:14:25 +0200 | [diff] [blame] | 27 | def templateBaseDir = "${env.WORKSPACE}/template" |
Jakub Josef | 4780ae6 | 2017-04-25 16:17:56 +0200 | [diff] [blame] | 28 | def templateDir = "${templateEnv}/template/dir" |
Jakub Josef | da18fc4 | 2017-04-26 14:14:25 +0200 | [diff] [blame] | 29 | def templateOutputDir = templateBaseDir |
Jakub Josef | 4780ae6 | 2017-04-25 16:17:56 +0200 | [diff] [blame] | 30 | def cutterEnv = "${env.WORKSPACE}/cutter" |
| 31 | def jinjaEnv = "${env.WORKSPACE}/jinja" |
| 32 | def clusterName = templateContext.default_context.cluster_name |
| 33 | def clusterDomain = templateContext.default_context.cluster_domain |
| 34 | def targetBranch = "feature/${clusterName}" |
| 35 | def outputDestination = "${modelEnv}/classes/cluster/${clusterName}" |
| 36 | |
| 37 | currentBuild.description = clusterName |
| 38 | print("Using context:\n" + COOKIECUTTER_TEMPLATE_CONTEXT) |
| 39 | |
| 40 | stage ('Download Cookiecutter template') { |
| 41 | git.checkoutGitRepository(templateEnv, COOKIECUTTER_TEMPLATE_URL, COOKIECUTTER_TEMPLATE_BRANCH, COOKIECUTTER_TEMPLATE_CREDENTIALS) |
| 42 | } |
| 43 | |
| 44 | stage ('Download full Reclass model') { |
| 45 | if (RECLASS_MODEL_URL != '') { |
| 46 | git.checkoutGitRepository(modelEnv, RECLASS_MODEL_URL, RECLASS_MODEL_BRANCH, RECLASS_MODEL_CREDENTIALS) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | stage('Generate base infrastructure') { |
| 51 | templateDir = "${templateEnv}/cluster_product/infra" |
| 52 | templateOutputDir = "${env.WORKSPACE}/template/output/infra" |
| 53 | sh "mkdir -p ${templateOutputDir}" |
| 54 | sh "mkdir -p ${outputDestination}" |
| 55 | python.setupCookiecutterVirtualenv(cutterEnv) |
Jakub Josef | da18fc4 | 2017-04-26 14:14:25 +0200 | [diff] [blame] | 56 | python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv, templateBaseDir) |
Jakub Josef | 4780ae6 | 2017-04-25 16:17:56 +0200 | [diff] [blame] | 57 | sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}" |
| 58 | } |
| 59 | |
| 60 | stage('Generate product CI/CD') { |
| 61 | if (templateContext.default_context.cicd_enabled.toBoolean()) { |
| 62 | templateDir = "${templateEnv}/cluster_product/cicd" |
| 63 | templateOutputDir = "${env.WORKSPACE}/template/output/cicd" |
| 64 | sh "mkdir -p ${templateOutputDir}" |
| 65 | python.setupCookiecutterVirtualenv(cutterEnv) |
Jakub Josef | da18fc4 | 2017-04-26 14:14:25 +0200 | [diff] [blame] | 66 | python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv, templateBaseDir) |
Jakub Josef | 4780ae6 | 2017-04-25 16:17:56 +0200 | [diff] [blame] | 67 | sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}" |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | stage('Generate product OpenContrail') { |
Jakub Josef | f6b556a | 2017-04-26 18:23:18 +0200 | [diff] [blame] | 72 | if (templateContext.default_context.opencontrail_enabled.toBoolean()) { |
Jakub Josef | 4780ae6 | 2017-04-25 16:17:56 +0200 | [diff] [blame] | 73 | templateDir = "${templateEnv}/cluster_product/opencontrail" |
| 74 | templateOutputDir = "${env.WORKSPACE}/template/output/opencontrail" |
| 75 | sh "mkdir -p ${templateOutputDir}" |
| 76 | python.setupCookiecutterVirtualenv(cutterEnv) |
Jakub Josef | da18fc4 | 2017-04-26 14:14:25 +0200 | [diff] [blame] | 77 | python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv, templateBaseDir) |
Jakub Josef | 4780ae6 | 2017-04-25 16:17:56 +0200 | [diff] [blame] | 78 | sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}" |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | stage('Generate product Kubernetes') { |
| 83 | if (templateContext.default_context.kubernetes_enabled.toBoolean()) { |
| 84 | templateDir = "${templateEnv}/cluster_product/kubernetes" |
| 85 | templateOutputDir = "${env.WORKSPACE}/template/output/kubernetes" |
| 86 | sh "mkdir -p ${templateOutputDir}" |
| 87 | python.setupCookiecutterVirtualenv(cutterEnv) |
Jakub Josef | da18fc4 | 2017-04-26 14:14:25 +0200 | [diff] [blame] | 88 | python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv, templateBaseDir) |
Jakub Josef | 4780ae6 | 2017-04-25 16:17:56 +0200 | [diff] [blame] | 89 | sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}" |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | stage('Generate product OpenStack') { |
| 94 | if (templateContext.default_context.openstack_enabled.toBoolean()) { |
| 95 | templateDir = "${templateEnv}/cluster_product/openstack" |
| 96 | templateOutputDir = "${env.WORKSPACE}/template/output/openstack" |
| 97 | sh "mkdir -p ${templateOutputDir}" |
| 98 | python.setupCookiecutterVirtualenv(cutterEnv) |
Jakub Josef | da18fc4 | 2017-04-26 14:14:25 +0200 | [diff] [blame] | 99 | python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv, templateBaseDir) |
Jakub Josef | 4780ae6 | 2017-04-25 16:17:56 +0200 | [diff] [blame] | 100 | sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}" |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | stage('Generate product StackLight') { |
| 105 | if (templateContext.default_context.stacklight_enabled.toBoolean()) { |
| 106 | templateDir = "${templateEnv}/cluster_product/stacklight" |
| 107 | templateOutputDir = "${env.WORKSPACE}/template/output/stacklight" |
| 108 | sh "mkdir -p ${templateOutputDir}" |
| 109 | python.setupCookiecutterVirtualenv(cutterEnv) |
Jakub Josef | da18fc4 | 2017-04-26 14:14:25 +0200 | [diff] [blame] | 110 | python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv, templateBaseDir) |
Jakub Josef | 4780ae6 | 2017-04-25 16:17:56 +0200 | [diff] [blame] | 111 | sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}" |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | stage('Generate new SaltMaster node') { |
| 116 | def nodeFile = "${modelEnv}/nodes/cfg01.${clusterDomain}.yml" |
| 117 | def nodeString = """classes: |
| 118 | - cluster.${clusterName}.infra.config |
| 119 | parameters: |
| 120 | _param: |
| 121 | linux_system_codename: xenial |
| 122 | reclass_data_revision: master |
| 123 | linux: |
| 124 | system: |
| 125 | name: cfg01 |
| 126 | domain: ${clusterDomain} |
| 127 | """ |
| 128 | sh "mkdir -p ${modelEnv}/nodes/" |
| 129 | writeFile(file: nodeFile, text: nodeString) |
| 130 | } |
| 131 | |
| 132 | stage ('Save changes to Reclass model') { |
| 133 | if (RECLASS_MODEL_URL != null && RECLASS_MODEL_URL != "") { |
| 134 | git.changeGitBranch(modelEnv, targetBranch) |
| 135 | git.commitGitChanges(modelEnv, "Added new cluster ${clusterName}") |
| 136 | git.pushGitChanges(modelEnv, targetBranch, 'origin', RECLASS_MODEL_CREDENTIALS) |
| 137 | } |
| 138 | sh(returnStatus: true, script: "tar -zcvf ${clusterName}.tar.gz -C ${modelEnv} .") |
| 139 | archiveArtifacts artifacts: "${clusterName}.tar.gz" |
Jakub Josef | 7d85422 | 2017-04-26 18:27:01 +0200 | [diff] [blame] | 140 | if(EMAIl_ADDRESS != null && EMAIL_ADDRESS){ |
Jakub Josef | 565804a | 2017-04-27 14:43:46 +0200 | [diff] [blame] | 141 | emailext(to: EMAIL_ADDRESS, attachmentsPattern: "^${clusterName}\\.tar\\.gz\$", body: "Mirantis Jenkins\n\nRequested reclass model ${clusterName} created and has been attached to this email.\nEnjoy!\n\nMirantis Inc.", subject: "Your Salt model ${clusterName}") |
Jakub Josef | 4780ae6 | 2017-04-25 16:17:56 +0200 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
| 145 | } catch (Throwable e) { |
| 146 | // If there was an error or exception thrown, the build failed |
| 147 | currentBuild.result = "FAILURE" |
| 148 | throw e |
| 149 | } finally { |
| 150 | stage ('Clean workspace directories') { |
| 151 | sh(returnStatus: true, script: "rm -rfv ${templateEnv}") |
| 152 | sh(returnStatus: true, script: "rm -rfv ${modelEnv}") |
| 153 | } |
| 154 | // common.sendNotification(currentBuild.result,"",["slack"]) |
| 155 | } |
| 156 | } |
| 157 | } |