| 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. | 
|  | 17 | * | 
|  | 18 | **/ | 
|  | 19 |  | 
|  | 20 | common = new com.mirantis.mk.Common() | 
|  | 21 | git = new com.mirantis.mk.Git() | 
|  | 22 | python = new com.mirantis.mk.Python() | 
|  | 23 |  | 
|  | 24 | timestamps { | 
|  | 25 | node() { | 
|  | 26 | try { | 
|  | 27 | def templateEnv = "${env.WORKSPACE}/template" | 
|  | 28 | def templateContext = python.loadJson(COOKIECUTTER_TEMPLATE_CONTEXT) | 
|  | 29 | def templateDir = "${templateEnv}/template/dir" | 
|  | 30 | def templateOutputDir = "${env.WORKSPACE}/template" | 
|  | 31 | def modelEnv = "${env.WORKSPACE}/model" | 
|  | 32 | def cutterEnv = "${env.WORKSPACE}/cutter" | 
|  | 33 | def jinjaEnv = "${env.WORKSPACE}/jinja" | 
|  | 34 | def clusterName = templateContext.cluster_name | 
|  | 35 | def clusterDomain = templateContext.cluster_domain | 
|  | 36 | def targetBranch = "feature/${clusterName}" | 
|  | 37 |  | 
|  | 38 | stage ('Download Cookiecutter template') { | 
|  | 39 | git.checkoutGitRepository(templateEnv, COOKIECUTTER_TEMPLATE_URL, COOKIECUTTER_TEMPLATE_BRANCH, COOKIECUTTER_TEMPLATE_CREDENTIALS) | 
|  | 40 | } | 
|  | 41 |  | 
|  | 42 | stage ('Download full Reclass model') { | 
|  | 43 | git.checkoutGitRepository(modelEnv, RECLASS_MODEL_URL, RECLASS_MODEL_BRANCH, RECLASS_MODEL_CREDENTIALS) | 
|  | 44 | } | 
|  | 45 |  | 
|  | 46 | stage('Generate base infrastructure') { | 
|  | 47 | templateDir = "${templateEnv}/cluster_product/infra" | 
|  | 48 | templateOutputDir = "${env.WORKSPACE}/template/output_infra" | 
|  | 49 | sh "mkdir -p ${templateOutputDir}" | 
|  | 50 | python.setupCookiecutterVirtualenv(cutterEnv) | 
|  | 51 | python.buildCookiecutterTemplate(templateDir, templateContext, templateOutputDir, cutterEnv) | 
|  | 52 | } | 
|  | 53 |  | 
|  | 54 | stage('Generate product CI/CD') { | 
|  | 55 | if (COOKIECUTTER_INSTALL_CICD.toBoolean()) { | 
|  | 56 | templateDir = "${templateEnv}/cluster_product/cicd" | 
|  | 57 | templateOutputDir = "${env.WORKSPACE}/template/output_cicd" | 
|  | 58 | sh "mkdir -p ${templateOutputDir}" | 
|  | 59 | python.setupCookiecutterVirtualenv(cutterEnv) | 
|  | 60 | python.buildCookiecutterTemplate(templateDir, templateContext, templateOutputDir, cutterEnv) | 
|  | 61 | } | 
|  | 62 | } | 
|  | 63 |  | 
|  | 64 | stage('Generate product OpenContrail') { | 
|  | 65 | if (COOKIECUTTER_INSTALL_CONTRAIL.toBoolean()) { | 
|  | 66 | templateDir = "${templateEnv}/cluster_product/contrail" | 
|  | 67 | templateOutputDir = "${env.WORKSPACE}/template/output_contrail" | 
|  | 68 | sh "mkdir -p ${templateOutputDir}" | 
|  | 69 | python.setupCookiecutterVirtualenv(cutterEnv) | 
|  | 70 | python.buildCookiecutterTemplate(templateDir, templateContext, templateOutputDir, cutterEnv) | 
|  | 71 | } | 
|  | 72 | } | 
|  | 73 |  | 
|  | 74 | stage('Generate product Kubernetes') { | 
|  | 75 | if (COOKIECUTTER_INSTALL_KUBERNETES.toBoolean()) { | 
|  | 76 | templateDir = "${templateEnv}/cluster_product/kubernetes" | 
|  | 77 | templateOutputDir = "${env.WORKSPACE}/template/output_kubernetes" | 
|  | 78 | sh "mkdir -p ${templateOutputDir}" | 
|  | 79 | python.setupCookiecutterVirtualenv(cutterEnv) | 
|  | 80 | python.buildCookiecutterTemplate(templateDir, templateContext, templateOutputDir, cutterEnv) | 
|  | 81 | } | 
|  | 82 | } | 
|  | 83 |  | 
|  | 84 | stage('Generate product OpenStack') { | 
|  | 85 | if (COOKIECUTTER_INSTALL_OPENSTACK.toBoolean()) { | 
|  | 86 | templateDir = "${templateEnv}/cluster_product/openstack" | 
|  | 87 | templateOutputDir = "${env.WORKSPACE}/template/output_openstack" | 
|  | 88 | sh "mkdir -p ${templateOutputDir}" | 
|  | 89 | python.setupCookiecutterVirtualenv(cutterEnv) | 
|  | 90 | python.buildCookiecutterTemplate(templateDir, templateContext, templateOutputDir, cutterEnv) | 
|  | 91 | } | 
|  | 92 | } | 
|  | 93 |  | 
|  | 94 | stage('Generate product StackLight') { | 
|  | 95 | if (COOKIECUTTER_INSTALL_STACKLIGHT.toBoolean()) { | 
|  | 96 | templateDir = "${templateEnv}/cluster_product/stacklight" | 
|  | 97 | templateOutputDir = "${env.WORKSPACE}/template/output_stacklight" | 
|  | 98 | sh "mkdir -p ${templateOutputDir}" | 
|  | 99 | python.setupCookiecutterVirtualenv(cutterEnv) | 
|  | 100 | python.buildCookiecutterTemplate(templateDir, templateContext, templateOutputDir, cutterEnv) | 
|  | 101 | } | 
|  | 102 | } | 
|  | 103 |  | 
|  | 104 | stage('Generate new SaltMaster node') { | 
|  | 105 | def nodeFile = "${modelEnv}/nodes/cfg01.${clusterDomain}.yml" | 
|  | 106 | def nodeString = """classes: | 
|  | 107 | - cluster.${clusterName}.infra.config | 
|  | 108 | parameters: | 
|  | 109 | _param: | 
|  | 110 | linux_system_codename: xenial | 
|  | 111 | reclass_data_revision: master | 
|  | 112 | linux: | 
|  | 113 | system: | 
|  | 114 | name: cfg01 | 
|  | 115 | domain: ${clusterDomain} | 
|  | 116 | """ | 
|  | 117 | writeFile(file: nodeFile, text: nodeString) | 
|  | 118 | } | 
|  | 119 |  | 
|  | 120 | stage('Inject changes to Reclass model') { | 
|  | 121 | git.changeGitBranch(modelEnv, targetBranch) | 
|  | 122 | def outputSource = "${templateOutputDir}/${clusterName}" | 
|  | 123 | def outputDestination = "${modelEnv}/classes/cluster/${clusterName}" | 
|  | 124 | sh(returnStdout: true, script: "cp ${outputSource} ${outputDestination} -r") | 
|  | 125 | git.commitGitChanges(modelEnv, "Added new cluster ${clusterName}") | 
|  | 126 | } | 
|  | 127 |  | 
|  | 128 | stage ('Push changes to Reclass model') { | 
|  | 129 | git.pushGitChanges(modelEnv, targetBranch, 'origin', RECLASS_MODEL_CREDENTIALS) | 
|  | 130 | } | 
|  | 131 |  | 
|  | 132 | stage ('Clean workspace directories') { | 
|  | 133 | sh(returnStdout: true, script: "rm ${templateEnv} -rf") | 
|  | 134 | sh(returnStdout: true, script: "rm ${modelEnv} -rf") | 
|  | 135 | } | 
|  | 136 |  | 
|  | 137 | } catch (Throwable e) { | 
|  | 138 | // If there was an error or exception thrown, the build failed | 
|  | 139 | currentBuild.result = "FAILURE" | 
|  | 140 | throw e | 
|  | 141 | } finally { | 
|  | 142 | // common.sendNotification(currentBuild.result,"",["slack"]) | 
|  | 143 | } | 
|  | 144 | } | 
|  | 145 | } |