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. |
| 9 | * COOKIECUTTER_INSTALL_CICD Whether to install CI/CD stack. |
| 10 | * COOKIECUTTER_INSTALL_CONTRAIL Whether to install OpenContrail SDN. |
| 11 | * COOKIECUTTER_INSTALL_KUBERNETES Whether to install Kubernetes. |
| 12 | * COOKIECUTTER_INSTALL_OPENSTACK Whether to install OpenStack cloud. |
| 13 | * COOKIECUTTER_INSTALL_STACKLIGHT Whether to install StackLight monitoring. |
| 14 | * RECLASS_MODEL_URL Reclass model repo address |
| 15 | * RECLASS_MODEL_CREDENTIALS Credentials to the Reclass model repo. |
| 16 | * RECLASS_MODEL_BRANCH Branch for the template to push to model. |
Tomáš Kukrál | 65f8f75 | 2017-03-30 17:51:46 +0200 | [diff] [blame] | 17 | * COMMIT_CHANGES Commit model to repo |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 18 | * |
| 19 | **/ |
| 20 | |
| 21 | common = new com.mirantis.mk.Common() |
| 22 | git = new com.mirantis.mk.Git() |
| 23 | python = new com.mirantis.mk.Python() |
| 24 | |
| 25 | timestamps { |
| 26 | node() { |
Tomáš Kukrál | 9f6260f | 2017-03-29 23:58:26 +0200 | [diff] [blame] | 27 | def templateEnv = "${env.WORKSPACE}/template" |
| 28 | def modelEnv = "${env.WORKSPACE}/model" |
| 29 | |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 30 | try { |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 31 | def templateContext = python.loadJson(COOKIECUTTER_TEMPLATE_CONTEXT) |
| 32 | def templateDir = "${templateEnv}/template/dir" |
| 33 | def templateOutputDir = "${env.WORKSPACE}/template" |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 34 | def cutterEnv = "${env.WORKSPACE}/cutter" |
| 35 | def jinjaEnv = "${env.WORKSPACE}/jinja" |
| 36 | def clusterName = templateContext.cluster_name |
| 37 | def clusterDomain = templateContext.cluster_domain |
| 38 | def targetBranch = "feature/${clusterName}" |
| 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 | git.checkoutGitRepository(modelEnv, RECLASS_MODEL_URL, RECLASS_MODEL_BRANCH, RECLASS_MODEL_CREDENTIALS) |
| 46 | } |
| 47 | |
| 48 | stage('Generate base infrastructure') { |
| 49 | templateDir = "${templateEnv}/cluster_product/infra" |
| 50 | templateOutputDir = "${env.WORKSPACE}/template/output_infra" |
| 51 | sh "mkdir -p ${templateOutputDir}" |
| 52 | python.setupCookiecutterVirtualenv(cutterEnv) |
| 53 | python.buildCookiecutterTemplate(templateDir, templateContext, templateOutputDir, cutterEnv) |
| 54 | } |
| 55 | |
| 56 | stage('Generate product CI/CD') { |
| 57 | if (COOKIECUTTER_INSTALL_CICD.toBoolean()) { |
| 58 | templateDir = "${templateEnv}/cluster_product/cicd" |
| 59 | templateOutputDir = "${env.WORKSPACE}/template/output_cicd" |
| 60 | sh "mkdir -p ${templateOutputDir}" |
| 61 | python.setupCookiecutterVirtualenv(cutterEnv) |
| 62 | python.buildCookiecutterTemplate(templateDir, templateContext, templateOutputDir, cutterEnv) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | stage('Generate product OpenContrail') { |
| 67 | if (COOKIECUTTER_INSTALL_CONTRAIL.toBoolean()) { |
| 68 | templateDir = "${templateEnv}/cluster_product/contrail" |
| 69 | templateOutputDir = "${env.WORKSPACE}/template/output_contrail" |
| 70 | sh "mkdir -p ${templateOutputDir}" |
| 71 | python.setupCookiecutterVirtualenv(cutterEnv) |
| 72 | python.buildCookiecutterTemplate(templateDir, templateContext, templateOutputDir, cutterEnv) |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | stage('Generate product Kubernetes') { |
| 77 | if (COOKIECUTTER_INSTALL_KUBERNETES.toBoolean()) { |
| 78 | templateDir = "${templateEnv}/cluster_product/kubernetes" |
| 79 | templateOutputDir = "${env.WORKSPACE}/template/output_kubernetes" |
| 80 | sh "mkdir -p ${templateOutputDir}" |
| 81 | python.setupCookiecutterVirtualenv(cutterEnv) |
| 82 | python.buildCookiecutterTemplate(templateDir, templateContext, templateOutputDir, cutterEnv) |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | stage('Generate product OpenStack') { |
| 87 | if (COOKIECUTTER_INSTALL_OPENSTACK.toBoolean()) { |
| 88 | templateDir = "${templateEnv}/cluster_product/openstack" |
| 89 | templateOutputDir = "${env.WORKSPACE}/template/output_openstack" |
| 90 | sh "mkdir -p ${templateOutputDir}" |
| 91 | python.setupCookiecutterVirtualenv(cutterEnv) |
| 92 | python.buildCookiecutterTemplate(templateDir, templateContext, templateOutputDir, cutterEnv) |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | stage('Generate product StackLight') { |
| 97 | if (COOKIECUTTER_INSTALL_STACKLIGHT.toBoolean()) { |
| 98 | templateDir = "${templateEnv}/cluster_product/stacklight" |
| 99 | templateOutputDir = "${env.WORKSPACE}/template/output_stacklight" |
| 100 | sh "mkdir -p ${templateOutputDir}" |
| 101 | python.setupCookiecutterVirtualenv(cutterEnv) |
| 102 | python.buildCookiecutterTemplate(templateDir, templateContext, templateOutputDir, cutterEnv) |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | stage('Generate new SaltMaster node') { |
| 107 | def nodeFile = "${modelEnv}/nodes/cfg01.${clusterDomain}.yml" |
| 108 | def nodeString = """classes: |
| 109 | - cluster.${clusterName}.infra.config |
| 110 | parameters: |
| 111 | _param: |
| 112 | linux_system_codename: xenial |
| 113 | reclass_data_revision: master |
| 114 | linux: |
| 115 | system: |
| 116 | name: cfg01 |
| 117 | domain: ${clusterDomain} |
| 118 | """ |
| 119 | writeFile(file: nodeFile, text: nodeString) |
| 120 | } |
| 121 | |
Tomáš Kukrál | 6c5957e | 2017-03-30 18:04:38 +0200 | [diff] [blame] | 122 | stage('Inject changes to Reclass model') { |
| 123 | git.changeGitBranch(modelEnv, targetBranch) |
| 124 | def outputSource = "${templateOutputDir}/${clusterName}" |
| 125 | def outputDestination = "${modelEnv}/classes/cluster/${clusterName}" |
| 126 | sh(returnStdout: true, script: "cp ${outputSource} ${outputDestination} -r") |
| 127 | } |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 128 | |
Tomáš Kukrál | 6c5957e | 2017-03-30 18:04:38 +0200 | [diff] [blame] | 129 | stage ('Save changes to Reclass model') { |
| 130 | if (COMMIT_CHANGES.toBoolean()) { |
| 131 | git.commitGitChanges(modelEnv, "Added new cluster ${clusterName}") |
Tomáš Kukrál | 65f8f75 | 2017-03-30 17:51:46 +0200 | [diff] [blame] | 132 | git.pushGitChanges(modelEnv, targetBranch, 'origin', RECLASS_MODEL_CREDENTIALS) |
| 133 | } |
Tomáš Kukrál | 4c98b58 | 2017-03-30 18:49:06 +0200 | [diff] [blame^] | 134 | sh(returnStatus: true, script: "tar zcvf ${clusterName}.tar.gz ${modelEnv}") |
| 135 | archiveArtifacts artifacts: "${clusterName}.tar.gz" |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 136 | } |
| 137 | |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 138 | } catch (Throwable e) { |
| 139 | // If there was an error or exception thrown, the build failed |
| 140 | currentBuild.result = "FAILURE" |
| 141 | throw e |
| 142 | } finally { |
Tomáš Kukrál | 8b91db9 | 2017-03-29 08:30:49 +0200 | [diff] [blame] | 143 | stage ('Clean workspace directories') { |
Tomáš Kukrál | 2b983c3 | 2017-03-30 18:09:02 +0200 | [diff] [blame] | 144 | sh(returnStatus: true, script: "rm -rfv ${templateEnv}") |
| 145 | sh(returnStatus: true, script: "rm -rfv ${modelEnv}") |
Tomáš Kukrál | 8b91db9 | 2017-03-29 08:30:49 +0200 | [diff] [blame] | 146 | } |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 147 | // common.sendNotification(currentBuild.result,"",["slack"]) |
| 148 | } |
| 149 | } |
| 150 | } |