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 | 0c6835d | 2017-07-18 16:00:27 +0200 | [diff] [blame] | 10 | * SHARED_RECLASS_URL Git repository with shared reclass |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 11 | * |
| 12 | **/ |
| 13 | |
| 14 | common = new com.mirantis.mk.Common() |
| 15 | git = new com.mirantis.mk.Git() |
| 16 | python = new com.mirantis.mk.Python() |
chnyda | 8919101 | 2017-05-29 15:38:35 +0200 | [diff] [blame] | 17 | saltModelTesting = new com.mirantis.mk.SaltModelTesting() |
Tomáš Kukrál | cebc1a0 | 2017-07-25 16:10:37 +0200 | [diff] [blame] | 18 | ssh = new com.mirantis.mk.Ssh() |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 19 | |
Tomáš Kukrál | 9f6260f | 2017-03-29 23:58:26 +0200 | [diff] [blame] | 20 | |
Ruslan Kamaldinov | 6feef40 | 2017-08-02 16:55:58 +0400 | [diff] [blame] | 21 | node("python&&docker") { |
| 22 | def templateEnv = "${env.WORKSPACE}/template" |
| 23 | def modelEnv = "${env.WORKSPACE}/model" |
| 24 | def testEnv = "${env.WORKSPACE}/test" |
Jiri Broulik | f672528 | 2017-09-05 16:23:35 +0200 | [diff] [blame] | 25 | def pipelineEnv = "${env.WORKSPACE}/pipelines" |
Tomáš Kukrál | c84d1f5 | 2017-07-19 17:37:00 +0200 | [diff] [blame] | 26 | |
Ruslan Kamaldinov | 6feef40 | 2017-08-02 16:55:58 +0400 | [diff] [blame] | 27 | try { |
| 28 | def templateContext = readYaml text: COOKIECUTTER_TEMPLATE_CONTEXT |
| 29 | def clusterDomain = templateContext.default_context.cluster_domain |
| 30 | def clusterName = templateContext.default_context.cluster_name |
Jiri Broulik | 19f8519 | 2017-09-18 13:55:14 +0200 | [diff] [blame] | 31 | def saltMaster = templateContext.default_context.salt_master_hostname |
Ruslan Kamaldinov | 6feef40 | 2017-08-02 16:55:58 +0400 | [diff] [blame] | 32 | def cutterEnv = "${env.WORKSPACE}/cutter" |
| 33 | def jinjaEnv = "${env.WORKSPACE}/jinja" |
| 34 | def outputDestination = "${modelEnv}/classes/cluster/${clusterName}" |
| 35 | def targetBranch = "feature/${clusterName}" |
| 36 | def templateBaseDir = "${env.WORKSPACE}/template" |
| 37 | def templateDir = "${templateEnv}/template/dir" |
| 38 | def templateOutputDir = templateBaseDir |
| 39 | def user |
| 40 | wrap([$class: 'BuildUser']) { |
| 41 | user = env.BUILD_USER_ID |
| 42 | } |
Tomáš Kukrál | 2a8fc32 | 2017-04-12 18:06:20 +0200 | [diff] [blame] | 43 | |
Ruslan Kamaldinov | 6feef40 | 2017-08-02 16:55:58 +0400 | [diff] [blame] | 44 | currentBuild.description = clusterName |
| 45 | print("Using context:\n" + COOKIECUTTER_TEMPLATE_CONTEXT) |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 46 | |
Ruslan Kamaldinov | 6feef40 | 2017-08-02 16:55:58 +0400 | [diff] [blame] | 47 | stage ('Download Cookiecutter template') { |
Tomáš Kukrál | a3f4ba7 | 2017-08-03 15:40:22 +0200 | [diff] [blame] | 48 | if (COOKIECUTTER_TEMPLATE_BRANCH.startsWith('refs/changes/')) { |
| 49 | git.checkoutGitRepository(templateEnv, COOKIECUTTER_TEMPLATE_URL, 'master', COOKIECUTTER_TEMPLATE_CREDENTIALS) |
| 50 | |
| 51 | dir(templateEnv) { |
| 52 | ssh.agentSh("git fetch ${COOKIECUTTER_TEMPLATE_URL} ${COOKIECUTTER_TEMPLATE_BRANCH} && git checkout FETCH_HEAD") |
| 53 | } |
| 54 | } else { |
| 55 | git.checkoutGitRepository(templateEnv, COOKIECUTTER_TEMPLATE_URL, COOKIECUTTER_TEMPLATE_BRANCH, COOKIECUTTER_TEMPLATE_CREDENTIALS) |
| 56 | } |
| 57 | |
Ruslan Kamaldinov | 6feef40 | 2017-08-02 16:55:58 +0400 | [diff] [blame] | 58 | } |
Tomáš Kukrál | 0c6835d | 2017-07-18 16:00:27 +0200 | [diff] [blame] | 59 | |
Ruslan Kamaldinov | 6feef40 | 2017-08-02 16:55:58 +0400 | [diff] [blame] | 60 | stage ('Create empty reclass model') { |
| 61 | dir(path: modelEnv) { |
| 62 | sh "rm -rfv .git" |
| 63 | sh "git init" |
| 64 | |
| 65 | if (SHARED_RECLASS_URL != '') { |
| 66 | ssh.agentSh "git submodule add \"${SHARED_RECLASS_URL}\" \"classes/system\"" |
| 67 | git.commitGitChanges(modelEnv, "Added new shared reclass submodule", "${user}@localhost", "${user}") |
Tomáš Kukrál | 4aac3aa | 2017-04-04 14:04:02 +0200 | [diff] [blame] | 68 | } |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 69 | } |
Ruslan Kamaldinov | 6feef40 | 2017-08-02 16:55:58 +0400 | [diff] [blame] | 70 | } |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 71 | |
Mikhail Ivanov | 9f81292 | 2017-11-07 18:52:02 +0400 | [diff] [blame] | 72 | def productList = ["infra", "cicd", "opencontrail", "kubernetes", "openstack", "oss", "stacklight", "ceph"] |
Ruslan Kamaldinov | 6feef40 | 2017-08-02 16:55:58 +0400 | [diff] [blame] | 73 | for (product in productList) { |
Tomáš Kukrál | 9a6821e | 2017-07-24 11:07:01 +0200 | [diff] [blame] | 74 | |
Ruslan Kamaldinov | 6feef40 | 2017-08-02 16:55:58 +0400 | [diff] [blame] | 75 | // get templateOutputDir and productDir |
| 76 | if (product.startsWith("stacklight")) { |
| 77 | templateOutputDir = "${env.WORKSPACE}/output/stacklight" |
Tomáš Kukrál | a3f4ba7 | 2017-08-03 15:40:22 +0200 | [diff] [blame] | 78 | |
| 79 | def stacklightVersion |
Ruslan Kamaldinov | 6feef40 | 2017-08-02 16:55:58 +0400 | [diff] [blame] | 80 | try { |
Tomáš Kukrál | a3f4ba7 | 2017-08-03 15:40:22 +0200 | [diff] [blame] | 81 | stacklightVersion = templateContext.default_context['stacklight_version'] |
Ruslan Kamaldinov | 6feef40 | 2017-08-02 16:55:58 +0400 | [diff] [blame] | 82 | } catch (Throwable e) { |
Tomáš Kukrál | a3f4ba7 | 2017-08-03 15:40:22 +0200 | [diff] [blame] | 83 | common.warningMsg('Stacklight version loading failed') |
| 84 | } |
| 85 | |
| 86 | if (stacklightVersion) { |
| 87 | productDir = "stacklight" + stacklightVersion |
| 88 | } else { |
Ruslan Kamaldinov | 6feef40 | 2017-08-02 16:55:58 +0400 | [diff] [blame] | 89 | productDir = "stacklight1" |
Tomáš Kukrál | 9a6821e | 2017-07-24 11:07:01 +0200 | [diff] [blame] | 90 | } |
Tomáš Kukrál | a3f4ba7 | 2017-08-03 15:40:22 +0200 | [diff] [blame] | 91 | |
Ruslan Kamaldinov | 6feef40 | 2017-08-02 16:55:58 +0400 | [diff] [blame] | 92 | } else { |
| 93 | templateOutputDir = "${env.WORKSPACE}/output/${product}" |
| 94 | productDir = product |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 95 | } |
| 96 | |
Ruslan Kamaldinov | 6feef40 | 2017-08-02 16:55:58 +0400 | [diff] [blame] | 97 | if (product == "infra" || (templateContext.default_context["${product}_enabled"] |
| 98 | && templateContext.default_context["${product}_enabled"].toBoolean())) { |
| 99 | |
| 100 | templateDir = "${templateEnv}/cluster_product/${productDir}" |
| 101 | common.infoMsg("Generating product " + product + " from " + templateDir + " to " + templateOutputDir) |
| 102 | |
| 103 | sh "rm -rf ${templateOutputDir} || true" |
| 104 | sh "mkdir -p ${templateOutputDir}" |
| 105 | sh "mkdir -p ${outputDestination}" |
| 106 | |
| 107 | python.setupCookiecutterVirtualenv(cutterEnv) |
| 108 | python.buildCookiecutterTemplate(templateDir, COOKIECUTTER_TEMPLATE_CONTEXT, templateOutputDir, cutterEnv, templateBaseDir) |
| 109 | sh "mv -v ${templateOutputDir}/${clusterName}/* ${outputDestination}" |
| 110 | } else { |
| 111 | common.warningMsg("Product " + product + " is disabled") |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | stage('Generate new SaltMaster node') { |
Jiri Broulik | 19f8519 | 2017-09-18 13:55:14 +0200 | [diff] [blame] | 116 | def nodeFile = "${modelEnv}/nodes/${saltMaster}.${clusterDomain}.yml" |
Ruslan Kamaldinov | 6feef40 | 2017-08-02 16:55:58 +0400 | [diff] [blame] | 117 | def nodeString = """classes: |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 118 | - cluster.${clusterName}.infra.config |
| 119 | parameters: |
Tomáš Kukrál | c0bffae | 2017-06-07 14:40:32 +0200 | [diff] [blame] | 120 | _param: |
| 121 | linux_system_codename: xenial |
| 122 | reclass_data_revision: master |
| 123 | linux: |
| 124 | system: |
Jiri Broulik | 19f8519 | 2017-09-18 13:55:14 +0200 | [diff] [blame] | 125 | name: ${saltMaster} |
Tomáš Kukrál | c0bffae | 2017-06-07 14:40:32 +0200 | [diff] [blame] | 126 | domain: ${clusterDomain} |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 127 | """ |
Ruslan Kamaldinov | 6feef40 | 2017-08-02 16:55:58 +0400 | [diff] [blame] | 128 | sh "mkdir -p ${modelEnv}/nodes/" |
| 129 | writeFile(file: nodeFile, text: nodeString) |
Tomáš Kukrál | 0c6835d | 2017-07-18 16:00:27 +0200 | [diff] [blame] | 130 | |
Ruslan Kamaldinov | 6feef40 | 2017-08-02 16:55:58 +0400 | [diff] [blame] | 131 | git.commitGitChanges(modelEnv, "Create model ${clusterName}", "${user}@localhost", "${user}") |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 132 | } |
Ruslan Kamaldinov | 6feef40 | 2017-08-02 16:55:58 +0400 | [diff] [blame] | 133 | |
| 134 | stage("Test") { |
| 135 | if (SHARED_RECLASS_URL != "" && TEST_MODEL && TEST_MODEL.toBoolean()) { |
| 136 | sh("cp -r ${modelEnv} ${testEnv}") |
Jiri Broulik | 19f8519 | 2017-09-18 13:55:14 +0200 | [diff] [blame] | 137 | saltModelTesting.setupAndTestNode("${saltMaster}.${clusterDomain}", "", testEnv) |
Ruslan Kamaldinov | 6feef40 | 2017-08-02 16:55:58 +0400 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
Richard Felkl | 9fd4794 | 2017-10-20 16:23:29 +0200 | [diff] [blame] | 141 | stage("Generate config drives") { |
Ruslan Kamaldinov | 6feef40 | 2017-08-02 16:55:58 +0400 | [diff] [blame] | 142 | // apt package genisoimage is required for this stage |
| 143 | |
| 144 | // download create-config-drive |
azvyagintsev | 3835ae8 | 2017-11-14 16:57:27 +0200 | [diff] [blame] | 145 | // FIXME: that should be refactored, to use git clone - to be able download it from custom repo. |
| 146 | def config_drive_script_url = "https://raw.githubusercontent.com/Mirantis/mcp-common-scripts/master/config-drive/create_config_drive.sh" |
| 147 | def user_data_script_url = "https://raw.githubusercontent.com/Mirantis/mcp-common-scripts/master/config-drive/master_config.sh" |
| 148 | |
Ruslan Kamaldinov | 6feef40 | 2017-08-02 16:55:58 +0400 | [diff] [blame] | 149 | sh "wget -O create-config-drive ${config_drive_script_url} && chmod +x create-config-drive" |
| 150 | sh "wget -O user_data.sh ${user_data_script_url}" |
| 151 | |
Jiri Broulik | f672528 | 2017-09-05 16:23:35 +0200 | [diff] [blame] | 152 | sh "git clone https://github.com/Mirantis/mk-pipelines.git ${pipelineEnv}/mk-pipelines" |
| 153 | sh "git clone https://github.com/Mirantis/pipeline-library.git ${pipelineEnv}/pipeline-library" |
Jiri Broulik | 19f8519 | 2017-09-18 13:55:14 +0200 | [diff] [blame] | 154 | args = "--user-data user_data.sh --hostname ${saltMaster} --model ${modelEnv} --mk-pipelines ${pipelineEnv}/mk-pipelines/ --pipeline-library ${pipelineEnv}/pipeline-library/ ${saltMaster}.${clusterDomain}-config.iso" |
Jiri Broulik | 85068ff | 2017-09-05 11:49:09 +0200 | [diff] [blame] | 155 | |
Ruslan Kamaldinov | 6feef40 | 2017-08-02 16:55:58 +0400 | [diff] [blame] | 156 | // load data from model |
| 157 | def smc = [:] |
Jiri Broulik | 19f8519 | 2017-09-18 13:55:14 +0200 | [diff] [blame] | 158 | smc['SALT_MASTER_MINION_ID'] = "${saltMaster}.${clusterDomain}" |
Ruslan Kamaldinov | 6feef40 | 2017-08-02 16:55:58 +0400 | [diff] [blame] | 159 | smc['SALT_MASTER_DEPLOY_IP'] = templateContext['default_context']['salt_master_management_address'] |
| 160 | smc['DEPLOY_NETWORK_GW'] = templateContext['default_context']['deploy_network_gateway'] |
| 161 | smc['DEPLOY_NETWORK_NETMASK'] = templateContext['default_context']['deploy_network_netmask'] |
| 162 | smc['DNS_SERVERS'] = templateContext['default_context']['dns_server01'] |
| 163 | |
| 164 | for (i in common.entries(smc)) { |
| 165 | sh "sed -i \"s,export ${i[0]}=.*,export ${i[0]}=${i[1]},\" user_data.sh" |
| 166 | } |
| 167 | |
Richard Felkl | 9fd4794 | 2017-10-20 16:23:29 +0200 | [diff] [blame] | 168 | // create cfg config-drive |
Jiri Broulik | 85068ff | 2017-09-05 11:49:09 +0200 | [diff] [blame] | 169 | sh "./create-config-drive ${args}" |
Jiri Broulik | 19f8519 | 2017-09-18 13:55:14 +0200 | [diff] [blame] | 170 | sh("mkdir output-${clusterName} && mv ${saltMaster}.${clusterDomain}-config.iso output-${clusterName}/") |
Richard Felkl | 9fd4794 | 2017-10-20 16:23:29 +0200 | [diff] [blame] | 171 | |
| 172 | // save cfg iso to artifacts |
Jiri Broulik | 19f8519 | 2017-09-18 13:55:14 +0200 | [diff] [blame] | 173 | archiveArtifacts artifacts: "output-${clusterName}/${saltMaster}.${clusterDomain}-config.iso" |
Richard Felkl | 9fd4794 | 2017-10-20 16:23:29 +0200 | [diff] [blame] | 174 | |
| 175 | if (templateContext.default_context.offline_deployment && templateContext.default_context.offline_deployment == 'True'){ |
| 176 | def aptlyServerHostname = templateContext.default_context.aptly_server_hostname |
azvyagintsev | 3835ae8 | 2017-11-14 16:57:27 +0200 | [diff] [blame] | 177 | def user_data_script_apt_url = "https://raw.githubusercontent.com/Mirantis/mcp-common-scripts/master/config-drive/mirror_config.sh" |
Richard Felkl | 9fd4794 | 2017-10-20 16:23:29 +0200 | [diff] [blame] | 178 | sh "wget -O mirror_config.sh ${user_data_script_apt_url}" |
| 179 | |
| 180 | def smc_apt = [:] |
| 181 | smc_apt['SALT_MASTER_DEPLOY_IP'] = templateContext['default_context']['salt_master_management_address'] |
| 182 | smc_apt['APTLY_DEPLOY_IP'] = templateContext['default_context']['aptly_server_address'] |
| 183 | smc_apt['APTLY_DEPLOY_NETMASK'] = templateContext['default_context']['deploy_network_netmask'] |
| 184 | smc_apt['APTLY_MINION_ID'] = "${aptlyServerHostname}.${clusterDomain}" |
| 185 | |
| 186 | for (i in common.entries(smc_apt)) { |
| 187 | sh "sed -i \"s,export ${i[0]}=.*,export ${i[0]}=${i[1]},\" mirror_config.sh" |
| 188 | } |
| 189 | |
| 190 | // create apt config-drive |
| 191 | sh "./create-config-drive --user-data mirror_config.sh --hostname ${aptlyServerHostname} ${aptlyServerHostname}.${clusterDomain}-config.iso" |
| 192 | sh("mv ${aptlyServerHostname}.${clusterDomain}-config.iso output-${clusterName}/") |
| 193 | |
| 194 | // save apt iso to artifacts |
| 195 | archiveArtifacts artifacts: "output-${clusterName}/${aptlyServerHostname}.${clusterDomain}-config.iso" |
| 196 | } |
Ruslan Kamaldinov | 6feef40 | 2017-08-02 16:55:58 +0400 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | stage ('Save changes reclass model') { |
Jakub Josef | 0dbf452 | 2017-08-31 14:39:50 +0200 | [diff] [blame] | 200 | sh(returnStatus: true, script: "tar -zcf output-${clusterName}/${clusterName}.tar.gz -C ${modelEnv} .") |
| 201 | archiveArtifacts artifacts: "output-${clusterName}/${clusterName}.tar.gz" |
Ruslan Kamaldinov | 6feef40 | 2017-08-02 16:55:58 +0400 | [diff] [blame] | 202 | |
| 203 | |
| 204 | if (EMAIL_ADDRESS != null && EMAIL_ADDRESS != "") { |
| 205 | emailext(to: EMAIL_ADDRESS, |
Jakub Josef | 0dbf452 | 2017-08-31 14:39:50 +0200 | [diff] [blame] | 206 | attachmentsPattern: "output-${clusterName}/*", |
Ruslan Kamaldinov | 6feef40 | 2017-08-02 16:55:58 +0400 | [diff] [blame] | 207 | body: "Mirantis Jenkins\n\nRequested reclass model ${clusterName} has been created and attached to this email.\nEnjoy!\n\nMirantis", |
| 208 | subject: "Your Salt model ${clusterName}") |
| 209 | } |
Jakub Josef | 0dbf452 | 2017-08-31 14:39:50 +0200 | [diff] [blame] | 210 | dir("output-${clusterName}"){ |
| 211 | deleteDir() |
| 212 | } |
Ruslan Kamaldinov | 6feef40 | 2017-08-02 16:55:58 +0400 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | } catch (Throwable e) { |
| 216 | // If there was an error or exception thrown, the build failed |
| 217 | currentBuild.result = "FAILURE" |
Jakub Josef | d2efd7d | 2017-08-22 17:49:57 +0200 | [diff] [blame] | 218 | currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message |
Ruslan Kamaldinov | 6feef40 | 2017-08-02 16:55:58 +0400 | [diff] [blame] | 219 | throw e |
| 220 | } finally { |
| 221 | stage ('Clean workspace directories') { |
| 222 | sh(returnStatus: true, script: "rm -rf ${templateEnv}") |
| 223 | sh(returnStatus: true, script: "rm -rf ${modelEnv}") |
Jiri Broulik | f672528 | 2017-09-05 16:23:35 +0200 | [diff] [blame] | 224 | sh(returnStatus: true, script: "rm -rf ${pipelineEnv}") |
Ruslan Kamaldinov | 6feef40 | 2017-08-02 16:55:58 +0400 | [diff] [blame] | 225 | } |
| 226 | // common.sendNotification(currentBuild.result,"",["slack"]) |
Tomáš Kukrál | 7ded364 | 2017-03-27 15:52:51 +0200 | [diff] [blame] | 227 | } |
Mikhail Ivanov | 9f81292 | 2017-11-07 18:52:02 +0400 | [diff] [blame] | 228 | } |